]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-ellipsize.c
ASSERT_STREQ for simple cases
[thirdparty/systemd.git] / src / test / test-ellipsize.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4
5 #include "alloc-util.h"
6 #include "constants.h"
7 #include "escape.h"
8 #include "string-util.h"
9 #include "strv.h"
10 #include "terminal-util.h"
11 #include "tests.h"
12 #include "utf8.h"
13
14 static void test_ellipsize_mem_one(const char *s, size_t old_length, size_t new_length) {
15 _cleanup_free_ char *n = NULL;
16 _cleanup_free_ char *t1 = NULL, *t2 = NULL, *t3 = NULL;
17 char buf[LINE_MAX];
18 bool has_wide_chars;
19 size_t max_width;
20
21 n = memdup_suffix0(s, old_length);
22
23 if (!utf8_is_valid(n))
24 /* We don't support invalid sequences… */
25 return;
26
27 /* Report out inputs. We duplicate the data so that cellescape
28 * can properly report truncated multibyte sequences. */
29 log_info("%s \"%s\" old_length=%zu/%zu new_length=%zu", __func__,
30 cellescape(buf, sizeof buf, n),
31 old_length, utf8_console_width(n),
32 new_length);
33
34 /* To keep this test simple, any case with wide chars starts with this glyph */
35 has_wide_chars = startswith(s, "你");
36 max_width = MIN(utf8_console_width(n), new_length);
37
38 t1 = ellipsize_mem(n, old_length, new_length, 30);
39 log_info("30%% → %s utf8_console_width=%zu", t1, utf8_console_width(t1));
40 if (!has_wide_chars)
41 assert_se(utf8_console_width(t1) == max_width);
42 else
43 assert_se(utf8_console_width(t1) <= max_width);
44
45 t2 = ellipsize_mem(n, old_length, new_length, 90);
46 log_info("90%% → %s utf8_console_width=%zu", t2, utf8_console_width(t2));
47 if (!has_wide_chars)
48 assert_se(utf8_console_width(t2) == max_width);
49 else
50 assert_se(utf8_console_width(t2) <= max_width);
51
52 t3 = ellipsize_mem(n, old_length, new_length, 100);
53 log_info("100%% → %s utf8_console_width=%zu", t3, utf8_console_width(t3));
54 if (!has_wide_chars)
55 assert_se(utf8_console_width(t3) == max_width);
56 else
57 assert_se(utf8_console_width(t3) <= max_width);
58
59 if (new_length >= old_length) {
60 ASSERT_STREQ(t1, n);
61 ASSERT_STREQ(t2, n);
62 ASSERT_STREQ(t3, n);
63 }
64 }
65
66 TEST(ellipsize_mem) {
67 FOREACH_STRING(s,
68 "_XXXXXXXXXXX_", /* ASCII */
69 "_aąęółśćńżźć_", /* two-byte utf-8 */
70 "გამარჯობა", /* multi-byte utf-8 */
71 "你好世界", /* wide characters */
72 "你გą世óoó界") /* a mix */
73 for (ssize_t l = strlen(s); l >= 0; l--)
74 for (ssize_t k = strlen(s) + 1; k >= 0; k--)
75 test_ellipsize_mem_one(s, l, k);
76 }
77
78 static void test_ellipsize_one(const char *p) {
79 _cleanup_free_ char *t = NULL;
80 t = ellipsize(p, columns(), 70);
81 puts(t);
82 free(t);
83 t = ellipsize(p, columns(), 0);
84 puts(t);
85 free(t);
86 t = ellipsize(p, columns(), 100);
87 puts(t);
88 free(t);
89 t = ellipsize(p, 0, 50);
90 puts(t);
91 free(t);
92 t = ellipsize(p, 1, 50);
93 puts(t);
94 free(t);
95 t = ellipsize(p, 2, 50);
96 puts(t);
97 free(t);
98 t = ellipsize(p, 3, 50);
99 puts(t);
100 free(t);
101 t = ellipsize(p, 4, 50);
102 puts(t);
103 free(t);
104 t = ellipsize(p, 5, 50);
105 puts(t);
106 }
107
108 TEST(ellipsize) {
109 test_ellipsize_one(DIGITS LETTERS DIGITS LETTERS);
110 test_ellipsize_one("한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어");
111 test_ellipsize_one("-日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国");
112 test_ellipsize_one("中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国-中国中国中国中国中国中国中国中国中国中国中国中国中国");
113 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");
114 test_ellipsize_one("🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮");
115 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.");
116 test_ellipsize_one("shórt");
117 }
118
119 TEST(ellipsize_ansi) {
120 const char *s = ANSI_HIGHLIGHT_YELLOW_UNDERLINE "yęllow"
121 ANSI_HIGHLIGHT_GREY_UNDERLINE "grěy"
122 ANSI_HIGHLIGHT_BLUE_UNDERLINE "blue"
123 ANSI_NORMAL "nórmął";
124 size_t len = strlen(s);
125
126 for (unsigned percent = 0; percent <= 100; percent += 15)
127 for (ssize_t x = 21; x >= 0; x--) {
128 _cleanup_free_ char *t = ellipsize_mem(s, len, x, percent);
129 printf("%02zd: \"%s\"\n", x, t);
130 assert_se(utf8_is_valid(t));
131
132 if (DEBUG_LOGGING) {
133 _cleanup_free_ char *e = cescape(t);
134 printf(" : \"%s\"\n", e);
135 }
136 }
137 }
138
139 TEST(ellipsize_ansi_cats) {
140 _cleanup_free_ char *e = NULL, *f = NULL, *g = NULL, *h = NULL;
141
142 /* Make sure we don't cut off in the middle of an ANSI escape sequence. */
143
144 e = ellipsize("01" ANSI_NORMAL "23", 4, 0);
145 puts(e);
146 ASSERT_STREQ(e, "01" ANSI_NORMAL "23");
147 f = ellipsize("ab" ANSI_NORMAL "cd", 4, 90);
148 puts(f);
149 ASSERT_STREQ(f, "ab" ANSI_NORMAL "cd");
150
151 g = ellipsize("🐱🐱" ANSI_NORMAL "🐱🐱" ANSI_NORMAL, 5, 0);
152 puts(g);
153 ASSERT_STREQ(g, "…" ANSI_NORMAL "🐱🐱" ANSI_NORMAL);
154 h = ellipsize("🐱🐱" ANSI_NORMAL "🐱🐱" ANSI_NORMAL, 5, 90);
155 puts(h);
156 ASSERT_STREQ(h, "🐱…" ANSI_NORMAL "🐱" ANSI_NORMAL);
157 }
158
159 DEFINE_TEST_MAIN(LOG_INFO);