]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-ellipsize.c
format-table: automatically show empty cells in grey
[thirdparty/systemd.git] / src / test / test-ellipsize.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
35d811f5
SL
2
3#include <stdio.h>
4
b5efdb8a 5#include "alloc-util.h"
35d811f5 6#include "def.h"
07630cea 7#include "string-util.h"
9924aef6 8#include "strv.h"
07630cea
LP
9#include "terminal-util.h"
10#include "util.h"
9924aef6 11#include "utf8.h"
35d811f5 12
9924aef6
ZJS
13static void test_ellipsize_mem_one(const char *s, size_t old_length, size_t new_length) {
14 _cleanup_free_ char *n = NULL;
15 _cleanup_free_ char *t1 = NULL, *t2 = NULL, *t3 = NULL;
16 char buf[LINE_MAX];
17 bool has_wide_chars;
18 size_t max_width;
19
20 n = memdup_suffix0(s, old_length);
21
22 if (!utf8_is_valid(n))
23 /* We don't support invalid sequences… */
24 return;
25
26 /* Report out inputs. We duplicate the data so that cellescape
27 * can properly report truncated multibyte sequences. */
28 log_info("%s \"%s\" old_length=%zu/%zu new_length=%zu", __func__,
29 cellescape(buf, sizeof buf, n),
30 old_length, utf8_console_width(n),
31 new_length);
32
33 /* To keep this test simple, any case with wide chars starts with this glyph */
34 has_wide_chars = startswith(s, "你");
35 max_width = MIN(utf8_console_width(n), new_length);
36
37 t1 = ellipsize_mem(n, old_length, new_length, 30);
38 log_info("30%% → %s utf8_console_width=%zu", t1, utf8_console_width(t1));
39 if (!has_wide_chars)
40 assert_se(utf8_console_width(t1) == max_width);
41 else
42 assert_se(utf8_console_width(t1) <= max_width);
43
44 t2 = ellipsize_mem(n, old_length, new_length, 90);
45 log_info("90%% → %s utf8_console_width=%zu", t2, utf8_console_width(t2));
46 if (!has_wide_chars)
47 assert_se(utf8_console_width(t2) == max_width);
48 else
49 assert_se(utf8_console_width(t2) <= max_width);
50
51 t3 = ellipsize_mem(n, old_length, new_length, 100);
52 log_info("100%% → %s utf8_console_width=%zu", t3, utf8_console_width(t3));
53 if (!has_wide_chars)
54 assert_se(utf8_console_width(t3) == max_width);
55 else
56 assert_se(utf8_console_width(t3) <= max_width);
57
58 if (new_length >= old_length) {
59 assert_se(streq(t1, n));
60 assert_se(streq(t2, n));
61 assert_se(streq(t3, n));
62 }
63}
64
65static void test_ellipsize_mem(void) {
66 const char *s;
67 ssize_t l, k;
68
69 FOREACH_STRING(s,
70 "_XXXXXXXXXXX_", /* ASCII */
71 "_aąęółśćńżźć_", /* two-byte utf-8 */
72 "გამარჯობა", /* multi-byte utf-8 */
73 "你好世界", /* wide characters */
74 "你გą世óoó界") /* a mix */
75
76 for (l = strlen(s); l >= 0; l--)
77 for (k = strlen(s) + 1; k >= 0; k--)
78 test_ellipsize_mem_one(s, l, k);
79}
80
81static void test_ellipsize_one(const char *p) {
35d811f5
SL
82 _cleanup_free_ char *t;
83 t = ellipsize(p, columns(), 70);
84 puts(t);
c30a49b2
LP
85 free(t);
86 t = ellipsize(p, columns(), 0);
87 puts(t);
88 free(t);
89 t = ellipsize(p, columns(), 100);
90 puts(t);
91 free(t);
92 t = ellipsize(p, 0, 50);
93 puts(t);
94 free(t);
95 t = ellipsize(p, 1, 50);
96 puts(t);
97 free(t);
98 t = ellipsize(p, 2, 50);
99 puts(t);
100 free(t);
101 t = ellipsize(p, 3, 50);
102 puts(t);
103 free(t);
104 t = ellipsize(p, 4, 50);
105 puts(t);
106 free(t);
107 t = ellipsize(p, 5, 50);
108 puts(t);
35d811f5
SL
109}
110
9924aef6
ZJS
111static void test_ellipsize(void) {
112 test_ellipsize_one(DIGITS LETTERS DIGITS LETTERS);
113 test_ellipsize_one("한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어");
114 test_ellipsize_one("-日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国");
115 test_ellipsize_one("中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国-中国中国中国中国中国中国中国中国中国中国中国中国中国");
116 test_ellipsize_one("sÿstëmd sÿstëmd sÿstëmd sÿstëmd sÿstëmd sÿstëmd sÿstëmd sÿstëmd sÿstëmd sÿstëmd sÿstëmd sÿstëmd sÿstëmd");
117 test_ellipsize_one("🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮");
118 test_ellipsize_one("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
119 test_ellipsize_one("shórt");
120}
121
35d811f5 122int main(int argc, char *argv[]) {
9924aef6
ZJS
123 test_ellipsize_mem();
124 test_ellipsize();
35d811f5
SL
125
126 return 0;
127}