1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4
5 #include "alloc-util.h"
6 #include "def.h"
7 #include "string-util.h"
8 #include "strv.h"
9 #include "terminal-util.h"
10 #include "tests.h"
11 #include "util.h"
12 #include "utf8.h"
13
test_ellipsize_mem_one(const char * s,size_t old_length,size_t new_length)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_se(streq(t1, n));
61 assert_se(streq(t2, n));
62 assert_se(streq(t3, n));
63 }
64 }
65
TEST(ellipsize_mem)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
test_ellipsize_one(const char * p)78 static void test_ellipsize_one(const char *p) {
79 _cleanup_free_ char *t;
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
TEST(ellipsize)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 DEFINE_TEST_MAIN(LOG_INFO);
120