]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-format-table.c
libudev: hide definition of struct udev_list from other libudev components
[thirdparty/systemd.git] / src / test / test-format-table.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "alloc-util.h"
4 #include "format-table.h"
5 #include "string-util.h"
6 #include "time-util.h"
7
8 static void test_issue_9549(void) {
9 _cleanup_(table_unrefp) Table *table = NULL;
10 _cleanup_free_ char *formatted = NULL;
11
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",
16 TABLE_STRING, "raw",
17 TABLE_BOOLEAN, false,
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);
21
22 table_set_width(table, 75);
23 assert_se(table_format(table, &formatted) >= 0);
24
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"
29 ));
30 }
31
32 int main(int argc, char *argv[]) {
33
34 _cleanup_(table_unrefp) Table *t = NULL;
35 _cleanup_free_ char *formatted = NULL;
36
37 assert_se(setenv("COLUMNS", "40", 1) >= 0);
38
39 assert_se(t = table_new("one", "two", "three"));
40
41 assert_se(table_set_align_percent(t, TABLE_HEADER_CELL(2), 100) >= 0);
42
43 assert_se(table_add_many(t,
44 TABLE_STRING, "xxx",
45 TABLE_STRING, "yyy",
46 TABLE_BOOLEAN, true) >= 0);
47
48 assert_se(table_add_many(t,
49 TABLE_STRING, "a long field",
50 TABLE_STRING, "yyy",
51 TABLE_BOOLEAN, false) >= 0);
52
53 assert_se(table_format(t, &formatted) >= 0);
54 printf("%s\n", formatted);
55
56 assert_se(streq(formatted,
57 "ONE TWO THREE\n"
58 "xxx yyy yes\n"
59 "a long field yyy no\n"));
60
61 formatted = mfree(formatted);
62
63 table_set_width(t, 40);
64
65 assert_se(table_format(t, &formatted) >= 0);
66 printf("%s\n", formatted);
67
68 assert_se(streq(formatted,
69 "ONE TWO THREE\n"
70 "xxx yyy yes\n"
71 "a long field yyy no\n"));
72
73 formatted = mfree(formatted);
74
75 table_set_width(t, 12);
76 assert_se(table_format(t, &formatted) >= 0);
77 printf("%s\n", formatted);
78
79 assert_se(streq(formatted,
80 "ONE TWO THR…\n"
81 "xxx yyy yes\n"
82 "a … yyy no\n"));
83
84 formatted = mfree(formatted);
85
86 table_set_width(t, 5);
87 assert_se(table_format(t, &formatted) >= 0);
88 printf("%s\n", formatted);
89
90 assert_se(streq(formatted,
91 "… … …\n"
92 "… … …\n"
93 "… … …\n"));
94
95 formatted = mfree(formatted);
96
97 table_set_width(t, 3);
98 assert_se(table_format(t, &formatted) >= 0);
99 printf("%s\n", formatted);
100
101 assert_se(streq(formatted,
102 "… … …\n"
103 "… … …\n"
104 "… … …\n"));
105
106 formatted = mfree(formatted);
107
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);
110
111 assert_se(table_format(t, &formatted) >= 0);
112 printf("%s\n", formatted);
113
114 assert_se(streq(formatted,
115 "ONE TWO THREE\n"
116 "a long field yyy no\n"
117 "xxx yyy yes\n"));
118
119 formatted = mfree(formatted);
120
121 table_set_header(t, false);
122
123 assert_se(table_add_many(t,
124 TABLE_STRING, "fäää",
125 TABLE_STRING, "uuu",
126 TABLE_BOOLEAN, true) >= 0);
127
128 assert_se(table_add_many(t,
129 TABLE_STRING, "fäää",
130 TABLE_STRING, "zzz",
131 TABLE_BOOLEAN, false) >= 0);
132
133 assert_se(table_add_many(t,
134 TABLE_EMPTY,
135 TABLE_SIZE, (uint64_t) 4711,
136 TABLE_TIMESPAN, (usec_t) 5*USEC_PER_MINUTE) >= 0);
137
138 assert_se(table_format(t, &formatted) >= 0);
139 printf("%s\n", formatted);
140
141 assert_se(streq(formatted,
142 "a long field yyy no\n"
143 "fäää zzz no\n"
144 "fäää uuu yes\n"
145 "xxx yyy yes\n"
146 " 4.6K 5min\n"));
147
148 formatted = mfree(formatted);
149
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);
151
152 assert_se(table_format(t, &formatted) >= 0);
153 printf("%s\n", formatted);
154
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"
160 "5min 5min \n"));
161
162 test_issue_9549();
163
164 return 0;
165 }