chiark / gitweb /
tree-wide: beautify remaining copyright statements
[elogind.git] / src / test / test-ellipsize.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   Copyright © 2013 Shawn Landden
4 ***/
5
6 #include <stdio.h>
7
8 #include "alloc-util.h"
9 #include "def.h"
10 #include "string-util.h"
11 //#include "strv.h"
12 #include "terminal-util.h"
13 #include "util.h"
14 //#include "utf8.h"
15
16 static void test_ellipsize_mem_one(const char *s, size_t old_length, size_t new_length) {
17         _cleanup_free_ char *n = NULL;
18         _cleanup_free_ char *t1 = NULL, *t2 = NULL, *t3 = NULL;
19         char buf[LINE_MAX];
20         bool has_wide_chars;
21         size_t max_width;
22
23         n = memdup_suffix0(s, old_length);
24
25         if (!utf8_is_valid(n))
26                 /* We don't support invalid sequences… */
27                 return;
28
29         /* Report out inputs. We duplicate the data so that cellescape
30          * can properly report truncated multibyte sequences. */
31         log_info("%s \"%s\" old_length=%zu/%zu new_length=%zu", __func__,
32                  cellescape(buf, sizeof buf, n),
33                  old_length, utf8_console_width(n),
34                  new_length);
35
36         /* To keep this test simple, any case with wide chars starts with this glyph */
37         has_wide_chars = startswith(s, "你");
38         max_width = MIN(utf8_console_width(n), new_length);
39
40         t1 = ellipsize_mem(n, old_length, new_length, 30);
41         log_info("30%% → %s utf8_console_width=%zu", t1, utf8_console_width(t1));
42         if (!has_wide_chars)
43                 assert_se(utf8_console_width(t1) == max_width);
44         else
45                 assert_se(utf8_console_width(t1) <= max_width);
46
47         t2 = ellipsize_mem(n, old_length, new_length, 90);
48         log_info("90%% → %s utf8_console_width=%zu", t2, utf8_console_width(t2));
49         if (!has_wide_chars)
50                 assert_se(utf8_console_width(t2) == max_width);
51         else
52                 assert_se(utf8_console_width(t2) <= max_width);
53
54         t3 = ellipsize_mem(n, old_length, new_length, 100);
55         log_info("100%% → %s utf8_console_width=%zu", t3, utf8_console_width(t3));
56         if (!has_wide_chars)
57                 assert_se(utf8_console_width(t3) == max_width);
58         else
59                 assert_se(utf8_console_width(t3) <= max_width);
60
61         if (new_length >= old_length) {
62                 assert_se(streq(t1, n));
63                 assert_se(streq(t2, n));
64                 assert_se(streq(t3, n));
65         }
66 }
67
68 static void test_ellipsize_mem(void) {
69         const char *s;
70         ssize_t l, k;
71
72         FOREACH_STRING(s,
73                        "_XXXXXXXXXXX_", /* ASCII */
74                        "_aąęółśćńżźć_", /* two-byte utf-8 */
75                        "გამარჯობა",     /* multi-byte utf-8 */
76                        "你好世界",       /* wide characters */
77                        "你გą世óoó界")    /* a mix */
78
79                 for (l = strlen(s); l >= 0; l--)
80                         for (k = strlen(s) + 1; k >= 0; k--)
81                                 test_ellipsize_mem_one(s, l, k);
82 }
83
84 static void test_ellipsize_one(const char *p) {
85         _cleanup_free_ char *t;
86         t = ellipsize(p, columns(), 70);
87         puts(t);
88         free(t);
89         t = ellipsize(p, columns(), 0);
90         puts(t);
91         free(t);
92         t = ellipsize(p, columns(), 100);
93         puts(t);
94         free(t);
95         t = ellipsize(p, 0, 50);
96         puts(t);
97         free(t);
98         t = ellipsize(p, 1, 50);
99         puts(t);
100         free(t);
101         t = ellipsize(p, 2, 50);
102         puts(t);
103         free(t);
104         t = ellipsize(p, 3, 50);
105         puts(t);
106         free(t);
107         t = ellipsize(p, 4, 50);
108         puts(t);
109         free(t);
110         t = ellipsize(p, 5, 50);
111         puts(t);
112 }
113
114 static void test_ellipsize(void) {
115         test_ellipsize_one(DIGITS LETTERS DIGITS LETTERS);
116         test_ellipsize_one("한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어");
117         test_ellipsize_one("-日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国");
118         test_ellipsize_one("中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国-中国中国中国中国中国中国中国中国中国中国中国中国中国");
119         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");
120         test_ellipsize_one("🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮");
121         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.");
122         test_ellipsize_one("shórt");
123 }
124
125 int main(int argc, char *argv[]) {
126         test_ellipsize_mem();
127         test_ellipsize();
128
129         return 0;
130 }