Debian Codegraph @

utest/unicode_test.c

raw
1 /*
2 * boxes - Command line filter to draw/remove ASCII boxes around text
3 * Copyright (c) 1999-2024 Thomas Jensen and the boxes contributors
4 *
5 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
6 * License, version 3, as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
8 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
9 * details.
10 * You should have received a copy of the GNU General Public License along with this program.
11 * If not, see <https://www.gnu.org/licenses/>.
12 *
13 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
14 */
15
16 /*
17 * Unit tests of the 'unicode' module
18 */
19
20 #include "config.h"
21
22 #include <setjmp.h>
23 #include <stdarg.h>
24 #include <stddef.h>
25
26 #include <cmocka.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistr.h>
30
31 #include "boxes.h"
32 #include "tools.h"
33 #include "unicode.h"
34 #include "unicode_test.h"
35
36
37
38 void test_to_utf32(void **state)
39 {
40 UNUSED(state);
41
42 uint32_t *ustr32 = u32_strconv_from_arg("A", "ASCII");
43 assert_non_null(ustr32);
44
45 ucs4_t actual_A = to_utf32('A');
46 ucs4_t actual_space = to_utf32(' ');
47 ucs4_t actual_invalid = to_utf32('\x1B');
48
49 assert_int_equal(0, memcmp(ustr32, &actual_A, sizeof(ucs4_t)));
50 assert_int_equal(0, memcmp(&char_space, &actual_space, sizeof(ucs4_t)));
51 assert_int_equal(0, memcmp(&char_nul, &actual_invalid, sizeof(ucs4_t)));
52 }
53
54
55
56 void test_is_blank(void **state)
57 {
58 UNUSED(state);
59
60 const ucs4_t char_emspace = 0x00002003;
61 const ucs4_t char_enspace = 0x00002002;
62
63 assert_int_equal(1, is_blank(char_space));
64 assert_int_equal(1, is_blank(char_emspace));
65 assert_int_equal(1, is_blank(char_enspace));
66 assert_int_equal(1, is_blank(char_tab));
67
68 assert_int_equal(0, is_blank(to_utf32('x')));
69 assert_int_equal(0, is_blank(char_cr));
70 assert_int_equal(0, is_blank(char_newline));
71 assert_int_equal(0, is_blank(char_esc));
72 }
73
74
75
76 void test_is_allowed_in_sample(void **state)
77 {
78 UNUSED(state);
79
80 const ucs4_t char_bell = 0x00000007;
81 const ucs4_t char_backsp = 0x00000008;
82 const ucs4_t char_u_umlaut = 0x000000fc;
83
84 assert_int_equal(1, is_allowed_in_sample(to_utf32('x')));
85 assert_int_equal(1, is_allowed_in_sample(char_u_umlaut));
86 assert_int_equal(1, is_allowed_in_sample(char_space));
87 assert_int_equal(1, is_allowed_in_sample(char_esc));
88 assert_int_equal(1, is_allowed_in_sample(char_cr));
89 assert_int_equal(1, is_allowed_in_sample(char_newline));
90 assert_int_equal(1, is_allowed_in_sample(char_tab));
91
92 assert_int_equal(0, is_allowed_in_sample(char_bell));
93 assert_int_equal(0, is_allowed_in_sample(char_backsp));
94 }
95
96
97
98 void test_is_allowed_in_shape(void **state)
99 {
100 UNUSED(state);
101
102 const ucs4_t char_bell = 0x00000007;
103 const ucs4_t char_backsp = 0x00000008;
104 const ucs4_t char_u_umlaut = 0x000000fc;
105
106 assert_int_equal(1, is_allowed_in_shape(to_utf32('x')));
107 assert_int_equal(1, is_allowed_in_shape(char_u_umlaut));
108 assert_int_equal(1, is_allowed_in_shape(char_space));
109 assert_int_equal(1, is_allowed_in_shape(char_esc));
110 assert_int_equal(1, is_allowed_in_shape(char_tab)); /* But tabs are deprecated in shapes! Temporary only. */
111
112 assert_int_equal(0, is_allowed_in_shape(char_bell));
113 assert_int_equal(0, is_allowed_in_shape(char_backsp));
114 assert_int_equal(0, is_allowed_in_shape(char_cr));
115 assert_int_equal(0, is_allowed_in_shape(char_newline));
116 }
117
118
119
120 void test_is_allowed_in_filename(void **state)
121 {
122 UNUSED(state);
123
124 const ucs4_t char_bell = 0x00000007;
125 const ucs4_t char_backsp = 0x00000008;
126 const ucs4_t char_u_umlaut = 0x000000fc;
127
128 assert_int_equal(1, is_allowed_in_filename(to_utf32('x')));
129 assert_int_equal(1, is_allowed_in_filename(char_u_umlaut));
130 assert_int_equal(1, is_allowed_in_filename(char_space));
131 assert_int_equal(1, is_allowed_in_filename(char_tab));
132
133 assert_int_equal(0, is_allowed_in_filename(char_esc));
134 assert_int_equal(0, is_allowed_in_filename(char_bell));
135 assert_int_equal(0, is_allowed_in_filename(char_backsp));
136 assert_int_equal(0, is_allowed_in_filename(char_cr));
137 assert_int_equal(0, is_allowed_in_filename(char_newline));
138 }
139
140
141
142 void test_is_allowed_in_kv_string(void **state)
143 {
144 UNUSED(state);
145
146 const ucs4_t char_bell = 0x00000007;
147 const ucs4_t char_backsp = 0x00000008;
148 const ucs4_t char_u_umlaut = 0x000000fc;
149
150 assert_int_equal(1, is_allowed_in_kv_string(to_utf32('x')));
151 assert_int_equal(1, is_allowed_in_kv_string(char_u_umlaut));
152 assert_int_equal(1, is_allowed_in_kv_string(char_space));
153 assert_int_equal(1, is_allowed_in_kv_string(char_tab));
154
155 assert_int_equal(0, is_allowed_in_kv_string(char_esc));
156 assert_int_equal(0, is_allowed_in_kv_string(char_bell));
157 assert_int_equal(0, is_allowed_in_kv_string(char_backsp));
158 assert_int_equal(0, is_allowed_in_kv_string(char_cr));
159 assert_int_equal(0, is_allowed_in_kv_string(char_newline));
160 }
161
162
163
164 void test_u32_strnrstr(void **state)
165 {
166 UNUSED(state);
167
168 uint32_t *haystack = u32_strconv_from_arg("a foo found found bar fou", "ASCII");
169 assert_non_null(haystack);
170 uint32_t *needle = u32_strconv_from_arg("found", "ASCII");
171 assert_non_null(needle);
172
173 assert_null(u32_strnrstr(NULL, needle, u32_strlen(needle)));
174 assert_ptr_equal(haystack, u32_strnrstr(haystack, NULL, 0));
175
176 uint32_t *actual = u32_strnrstr(haystack, needle, u32_strlen(needle));
177 assert_ptr_equal(haystack + 12, actual);
178
179 BFREE(haystack);
180 BFREE(needle);
181 }
182
183
184
185 void test_u32_insert_space_at(void **state)
186 {
187 UNUSED(state);
188
189 uint32_t *expected = u32_strconv_from_arg("x xxx ", "ASCII");
190 assert_non_null(expected);
191 uint32_t *s = u32_strconv_from_arg("xxxx", "ASCII");
192 assert_non_null(s);
193
194 u32_insert_space_at(NULL, 2, 3);
195 u32_insert_space_at(&s, 3, 0);
196 u32_insert_space_at(&s, 1, 1);
197 u32_insert_space_at(&s, 10000, 2);
198
199 assert_non_null(s);
200 assert_int_equal(0, u32_strcmp(expected, s));
201
202 BFREE(s);
203 BFREE(expected);
204 }
205
206
207 /* vim: set cindent sw=4: */