]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-format-table.c
Merge pull request #16532 from yuwata/network-sync-state-file
[thirdparty/systemd.git] / src / test / test-format-table.c
CommitLineData
1960e736
LP
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
0db41a8f
ZJS
3#include <unistd.h>
4
1960e736
LP
5#include "alloc-util.h"
6#include "format-table.h"
7#include "string-util.h"
bbaba574 8#include "strv.h"
1960e736
LP
9#include "time-util.h"
10
a6e96471
YW
11static void test_issue_9549(void) {
12 _cleanup_(table_unrefp) Table *table = NULL;
13 _cleanup_free_ char *formatted = NULL;
14
9969b542 15 assert_se(table = table_new("name", "type", "ro", "usage", "created", "modified"));
a6e96471
YW
16 assert_se(table_set_align_percent(table, TABLE_HEADER_CELL(3), 100) >= 0);
17 assert_se(table_add_many(table,
18 TABLE_STRING, "foooo",
19 TABLE_STRING, "raw",
20 TABLE_BOOLEAN, false,
21 TABLE_SIZE, (uint64_t) (673.7*1024*1024),
22 TABLE_STRING, "Wed 2018-07-11 00:10:33 JST",
23 TABLE_STRING, "Wed 2018-07-11 00:16:00 JST") >= 0);
24
25 table_set_width(table, 75);
26 assert_se(table_format(table, &formatted) >= 0);
27
28 printf("%s\n", formatted);
29 assert_se(streq(formatted,
30 "NAME TYPE RO USAGE CREATED MODIFIED \n"
31 "foooo raw no 673.6M Wed 2018-07-11 00:10:33 J… Wed 2018-07-11 00:16:00 JST\n"
32 ));
33}
34
d91614e7
LP
35static void test_multiline(void) {
36 _cleanup_(table_unrefp) Table *table = NULL;
37 _cleanup_free_ char *formatted = NULL;
38
39 assert_se(table = table_new("foo", "bar"));
40
41 assert_se(table_set_align_percent(table, TABLE_HEADER_CELL(1), 100) >= 0);
42
43 assert_se(table_add_many(table,
44 TABLE_STRING, "three\ndifferent\nlines",
45 TABLE_STRING, "two\nlines\n") >= 0);
46
47 table_set_cell_height_max(table, 1);
48 assert_se(table_format(table, &formatted) >= 0);
49 fputs(formatted, stdout);
50 assert_se(streq(formatted,
51 "FOO BAR\n"
52 "three… two…\n"));
53 formatted = mfree(formatted);
54
55 table_set_cell_height_max(table, 2);
56 assert_se(table_format(table, &formatted) >= 0);
57 fputs(formatted, stdout);
58 assert_se(streq(formatted,
59 "FOO BAR\n"
60 "three two\n"
61 "different… lines\n"));
62 formatted = mfree(formatted);
63
64 table_set_cell_height_max(table, 3);
65 assert_se(table_format(table, &formatted) >= 0);
66 fputs(formatted, stdout);
67 assert_se(streq(formatted,
68 "FOO BAR\n"
69 "three two\n"
70 "different lines\n"
71 "lines \n"));
72 formatted = mfree(formatted);
73
74 table_set_cell_height_max(table, (size_t) -1);
75 assert_se(table_format(table, &formatted) >= 0);
76 fputs(formatted, stdout);
77 assert_se(streq(formatted,
78 "FOO BAR\n"
79 "three two\n"
80 "different lines\n"
81 "lines \n"));
82 formatted = mfree(formatted);
83
84 assert_se(table_add_many(table,
85 TABLE_STRING, "short",
86 TABLE_STRING, "a\npair") >= 0);
87
88 assert_se(table_add_many(table,
89 TABLE_STRING, "short2\n",
90 TABLE_STRING, "a\nfour\nline\ncell") >= 0);
91
92 table_set_cell_height_max(table, 1);
93 assert_se(table_format(table, &formatted) >= 0);
94 fputs(formatted, stdout);
95 assert_se(streq(formatted,
96 "FOO BAR\n"
97 "three… two…\n"
98 "short a…\n"
99 "short2 a…\n"));
100 formatted = mfree(formatted);
101
102 table_set_cell_height_max(table, 2);
103 assert_se(table_format(table, &formatted) >= 0);
104 fputs(formatted, stdout);
105 assert_se(streq(formatted,
106 "FOO BAR\n"
107 "three two\n"
108 "different… lines\n"
109 "short a\n"
110 " pair\n"
111 "short2 a\n"
112 " four…\n"));
113 formatted = mfree(formatted);
114
115 table_set_cell_height_max(table, 3);
116 assert_se(table_format(table, &formatted) >= 0);
117 fputs(formatted, stdout);
118 assert_se(streq(formatted,
119 "FOO BAR\n"
120 "three two\n"
121 "different lines\n"
122 "lines \n"
123 "short a\n"
124 " pair\n"
125 "short2 a\n"
126 " four\n"
127 " line…\n"));
128 formatted = mfree(formatted);
129
130 table_set_cell_height_max(table, (size_t) -1);
131 assert_se(table_format(table, &formatted) >= 0);
132 fputs(formatted, stdout);
133 assert_se(streq(formatted,
134 "FOO BAR\n"
135 "three two\n"
136 "different lines\n"
137 "lines \n"
138 "short a\n"
139 " pair\n"
140 "short2 a\n"
141 " four\n"
142 " line\n"
143 " cell\n"));
144 formatted = mfree(formatted);
145}
146
bbaba574
YW
147static void test_strv(void) {
148 _cleanup_(table_unrefp) Table *table = NULL;
149 _cleanup_free_ char *formatted = NULL;
150
151 assert_se(table = table_new("foo", "bar"));
152
153 assert_se(table_set_align_percent(table, TABLE_HEADER_CELL(1), 100) >= 0);
154
155 assert_se(table_add_many(table,
156 TABLE_STRV, STRV_MAKE("three", "different", "lines"),
157 TABLE_STRV, STRV_MAKE("two", "lines")) >= 0);
158
159 table_set_cell_height_max(table, 1);
160 assert_se(table_format(table, &formatted) >= 0);
161 fputs(formatted, stdout);
162 assert_se(streq(formatted,
163 "FOO BAR\n"
164 "three… two…\n"));
165 formatted = mfree(formatted);
166
167 table_set_cell_height_max(table, 2);
168 assert_se(table_format(table, &formatted) >= 0);
169 fputs(formatted, stdout);
170 assert_se(streq(formatted,
171 "FOO BAR\n"
172 "three two\n"
173 "different… lines\n"));
174 formatted = mfree(formatted);
175
176 table_set_cell_height_max(table, 3);
177 assert_se(table_format(table, &formatted) >= 0);
178 fputs(formatted, stdout);
179 assert_se(streq(formatted,
180 "FOO BAR\n"
181 "three two\n"
182 "different lines\n"
183 "lines \n"));
184 formatted = mfree(formatted);
185
186 table_set_cell_height_max(table, (size_t) -1);
187 assert_se(table_format(table, &formatted) >= 0);
188 fputs(formatted, stdout);
189 assert_se(streq(formatted,
190 "FOO BAR\n"
191 "three two\n"
192 "different lines\n"
193 "lines \n"));
194 formatted = mfree(formatted);
195
196 assert_se(table_add_many(table,
197 TABLE_STRING, "short",
198 TABLE_STRV, STRV_MAKE("a", "pair")) >= 0);
199
200 assert_se(table_add_many(table,
201 TABLE_STRV, STRV_MAKE("short2"),
202 TABLE_STRV, STRV_MAKE("a", "four", "line", "cell")) >= 0);
203
204 table_set_cell_height_max(table, 1);
205 assert_se(table_format(table, &formatted) >= 0);
206 fputs(formatted, stdout);
207 assert_se(streq(formatted,
208 "FOO BAR\n"
209 "three… two…\n"
210 "short a…\n"
211 "short2 a…\n"));
212 formatted = mfree(formatted);
213
214 table_set_cell_height_max(table, 2);
215 assert_se(table_format(table, &formatted) >= 0);
216 fputs(formatted, stdout);
217 assert_se(streq(formatted,
218 "FOO BAR\n"
219 "three two\n"
220 "different… lines\n"
221 "short a\n"
222 " pair\n"
223 "short2 a\n"
224 " four…\n"));
225 formatted = mfree(formatted);
226
227 table_set_cell_height_max(table, 3);
228 assert_se(table_format(table, &formatted) >= 0);
229 fputs(formatted, stdout);
230 assert_se(streq(formatted,
231 "FOO BAR\n"
232 "three two\n"
233 "different lines\n"
234 "lines \n"
235 "short a\n"
236 " pair\n"
237 "short2 a\n"
238 " four\n"
239 " line…\n"));
240 formatted = mfree(formatted);
241
242 table_set_cell_height_max(table, (size_t) -1);
243 assert_se(table_format(table, &formatted) >= 0);
244 fputs(formatted, stdout);
245 assert_se(streq(formatted,
246 "FOO BAR\n"
247 "three two\n"
248 "different lines\n"
249 "lines \n"
250 "short a\n"
251 " pair\n"
252 "short2 a\n"
253 " four\n"
254 " line\n"
255 " cell\n"));
256 formatted = mfree(formatted);
257}
258
1960e736
LP
259int main(int argc, char *argv[]) {
260
261 _cleanup_(table_unrefp) Table *t = NULL;
262 _cleanup_free_ char *formatted = NULL;
263
dea55040 264 assert_se(setenv("SYSTEMD_COLORS", "0", 1) >= 0);
1960e736
LP
265 assert_se(setenv("COLUMNS", "40", 1) >= 0);
266
9969b542 267 assert_se(t = table_new("one", "two", "three"));
1960e736
LP
268
269 assert_se(table_set_align_percent(t, TABLE_HEADER_CELL(2), 100) >= 0);
270
271 assert_se(table_add_many(t,
272 TABLE_STRING, "xxx",
273 TABLE_STRING, "yyy",
274 TABLE_BOOLEAN, true) >= 0);
275
276 assert_se(table_add_many(t,
277 TABLE_STRING, "a long field",
278 TABLE_STRING, "yyy",
8792cecc 279 TABLE_SET_UPPERCASE, 1,
1960e736
LP
280 TABLE_BOOLEAN, false) >= 0);
281
282 assert_se(table_format(t, &formatted) >= 0);
283 printf("%s\n", formatted);
284
285 assert_se(streq(formatted,
286 "ONE TWO THREE\n"
287 "xxx yyy yes\n"
8792cecc 288 "a long field YYY no\n"));
1960e736
LP
289
290 formatted = mfree(formatted);
291
292 table_set_width(t, 40);
293
294 assert_se(table_format(t, &formatted) >= 0);
295 printf("%s\n", formatted);
296
297 assert_se(streq(formatted,
298 "ONE TWO THREE\n"
299 "xxx yyy yes\n"
8792cecc 300 "a long field YYY no\n"));
1960e736
LP
301
302 formatted = mfree(formatted);
303
304 table_set_width(t, 12);
305 assert_se(table_format(t, &formatted) >= 0);
306 printf("%s\n", formatted);
307
308 assert_se(streq(formatted,
309 "ONE TWO THR…\n"
310 "xxx yyy yes\n"
8792cecc 311 "a … YYY no\n"));
1960e736
LP
312
313 formatted = mfree(formatted);
314
315 table_set_width(t, 5);
316 assert_se(table_format(t, &formatted) >= 0);
317 printf("%s\n", formatted);
318
319 assert_se(streq(formatted,
320 "… … …\n"
321 "… … …\n"
322 "… … …\n"));
323
324 formatted = mfree(formatted);
325
326 table_set_width(t, 3);
327 assert_se(table_format(t, &formatted) >= 0);
328 printf("%s\n", formatted);
329
330 assert_se(streq(formatted,
331 "… … …\n"
332 "… … …\n"
333 "… … …\n"));
334
335 formatted = mfree(formatted);
336
337 table_set_width(t, (size_t) -1);
338 assert_se(table_set_sort(t, (size_t) 0, (size_t) 2, (size_t) -1) >= 0);
339
340 assert_se(table_format(t, &formatted) >= 0);
341 printf("%s\n", formatted);
342
343 assert_se(streq(formatted,
344 "ONE TWO THREE\n"
8792cecc 345 "a long field YYY no\n"
1960e736
LP
346 "xxx yyy yes\n"));
347
348 formatted = mfree(formatted);
349
350 table_set_header(t, false);
351
352 assert_se(table_add_many(t,
353 TABLE_STRING, "fäää",
354 TABLE_STRING, "uuu",
355 TABLE_BOOLEAN, true) >= 0);
356
357 assert_se(table_add_many(t,
358 TABLE_STRING, "fäää",
359 TABLE_STRING, "zzz",
360 TABLE_BOOLEAN, false) >= 0);
361
362 assert_se(table_add_many(t,
363 TABLE_EMPTY,
364 TABLE_SIZE, (uint64_t) 4711,
365 TABLE_TIMESPAN, (usec_t) 5*USEC_PER_MINUTE) >= 0);
366
367 assert_se(table_format(t, &formatted) >= 0);
368 printf("%s\n", formatted);
369
370 assert_se(streq(formatted,
8792cecc 371 "a long field YYY no\n"
1960e736
LP
372 "fäää zzz no\n"
373 "fäää uuu yes\n"
374 "xxx yyy yes\n"
375 " 4.6K 5min\n"));
376
377 formatted = mfree(formatted);
378
379 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);
380
381 assert_se(table_format(t, &formatted) >= 0);
382 printf("%s\n", formatted);
383
0db41a8f
ZJS
384 if (isatty(STDOUT_FILENO))
385 assert_se(streq(formatted,
386 " no a long f… no a long f… a long fi…\n"
387 " no fäää no fäää fäää \n"
388 " yes fäää yes fäää fäää \n"
389 " yes xxx yes xxx xxx \n"
390 "5min 5min \n"));
391 else
392 assert_se(streq(formatted,
393 " no a long field no a long field a long field\n"
394 " no fäää no fäää fäää \n"
395 " yes fäää yes fäää fäää \n"
396 " yes xxx yes xxx xxx \n"
397 "5min 5min \n"));
1960e736 398
a6e96471 399 test_issue_9549();
d91614e7 400 test_multiline();
bbaba574 401 test_strv();
a6e96471 402
1960e736
LP
403 return 0;
404}