chiark / gitweb /
Prep v239: Mask all unneeded functions in the new format-table.[hc] files.
[elogind.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 int main(int argc, char *argv[]) {
9
10         _cleanup_(table_unrefp) Table *t = NULL;
11         _cleanup_free_ char *formatted = NULL;
12
13         assert_se(setenv("COLUMNS", "40", 1) >= 0);
14
15         assert_se(t = table_new("ONE", "TWO", "THREE"));
16
17         assert_se(table_set_align_percent(t, TABLE_HEADER_CELL(2), 100) >= 0);
18
19         assert_se(table_add_many(t,
20                                  TABLE_STRING, "xxx",
21                                  TABLE_STRING, "yyy",
22                                  TABLE_BOOLEAN, true) >= 0);
23
24         assert_se(table_add_many(t,
25                                  TABLE_STRING, "a long field",
26                                  TABLE_STRING, "yyy",
27                                  TABLE_BOOLEAN, false) >= 0);
28
29         assert_se(table_format(t, &formatted) >= 0);
30         printf("%s\n", formatted);
31
32         assert_se(streq(formatted,
33                         "ONE          TWO THREE\n"
34                         "xxx          yyy   yes\n"
35                         "a long field yyy    no\n"));
36
37         formatted = mfree(formatted);
38
39         table_set_width(t, 40);
40
41         assert_se(table_format(t, &formatted) >= 0);
42         printf("%s\n", formatted);
43
44         assert_se(streq(formatted,
45                         "ONE                TWO             THREE\n"
46                         "xxx                yyy               yes\n"
47                         "a long field       yyy                no\n"));
48
49         formatted = mfree(formatted);
50
51         table_set_width(t, 12);
52         assert_se(table_format(t, &formatted) >= 0);
53         printf("%s\n", formatted);
54
55         assert_se(streq(formatted,
56                         "ONE TWO THR…\n"
57                         "xxx yyy  yes\n"
58                         "a … yyy   no\n"));
59
60         formatted = mfree(formatted);
61
62         table_set_width(t, 5);
63         assert_se(table_format(t, &formatted) >= 0);
64         printf("%s\n", formatted);
65
66         assert_se(streq(formatted,
67                         "… … …\n"
68                         "… … …\n"
69                         "… … …\n"));
70
71         formatted = mfree(formatted);
72
73         table_set_width(t, 3);
74         assert_se(table_format(t, &formatted) >= 0);
75         printf("%s\n", formatted);
76
77         assert_se(streq(formatted,
78                         "… … …\n"
79                         "… … …\n"
80                         "… … …\n"));
81
82         formatted = mfree(formatted);
83
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);
86
87         assert_se(table_format(t, &formatted) >= 0);
88         printf("%s\n", formatted);
89
90         assert_se(streq(formatted,
91                         "ONE          TWO THREE\n"
92                         "a long field yyy    no\n"
93                         "xxx          yyy   yes\n"));
94
95         formatted = mfree(formatted);
96
97         table_set_header(t, false);
98
99         assert_se(table_add_many(t,
100                                  TABLE_STRING, "fäää",
101                                  TABLE_STRING, "uuu",
102                                  TABLE_BOOLEAN, true) >= 0);
103
104         assert_se(table_add_many(t,
105                                  TABLE_STRING, "fäää",
106                                  TABLE_STRING, "zzz",
107                                  TABLE_BOOLEAN, false) >= 0);
108
109         assert_se(table_add_many(t,
110                                  TABLE_EMPTY,
111                                  TABLE_SIZE, (uint64_t) 4711,
112                                  TABLE_TIMESPAN, (usec_t) 5*USEC_PER_MINUTE) >= 0);
113
114         assert_se(table_format(t, &formatted) >= 0);
115         printf("%s\n", formatted);
116
117         assert_se(streq(formatted,
118                         "a long field yyy    no\n"
119                         "fäää         zzz    no\n"
120                         "fäää         uuu   yes\n"
121                         "xxx          yyy   yes\n"
122                         "             4.6K 5min\n"));
123
124         formatted = mfree(formatted);
125
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);
127
128         assert_se(table_format(t, &formatted) >= 0);
129         printf("%s\n", formatted);
130
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"
136                         "5min           5min                     \n"));
137
138         return 0;
139 }