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