]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-format-table.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "format-table.h"
7 #include "string-util.h"
11 static void test_issue_9549(void) {
12 _cleanup_(table_unrefp
) Table
*table
= NULL
;
13 _cleanup_free_
char *formatted
= NULL
;
15 log_info("/* %s */", __func__
);
17 assert_se(table
= table_new("name", "type", "ro", "usage", "created", "modified"));
18 assert_se(table_set_align_percent(table
, TABLE_HEADER_CELL(3), 100) >= 0);
19 assert_se(table_add_many(table
,
20 TABLE_STRING
, "foooo",
23 TABLE_SIZE
, (uint64_t) (673.7*1024*1024),
24 TABLE_STRING
, "Wed 2018-07-11 00:10:33 JST",
25 TABLE_STRING
, "Wed 2018-07-11 00:16:00 JST") >= 0);
27 table_set_width(table
, 75);
28 assert_se(table_format(table
, &formatted
) >= 0);
30 printf("%s\n", formatted
);
31 assert_se(streq(formatted
,
32 "NAME TYPE RO USAGE CREATED MODIFIED \n"
33 "foooo raw no 673.6M Wed 2018-07-11 00:10:33 J… Wed 2018-07-11 00:16:00 JST\n"
37 static void test_multiline(void) {
38 _cleanup_(table_unrefp
) Table
*table
= NULL
;
39 _cleanup_free_
char *formatted
= NULL
;
41 log_info("/* %s */", __func__
);
43 assert_se(table
= table_new("foo", "bar"));
45 assert_se(table_set_align_percent(table
, TABLE_HEADER_CELL(1), 100) >= 0);
47 assert_se(table_add_many(table
,
48 TABLE_STRING
, "three\ndifferent\nlines",
49 TABLE_STRING
, "two\nlines\n") >= 0);
51 table_set_cell_height_max(table
, 1);
52 assert_se(table_format(table
, &formatted
) >= 0);
53 fputs(formatted
, stdout
);
54 assert_se(streq(formatted
,
57 formatted
= mfree(formatted
);
59 table_set_cell_height_max(table
, 2);
60 assert_se(table_format(table
, &formatted
) >= 0);
61 fputs(formatted
, stdout
);
62 assert_se(streq(formatted
,
65 "different… lines\n"));
66 formatted
= mfree(formatted
);
68 table_set_cell_height_max(table
, 3);
69 assert_se(table_format(table
, &formatted
) >= 0);
70 fputs(formatted
, stdout
);
71 assert_se(streq(formatted
,
76 formatted
= mfree(formatted
);
78 table_set_cell_height_max(table
, (size_t) -1);
79 assert_se(table_format(table
, &formatted
) >= 0);
80 fputs(formatted
, stdout
);
81 assert_se(streq(formatted
,
86 formatted
= mfree(formatted
);
88 assert_se(table_add_many(table
,
89 TABLE_STRING
, "short",
90 TABLE_STRING
, "a\npair") >= 0);
92 assert_se(table_add_many(table
,
93 TABLE_STRING
, "short2\n",
94 TABLE_STRING
, "a\nfour\nline\ncell") >= 0);
96 table_set_cell_height_max(table
, 1);
97 assert_se(table_format(table
, &formatted
) >= 0);
98 fputs(formatted
, stdout
);
99 assert_se(streq(formatted
,
104 formatted
= mfree(formatted
);
106 table_set_cell_height_max(table
, 2);
107 assert_se(table_format(table
, &formatted
) >= 0);
108 fputs(formatted
, stdout
);
109 assert_se(streq(formatted
,
117 formatted
= mfree(formatted
);
119 table_set_cell_height_max(table
, 3);
120 assert_se(table_format(table
, &formatted
) >= 0);
121 fputs(formatted
, stdout
);
122 assert_se(streq(formatted
,
132 formatted
= mfree(formatted
);
134 table_set_cell_height_max(table
, (size_t) -1);
135 assert_se(table_format(table
, &formatted
) >= 0);
136 fputs(formatted
, stdout
);
137 assert_se(streq(formatted
,
148 formatted
= mfree(formatted
);
151 static void test_strv(void) {
152 _cleanup_(table_unrefp
) Table
*table
= NULL
;
153 _cleanup_free_
char *formatted
= NULL
;
155 log_info("/* %s */", __func__
);
157 assert_se(table
= table_new("foo", "bar"));
159 assert_se(table_set_align_percent(table
, TABLE_HEADER_CELL(1), 100) >= 0);
161 assert_se(table_add_many(table
,
162 TABLE_STRV
, STRV_MAKE("three", "different", "lines"),
163 TABLE_STRV
, STRV_MAKE("two", "lines")) >= 0);
165 table_set_cell_height_max(table
, 1);
166 assert_se(table_format(table
, &formatted
) >= 0);
167 fputs(formatted
, stdout
);
168 assert_se(streq(formatted
,
171 formatted
= mfree(formatted
);
173 table_set_cell_height_max(table
, 2);
174 assert_se(table_format(table
, &formatted
) >= 0);
175 fputs(formatted
, stdout
);
176 assert_se(streq(formatted
,
179 "different… lines\n"));
180 formatted
= mfree(formatted
);
182 table_set_cell_height_max(table
, 3);
183 assert_se(table_format(table
, &formatted
) >= 0);
184 fputs(formatted
, stdout
);
185 assert_se(streq(formatted
,
190 formatted
= mfree(formatted
);
192 table_set_cell_height_max(table
, (size_t) -1);
193 assert_se(table_format(table
, &formatted
) >= 0);
194 fputs(formatted
, stdout
);
195 assert_se(streq(formatted
,
200 formatted
= mfree(formatted
);
202 assert_se(table_add_many(table
,
203 TABLE_STRING
, "short",
204 TABLE_STRV
, STRV_MAKE("a", "pair")) >= 0);
206 assert_se(table_add_many(table
,
207 TABLE_STRV
, STRV_MAKE("short2"),
208 TABLE_STRV
, STRV_MAKE("a", "four", "line", "cell")) >= 0);
210 table_set_cell_height_max(table
, 1);
211 assert_se(table_format(table
, &formatted
) >= 0);
212 fputs(formatted
, stdout
);
213 assert_se(streq(formatted
,
218 formatted
= mfree(formatted
);
220 table_set_cell_height_max(table
, 2);
221 assert_se(table_format(table
, &formatted
) >= 0);
222 fputs(formatted
, stdout
);
223 assert_se(streq(formatted
,
231 formatted
= mfree(formatted
);
233 table_set_cell_height_max(table
, 3);
234 assert_se(table_format(table
, &formatted
) >= 0);
235 fputs(formatted
, stdout
);
236 assert_se(streq(formatted
,
246 formatted
= mfree(formatted
);
248 table_set_cell_height_max(table
, (size_t) -1);
249 assert_se(table_format(table
, &formatted
) >= 0);
250 fputs(formatted
, stdout
);
251 assert_se(streq(formatted
,
262 formatted
= mfree(formatted
);
265 static void test_strv_wrapped(void) {
266 _cleanup_(table_unrefp
) Table
*table
= NULL
;
267 _cleanup_free_
char *formatted
= NULL
;
269 log_info("/* %s */", __func__
);
271 assert_se(table
= table_new("foo", "bar"));
273 assert_se(table_set_align_percent(table
, TABLE_HEADER_CELL(1), 100) >= 0);
275 assert_se(table_add_many(table
,
276 TABLE_STRV_WRAPPED
, STRV_MAKE("three", "different", "lines"),
277 TABLE_STRV_WRAPPED
, STRV_MAKE("two", "lines")) >= 0);
279 table_set_cell_height_max(table
, 1);
280 assert_se(table_format(table
, &formatted
) >= 0);
281 fputs(formatted
, stdout
);
282 assert_se(streq(formatted
,
284 "three different lines two lines\n"));
285 formatted
= mfree(formatted
);
287 table_set_cell_height_max(table
, 2);
288 assert_se(table_format(table
, &formatted
) >= 0);
289 fputs(formatted
, stdout
);
290 assert_se(streq(formatted
,
292 "three different lines two lines\n"));
293 formatted
= mfree(formatted
);
295 table_set_cell_height_max(table
, 3);
296 assert_se(table_format(table
, &formatted
) >= 0);
297 fputs(formatted
, stdout
);
298 assert_se(streq(formatted
,
300 "three different lines two lines\n"));
301 formatted
= mfree(formatted
);
303 table_set_cell_height_max(table
, (size_t) -1);
304 assert_se(table_format(table
, &formatted
) >= 0);
305 fputs(formatted
, stdout
);
306 assert_se(streq(formatted
,
308 "three different lines two lines\n"));
309 formatted
= mfree(formatted
);
311 assert_se(table_add_many(table
,
312 TABLE_STRING
, "short",
313 TABLE_STRV_WRAPPED
, STRV_MAKE("a", "pair")) >= 0);
315 assert_se(table_add_many(table
,
316 TABLE_STRV_WRAPPED
, STRV_MAKE("short2"),
317 TABLE_STRV_WRAPPED
, STRV_MAKE("a", "eight", "line", "ćęłł",
318 "___5___", "___6___", "___7___", "___8___")) >= 0);
320 table_set_cell_height_max(table
, 1);
321 assert_se(table_format(table
, &formatted
) >= 0);
322 fputs(formatted
, stdout
);
323 assert_se(streq(formatted
,
325 "three different… two lines\n"
327 "short2 a eight line ćęłł…\n"));
328 formatted
= mfree(formatted
);
330 table_set_cell_height_max(table
, 2);
331 assert_se(table_format(table
, &formatted
) >= 0);
332 fputs(formatted
, stdout
);
333 assert_se(streq(formatted
,
335 "three different two lines\n"
338 "short2 a eight line ćęłł\n"
339 " ___5___ ___6___…\n"));
340 formatted
= mfree(formatted
);
342 table_set_cell_height_max(table
, 3);
343 assert_se(table_format(table
, &formatted
) >= 0);
344 fputs(formatted
, stdout
);
345 assert_se(streq(formatted
,
347 "three different two lines\n"
350 "short2 a eight line ćęłł\n"
352 " ___7___ ___8___\n"));
353 formatted
= mfree(formatted
);
355 table_set_cell_height_max(table
, (size_t) -1);
356 assert_se(table_format(table
, &formatted
) >= 0);
357 fputs(formatted
, stdout
);
358 assert_se(streq(formatted
,
360 "three different two lines\n"
363 "short2 a eight line ćęłł\n"
365 " ___7___ ___8___\n"));
366 formatted
= mfree(formatted
);
369 int main(int argc
, char *argv
[]) {
370 _cleanup_(table_unrefp
) Table
*t
= NULL
;
371 _cleanup_free_
char *formatted
= NULL
;
373 assert_se(setenv("SYSTEMD_COLORS", "0", 1) >= 0);
374 assert_se(setenv("COLUMNS", "40", 1) >= 0);
376 assert_se(t
= table_new("one", "two", "three"));
378 assert_se(table_set_align_percent(t
, TABLE_HEADER_CELL(2), 100) >= 0);
380 assert_se(table_add_many(t
,
383 TABLE_BOOLEAN
, true) >= 0);
385 assert_se(table_add_many(t
,
386 TABLE_STRING
, "a long field",
388 TABLE_SET_UPPERCASE
, 1,
389 TABLE_BOOLEAN
, false) >= 0);
391 assert_se(table_format(t
, &formatted
) >= 0);
392 printf("%s\n", formatted
);
394 assert_se(streq(formatted
,
397 "a long field YYY no\n"));
399 formatted
= mfree(formatted
);
401 table_set_width(t
, 40);
403 assert_se(table_format(t
, &formatted
) >= 0);
404 printf("%s\n", formatted
);
406 assert_se(streq(formatted
,
409 "a long field YYY no\n"));
411 formatted
= mfree(formatted
);
413 table_set_width(t
, 12);
414 assert_se(table_format(t
, &formatted
) >= 0);
415 printf("%s\n", formatted
);
417 assert_se(streq(formatted
,
422 formatted
= mfree(formatted
);
424 table_set_width(t
, 5);
425 assert_se(table_format(t
, &formatted
) >= 0);
426 printf("%s\n", formatted
);
428 assert_se(streq(formatted
,
433 formatted
= mfree(formatted
);
435 table_set_width(t
, 3);
436 assert_se(table_format(t
, &formatted
) >= 0);
437 printf("%s\n", formatted
);
439 assert_se(streq(formatted
,
444 formatted
= mfree(formatted
);
446 table_set_width(t
, (size_t) -1);
447 assert_se(table_set_sort(t
, (size_t) 0, (size_t) 2, (size_t) -1) >= 0);
449 assert_se(table_format(t
, &formatted
) >= 0);
450 printf("%s\n", formatted
);
452 assert_se(streq(formatted
,
454 "a long field YYY no\n"
457 formatted
= mfree(formatted
);
459 table_set_header(t
, false);
461 assert_se(table_add_many(t
,
462 TABLE_STRING
, "fäää",
464 TABLE_BOOLEAN
, true) >= 0);
466 assert_se(table_add_many(t
,
467 TABLE_STRING
, "fäää",
469 TABLE_BOOLEAN
, false) >= 0);
471 assert_se(table_add_many(t
,
473 TABLE_SIZE
, (uint64_t) 4711,
474 TABLE_TIMESPAN
, (usec_t
) 5*USEC_PER_MINUTE
) >= 0);
476 assert_se(table_format(t
, &formatted
) >= 0);
477 printf("%s\n", formatted
);
479 assert_se(streq(formatted
,
480 "a long field YYY no\n"
486 formatted
= mfree(formatted
);
488 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);
490 assert_se(table_format(t
, &formatted
) >= 0);
491 printf("%s\n", formatted
);
493 if (isatty(STDOUT_FILENO
))
494 assert_se(streq(formatted
,
495 " no a long f… no a long f… a long fi…\n"
496 " no fäää no fäää fäää \n"
497 " yes fäää yes fäää fäää \n"
498 " yes xxx yes xxx xxx \n"
501 assert_se(streq(formatted
,
502 " no a long field no a long field a long field\n"
503 " no fäää no fäää fäää \n"
504 " yes fäää yes fäää fäää \n"
505 " yes xxx yes xxx xxx \n"