]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-format-table.c
b2943e6fc1c44d2e3f83be0159ada7978c88ecb5
[thirdparty/systemd.git] / src / test / test-format-table.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "alloc-util.h"
6 #include "format-table.h"
7 #include "string-util.h"
8 #include "strv.h"
9 #include "time-util.h"
10
11 static void test_issue_9549(void) {
12 _cleanup_(table_unrefp) Table *table = NULL;
13 _cleanup_free_ char *formatted = NULL;
14
15 log_info("/* %s */", __func__);
16
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",
21 TABLE_STRING, "raw",
22 TABLE_BOOLEAN, false,
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);
26
27 table_set_width(table, 75);
28 assert_se(table_format(table, &formatted) >= 0);
29
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"
34 ));
35 }
36
37 static void test_multiline(void) {
38 _cleanup_(table_unrefp) Table *table = NULL;
39 _cleanup_free_ char *formatted = NULL;
40
41 log_info("/* %s */", __func__);
42
43 assert_se(table = table_new("foo", "bar"));
44
45 assert_se(table_set_align_percent(table, TABLE_HEADER_CELL(1), 100) >= 0);
46
47 assert_se(table_add_many(table,
48 TABLE_STRING, "three\ndifferent\nlines",
49 TABLE_STRING, "two\nlines\n") >= 0);
50
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,
55 "FOO BAR\n"
56 "three… two…\n"));
57 formatted = mfree(formatted);
58
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,
63 "FOO BAR\n"
64 "three two\n"
65 "different… lines\n"));
66 formatted = mfree(formatted);
67
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,
72 "FOO BAR\n"
73 "three two\n"
74 "different lines\n"
75 "lines \n"));
76 formatted = mfree(formatted);
77
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,
82 "FOO BAR\n"
83 "three two\n"
84 "different lines\n"
85 "lines \n"));
86 formatted = mfree(formatted);
87
88 assert_se(table_add_many(table,
89 TABLE_STRING, "short",
90 TABLE_STRING, "a\npair") >= 0);
91
92 assert_se(table_add_many(table,
93 TABLE_STRING, "short2\n",
94 TABLE_STRING, "a\nfour\nline\ncell") >= 0);
95
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,
100 "FOO BAR\n"
101 "three… two…\n"
102 "short a…\n"
103 "short2 a…\n"));
104 formatted = mfree(formatted);
105
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,
110 "FOO BAR\n"
111 "three two\n"
112 "different… lines\n"
113 "short a\n"
114 " pair\n"
115 "short2 a\n"
116 " four…\n"));
117 formatted = mfree(formatted);
118
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,
123 "FOO BAR\n"
124 "three two\n"
125 "different lines\n"
126 "lines \n"
127 "short a\n"
128 " pair\n"
129 "short2 a\n"
130 " four\n"
131 " line…\n"));
132 formatted = mfree(formatted);
133
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,
138 "FOO BAR\n"
139 "three two\n"
140 "different lines\n"
141 "lines \n"
142 "short a\n"
143 " pair\n"
144 "short2 a\n"
145 " four\n"
146 " line\n"
147 " cell\n"));
148 formatted = mfree(formatted);
149 }
150
151 static void test_strv(void) {
152 _cleanup_(table_unrefp) Table *table = NULL;
153 _cleanup_free_ char *formatted = NULL;
154
155 log_info("/* %s */", __func__);
156
157 assert_se(table = table_new("foo", "bar"));
158
159 assert_se(table_set_align_percent(table, TABLE_HEADER_CELL(1), 100) >= 0);
160
161 assert_se(table_add_many(table,
162 TABLE_STRV, STRV_MAKE("three", "different", "lines"),
163 TABLE_STRV, STRV_MAKE("two", "lines")) >= 0);
164
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,
169 "FOO BAR\n"
170 "three… two…\n"));
171 formatted = mfree(formatted);
172
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,
177 "FOO BAR\n"
178 "three two\n"
179 "different… lines\n"));
180 formatted = mfree(formatted);
181
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,
186 "FOO BAR\n"
187 "three two\n"
188 "different lines\n"
189 "lines \n"));
190 formatted = mfree(formatted);
191
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,
196 "FOO BAR\n"
197 "three two\n"
198 "different lines\n"
199 "lines \n"));
200 formatted = mfree(formatted);
201
202 assert_se(table_add_many(table,
203 TABLE_STRING, "short",
204 TABLE_STRV, STRV_MAKE("a", "pair")) >= 0);
205
206 assert_se(table_add_many(table,
207 TABLE_STRV, STRV_MAKE("short2"),
208 TABLE_STRV, STRV_MAKE("a", "four", "line", "cell")) >= 0);
209
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,
214 "FOO BAR\n"
215 "three… two…\n"
216 "short a…\n"
217 "short2 a…\n"));
218 formatted = mfree(formatted);
219
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,
224 "FOO BAR\n"
225 "three two\n"
226 "different… lines\n"
227 "short a\n"
228 " pair\n"
229 "short2 a\n"
230 " four…\n"));
231 formatted = mfree(formatted);
232
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,
237 "FOO BAR\n"
238 "three two\n"
239 "different lines\n"
240 "lines \n"
241 "short a\n"
242 " pair\n"
243 "short2 a\n"
244 " four\n"
245 " line…\n"));
246 formatted = mfree(formatted);
247
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,
252 "FOO BAR\n"
253 "three two\n"
254 "different lines\n"
255 "lines \n"
256 "short a\n"
257 " pair\n"
258 "short2 a\n"
259 " four\n"
260 " line\n"
261 " cell\n"));
262 formatted = mfree(formatted);
263 }
264
265 static void test_strv_wrapped(void) {
266 _cleanup_(table_unrefp) Table *table = NULL;
267 _cleanup_free_ char *formatted = NULL;
268
269 log_info("/* %s */", __func__);
270
271 assert_se(table = table_new("foo", "bar"));
272
273 assert_se(table_set_align_percent(table, TABLE_HEADER_CELL(1), 100) >= 0);
274
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);
278
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,
283 "FOO BAR\n"
284 "three different lines two lines\n"));
285 formatted = mfree(formatted);
286
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,
291 "FOO BAR\n"
292 "three different lines two lines\n"));
293 formatted = mfree(formatted);
294
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,
299 "FOO BAR\n"
300 "three different lines two lines\n"));
301 formatted = mfree(formatted);
302
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,
307 "FOO BAR\n"
308 "three different lines two lines\n"));
309 formatted = mfree(formatted);
310
311 assert_se(table_add_many(table,
312 TABLE_STRING, "short",
313 TABLE_STRV_WRAPPED, STRV_MAKE("a", "pair")) >= 0);
314
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);
319
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,
324 "FOO BAR\n"
325 "three different… two lines\n"
326 "short a pair\n"
327 "short2 a eight line ćęłł…\n"));
328 formatted = mfree(formatted);
329
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,
334 "FOO BAR\n"
335 "three different two lines\n"
336 "lines \n"
337 "short a pair\n"
338 "short2 a eight line ćęłł\n"
339 " ___5___ ___6___…\n"));
340 formatted = mfree(formatted);
341
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,
346 "FOO BAR\n"
347 "three different two lines\n"
348 "lines \n"
349 "short a pair\n"
350 "short2 a eight line ćęłł\n"
351 " ___5___ ___6___\n"
352 " ___7___ ___8___\n"));
353 formatted = mfree(formatted);
354
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,
359 "FOO BAR\n"
360 "three different two lines\n"
361 "lines \n"
362 "short a pair\n"
363 "short2 a eight line ćęłł\n"
364 " ___5___ ___6___\n"
365 " ___7___ ___8___\n"));
366 formatted = mfree(formatted);
367 }
368
369 int main(int argc, char *argv[]) {
370 _cleanup_(table_unrefp) Table *t = NULL;
371 _cleanup_free_ char *formatted = NULL;
372
373 assert_se(setenv("SYSTEMD_COLORS", "0", 1) >= 0);
374 assert_se(setenv("COLUMNS", "40", 1) >= 0);
375
376 assert_se(t = table_new("one", "two", "three"));
377
378 assert_se(table_set_align_percent(t, TABLE_HEADER_CELL(2), 100) >= 0);
379
380 assert_se(table_add_many(t,
381 TABLE_STRING, "xxx",
382 TABLE_STRING, "yyy",
383 TABLE_BOOLEAN, true) >= 0);
384
385 assert_se(table_add_many(t,
386 TABLE_STRING, "a long field",
387 TABLE_STRING, "yyy",
388 TABLE_SET_UPPERCASE, 1,
389 TABLE_BOOLEAN, false) >= 0);
390
391 assert_se(table_format(t, &formatted) >= 0);
392 printf("%s\n", formatted);
393
394 assert_se(streq(formatted,
395 "ONE TWO THREE\n"
396 "xxx yyy yes\n"
397 "a long field YYY no\n"));
398
399 formatted = mfree(formatted);
400
401 table_set_width(t, 40);
402
403 assert_se(table_format(t, &formatted) >= 0);
404 printf("%s\n", formatted);
405
406 assert_se(streq(formatted,
407 "ONE TWO THREE\n"
408 "xxx yyy yes\n"
409 "a long field YYY no\n"));
410
411 formatted = mfree(formatted);
412
413 table_set_width(t, 12);
414 assert_se(table_format(t, &formatted) >= 0);
415 printf("%s\n", formatted);
416
417 assert_se(streq(formatted,
418 "ONE TWO THR…\n"
419 "xxx yyy yes\n"
420 "a … YYY no\n"));
421
422 formatted = mfree(formatted);
423
424 table_set_width(t, 5);
425 assert_se(table_format(t, &formatted) >= 0);
426 printf("%s\n", formatted);
427
428 assert_se(streq(formatted,
429 "… … …\n"
430 "… … …\n"
431 "… … …\n"));
432
433 formatted = mfree(formatted);
434
435 table_set_width(t, 3);
436 assert_se(table_format(t, &formatted) >= 0);
437 printf("%s\n", formatted);
438
439 assert_se(streq(formatted,
440 "… … …\n"
441 "… … …\n"
442 "… … …\n"));
443
444 formatted = mfree(formatted);
445
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);
448
449 assert_se(table_format(t, &formatted) >= 0);
450 printf("%s\n", formatted);
451
452 assert_se(streq(formatted,
453 "ONE TWO THREE\n"
454 "a long field YYY no\n"
455 "xxx yyy yes\n"));
456
457 formatted = mfree(formatted);
458
459 table_set_header(t, false);
460
461 assert_se(table_add_many(t,
462 TABLE_STRING, "fäää",
463 TABLE_STRING, "uuu",
464 TABLE_BOOLEAN, true) >= 0);
465
466 assert_se(table_add_many(t,
467 TABLE_STRING, "fäää",
468 TABLE_STRING, "zzz",
469 TABLE_BOOLEAN, false) >= 0);
470
471 assert_se(table_add_many(t,
472 TABLE_EMPTY,
473 TABLE_SIZE, (uint64_t) 4711,
474 TABLE_TIMESPAN, (usec_t) 5*USEC_PER_MINUTE) >= 0);
475
476 assert_se(table_format(t, &formatted) >= 0);
477 printf("%s\n", formatted);
478
479 assert_se(streq(formatted,
480 "a long field YYY no\n"
481 "fäää zzz no\n"
482 "fäää uuu yes\n"
483 "xxx yyy yes\n"
484 " 4.6K 5min\n"));
485
486 formatted = mfree(formatted);
487
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);
489
490 assert_se(table_format(t, &formatted) >= 0);
491 printf("%s\n", formatted);
492
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"
499 "5min 5min \n"));
500 else
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"
506 "5min 5min \n"));
507
508 test_issue_9549();
509 test_multiline();
510 test_strv();
511 test_strv_wrapped();
512
513 return 0;
514 }