comparison tests/test-unicode.c @ 28:f06312a7432b

cmake: enable tests
author David Demelier <markand@malikania.fr>
date Tue, 07 Feb 2023 14:30:33 +0100
parents 4da5819148c6
children 303403de1314
comparison
equal deleted inserted replaced
27:4da5819148c6 28:f06312a7432b
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */ 17 */
18 18
19 #include <errno.h> 19 #include <errno.h>
20 20
21 #include <rexo.h> 21 #include <dt.h>
22 22
23 #include "unicode.h" 23 #include "unicode.h"
24 24
25 /* 25 /*
26 * /!\ Be sure to keep this file with UTF-8 encoding /!\ 26 * /!\ Be sure to keep this file with UTF-8 encoding /!\
44 const size_t l2 = u32len(s2); 44 const size_t l2 = u32len(s2);
45 45
46 return l1 == l2 && memcmp(s1, s2, l1) == 0; 46 return l1 == l2 && memcmp(s1, s2, l1) == 0;
47 } 47 }
48 48
49 RX_TEST_CASE(uni8_encode, simple) 49 static void
50 uni8_encode_simple(void)
50 { 51 {
51 size_t r; 52 size_t r;
52 53
53 /* a -> 1 bytes. */ 54 /* a -> 1 bytes. */
54 { 55 {
55 uint8_t buffer[5] = { 0 }; 56 uint8_t buffer[5] = { 0 };
56 57
57 r = uni8_encode(buffer, sizeof (buffer), U'a'); 58 r = uni8_encode(buffer, sizeof (buffer), U'a');
58 RX_INT_REQUIRE_EQUAL(r, 1); 59 DT_EQ_INT(r, 1);
59 RX_STR_REQUIRE_EQUAL((const char *)buffer, (const char *)u8"a"); 60 DT_EQ_STR((const char *)buffer, (const char *)u8"a");
60 } 61 }
61 62
62 /* é -> 2 bytes. */ 63 /* é -> 2 bytes. */
63 { 64 {
64 uint8_t buffer[5] = { 0 }; 65 uint8_t buffer[5] = { 0 };
65 66
66 r = uni8_encode(buffer, sizeof (buffer), U'é'); 67 r = uni8_encode(buffer, sizeof (buffer), U'é');
67 RX_INT_REQUIRE_EQUAL(r, 2); 68 DT_EQ_INT(r, 2);
68 RX_STR_REQUIRE_EQUAL((const char *)buffer, (const char *)u8"é"); 69 DT_EQ_STR((const char *)buffer, (const char *)u8"é");
69 } 70 }
70 } 71 }
71 72
72 RX_TEST_CASE(uni8_encode, invalid) 73 static void
74 uni8_encode_invalid(void)
73 { 75 {
74 size_t r; 76 size_t r;
75 uint8_t buffer[5] = { 0 }; 77 uint8_t buffer[5] = { 0 };
76 78
77 r = uni8_encode(buffer, sizeof (buffer), 0xffffffff); 79 r = uni8_encode(buffer, sizeof (buffer), 0xffffffff);
78 RX_UINT_REQUIRE_EQUAL(r, (size_t)-1); 80 DT_EQ_SIZE(r, (size_t)-1);
79 RX_INT_REQUIRE_EQUAL(errno, EILSEQ); 81 DT_EQ_INT(errno, EILSEQ);
80 } 82 }
81 83
82 RX_TEST_CASE(uni8_encode, toosmall) 84 static void
85 uni8_encode_toosmall(void)
83 { 86 {
84 size_t r; 87 size_t r;
85 uint8_t buffer[1] = { 0 }; 88 uint8_t buffer[1] = { 0 };
86 89
87 r = uni8_encode(buffer, sizeof (buffer), U'é'); 90 r = uni8_encode(buffer, sizeof (buffer), U'é');
88 RX_UINT_REQUIRE_EQUAL(r, (size_t)-1); 91 DT_EQ_SIZE(r, (size_t)-1);
89 RX_INT_REQUIRE_EQUAL(errno, ERANGE); 92 DT_EQ_INT(errno, ERANGE);
90 } 93 }
91 94
92 RX_TEST_CASE(unit8_decode, simple) 95 static void
96 uni8_decode_simple(void)
93 { 97 {
94 size_t r; 98 size_t r;
95 99
96 /* a -> 1 bytes. */ 100 /* a -> 1 bytes. */
97 { 101 {
98 uint32_t code = -1; 102 uint32_t code = -1;
99 103
100 r = uni8_decode((const uint8_t *)u8"a", &code); 104 r = uni8_decode((const uint8_t *)u8"a", &code);
101 RX_UINT_REQUIRE_EQUAL(r, 1U); 105 DT_EQ_SIZE(r, 1U);
102 RX_INT_REQUIRE_EQUAL(code, 'a'); 106 DT_EQ_INT(code, 'a');
103 } 107 }
104 108
105 /* é -> 2 bytes. */ 109 /* é -> 2 bytes. */
106 { 110 {
107 uint32_t code = -1; 111 uint32_t code = -1;
108 112
109 r = uni8_decode((const uint8_t *)u8"é", &code); 113 r = uni8_decode((const uint8_t *)u8"é", &code);
110 RX_UINT_REQUIRE_EQUAL(r, 2U); 114 DT_EQ_SIZE(r, 2U);
111 RX_INT_REQUIRE_EQUAL(code, U'é'); 115 DT_EQ_INT(code, U'é');
112 } 116 }
113 } 117 }
114 118
115 RX_TEST_CASE(uni8_decode, invalid) 119 static void
120 uni8_decode_invalid(void)
116 { 121 {
117 size_t r; 122 size_t r;
118 123
119 /* Invalid UTF-8 sequence. */ 124 /* Invalid UTF-8 sequence. */
120 { 125 {
121 uint32_t code = -1; 126 uint32_t code = -1;
122 127
123 r = uni8_decode((const uint8_t *)u8"\xff""a", &code); 128 r = uni8_decode((const uint8_t *)u8"\xff""a", &code);
124 RX_UINT_REQUIRE_EQUAL(r, (size_t)-1); 129 DT_EQ_SIZE(r, (size_t)-1);
125 RX_UINT_REQUIRE_EQUAL(code, (uint32_t)-1); 130 DT_EQ_SIZE(code, (uint32_t)-1);
126 RX_INT_REQUIRE_EQUAL(errno, EILSEQ); 131 DT_EQ_INT(errno, EILSEQ);
127 } 132 }
128 133
129 /* Valid "€" but unfinished sequence. */ 134 /* Valid "€" but unfinished sequence. */
130 { 135 {
131 uint32_t code = -1; 136 uint32_t code = -1;
132 137
133 r = uni8_decode((const uint8_t []){ -30, 0 }, &code); 138 r = uni8_decode((const uint8_t []){ -30, 0 }, &code);
134 RX_UINT_REQUIRE_EQUAL(r, (size_t)-1); 139 DT_EQ_SIZE(r, (size_t)-1);
135 RX_UINT_REQUIRE_EQUAL(code, (uint32_t)-1); 140 DT_EQ_SIZE(code, (uint32_t)-1);
136 RX_INT_REQUIRE_EQUAL(errno, EILSEQ); 141 DT_EQ_INT(errno, EILSEQ);
137 } 142 }
138 } 143 }
139 144
140 RX_TEST_CASE(uni8_sizeof, simple) 145 static void
141 { 146 uni8_sizeof_simple(void)
142 RX_INT_REQUIRE_EQUAL(uni8_sizeof(u8"a"[0]), 1U); 147 {
143 RX_INT_REQUIRE_EQUAL(uni8_sizeof(u8"é"[0]), 2U); 148 DT_EQ_INT(uni8_sizeof(u8"a"[0]), 1U);
144 RX_INT_REQUIRE_EQUAL(uni8_sizeof(u8"€"[0]), 3U); 149 DT_EQ_INT(uni8_sizeof(u8"é"[0]), 2U);
145 RX_INT_REQUIRE_EQUAL(uni8_sizeof(u8"𐍈"[0]), 4U); 150 DT_EQ_INT(uni8_sizeof(u8"€"[0]), 3U);
146 } 151 DT_EQ_INT(uni8_sizeof(u8"𐍈"[0]), 4U);
147 152 }
148 RX_TEST_CASE(uni8_sizeof, invalid) 153
149 { 154 static void
150 RX_UINT_REQUIRE_EQUAL((size_t)-1, uni8_sizeof(u8"\xff"[0])); 155 uni8_sizeof_invalid(void)
151 RX_INT_REQUIRE_EQUAL(errno, EILSEQ); 156 {
152 } 157 DT_EQ_SIZE((size_t)-1, uni8_sizeof(u8"\xff"[0]));
153 158 DT_EQ_INT(errno, EILSEQ);
154 RX_TEST_CASE(uni8_length, simple) 159 }
155 { 160
156 RX_UINT_REQUIRE_EQUAL(uni8_length((const uint8_t *)"abc"), 3U); 161 static void
157 RX_UINT_REQUIRE_EQUAL(uni8_length((const uint8_t *)"5€"), 2U); 162 uni8_length_simple(void)
158 } 163 {
159 164 DT_EQ_SIZE(uni8_length((const uint8_t *)"abc"), 3U);
160 RX_TEST_CASE(uni8_length, invalid) 165 DT_EQ_SIZE(uni8_length((const uint8_t *)"5€"), 2U);
161 { 166 }
162 RX_UINT_REQUIRE_EQUAL((size_t)-1, uni8_length((const uint8_t *)"a""\xff""b")); 167
163 RX_INT_REQUIRE_EQUAL(errno, EILSEQ); 168 static void
164 } 169 uni8_length_invalid(void)
165 170 {
166 RX_TEST_CASE(uni8_to32, simple) 171 DT_EQ_SIZE((size_t)-1, uni8_length((const uint8_t *)"a""\xff""b"));
172 DT_EQ_INT(errno, EILSEQ);
173 }
174
175 static void
176 uni8_to32_simple(void)
167 { 177 {
168 size_t r; 178 size_t r;
169 179
170 { 180 {
171 uint32_t buffer[10] = { 0 }; 181 uint32_t buffer[10] = { 0 };
172 uint32_t expected[] = { U'a', U'b', U'c', 0 }; 182 uint32_t expected[] = { U'a', U'b', U'c', 0 };
173 183
174 r = uni8_to32((const uint8_t *)"abc", buffer, 10); 184 r = uni8_to32((const uint8_t *)"abc", buffer, 10);
175 RX_UINT_REQUIRE_EQUAL(r, 3U); 185 DT_EQ_SIZE(r, 3U);
176 RX_REQUIRE(u32cmp(buffer, expected)); 186 DT_ASSERT(u32cmp(buffer, expected));
177 } 187 }
178 188
179 { 189 {
180 uint32_t buffer[10] = { 0 }; 190 uint32_t buffer[10] = { 0 };
181 uint32_t expected[] = { U'a', U'é', U'c', 0 }; 191 uint32_t expected[] = { U'a', U'é', U'c', 0 };
182 192
183 r = uni8_to32((const uint8_t *)"aéc", buffer, 10); 193 r = uni8_to32((const uint8_t *)"aéc", buffer, 10);
184 RX_UINT_REQUIRE_EQUAL(r, 3); 194 DT_EQ_SIZE(r, 3);
185 RX_REQUIRE(u32cmp(buffer, expected)); 195 DT_ASSERT(u32cmp(buffer, expected));
186 } 196 }
187 } 197 }
188 198
189 RX_TEST_CASE(uni8_to32, invalid) 199 static void
200 uni8_to32_invalid(void)
190 { 201 {
191 size_t r; 202 size_t r;
192 uint32_t buffer[10] = { 0 }; 203 uint32_t buffer[10] = { 0 };
193 204
194 /* Invalid UTF-8 sequence. */ 205 /* Invalid UTF-8 sequence. */
195 r = uni8_to32((const uint8_t *)u8"\xff""a", buffer, 10); 206 r = uni8_to32((const uint8_t *)u8"\xff""a", buffer, 10);
196 RX_UINT_REQUIRE_EQUAL(r, (size_t)-1); 207 DT_EQ_SIZE(r, (size_t)-1);
197 RX_INT_REQUIRE_EQUAL(errno, EILSEQ); 208 DT_EQ_INT(errno, EILSEQ);
198 209
199 /* Valid "€" but unfinished sequence. */ 210 /* Valid "€" but unfinished sequence. */
200 r = uni8_to32((const uint8_t []){ -30, 0 }, buffer, 10); 211 r = uni8_to32((const uint8_t []){ -30, 0 }, buffer, 10);
201 RX_UINT_REQUIRE_EQUAL(r, (size_t)-1); 212 DT_EQ_SIZE(r, (size_t)-1);
202 RX_INT_REQUIRE_EQUAL(errno, EILSEQ); 213 DT_EQ_INT(errno, EILSEQ);
203 } 214 }
204 215
205 RX_TEST_CASE(uni8_to32, toosmall) 216 static void
217 uni8_to32_toosmall(void)
206 { 218 {
207 size_t r; 219 size_t r;
208 uint32_t buffer[4] = { 0 }; 220 uint32_t buffer[4] = { 0 };
209 221
210 r = uni8_to32((const uint8_t *)u8"bonjour à tous", buffer, 1); 222 r = uni8_to32((const uint8_t *)u8"bonjour à tous", buffer, 1);
211 RX_UINT_REQUIRE_EQUAL(r, (size_t)-1); 223 DT_EQ_SIZE(r, (size_t)-1);
212 RX_INT_REQUIRE_EQUAL(errno, ERANGE); 224 DT_EQ_INT(errno, ERANGE);
213 } 225 }
214 226
215 RX_TEST_CASE(uni32_sizeof, simple) 227 static void
216 { 228 uni32_sizeof_simple(void)
217 RX_UINT_REQUIRE_EQUAL(uni32_sizeof(U'a'), 1); 229 {
218 RX_UINT_REQUIRE_EQUAL(uni32_sizeof(U'é'), 2); 230 DT_EQ_SIZE(uni32_sizeof(U'a'), 1);
219 RX_UINT_REQUIRE_EQUAL(uni32_sizeof(U'€'), 3); 231 DT_EQ_SIZE(uni32_sizeof(U'é'), 2);
220 RX_UINT_REQUIRE_EQUAL(uni32_sizeof(U'𐍈'), 4); 232 DT_EQ_SIZE(uni32_sizeof(U'€'), 3);
221 } 233 DT_EQ_SIZE(uni32_sizeof(U'𐍈'), 4);
222 234 }
223 RX_TEST_CASE(uni32_sizeof, invalid) 235
224 { 236 static void
225 RX_UINT_REQUIRE_EQUAL((size_t)-1, uni32_sizeof(0xffffffff)); 237 uni32_sizeof_invalid(void)
226 RX_INT_REQUIRE_EQUAL(errno, EILSEQ); 238 {
227 } 239 DT_EQ_SIZE((size_t)-1, uni32_sizeof(0xffffffff));
228 240 DT_EQ_INT(errno, EILSEQ);
229 RX_TEST_CASE(uni32_length, simple) 241 }
230 { 242
231 RX_UINT_REQUIRE_EQUAL(uni32_length((const uint32_t []){ U'a', U'é', U'c', 0 }), 3U); 243 static void
232 } 244 uni32_length_simple(void)
233 245 {
234 RX_TEST_CASE(uni32_requires, simple) 246 DT_EQ_SIZE(uni32_length((const uint32_t []){ U'a', U'é', U'c', 0 }), 3U);
235 { 247 }
236 RX_UINT_REQUIRE_EQUAL(uni32_requires(U"abc"), 3U); 248
237 RX_UINT_REQUIRE_EQUAL(uni32_requires(U"é€𐍈"), 9U); 249 static void
238 } 250 uni32_requires_simple(void)
239 251 {
240 RX_TEST_CASE(uni32_requires, invalid) 252 DT_EQ_SIZE(uni32_requires(U"abc"), 3U);
241 { 253 DT_EQ_SIZE(uni32_requires(U"é€𐍈"), 9U);
242 RX_UINT_REQUIRE_EQUAL((size_t)-1, uni32_requires(U"\xffffffff")); 254 }
243 RX_INT_REQUIRE_EQUAL(errno, EILSEQ); 255
244 } 256 static void
245 257 uni32_requires_invalid(void)
246 RX_TEST_CASE(uni32_to8, simple) 258 {
259 DT_EQ_SIZE((size_t)-1, uni32_requires(U"\xffffffff"));
260 DT_EQ_INT(errno, EILSEQ);
261 }
262
263 static void
264 uni32_to8_simple(void)
247 { 265 {
248 size_t r; 266 size_t r;
249 267
250 { 268 {
251 uint8_t buffer[10] = { 0 }; 269 uint8_t buffer[10] = { 0 };
252 270
253 r = uni32_to8(U"abc", buffer, sizeof (buffer)); 271 r = uni32_to8(U"abc", buffer, sizeof (buffer));
254 RX_UINT_REQUIRE_EQUAL(r, 3U); 272 DT_EQ_SIZE(r, 3U);
255 RX_STR_REQUIRE_EQUAL((const char *)buffer, (const char *)u8"abc"); 273 DT_EQ_STR((const char *)buffer, (const char *)u8"abc");
256 } 274 }
257 275
258 { 276 {
259 uint8_t buffer[20] = { 0 }; 277 uint8_t buffer[20] = { 0 };
260 278
261 r = uni32_to8(U"ça va, 5€ ?", buffer, sizeof (buffer)); 279 r = uni32_to8(U"ça va, 5€ ?", buffer, sizeof (buffer));
262 RX_UINT_REQUIRE_EQUAL(r, 14U); 280 DT_EQ_SIZE(r, 14U);
263 RX_STR_REQUIRE_EQUAL((const char *)buffer, (const char *)u8"ça va, 5€ ?"); 281 DT_EQ_STR((const char *)buffer, (const char *)u8"ça va, 5€ ?");
264 } 282 }
265 } 283 }
266 284
267 RX_TEST_CASE(uni32_to8, invalid) 285 static void
286 uni32_to8_invalid(void)
268 { 287 {
269 uint8_t buffer[10] = { 0 }; 288 uint8_t buffer[10] = { 0 };
270 289
271 RX_INT_REQUIRE_EQUAL(uni32_to8(U"\xffffffff", buffer, sizeof (buffer)), (size_t)-1); 290 DT_EQ_SIZE(uni32_to8(U"\xffffffff", buffer, sizeof (buffer)), (size_t)-1);
272 RX_UINT_REQUIRE_EQUAL(errno, EILSEQ); 291 DT_EQ_INT(errno, EILSEQ);
273 } 292 }
274 293
275 RX_TEST_CASE(uni32_to8, toosmall) 294 static void
295 uni32_to8_toosmall(void)
276 { 296 {
277 size_t r; 297 size_t r;
278 uint8_t buffer[3] = { 0 }; 298 uint8_t buffer[3] = { 0 };
279 299
280 r = uni32_to8(U"ça va ?", buffer, sizeof (buffer)); 300 r = uni32_to8(U"ça va ?", buffer, sizeof (buffer));
281 RX_UINT_REQUIRE_EQUAL(r, (size_t)-1); 301 DT_EQ_SIZE(r, (size_t)-1);
282 RX_INT_REQUIRE_EQUAL(errno, ERANGE); 302 DT_EQ_INT(errno, ERANGE);
283 } 303 }
284 304
285 RX_TEST_CASE(misc, isalpha) 305 static void
286 { 306 misc_isalpha(void)
287 RX_REQUIRE(uni_isalpha(U'é')); 307 {
288 RX_REQUIRE(!uni_isalpha(U'€')); 308 DT_ASSERT(uni_isalpha(U'é'));
289 } 309 DT_ASSERT(!uni_isalpha(U'€'));
290 310 }
291 RX_TEST_CASE(misc, isdigit) 311
292 { 312 static void
293 RX_REQUIRE(uni_isdigit(U'۱')); 313 misc_isdigit(void)
294 RX_REQUIRE(!uni_isdigit(U'€')); 314 {
295 } 315 DT_ASSERT(uni_isdigit(U'۱'));
296 316 DT_ASSERT(!uni_isdigit(U'€'));
297 RX_TEST_CASE(misc, islower) 317 }
298 { 318
299 RX_REQUIRE(uni_islower(U'a')); 319 static void
300 RX_REQUIRE(uni_islower(U'é')); 320 misc_islower(void)
301 RX_REQUIRE(!uni_islower(U'A')); 321 {
302 RX_REQUIRE(!uni_islower(U'É')); 322 DT_ASSERT(uni_islower(U'a'));
303 } 323 DT_ASSERT(uni_islower(U'é'));
304 324 DT_ASSERT(!uni_islower(U'A'));
305 RX_TEST_CASE(misc, isspace) 325 DT_ASSERT(!uni_islower(U'É'));
306 { 326 }
307 RX_REQUIRE(uni_isspace(U' ')); 327
308 RX_REQUIRE(!uni_isspace(U'é')); 328 static void
309 } 329 misc_isspace(void)
310 330 {
311 RX_TEST_CASE(misc, istitle) 331 DT_ASSERT(uni_isspace(U' '));
312 { 332 DT_ASSERT(!uni_isspace(U'é'));
313 RX_REQUIRE(uni_istitle(U'Dž')); 333 }
314 RX_REQUIRE(!uni_istitle(U'€')); 334
315 } 335 static void
316 336 misc_istitle(void)
317 RX_TEST_CASE(misc, isupper) 337 {
318 { 338 DT_ASSERT(uni_istitle(U'Dž'));
319 RX_REQUIRE(!uni_isupper('a')); 339 DT_ASSERT(!uni_istitle(U'€'));
320 RX_REQUIRE(!uni_isupper(U'é')); 340 }
321 RX_REQUIRE(uni_isupper('A')); 341
322 RX_REQUIRE(uni_isupper(U'É')); 342 static void
343 misc_isupper(void)
344 {
345 DT_ASSERT(!uni_isupper('a'));
346 DT_ASSERT(!uni_isupper(U'é'));
347 DT_ASSERT(uni_isupper('A'));
348 DT_ASSERT(uni_isupper(U'É'));
323 } 349 }
324 350
325 int 351 int
326 main(int argc, char **argv) 352 main(int argc, char **argv)
327 { 353 {
328 return rx_main(0, NULL, argc, (const char **)argv) == RX_SUCCESS ? 0 : 1; 354 DT_RUN(uni8_encode_simple);
329 } 355 DT_RUN(uni8_encode_invalid);
356 DT_RUN(uni8_encode_toosmall);
357 DT_RUN(uni8_decode_simple);
358 DT_RUN(uni8_decode_invalid);
359 DT_RUN(uni8_sizeof_simple);
360 DT_RUN(uni8_sizeof_invalid);
361 DT_RUN(uni8_length_simple);
362 DT_RUN(uni8_length_invalid);
363 DT_RUN(uni8_to32_simple);
364 DT_RUN(uni8_to32_invalid);
365 DT_RUN(uni8_to32_toosmall);
366 DT_RUN(uni32_sizeof_simple);
367 DT_RUN(uni32_sizeof_invalid);
368 DT_RUN(uni32_length_simple);
369 DT_RUN(uni32_requires_simple);
370 DT_RUN(uni32_requires_invalid);
371 DT_RUN(uni32_to8_simple);
372 DT_RUN(uni32_to8_invalid);
373 DT_RUN(uni32_to8_toosmall);
374 DT_RUN(misc_isalpha);
375 DT_RUN(misc_isdigit);
376 DT_RUN(misc_islower);
377 DT_RUN(misc_isspace);
378 DT_RUN(misc_istitle);
379 DT_RUN(misc_isupper);
380 DT_SUMMARY();
381 }