Debian Codegraph @

utest/tools_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 'tools' 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
28 #include "tools.h"
29 #include "tools_test.h"
30 #include "unicode.h"
31
32
33 void test_strisyes_true(void **state)
34 {
35 (void) state; /* unused */
36
37 assert_int_equal(1, strisyes("On"));
38 assert_int_equal(1, strisyes("on"));
39 assert_int_equal(1, strisyes("yes"));
40 assert_int_equal(1, strisyes("YEs"));
41 assert_int_equal(1, strisyes("true"));
42 assert_int_equal(1, strisyes("True"));
43 assert_int_equal(1, strisyes("1"));
44 assert_int_equal(1, strisyes("t"));
45 assert_int_equal(1, strisyes("T"));
46 }
47
48
49 void test_strisyes_false(void **state)
50 {
51 (void) state; /* unused */
52
53 assert_int_equal(0, strisyes(NULL));
54 assert_int_equal(0, strisyes(""));
55 assert_int_equal(0, strisyes(" "));
56 assert_int_equal(0, strisyes("off"));
57 assert_int_equal(0, strisyes("false"));
58 assert_int_equal(0, strisyes("no"));
59 assert_int_equal(0, strisyes("0"));
60 assert_int_equal(0, strisyes("f"));
61 assert_int_equal(0, strisyes("F"));
62 assert_int_equal(0, strisyes("tru"));
63 assert_int_equal(0, strisyes("yes sir"));
64 assert_int_equal(0, strisyes("100"));
65 }
66
67
68 void test_strisno_true(void **state)
69 {
70 (void) state; /* unused */
71
72 assert_int_equal(1, strisno("off"));
73 assert_int_equal(1, strisno("Off"));
74 assert_int_equal(1, strisno("no"));
75 assert_int_equal(1, strisno("NO"));
76 assert_int_equal(1, strisno("false"));
77 assert_int_equal(1, strisno("False"));
78 assert_int_equal(1, strisno("0"));
79 assert_int_equal(1, strisno("f"));
80 assert_int_equal(1, strisno("F"));
81 }
82
83
84 void test_strisno_false(void **state)
85 {
86 (void) state; /* unused */
87
88 assert_int_equal(0, strisno(NULL));
89 assert_int_equal(0, strisno(""));
90 assert_int_equal(0, strisno(" "));
91 assert_int_equal(0, strisno("on"));
92 assert_int_equal(0, strisno("true"));
93 assert_int_equal(0, strisno("yes"));
94 assert_int_equal(0, strisno("1"));
95 assert_int_equal(0, strisno("t"));
96 assert_int_equal(0, strisno("T"));
97 assert_int_equal(0, strisno("fal"));
98 assert_int_equal(0, strisno("no sir"));
99 assert_int_equal(0, strisno("00"));
100 }
101
102
103 void test_my_strrspn_edge(void **state)
104 {
105 (void) state; /* unused */
106
107 assert_int_equal(0, (int) my_strrspn(NULL, "abc"));
108 assert_int_equal(0, (int) my_strrspn("", "abc"));
109 assert_int_equal(0, (int) my_strrspn("abc", NULL));
110 assert_int_equal(0, (int) my_strrspn("abc", ""));
111 assert_int_equal(0, (int) my_strrspn(NULL, NULL));
112 }
113
114
115 void test_my_strrspn(void **state)
116 {
117 (void) state; /* unused */
118
119 assert_int_equal(2, (int) my_strrspn("foo", "o"));
120 assert_int_equal(0, (int) my_strrspn("foo", "ABC"));
121 assert_int_equal(3, (int) my_strrspn("foo", "foobar"));
122 assert_int_equal(1, (int) my_strrspn("foo ", " "));
123 assert_int_equal(1, (int) my_strrspn("a", "a"));
124 assert_int_equal(0, (int) my_strrspn("a", "A"));
125 assert_int_equal(2, (int) my_strrspn("axxaa", "a"));
126 }
127
128
129 void test_is_csi_reset(void **state)
130 {
131 (void) state; /* unused */
132
133 assert_int_equal(1, is_csi_reset(u32_strconv_from_arg("\x1b[0m", "ASCII")));
134 assert_int_equal(1, is_csi_reset(u32_strconv_from_arg("\x1b[m", "ASCII")));
135 assert_int_equal(1, is_csi_reset(u32_strconv_from_arg("\x1b(0m", "ASCII")));
136 assert_int_equal(1, is_csi_reset(u32_strconv_from_arg("\x1b(m", "ASCII")));
137 assert_int_equal(1, is_csi_reset(u32_strconv_from_arg("\x1b[0m foo", "ASCII")));
138
139 assert_int_equal(0, is_csi_reset(u32_strconv_from_arg("", "ASCII")));
140 assert_int_equal(0, is_csi_reset(u32_strconv_from_arg("normal", "ASCII")));
141 assert_int_equal(0, is_csi_reset(u32_strconv_from_arg("\x1b[", "ASCII")));
142 assert_int_equal(0, is_csi_reset(u32_strconv_from_arg("not yet \x1b[0m", "ASCII")));
143 assert_int_equal(0, is_csi_reset(u32_strconv_from_arg("\x1b[38;5;203m", "ASCII")));
144 assert_int_equal(0, is_csi_reset(u32_strconv_from_arg("\x1b_BROKEN", "ASCII")));
145 }
146
147
148 void test_is_ascii_id_valid(void **state)
149 {
150 (void) state; /* unused */
151
152 bxstr_t *s = bxs_from_ascii("valid");
153 assert_int_equal(1, is_ascii_id(s, 0));
154 bxs_free(s);
155
156 s = bxs_from_ascii("also-valid");
157 assert_int_equal(1, is_ascii_id(s, 0));
158 bxs_free(s);
159
160 s = bxs_from_ascii("fine2");
161 assert_int_equal(1, is_ascii_id(s, 0));
162 bxs_free(s);
163
164 s = bxs_from_ascii("A");
165 assert_int_equal(1, is_ascii_id(s, 0));
166 bxs_free(s);
167
168 s = bxs_from_ascii("A_2");
169 assert_int_equal(1, is_ascii_id(s, 0));
170 bxs_free(s);
171 }
172
173
174 void test_is_ascii_id_invalid(void **state)
175 {
176 (void) state; /* unused */
177
178 assert_int_equal(0, is_ascii_id(NULL, 0));
179
180 bxstr_t *s = bxs_from_ascii("");
181 assert_int_equal(0, is_ascii_id(s, 0));
182 bxs_free(s);
183
184 s = bxs_from_ascii("a--b");
185 assert_int_equal(0, is_ascii_id(s, 0));
186 bxs_free(s);
187
188 s = bxs_from_ascii("also-_invalid");
189 assert_int_equal(0, is_ascii_id(s, 0));
190 bxs_free(s);
191
192 s = bxs_from_ascii("invalid-");
193 assert_int_equal(0, is_ascii_id(s, 0));
194 bxs_free(s);
195
196 s = bxs_from_ascii("42");
197 assert_int_equal(0, is_ascii_id(s, 0));
198 bxs_free(s);
199
200 uint32_t *ustr32 = u32_strconv_from_arg(
201 "\xe5\x85\xac\xe7\x88\xb8\xe8\xa6\x81\xe9\x81\x93\xef\xbc\x81", /* 公爸要道! */
202 "UTF-8");
203 s = bxs_from_unicode(ustr32);
204 assert_int_equal(0, is_ascii_id(s, 0));
205 bxs_free(s);
206 BFREE(ustr32);
207 }
208
209
210 void test_is_ascii_id_strict_valid(void **state)
211 {
212 (void) state; /* unused */
213
214 bxstr_t *s = bxs_from_ascii("valid");
215 assert_int_equal(1, is_ascii_id(s, 1));
216 bxs_free(s);
217
218 s = bxs_from_ascii("also-valid");
219 assert_int_equal(1, is_ascii_id(s, 1));
220 bxs_free(s);
221
222 s = bxs_from_ascii("fine2");
223 assert_int_equal(1, is_ascii_id(s, 1));
224 bxs_free(s);
225
226 s = bxs_from_ascii("a");
227 assert_int_equal(1, is_ascii_id(s, 1));
228 bxs_free(s);
229 }
230
231
232 void test_is_ascii_id_strict_invalid(void **state)
233 {
234 (void) state; /* unused */
235
236 assert_int_equal(0, is_ascii_id(NULL, 1));
237
238 bxstr_t *s = bxs_from_ascii("");
239 assert_int_equal(0, is_ascii_id(s, 1));
240 bxs_free(s);
241
242 s = bxs_from_ascii("a--b");
243 assert_int_equal(0, is_ascii_id(s, 1));
244 bxs_free(s);
245
246 s = bxs_from_ascii("also-_invalid");
247 assert_int_equal(0, is_ascii_id(s, 1));
248 bxs_free(s);
249
250 s = bxs_from_ascii("invalid-");
251 assert_int_equal(0, is_ascii_id(s, 1));
252 bxs_free(s);
253
254 s = bxs_from_ascii("42");
255 assert_int_equal(0, is_ascii_id(s, 1));
256 bxs_free(s);
257
258 s = bxs_from_ascii("A_2");
259 assert_int_equal(0, is_ascii_id(s, 1));
260 bxs_free(s);
261 }
262
263
264 void test_repeat(void **state)
265 {
266 (void) state; /* unused */
267
268 char *actual = repeat(NULL, 1);
269 assert_null(actual);
270
271 actual = repeat("x", 0);
272 assert_string_equal("", actual);
273 BFREE(actual);
274
275 actual = repeat("x", 3);
276 assert_string_equal("xxx", actual);
277 BFREE(actual);
278
279 actual = repeat("abc", 3);
280 assert_string_equal("abcabcabc", actual);
281 BFREE(actual);
282 }
283
284
285 /* vim: set cindent sw=4: */