1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 //#include "alloc-util.h"
4 //#include "format-table.h"
5 //#include "string-util.h"
6 //#include "time-util.h"
8 int main(int argc, char *argv[]) {
10 _cleanup_(table_unrefp) Table *t = NULL;
11 _cleanup_free_ char *formatted = NULL;
13 assert_se(setenv("COLUMNS", "40", 1) >= 0);
15 assert_se(t = table_new("ONE", "TWO", "THREE"));
17 assert_se(table_set_align_percent(t, TABLE_HEADER_CELL(2), 100) >= 0);
19 assert_se(table_add_many(t,
22 TABLE_BOOLEAN, true) >= 0);
24 assert_se(table_add_many(t,
25 TABLE_STRING, "a long field",
27 TABLE_BOOLEAN, false) >= 0);
29 assert_se(table_format(t, &formatted) >= 0);
30 printf("%s\n", formatted);
32 assert_se(streq(formatted,
35 "a long field yyy no\n"));
37 formatted = mfree(formatted);
39 table_set_width(t, 40);
41 assert_se(table_format(t, &formatted) >= 0);
42 printf("%s\n", formatted);
44 assert_se(streq(formatted,
47 "a long field yyy no\n"));
49 formatted = mfree(formatted);
51 table_set_width(t, 12);
52 assert_se(table_format(t, &formatted) >= 0);
53 printf("%s\n", formatted);
55 assert_se(streq(formatted,
60 formatted = mfree(formatted);
62 table_set_width(t, 5);
63 assert_se(table_format(t, &formatted) >= 0);
64 printf("%s\n", formatted);
66 assert_se(streq(formatted,
71 formatted = mfree(formatted);
73 table_set_width(t, 3);
74 assert_se(table_format(t, &formatted) >= 0);
75 printf("%s\n", formatted);
77 assert_se(streq(formatted,
82 formatted = mfree(formatted);
84 table_set_width(t, (size_t) -1);
85 assert_se(table_set_sort(t, (size_t) 0, (size_t) 2, (size_t) -1) >= 0);
87 assert_se(table_format(t, &formatted) >= 0);
88 printf("%s\n", formatted);
90 assert_se(streq(formatted,
92 "a long field yyy no\n"
95 formatted = mfree(formatted);
97 table_set_header(t, false);
99 assert_se(table_add_many(t,
100 TABLE_STRING, "fäää",
102 TABLE_BOOLEAN, true) >= 0);
104 assert_se(table_add_many(t,
105 TABLE_STRING, "fäää",
107 TABLE_BOOLEAN, false) >= 0);
109 assert_se(table_add_many(t,
111 TABLE_SIZE, (uint64_t) 4711,
112 TABLE_TIMESPAN, (usec_t) 5*USEC_PER_MINUTE) >= 0);
114 assert_se(table_format(t, &formatted) >= 0);
115 printf("%s\n", formatted);
117 assert_se(streq(formatted,
118 "a long field yyy no\n"
124 formatted = mfree(formatted);
126 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);
128 assert_se(table_format(t, &formatted) >= 0);
129 printf("%s\n", formatted);
131 assert_se(streq(formatted,
132 " no a long f… no a long f… a long fi…\n"
133 " no fäää no fäää fäää \n"
134 " yes fäää yes fäää fäää \n"
135 " yes xxx yes xxx xxx \n"