]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-format-table.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
4 #include "format-table.h"
5 #include "string-util.h"
8 static void test_issue_9549(void) {
9 _cleanup_(table_unrefp
) Table
*table
= NULL
;
10 _cleanup_free_
char *formatted
= NULL
;
12 assert_se(table
= table_new("name", "type", "ro", "usage", "created", "modified"));
13 assert_se(table_set_align_percent(table
, TABLE_HEADER_CELL(3), 100) >= 0);
14 assert_se(table_add_many(table
,
15 TABLE_STRING
, "foooo",
18 TABLE_SIZE
, (uint64_t) (673.7*1024*1024),
19 TABLE_STRING
, "Wed 2018-07-11 00:10:33 JST",
20 TABLE_STRING
, "Wed 2018-07-11 00:16:00 JST") >= 0);
22 table_set_width(table
, 75);
23 assert_se(table_format(table
, &formatted
) >= 0);
25 printf("%s\n", formatted
);
26 assert_se(streq(formatted
,
27 "NAME TYPE RO USAGE CREATED MODIFIED \n"
28 "foooo raw no 673.6M Wed 2018-07-11 00:10:33 J… Wed 2018-07-11 00:16:00 JST\n"
32 int main(int argc
, char *argv
[]) {
34 _cleanup_(table_unrefp
) Table
*t
= NULL
;
35 _cleanup_free_
char *formatted
= NULL
;
37 assert_se(setenv("COLUMNS", "40", 1) >= 0);
39 assert_se(t
= table_new("one", "two", "three"));
41 assert_se(table_set_align_percent(t
, TABLE_HEADER_CELL(2), 100) >= 0);
43 assert_se(table_add_many(t
,
46 TABLE_BOOLEAN
, true) >= 0);
48 assert_se(table_add_many(t
,
49 TABLE_STRING
, "a long field",
51 TABLE_BOOLEAN
, false) >= 0);
53 assert_se(table_format(t
, &formatted
) >= 0);
54 printf("%s\n", formatted
);
56 assert_se(streq(formatted
,
59 "a long field yyy no\n"));
61 formatted
= mfree(formatted
);
63 table_set_width(t
, 40);
65 assert_se(table_format(t
, &formatted
) >= 0);
66 printf("%s\n", formatted
);
68 assert_se(streq(formatted
,
71 "a long field yyy no\n"));
73 formatted
= mfree(formatted
);
75 table_set_width(t
, 12);
76 assert_se(table_format(t
, &formatted
) >= 0);
77 printf("%s\n", formatted
);
79 assert_se(streq(formatted
,
84 formatted
= mfree(formatted
);
86 table_set_width(t
, 5);
87 assert_se(table_format(t
, &formatted
) >= 0);
88 printf("%s\n", formatted
);
90 assert_se(streq(formatted
,
95 formatted
= mfree(formatted
);
97 table_set_width(t
, 3);
98 assert_se(table_format(t
, &formatted
) >= 0);
99 printf("%s\n", formatted
);
101 assert_se(streq(formatted
,
106 formatted
= mfree(formatted
);
108 table_set_width(t
, (size_t) -1);
109 assert_se(table_set_sort(t
, (size_t) 0, (size_t) 2, (size_t) -1) >= 0);
111 assert_se(table_format(t
, &formatted
) >= 0);
112 printf("%s\n", formatted
);
114 assert_se(streq(formatted
,
116 "a long field yyy no\n"
119 formatted
= mfree(formatted
);
121 table_set_header(t
, false);
123 assert_se(table_add_many(t
,
124 TABLE_STRING
, "fäää",
126 TABLE_BOOLEAN
, true) >= 0);
128 assert_se(table_add_many(t
,
129 TABLE_STRING
, "fäää",
131 TABLE_BOOLEAN
, false) >= 0);
133 assert_se(table_add_many(t
,
135 TABLE_SIZE
, (uint64_t) 4711,
136 TABLE_TIMESPAN
, (usec_t
) 5*USEC_PER_MINUTE
) >= 0);
138 assert_se(table_format(t
, &formatted
) >= 0);
139 printf("%s\n", formatted
);
141 assert_se(streq(formatted
,
142 "a long field yyy no\n"
148 formatted
= mfree(formatted
);
150 assert_se(table_set_display(t
, (size_t) 2, (size_t) 0, (size_t) 2, (size_t) 0, (size_t) 0, (size_t) -1) >= 0);
152 assert_se(table_format(t
, &formatted
) >= 0);
153 printf("%s\n", formatted
);
155 assert_se(streq(formatted
,
156 " no a long f… no a long f… a long fi…\n"
157 " no fäää no fäää fäää \n"
158 " yes fäää yes fäää fäää \n"
159 " yes xxx yes xxx xxx \n"