Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/io/charset.cpp

raw
1 /*
2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2020 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 13 февр. 2019 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/common/types.h>
23 #include <lsp-plug.in/test-fw/utest.h>
24 #include <lsp-plug.in/common/endian.h>
25
26 #include <lsp-plug.in/io/charset.h>
27
28 // This strlen does not analyze surrogate pairs
29 namespace lsp
30 {
31 template <class char_t>
32 static size_t strlen_test(const char_t *s)
33 {
34 size_t n = 0;
35 for (; *s != 0; ++s, ++n) {}
36 return n;
37 }
38
39 static size_t utf16_count_invalid(const lsp_utf16_t *s)
40 {
41 size_t n = 0;
42 for (; *s != 0; ++s) {
43 if (*s == 0xfffd)
44 ++n;
45 }
46 return n;
47 }
48
49 static size_t utf32_count_invalid(const lsp_utf32_t *s)
50 {
51 size_t n = 0;
52 for (; *s != 0; ++s) {
53 if (*s == 0xfffd)
54 ++n;
55 }
56 return n;
57 }
58
59 static size_t utf8_count_invalid(const char *s)
60 {
61 lsp_utf32_t cp;
62 size_t n = 0;
63
64 while ((cp = lsp::read_utf8_codepoint(&s)) != 0)
65 {
66 if (cp == 0xfffd)
67 ++n;
68 }
69 return n;
70 }
71
72 template <class char_t>
73 static char_t *strdup_bswap(const char_t *src, bool le)
74 {
75 // Estimate string length
76 size_t len = 0;
77 for (const char_t *p = src; *p != 0; ++p)
78 ++len;
79
80 // Allocate memory
81 char_t *dst = reinterpret_cast<char_t *>(::malloc(sizeof(char_t) * (len + 1)));
82 if (dst == NULL)
83 return NULL;
84
85 // Do byte-reversed copy (if required)
86 bool swap = __IF_LEBE(!le, le);
87 char_t *p = dst;
88
89 if (swap)
90 {
91 while (*src != 0)
92 *(p++) = byte_swap(*(src++));
93 }
94 else
95 {
96 while (*src != 0)
97 *(p++) = *(src++);
98 }
99 *p = 0;
100
101 return dst;
102 }
103
104 template <class char_t>
105 static ssize_t strcmp_test(const char_t *s1, const char_t *s2)
106 {
107 ssize_t diff;
108 do {
109 diff = ssize_t(*s1) - ssize_t(*s2);
110 } while ((diff == 0) && (*(s1++) != 0) && (*(s2++) != 0));
111
112 return diff;
113 }
114
115 template <class char_t>
116 static ssize_t strcmp_bswap(const char_t *s1, const char_t *s2, bool le)
117 {
118 ssize_t diff;
119
120 bool swap = __IF_LEBE(!le, le);
121 if (swap)
122 {
123 do {
124 diff = ssize_t(*s1) - ssize_t(byte_swap(*s2));
125 } while ((diff == 0) && (*(s1++) != 0) && (*(s2++) != 0));
126 }
127 else
128 {
129 do {
130 diff = ssize_t(*s1) - ssize_t(*s2);
131 } while ((diff == 0) && (*(s1++) != 0) && (*(s2++) != 0));
132 }
133
134 return diff;
135 }
136
137 typedef struct utf8_check_t
138 {
139 const char *s;
140 size_t u16strlen;
141 size_t u32strlen;
142 size_t invalid;
143 } utf8_check_t;
144
145 typedef struct utf16_check_t
146 {
147 const uint16_t *s;
148 size_t u8strlen;
149 size_t u32strlen;
150 size_t invalid;
151 } utf16_check_t;
152
153 static utf8_check_t utf8_check[] =
154 {
155 { "", 0, 0, 0 },
156 { "Test text in ASCII", 18, 18, 0 },
157 { "Тестовый текст на русском языке", 31, 31, 0 },
158 { "日本語の物語。それはテスト。", 14, 14, 0 },
159 { "Test text in ASCII. Тестовый текст на русском языке. 日本語の物語。それはテスト。", 67, 67, 0 },
160 { "\x80", 1, 1, 1 }, // Invalid sequence
161 { "\x80Test", 5, 5, 1 }, // Invalid sequence + text
162 { "\xe0\x80\x80", 3, 3, 3 }, // Invalid sequence
163 { "\xe0\x80\x80Test", 7, 7, 3 }, // Invalid sequence + text
164 { "\xe0\x80\x80\x80", 4, 4, 4 }, // Two invalid sequences
165 { "\xe0\x80\x80\x80Test", 8, 8, 4 }, // Two invalid sequences + text
166 { "\xed\xa0\x80", 1, 1, 1 }, // Invalid codepoint (surrogate)
167 { "\xed\xa0\x80Test", 5, 5, 1 }, // Invalid codepoint (surrogate) + text
168 { "\xed\xa0\x80\xed\xa0\x8f", 2, 2, 2 }, // Two invalid codepoints (surrogate)
169 { "\xed\xa0\x80\xed\xa0\x8fTest", 6, 6, 2 }, // Two invalid codepoints (surrogate) + text
170 { "\xc0\xbf\xcb\xbf", 3, 3, 2 }, // One valid codepoint, one invalid (2 errors)
171 { "\xc0\xbf\xcb\xbfTest", 7, 7, 2 }, // One valid codepoint, one invalid (2 errors) + text
172 { "\xf0\x90\x80\x8f", 2, 1, 0 }, // Surrogate pair at output
173 { "\xf0\x90\x80\x8fTest", 6, 5, 0 } // Surrogate pair at output + text
174 };
175
176 static const uint16_t u16str_0[] = { 0xd801, 0xdc37, 0 };
177 static const uint16_t u16str_1[] = { 0xd801, 0xdc37, 'T', 'e', 's', 't', 0 };
178 static const uint16_t u16str_2[] = { 0xdc37, 0xd801, 0 };
179 static const uint16_t u16str_3[] = { 0xdc37, 0xd801, 'T', 'e', 's', 't', 0 };
180 static const uint16_t u16str_4[] = { 0xd801, 0 };
181 static const uint16_t u16str_5[] = { 0xd801, 'T', 'e', 's', 't', 0 };
182 static const uint16_t u16str_6[] = { 0xdc01, 0 };
183 static const uint16_t u16str_7[] = { 0xdc01, 'T', 'e', 's', 't', 0 };
184 static const uint16_t u16str_8[] = { 0 };
185 static const uint16_t u16str_9[] = { 'T', 'e', 's', 't', ' ', 't', 'e', 'x', 't', ' ', 'i', 'n', ' ', 'A', 'S', 'C', 'I', 'I', 0 };
186 static const uint16_t u16str_10[] = {
187 0x0422, 0x0435, 0x0441, 0x0442, 0x043e, 0x0432, 0x044b, 0x0439,
188 0x0020, 0x0442, 0x0435, 0x043a, 0x0441, 0x0442, 0x0020, 0x043d,
189 0x0430, 0x0020, 0x0440, 0x0443, 0x0441, 0x0441, 0x043a, 0x043e,
190 0x043c, 0x0020, 0x044f, 0x0437, 0x044b, 0x043a, 0x0435, 0
191 };
192
193 static const uint16_t u16str_11[] = {
194 0x65e5, 0x672c, 0x8a9e, 0x306e, 0x7269, 0x8a9e, 0x3002, 0x305d,
195 0x308c, 0x306f, 0x30c6, 0x30b9, 0x30c8, 0x3002, 0
196 };
197
198 static const uint16_t u16str_12[] = {
199 'T', 'e', 's', 't', ' ', 't', 'e', 'x', 't', ' ', 'i', 'n', ' ', 'A', 'S', 'C', 'I', 'I',
200 '.', ' ',
201 0x0422, 0x0435, 0x0441, 0x0442, 0x043e, 0x0432, 0x044b, 0x0439,
202 0x0020, 0x0442, 0x0435, 0x043a, 0x0441, 0x0442, 0x0020, 0x043d,
203 0x0430, 0x0020, 0x0440, 0x0443, 0x0441, 0x0441, 0x043a, 0x043e,
204 0x043c, 0x0020, 0x044f, 0x0437, 0x044b, 0x043a, 0x0435,
205 '.', ' ',
206 0x65e5, 0x672c, 0x8a9e, 0x306e, 0x7269, 0x8a9e, 0x3002, 0x305d,
207 0x308c, 0x306f, 0x30c6, 0x30b9, 0x30c8, 0x3002,
208 0
209 };
210
211 static utf16_check_t utf16_check[] =
212 {
213 { u16str_0, 4, 1, 0 }, // Surrogate pair
214 { u16str_1, 8, 5, 0 }, // Surrogate pair + text
215 { u16str_2, 4, 1, 0 }, // Another surrogate pair
216 { u16str_3, 8, 5, 0 }, // Another surrogate pair + text
217 { u16str_4, 3, 1, 1 }, // Invalid surrogate
218 { u16str_5, 7, 5, 1 }, // Invalid surrogate + text
219 { u16str_6, 3, 1, 1 }, // Another invalid surrogate
220 { u16str_7, 7, 5, 1 }, // Another invalid surrogate + text
221 { u16str_8, 0, 0, 0 }, // ""
222 { u16str_9, 18, 18, 0 }, // "Test text in ASCII"
223 { u16str_10, 58, 31, 0 }, // "Тестовый текст на русском языке"
224 { u16str_11, 42, 14, 0 }, // "日本語の物語。それはテスト。"
225 { u16str_12, 122, 67, 0 } // "Test text in ASCII. Тестовый текст на русском языке. 日本語の物語。それはテスト。"
226 };
227 }
228
229 UTEST_BEGIN("runtime.io", charset)
230
231 void check_utf8_to_utf16()
232 {
233 lsp_utf16_t *s_na = NULL, *s_le = NULL, *s_be = NULL;
234 size_t len;
235
236 printf("Testing utf8_to_utf16\n");
237
238 for (size_t i=0, n=sizeof(utf8_check)/sizeof(utf8_check_t); i<n; ++i)
239 {
240 printf(" checking test line %d...\n", int(i));
241
242 utf8_check_t *ck = &utf8_check[i];
243
244 // UTF8 -> UTF16
245 s_na = lsp::utf8_to_utf16(ck->s);
246 UTEST_ASSERT(s_na != NULL);
247 lsp_finally{ free(s_na); };
248
249 s_le = lsp::utf8_to_utf16le(ck->s);
250 UTEST_ASSERT(s_le != NULL);
251 lsp_finally{ free(s_le); };
252
253 s_be = lsp::utf8_to_utf16be(ck->s);
254 UTEST_ASSERT(s_be != NULL);
255 lsp_finally{ free(s_be); };
256
257 UTEST_ASSERT_MSG(((len = strlen_test(s_na)) == ck->u16strlen),
258 "Error checking line %d: utf16_strlen=%d, expected=%d",
259 int(i), int(len), int(ck->u16strlen));
260 UTEST_ASSERT_MSG(((len = utf16_count_invalid(s_na)) == ck->invalid),
261 "Error checking line %d: utf16_count_invalid=%d, expected=%d",
262 int(i), int(len), int(ck->invalid));
263
264 UTEST_ASSERT(strcmp_bswap(s_na, s_le, true) == 0);
265 UTEST_ASSERT(strcmp_bswap(s_na, s_be, false) == 0);
266 if (len > 0)
267 UTEST_ASSERT(strcmp_test(s_le, s_be) != 0);
268 }
269 }
270
271 void check_utf8_to_utf16_buf()
272 {
273 constexpr size_t buf_size = 10;
274 lsp_utf16_t s_na[buf_size], s_le[buf_size], s_be[buf_size];
275
276 printf("Testing utf8_to_utf16_buf\n");
277
278 for (size_t i=0, n=sizeof(utf8_check)/sizeof(utf8_check_t); i<n; ++i)
279 {
280 printf(" checking test line %d...\n", int(i));
281
282 utf8_check_t *ck = &utf8_check[i];
283
284 memset(s_na, 0xcc, sizeof(s_na));
285 memset(s_le, 0xcc, sizeof(s_le));
286 memset(s_be, 0xcc, sizeof(s_be));
287
288 // UTF8 -> UTF16
289 size_t n_na = lsp::utf8_to_utf16(s_na, ck->s, buf_size);
290 UTEST_ASSERT(n_na <= buf_size);
291 size_t n_le = lsp::utf8_to_utf16le(s_le, ck->s, buf_size);
292 UTEST_ASSERT(n_le <= buf_size);
293 size_t n_be = lsp::utf8_to_utf16be(s_be, ck->s, buf_size);
294 UTEST_ASSERT(n_be <= buf_size);
295
296 UTEST_ASSERT(n_na == n_le);
297 UTEST_ASSERT(n_na == n_be);
298
299 if (n_na == 0)
300 continue;
301
302 // Check that line terminator is present
303 UTEST_ASSERT(s_na[n_na-1] == 0);
304 UTEST_ASSERT(s_le[n_le-1] == 0);
305 UTEST_ASSERT(s_be[n_be-1] == 0);
306
307 // Check that strings are equal
308 UTEST_ASSERT(strcmp_bswap(s_na, s_le, true) == 0);
309 UTEST_ASSERT(strcmp_bswap(s_na, s_be, false) == 0);
310 if (n_na > 1)
311 UTEST_ASSERT(strcmp_test(s_le, s_be) != 0);
312 }
313 }
314
315 void check_utf8_to_utf32()
316 {
317 lsp_utf32_t *s32_na = NULL, *s32_le = NULL, *s32_be = NULL;
318 size_t len;
319
320 printf("Testing utf8_to_utf32\n");
321
322 for (size_t i=0, n=sizeof(utf8_check)/sizeof(utf8_check_t); i<n; ++i)
323 {
324 printf(" checking test line %d...\n", int(i));
325
326 utf8_check_t *ck = &utf8_check[i];
327
328 // UTF8 -> UTF32
329 s32_na = lsp::utf8_to_utf32(ck->s);
330 UTEST_ASSERT(s32_na != NULL);
331 lsp_finally{ free(s32_na); };
332
333 s32_le = lsp::utf8_to_utf32le(ck->s);
334 UTEST_ASSERT(s32_le != NULL);
335 lsp_finally{ free(s32_le); };
336
337 s32_be = lsp::utf8_to_utf32be(ck->s);
338 UTEST_ASSERT(s32_be != NULL);
339 lsp_finally{ free(s32_be); };
340
341 UTEST_ASSERT_MSG(((len = strlen_test(s32_na)) == ck->u32strlen),
342 "Error checking line %d: utf32_strlen=%d, expected=%d",
343 int(i), int(len), int(ck->u32strlen));
344 UTEST_ASSERT_MSG(((len = utf32_count_invalid(s32_na)) == ck->invalid),
345 "Error checking line %d: utf32_count_invalid=%d, expected=%d",
346 int(i), int(len), int(ck->invalid));
347
348 UTEST_ASSERT(strcmp_bswap(s32_na, s32_le, true) == 0);
349 UTEST_ASSERT(strcmp_bswap(s32_na, s32_be, false) == 0);
350 if (len > 0)
351 UTEST_ASSERT(strcmp_test(s32_le, s32_be) != 0);
352 }
353 }
354
355 void check_utf8_to_utf32_buf()
356 {
357 constexpr size_t buf_size = 10;
358 lsp_utf32_t s_na[buf_size], s_le[buf_size], s_be[buf_size];
359
360 printf("Testing utf8_to_utf32_buf\n");
361
362 for (size_t i=0, n=sizeof(utf8_check)/sizeof(utf8_check_t); i<n; ++i)
363 {
364 printf(" checking test line %d...\n", int(i));
365
366 utf8_check_t *ck = &utf8_check[i];
367
368 memset(s_na, 0xcc, sizeof(s_na));
369 memset(s_le, 0xcc, sizeof(s_le));
370 memset(s_be, 0xcc, sizeof(s_be));
371
372 // UTF8 -> UTF32
373 size_t n_na = lsp::utf8_to_utf32(s_na, ck->s, buf_size);
374 UTEST_ASSERT(n_na <= buf_size);
375 size_t n_le = lsp::utf8_to_utf32le(s_le, ck->s, buf_size);
376 UTEST_ASSERT(n_le <= buf_size);
377 size_t n_be = lsp::utf8_to_utf32be(s_be, ck->s, buf_size);
378 UTEST_ASSERT(n_be <= buf_size);
379
380 UTEST_ASSERT(n_na == n_le);
381 UTEST_ASSERT(n_na == n_be);
382
383 if (n_na == 0)
384 continue;
385
386 // Check that line terminator is present
387 UTEST_ASSERT(s_na[n_na-1] == 0);
388 UTEST_ASSERT(s_le[n_le-1] == 0);
389 UTEST_ASSERT(s_be[n_be-1] == 0);
390
391 // Check that strings are equal
392 UTEST_ASSERT(strcmp_bswap(s_na, s_le, true) == 0);
393 UTEST_ASSERT(strcmp_bswap(s_na, s_be, false) == 0);
394 if (n_na > 1)
395 UTEST_ASSERT(strcmp_test(s_le, s_be) != 0);
396 }
397 }
398
399 void check_utf16_to_utf8()
400 {
401 lsp_utf16_t *le = NULL, *be = NULL;
402 const lsp_utf16_t *na = NULL;
403 char *s_na = NULL, *s_le = NULL, *s_be = NULL;
404 size_t len;
405 printf("Testing utf16_to_utf8\n");
406
407 for (size_t i=0, n=sizeof(utf16_check)/sizeof(utf16_check_t); i<n; ++i)
408 {
409 printf(" checking test line %d...\n", int(i));
410
411 utf16_check_t *ck = &utf16_check[i];
412
413 // Obtain native string and it's LE and BE copies
414 na = reinterpret_cast<const lsp_utf16_t *>(ck->s);
415 le = strdup_bswap(na, true);
416 lsp_finally { free(le); };
417 be = strdup_bswap(na, false);
418 lsp_finally { free(be); };
419
420 len = strlen_test(na);
421 UTEST_ASSERT(le != NULL);
422 UTEST_ASSERT(be != NULL);
423 if (len > 0)
424 UTEST_ASSERT(strcmp_test(le, be) != 0);
425
426 // UTF16 -> UTF8
427 s_na = lsp::utf16_to_utf8(na);
428 lsp_finally { free(s_na); };
429 s_le = lsp::utf16le_to_utf8(le);
430 lsp_finally { free(s_le); };
431 s_be = lsp::utf16be_to_utf8(be);
432 lsp_finally { free(s_be); };
433
434 UTEST_ASSERT(s_na != NULL);
435 UTEST_ASSERT(s_le != NULL);
436 UTEST_ASSERT(s_be != NULL);
437
438 UTEST_ASSERT_MSG(((len = strlen_test(s_na)) == ck->u8strlen),
439 "Error checking line %d: strlen=%d, expected=%d",
440 int(i), int(len), int(ck->u8strlen));
441 UTEST_ASSERT_MSG(((len = utf8_count_invalid(s_na)) == ck->invalid),
442 "Error checking line %d: utf8_count_invalid=%d, expected=%d",
443 int(i), int(len), int(ck->invalid));
444
445 UTEST_ASSERT(strcmp_test(s_le, s_be) == 0);
446 UTEST_ASSERT(strcmp_test(s_na, s_le) == 0);
447 UTEST_ASSERT(strcmp_test(s_na, s_be) == 0);
448 }
449 }
450
451 void check_utf16_to_utf8_buf()
452 {
453 constexpr size_t buf_size = 10;
454 const lsp_utf16_t *na = NULL;
455 lsp_utf16_t *le = NULL, *be = NULL;
456
457 char s_na[buf_size], s_le[buf_size], s_be[buf_size];
458 size_t len;
459 printf("Testing utf16_to_utf8_buf\n");
460
461 for (size_t i=0, n=sizeof(utf16_check)/sizeof(utf16_check_t); i<n; ++i)
462 {
463 printf(" checking test line %d...\n", int(i));
464
465 utf16_check_t *ck = &utf16_check[i];
466
467 // Obtain native string and it's LE and BE copies
468 na = reinterpret_cast<const lsp_utf16_t *>(ck->s);
469 le = strdup_bswap(na, true);
470 lsp_finally { free(le); };
471 be = strdup_bswap(na, false);
472 lsp_finally { free(be); };
473
474 len = strlen_test(na);
475 UTEST_ASSERT(le != NULL);
476 UTEST_ASSERT(be != NULL);
477 if (len > 0)
478 UTEST_ASSERT(strcmp_test(le, be) != 0);
479
480 // UTF16 -> UTF8
481 memset(s_na, 0xcc, sizeof(s_na));
482 memset(s_le, 0xcc, sizeof(s_le));
483 memset(s_be, 0xcc, sizeof(s_be));
484
485 size_t n_na = lsp::utf16_to_utf8(s_na, na, buf_size);
486 size_t n_le = lsp::utf16le_to_utf8(s_le, le, buf_size);
487 size_t n_be = lsp::utf16be_to_utf8(s_be, be, buf_size);
488
489 UTEST_ASSERT(n_na == n_le);
490 UTEST_ASSERT(n_na == n_be);
491 if (n_na == 0)
492 continue;
493
494 // Check that line terminator is present
495 UTEST_ASSERT(s_na[n_na-1] == 0);
496 UTEST_ASSERT(s_le[n_le-1] == 0);
497 UTEST_ASSERT(s_be[n_be-1] == 0);
498
499 UTEST_ASSERT(strcmp_test(s_le, s_be) == 0);
500 UTEST_ASSERT(strcmp_test(s_na, s_le) == 0);
501 UTEST_ASSERT(strcmp_test(s_na, s_be) == 0);
502 }
503 }
504
505 void check_utf16_to_utf32()
506 {
507 lsp_utf16_t *le = NULL, *be = NULL;
508 const lsp_utf16_t *na = NULL;
509 lsp_utf32_t *s32[9];
510 size_t len;
511 printf("Testing utf16_to_utf32\n");
512
513 for (size_t i=0, n=sizeof(utf16_check)/sizeof(utf16_check_t); i<n; ++i)
514 {
515 printf(" checking test line %d...\n", int(i));
516
517 utf16_check_t *ck = &utf16_check[i];
518
519 // Obtain native string and it's LE and BE copies
520 na = reinterpret_cast<const lsp_utf16_t *>(ck->s);
521 le = strdup_bswap(na, true);
522 be = strdup_bswap(na, false);
523
524 len = strlen_test(na);
525 UTEST_ASSERT(le != NULL);
526 UTEST_ASSERT(be != NULL);
527 if (len > 0)
528 UTEST_ASSERT(strcmp_test(le, be) != 0);
529
530 // UTF16 -> UTF32
531 s32[0] = lsp::utf16_to_utf32(na);
532 s32[1] = lsp::utf16le_to_utf32(le);
533 s32[2] = lsp::utf16be_to_utf32(be);
534 s32[3] = lsp::utf16_to_utf32le(na);
535 s32[4] = lsp::utf16le_to_utf32le(le);
536 s32[5] = lsp::utf16be_to_utf32le(be);
537 s32[6] = lsp::utf16_to_utf32be(na);
538 s32[7] = lsp::utf16le_to_utf32be(le);
539 s32[8] = lsp::utf16be_to_utf32be(be);
540
541 for (size_t j=0; j<9; ++j)
542 UTEST_ASSERT(s32[j] != NULL);
543
544 UTEST_ASSERT_MSG(((len = strlen_test(s32[0])) == ck->u32strlen),
545 "Error checking line %d: strlen=%d, expected=%d",
546 int(i), int(len), int(ck->u32strlen));
547 UTEST_ASSERT_MSG(((len = utf32_count_invalid(s32[0])) == ck->invalid),
548 "Error checking line %d: utf32_count_invalid=%d, expected=%d",
549 int(i), int(len), int(ck->invalid));
550
551 UTEST_ASSERT(strcmp_test(s32[0], s32[1]) == 0);
552 UTEST_ASSERT(strcmp_test(s32[0], s32[2]) == 0);
553 UTEST_ASSERT(strcmp_bswap(s32[0], s32[3], true) == 0);
554 UTEST_ASSERT(strcmp_bswap(s32[0], s32[4], true) == 0);
555 UTEST_ASSERT(strcmp_bswap(s32[0], s32[5], true) == 0);
556 UTEST_ASSERT(strcmp_bswap(s32[0], s32[6], false) == 0);
557 UTEST_ASSERT(strcmp_bswap(s32[0], s32[7], false) == 0);
558 UTEST_ASSERT(strcmp_bswap(s32[0], s32[8], false) == 0);
559
560 if (len > 0)
561 {
562 UTEST_ASSERT(strcmp_test(s32[4], s32[7]) != 0);
563 UTEST_ASSERT(strcmp_test(s32[5], s32[7]) != 0);
564 UTEST_ASSERT(strcmp_test(s32[4], s32[8]) != 0);
565 UTEST_ASSERT(strcmp_test(s32[5], s32[8]) != 0);
566 }
567
568 for (size_t j=0; j<9; ++j)
569 free(s32[j]);
570
571 // Free LE and BE copies
572 free(le);
573 free(be);
574 }
575 }
576
577 void check_utf16_to_utf32_buf()
578 {
579 constexpr size_t buf_size = 10;
580 lsp_utf16_t *le = NULL, *be = NULL;
581 const lsp_utf16_t *na = NULL;
582 lsp_utf32_t s32[9][buf_size];
583 size_t len, nl[9];
584 printf("Testing utf16_to_utf32_buf\n");
585
586 for (size_t i=0, n=sizeof(utf16_check)/sizeof(utf16_check_t); i<n; ++i)
587 {
588 printf(" checking test line %d...\n", int(i));
589
590 utf16_check_t *ck = &utf16_check[i];
591
592 // Obtain native string and it's LE and BE copies
593 na = reinterpret_cast<const lsp_utf16_t *>(ck->s);
594 le = strdup_bswap(na, true);
595 lsp_finally { free(le); };
596 be = strdup_bswap(na, false);
597 lsp_finally { free(be); };
598
599 len = strlen_test(na);
600 UTEST_ASSERT(le != NULL);
601 UTEST_ASSERT(be != NULL);
602 if (len > 0)
603 UTEST_ASSERT(strcmp_test(le, be) != 0);
604
605 for (size_t j=0; j<9; ++j)
606 memset(s32[j], 0xcc, sizeof(lsp_utf32_t) * buf_size);
607
608 // UTF16 -> UTF32
609 nl[0] = lsp::utf16_to_utf32(s32[0], na, buf_size);
610 nl[1] = lsp::utf16le_to_utf32(s32[1], le, buf_size);
611 nl[2] = lsp::utf16be_to_utf32(s32[2], be, buf_size);
612 nl[3] = lsp::utf16_to_utf32le(s32[3], na, buf_size);
613 nl[4] = lsp::utf16le_to_utf32le(s32[4], le, buf_size);
614 nl[5] = lsp::utf16be_to_utf32le(s32[5], be, buf_size);
615 nl[6] = lsp::utf16_to_utf32be(s32[6], na, buf_size);
616 nl[7] = lsp::utf16le_to_utf32be(s32[7], le, buf_size);
617 nl[8] = lsp::utf16be_to_utf32be(s32[8], be, buf_size);
618
619 for (size_t j=1; j<9; ++j)
620 UTEST_ASSERT(nl[j] == nl[0]);
621 if (nl[0] == 0)
622 continue;
623
624 UTEST_ASSERT(strcmp_test(s32[0], s32[1]) == 0);
625 UTEST_ASSERT(strcmp_test(s32[0], s32[2]) == 0);
626 UTEST_ASSERT(strcmp_bswap(s32[0], s32[3], true) == 0);
627 UTEST_ASSERT(strcmp_bswap(s32[0], s32[4], true) == 0);
628 UTEST_ASSERT(strcmp_bswap(s32[0], s32[5], true) == 0);
629 UTEST_ASSERT(strcmp_bswap(s32[0], s32[6], false) == 0);
630 UTEST_ASSERT(strcmp_bswap(s32[0], s32[7], false) == 0);
631 UTEST_ASSERT(strcmp_bswap(s32[0], s32[8], false) == 0);
632
633 if (len > 0)
634 {
635 UTEST_ASSERT(strcmp_test(s32[4], s32[7]) != 0);
636 UTEST_ASSERT(strcmp_test(s32[5], s32[7]) != 0);
637 UTEST_ASSERT(strcmp_test(s32[4], s32[8]) != 0);
638 UTEST_ASSERT(strcmp_test(s32[5], s32[8]) != 0);
639 }
640 }
641 }
642
643 void check_latin_lower_upper()
644 {
645 static const lsp_wchar_t lower[] = {
646 'a', 'b', 'c', 'd',
647 'e', 'f', 'g', 'h',
648 'i', 'j', 'k', 'l',
649 'm', 'n', 'o', 'p',
650 'q', 'r', 's', 't',
651 'u', 'v', 'w', 'x',
652 'y', 'z'
653 };
654
655 static const lsp_wchar_t upper[] = {
656 'A', 'B', 'C', 'D',
657 'E', 'F', 'G', 'H',
658 'I', 'J', 'K', 'L',
659 'M', 'N', 'O', 'P',
660 'Q', 'R', 'S', 'T',
661 'U', 'V', 'W', 'X',
662 'Y', 'Z'
663 };
664
665 printf("Testing lsp::to_lower() and lsp::to_upper() for latin character set\n");
666
667 for (size_t i=0; i<sizeof(lower)/sizeof(lower[0]); ++i)
668 {
669 lsp_wchar_t lc = lsp::to_lower(upper[i]);
670 lsp_wchar_t uc = lsp::to_upper(lower[i]);
671
672 UTEST_ASSERT_MSG(
673 lc == lower[i],
674 "Failed conversion of character 0x%x to lower value 0x%x, result value is 0x%x (index=%d)",
675 int(upper[i]), int(lower[i]), int(lc), int(i));
676 UTEST_ASSERT_MSG(
677 uc == upper[i],
678 "Failed conversion of character 0x%x to upper value 0x%x, result value is 0x%x (index=%d)",
679 int(lower[i]), int(upper[i]), int(uc), int(i));
680 }
681 }
682
683 void check_cyrillic_lower_upper()
684 {
685 static const lsp_wchar_t lower[] = {
686 0x450, 0x451, 0x452, 0x453,
687 0x454, 0x455, 0x456, 0x457,
688 0x458, 0x459, 0x45a, 0x45b,
689 0x45c, 0x45d, 0x45e, 0x45f,
690
691 0x430, 0x431, 0x432, 0x433,
692 0x434, 0x435, 0x436, 0x437,
693 0x438, 0x439, 0x43a, 0x43b,
694 0x43c, 0x43d, 0x43e, 0x43f,
695 0x440, 0x441, 0x442, 0x443,
696 0x444, 0x445, 0x446, 0x447,
697 0x448, 0x449, 0x44a, 0x44b,
698 0x44c, 0x44d, 0x44e, 0x44f,
699
700 0x461, 0x463, 0x465, 0x467,
701 0x469, 0x46b, 0x46d, 0x46f,
702 0x471, 0x473, 0x475, 0x477,
703 0x479, 0x47b, 0x47d, 0x47f,
704 0x481,
705
706 0x482, 0x483, 0x484, 0x485,
707 0x486, 0x487, 0x488, 0x489,
708
709 0x48b, 0x48d, 0x48f,
710 0x491, 0x493, 0x495, 0x497,
711 0x499, 0x49b, 0x49d, 0x49f,
712 0x4a1, 0x4a3, 0x4a5, 0x4a7,
713 0x4a9, 0x4ab, 0x4ad, 0x4af,
714 0x4b1, 0x4b3, 0x4b5, 0x4b7,
715 0x4b9, 0x4bb, 0x4bd, 0x4bf,
716 0x4c1, 0x4c3, 0x4c5, 0x4c7,
717 0x4c9, 0x4cb, 0x4cd, 0x4cf,
718 0x4d1, 0x4d3, 0x4d5, 0x4d7,
719 0x4d9, 0x4db, 0x4dd, 0x4df,
720 0x4e1, 0x4e3, 0x4e5, 0x4e7,
721 0x4e9, 0x4eb, 0x4ed, 0x4ef,
722 0x4f1, 0x4f3, 0x4f5, 0x4f7,
723 0x4f9, 0x4fb, 0x4fd, 0x4ff,
724
725 0x501, 0x503, 0x505, 0x507,
726 0x509, 0x50b, 0x50d, 0x50f,
727 0x511, 0x513, 0x515, 0x517,
728 0x519, 0x51b, 0x51d, 0x51f,
729 0x521, 0x523, 0x525, 0x527,
730 0x529, 0x52b, 0x52d, 0x52f,
731
732 0xa641, 0xa643, 0xa645, 0xa647,
733 0xa649, 0xa64b, 0xa64d, 0xa64f,
734 0xa651, 0xa653, 0xa655, 0xa657,
735 0xa659, 0xa65b, 0xa65d, 0xa65f,
736 0xa661, 0xa663, 0xa665, 0xa667,
737 0xa669, 0xa66b, 0xa66d, 0xa66e,
738
739 0xa681, 0xa683, 0xa685, 0xa687,
740 0xa689, 0xa68b, 0xa68d, 0xa68f,
741 0xa691, 0xa693, 0xa695, 0xa697,
742 0xa699, 0xa69b, 0xa69c, 0xa69e,
743 };
744
745 static const lsp_wchar_t upper[] = {
746 0x400, 0x401, 0x402, 0x403,
747 0x404, 0x405, 0x406, 0x407,
748 0x408, 0x409, 0x40a, 0x40b,
749 0x40c, 0x40d, 0x40e, 0x40f,
750
751 0x410, 0x411, 0x412, 0x413,
752 0x414, 0x415, 0x416, 0x417,
753 0x418, 0x419, 0x41a, 0x41b,
754 0x41c, 0x41d, 0x41e, 0x41f,
755 0x420, 0x421, 0x422, 0x423,
756 0x424, 0x425, 0x426, 0x427,
757 0x428, 0x429, 0x42a, 0x42b,
758 0x42c, 0x42d, 0x42e, 0x42f,
759
760 0x460, 0x462, 0x464, 0x466,
761 0x468, 0x46a, 0x46c, 0x46e,
762 0x470, 0x472, 0x474, 0x476,
763 0x478, 0x47a, 0x47c, 0x47e,
764 0x480,
765
766 0x482, 0x483, 0x484, 0x485,
767 0x486, 0x487, 0x488, 0x489,
768
769 0x48a, 0x48c, 0x48e,
770 0x490, 0x492, 0x494, 0x496,
771 0x498, 0x49a, 0x49c, 0x49e,
772 0x4a0, 0x4a2, 0x4a4, 0x4a6,
773 0x4a8, 0x4aa, 0x4ac, 0x4ae,
774 0x4b0, 0x4b2, 0x4b4, 0x4b6,
775 0x4b8, 0x4ba, 0x4bc, 0x4be,
776 0x4c0, 0x4c2, 0x4c4, 0x4c6,
777 0x4c8, 0x4ca, 0x4cc, 0x4ce,
778 0x4d0, 0x4d2, 0x4d4, 0x4d6,
779 0x4d8, 0x4da, 0x4dc, 0x4de,
780 0x4e0, 0x4e2, 0x4e4, 0x4e6,
781 0x4e8, 0x4ea, 0x4ec, 0x4ee,
782 0x4f0, 0x4f2, 0x4f4, 0x4f6,
783 0x4f8, 0x4fa, 0x4fc, 0x4fe,
784
785 0x500, 0x502, 0x504, 0x506,
786 0x508, 0x50a, 0x50c, 0x50e,
787 0x510, 0x512, 0x514, 0x516,
788 0x518, 0x51a, 0x51c, 0x51e,
789 0x520, 0x522, 0x524, 0x526,
790 0x528, 0x52a, 0x52c, 0x52e,
791
792 0xa640, 0xa642, 0xa644, 0xa646,
793 0xa648, 0xa64a, 0xa64c, 0xa64e,
794 0xa650, 0xa652, 0xa654, 0xa656,
795 0xa658, 0xa65a, 0xa65c, 0xa65e,
796 0xa660, 0xa662, 0xa664, 0xa666,
797 0xa668, 0xa66a, 0xa66c, 0xa66e,
798
799 0xa680, 0xa682, 0xa684, 0xa686,
800 0xa688, 0xa68a, 0xa68c, 0xa68e,
801 0xa690, 0xa692, 0xa694, 0xa696,
802 0xa698, 0xa69a, 0xa69c, 0xa69e,
803 };
804
805 printf("Testing lsp::to_lower() and lsp::to_upper() for cyrillic character set\n");
806
807 for (size_t i=0; i<sizeof(lower)/sizeof(lower[0]); ++i)
808 {
809 lsp_wchar_t lc = lsp::to_lower(upper[i]);
810 lsp_wchar_t uc = lsp::to_upper(lower[i]);
811
812 UTEST_ASSERT_MSG(
813 lc == lower[i],
814 "Failed conversion of character 0x%x to lower value 0x%x, result value is 0x%x (index=%d)",
815 int(upper[i]), int(lower[i]), int(lc), int(i));
816 UTEST_ASSERT_MSG(
817 uc == upper[i],
818 "Failed conversion of character 0x%x to upper value 0x%x, result value is 0x%x (index=%d)",
819 int(lower[i]), int(upper[i]), int(uc), int(i));
820 }
821 }
822
823 UTEST_MAIN
824 {
825 check_utf8_to_utf16();
826 check_utf8_to_utf32();
827
828 check_utf8_to_utf16_buf();
829 check_utf8_to_utf32_buf();
830
831 check_utf16_to_utf8();
832 check_utf16_to_utf32();
833
834 check_utf16_to_utf8_buf();
835 check_utf16_to_utf32_buf();
836
837 check_latin_lower_upper();
838 check_cyrillic_lower_upper();
839 }
840 UTEST_END;
841
842
843