]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-ellipsize.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / test / test-ellipsize.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
35d811f5 2/***
96b2fb93 3 Copyright © 2013 Shawn Landden
35d811f5
SL
4***/
5
6#include <stdio.h>
7
b5efdb8a 8#include "alloc-util.h"
35d811f5 9#include "def.h"
07630cea 10#include "string-util.h"
9924aef6 11#include "strv.h"
07630cea
LP
12#include "terminal-util.h"
13#include "util.h"
9924aef6 14#include "utf8.h"
35d811f5 15
9924aef6
ZJS
16static 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
68static 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
84static void test_ellipsize_one(const char *p) {
35d811f5
SL
85 _cleanup_free_ char *t;
86 t = ellipsize(p, columns(), 70);
87 puts(t);
c30a49b2
LP
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);
35d811f5
SL
112}
113
9924aef6
ZJS
114static 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
35d811f5 125int main(int argc, char *argv[]) {
9924aef6
ZJS
126 test_ellipsize_mem();
127 test_ellipsize();
35d811f5
SL
128
129 return 0;
130}