1 // SPDX-License-Identifier: GPL-2.0+
3 * Unit tests for Unicode functions
5 * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
14 #include <test/test.h>
15 #include <test/suites.h>
18 /* Linker list entry for a Unicode test */
19 #define UNICODE_TEST(_name) UNIT_TEST(_name, 0, unicode_test)
21 /* Constants c1-c4 and d1-d4 encode the same letters */
23 /* Six characters translating to one utf-8 byte each. */
24 static const u16 c1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
25 /* One character translating to two utf-8 bytes */
26 static const u16 c2[] = {0x6b, 0x61, 0x66, 0x62, 0xe1, 0x74, 0x75, 0x72, 0x00};
27 /* Three characters translating to three utf-8 bytes each */
28 static const u16 c3[] = {0x6f5c, 0x6c34, 0x8266, 0x00};
29 /* Three letters translating to four utf-8 bytes each */
30 static const u16 c4[] = {0xd801, 0xdc8d, 0xd801, 0xdc96, 0xd801, 0xdc87,
33 /* Illegal utf-16 strings */
34 static const u16 i1[] = {0x69, 0x31, 0xdc87, 0x6c, 0x00};
35 static const u16 i2[] = {0x69, 0x32, 0xd801, 0xd801, 0x6c, 0x00};
36 static const u16 i3[] = {0x69, 0x33, 0xd801, 0x00};
38 /* Six characters translating to one utf-16 word each. */
39 static const char d1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
40 /* Eight characters translating to one utf-16 word each */
41 static const char d2[] = {0x6b, 0x61, 0x66, 0x62, 0xc3, 0xa1, 0x74, 0x75,
43 /* Three characters translating to one utf-16 word each */
44 static const char d3[] = {0xe6, 0xbd, 0x9c, 0xe6, 0xb0, 0xb4, 0xe8, 0x89,
46 /* Three letters translating to two utf-16 word each */
47 static const char d4[] = {0xf0, 0x90, 0x92, 0x8d, 0xf0, 0x90, 0x92, 0x96,
48 0xf0, 0x90, 0x92, 0x87, 0x00};
50 /* Illegal utf-8 strings */
51 static const char j1[] = {0x6a, 0x31, 0xa1, 0x6c, 0x00};
52 static const char j2[] = {0x6a, 0x32, 0xc3, 0xc3, 0x6c, 0x00};
53 static const char j3[] = {0x6a, 0x33, 0xf0, 0x90, 0xf0, 0x00};
55 static int unicode_test_u16_strlen(struct unit_test_state *uts)
57 ut_asserteq(6, u16_strlen(c1));
58 ut_asserteq(8, u16_strlen(c2));
59 ut_asserteq(3, u16_strlen(c3));
60 ut_asserteq(6, u16_strlen(c4));
63 UNICODE_TEST(unicode_test_u16_strlen);
65 static int unicode_test_u16_strdup(struct unit_test_state *uts)
67 u16 *copy = u16_strdup(c4);
69 ut_assert(copy != c4);
70 ut_asserteq_mem(copy, c4, sizeof(c4));
75 UNICODE_TEST(unicode_test_u16_strdup);
77 static int unicode_test_u16_strcpy(struct unit_test_state *uts)
82 r = u16_strcpy(copy, c1);
84 ut_asserteq_mem(copy, c1, sizeof(c1));
88 UNICODE_TEST(unicode_test_u16_strcpy);
90 /* U-Boot uses UTF-16 strings in the EFI context only. */
91 #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
92 static int unicode_test_string16(struct unit_test_state *uts)
96 /* Test length and precision */
97 memset(buf, 0xff, sizeof(buf));
98 sprintf(buf, "%8.6ls", c2);
99 ut_asserteq(' ', buf[1]);
100 ut_assert(!strncmp(&buf[2], d2, 7));
103 memset(buf, 0xff, sizeof(buf));
104 sprintf(buf, "%8.6ls", c4);
105 ut_asserteq(' ', buf[4]);
106 ut_assert(!strncmp(&buf[5], d4, 12));
109 memset(buf, 0xff, sizeof(buf));
110 sprintf(buf, "%-8.2ls", c4);
111 ut_asserteq(' ', buf[8]);
112 ut_assert(!strncmp(buf, d4, 8));
115 /* Test handling of illegal utf-16 sequences */
116 memset(buf, 0xff, sizeof(buf));
117 sprintf(buf, "%ls", i1);
118 ut_asserteq_str("i1?l", buf);
120 memset(buf, 0xff, sizeof(buf));
121 sprintf(buf, "%ls", i2);
122 ut_asserteq_str("i2?l", buf);
124 memset(buf, 0xff, sizeof(buf));
125 sprintf(buf, "%ls", i3);
126 ut_asserteq_str("i3?", buf);
130 UNICODE_TEST(unicode_test_string16);
133 static int unicode_test_utf8_get(struct unit_test_state *uts)
139 /* Check characters less than 0x800 */
141 for (i = 0; i < 8; ++i) {
142 code = utf8_get((const char **)&s);
143 /* c2 is the utf-8 encoding of d2 */
144 ut_asserteq(c2[i], code);
148 ut_asserteq_ptr(s, d2 + 9)
150 /* Check characters less than 0x10000 */
152 for (i = 0; i < 4; ++i) {
153 code = utf8_get((const char **)&s);
154 /* c3 is the utf-8 encoding of d3 */
155 ut_asserteq(c3[i], code);
159 ut_asserteq_ptr(s, d3 + 9)
161 /* Check character greater 0xffff */
163 code = utf8_get((const char **)&s);
164 ut_asserteq(0x0001048d, code);
165 ut_asserteq_ptr(s, d4 + 4);
169 UNICODE_TEST(unicode_test_utf8_get);
171 static int unicode_test_utf8_put(struct unit_test_state *uts)
173 char buffer[8] = { 0, };
176 /* Commercial at, translates to one character */
178 ut_assert(!utf8_put('@', &pos))
179 ut_asserteq(1, pos - buffer);
180 ut_asserteq('@', buffer[0]);
181 ut_assert(!buffer[1]);
183 /* Latin letter G with acute, translates to two charactes */
185 ut_assert(!utf8_put(0x1f4, &pos));
186 ut_asserteq(2, pos - buffer);
187 ut_asserteq_str("\xc7\xb4", buffer);
189 /* Tagalog letter i, translates to three characters */
191 ut_assert(!utf8_put(0x1701, &pos));
192 ut_asserteq(3, pos - buffer);
193 ut_asserteq_str("\xe1\x9c\x81", buffer);
195 /* Hamster face, translates to four characters */
197 ut_assert(!utf8_put(0x1f439, &pos));
198 ut_asserteq(4, pos - buffer);
199 ut_asserteq_str("\xf0\x9f\x90\xb9", buffer);
203 ut_asserteq(-1, utf8_put(0xd888, &pos));
207 UNICODE_TEST(unicode_test_utf8_put);
209 static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts)
211 ut_asserteq(6, utf8_utf16_strlen(d1));
212 ut_asserteq(8, utf8_utf16_strlen(d2));
213 ut_asserteq(3, utf8_utf16_strlen(d3));
214 ut_asserteq(6, utf8_utf16_strlen(d4));
216 /* illegal utf-8 sequences */
217 ut_asserteq(4, utf8_utf16_strlen(j1));
218 ut_asserteq(4, utf8_utf16_strlen(j2));
219 ut_asserteq(3, utf8_utf16_strlen(j3));
223 UNICODE_TEST(unicode_test_utf8_utf16_strlen);
225 static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts)
227 ut_asserteq(3, utf8_utf16_strnlen(d1, 3));
228 ut_asserteq(6, utf8_utf16_strnlen(d1, 13));
229 ut_asserteq(6, utf8_utf16_strnlen(d2, 6));
230 ut_asserteq(2, utf8_utf16_strnlen(d3, 2));
231 ut_asserteq(4, utf8_utf16_strnlen(d4, 2));
232 ut_asserteq(6, utf8_utf16_strnlen(d4, 3));
234 /* illegal utf-8 sequences */
235 ut_asserteq(4, utf8_utf16_strnlen(j1, 16));
236 ut_asserteq(4, utf8_utf16_strnlen(j2, 16));
237 ut_asserteq(3, utf8_utf16_strnlen(j3, 16));
241 UNICODE_TEST(unicode_test_utf8_utf16_strnlen);
244 * ut_u16_strcmp() - Compare to u16 strings.
248 * @count: number of u16 to compare
249 * Return: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
251 static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count)
253 for (; (*a1 || *a2) && count; ++a1, ++a2, --count) {
262 static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts)
268 utf8_utf16_strcpy(&pos, d1);
269 ut_asserteq(6, pos - buf);
270 ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX));
273 utf8_utf16_strcpy(&pos, d2);
274 ut_asserteq(8, pos - buf);
275 ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
278 utf8_utf16_strcpy(&pos, d3);
279 ut_asserteq(3, pos - buf);
280 ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX));
283 utf8_utf16_strcpy(&pos, d4);
284 ut_asserteq(6, pos - buf);
285 ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
287 /* Illegal utf-8 strings */
289 utf8_utf16_strcpy(&pos, j1);
290 ut_asserteq(4, pos - buf);
291 ut_assert(!unicode_test_u16_strcmp(buf, L"j1?l", SIZE_MAX));
294 utf8_utf16_strcpy(&pos, j2);
295 ut_asserteq(4, pos - buf);
296 ut_assert(!unicode_test_u16_strcmp(buf, L"j2?l", SIZE_MAX));
299 utf8_utf16_strcpy(&pos, j3);
300 ut_asserteq(3, pos - buf);
301 ut_assert(!unicode_test_u16_strcmp(buf, L"j3?", SIZE_MAX));
305 UNICODE_TEST(unicode_test_utf8_utf16_strcpy);
307 static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts)
313 memset(buf, 0, sizeof(buf));
314 utf8_utf16_strncpy(&pos, d1, 4);
315 ut_asserteq(4, pos - buf);
317 ut_assert(!unicode_test_u16_strcmp(buf, c1, 4));
320 memset(buf, 0, sizeof(buf));
321 utf8_utf16_strncpy(&pos, d2, 10);
322 ut_asserteq(8, pos - buf);
324 ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
327 memset(buf, 0, sizeof(buf));
328 utf8_utf16_strncpy(&pos, d3, 2);
329 ut_asserteq(2, pos - buf);
331 ut_assert(!unicode_test_u16_strcmp(buf, c3, 2));
334 memset(buf, 0, sizeof(buf));
335 utf8_utf16_strncpy(&pos, d4, 2);
336 ut_asserteq(4, pos - buf);
338 ut_assert(!unicode_test_u16_strcmp(buf, c4, 4));
341 memset(buf, 0, sizeof(buf));
342 utf8_utf16_strncpy(&pos, d4, 10);
343 ut_asserteq(6, pos - buf);
345 ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
349 UNICODE_TEST(unicode_test_utf8_utf16_strncpy);
351 static int unicode_test_utf16_get(struct unit_test_state *uts)
357 /* Check characters less than 0x10000 */
359 for (i = 0; i < 9; ++i) {
360 code = utf16_get((const u16 **)&s);
361 ut_asserteq(c2[i], code);
365 ut_asserteq_ptr(c2 + 8, s);
367 /* Check character greater 0xffff */
369 code = utf16_get((const u16 **)&s);
370 ut_asserteq(0x0001048d, code);
371 ut_asserteq_ptr(c4 + 2, s);
375 UNICODE_TEST(unicode_test_utf16_get);
377 static int unicode_test_utf16_put(struct unit_test_state *uts)
379 u16 buffer[4] = { 0, };
382 /* Commercial at, translates to one word */
384 ut_assert(!utf16_put('@', &pos));
385 ut_asserteq(1, pos - buffer);
386 ut_asserteq((u16)'@', buffer[0]);
387 ut_assert(!buffer[1]);
389 /* Hamster face, translates to two words */
391 ut_assert(!utf16_put(0x1f439, &pos));
392 ut_asserteq(2, pos - buffer);
393 ut_asserteq((u16)0xd83d, buffer[0]);
394 ut_asserteq((u16)0xdc39, buffer[1]);
395 ut_assert(!buffer[2]);
399 ut_asserteq(-1, utf16_put(0xd888, &pos));
403 UNICODE_TEST(unicode_test_utf16_put);
405 static int unicode_test_utf16_strnlen(struct unit_test_state *uts)
407 ut_asserteq(3, utf16_strnlen(c1, 3));
408 ut_asserteq(6, utf16_strnlen(c1, 13));
409 ut_asserteq(6, utf16_strnlen(c2, 6));
410 ut_asserteq(2, utf16_strnlen(c3, 2));
411 ut_asserteq(2, utf16_strnlen(c4, 2));
412 ut_asserteq(3, utf16_strnlen(c4, 3));
414 /* illegal utf-16 word sequences */
415 ut_asserteq(4, utf16_strnlen(i1, 16));
416 ut_asserteq(4, utf16_strnlen(i2, 16));
417 ut_asserteq(3, utf16_strnlen(i3, 16));
421 UNICODE_TEST(unicode_test_utf16_strnlen);
423 static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts)
425 ut_asserteq(6, utf16_utf8_strlen(c1));
426 ut_asserteq(9, utf16_utf8_strlen(c2));
427 ut_asserteq(9, utf16_utf8_strlen(c3));
428 ut_asserteq(12, utf16_utf8_strlen(c4));
430 /* illegal utf-16 word sequences */
431 ut_asserteq(4, utf16_utf8_strlen(i1));
432 ut_asserteq(4, utf16_utf8_strlen(i2));
433 ut_asserteq(3, utf16_utf8_strlen(i3));
437 UNICODE_TEST(unicode_test_utf16_utf8_strlen);
439 static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts)
441 ut_asserteq(3, utf16_utf8_strnlen(c1, 3));
442 ut_asserteq(6, utf16_utf8_strnlen(c1, 13));
443 ut_asserteq(7, utf16_utf8_strnlen(c2, 6));
444 ut_asserteq(6, utf16_utf8_strnlen(c3, 2));
445 ut_asserteq(8, utf16_utf8_strnlen(c4, 2));
446 ut_asserteq(12, utf16_utf8_strnlen(c4, 3));
449 UNICODE_TEST(unicode_test_utf16_utf8_strnlen);
451 static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts)
457 utf16_utf8_strcpy(&pos, c1);
458 ut_asserteq(6, pos - buf);
459 ut_asserteq_str(d1, buf);
462 utf16_utf8_strcpy(&pos, c2);
463 ut_asserteq(9, pos - buf);
464 ut_asserteq_str(d2, buf);
467 utf16_utf8_strcpy(&pos, c3);
468 ut_asserteq(9, pos - buf);
469 ut_asserteq_str(d3, buf);
472 utf16_utf8_strcpy(&pos, c4);
473 ut_asserteq(12, pos - buf);
474 ut_asserteq_str(d4, buf);
476 /* Illegal utf-16 strings */
478 utf16_utf8_strcpy(&pos, i1);
479 ut_asserteq(4, pos - buf);
480 ut_asserteq_str("i1?l", buf);
483 utf16_utf8_strcpy(&pos, i2);
484 ut_asserteq(4, pos - buf);
485 ut_asserteq_str("i2?l", buf);
488 utf16_utf8_strcpy(&pos, i3);
489 ut_asserteq(3, pos - buf);
490 ut_asserteq_str("i3?", buf);
494 UNICODE_TEST(unicode_test_utf16_utf8_strcpy);
496 static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts)
502 memset(buf, 0, sizeof(buf));
503 utf16_utf8_strncpy(&pos, c1, 4);
504 ut_asserteq(4, pos - buf);
506 ut_assert(!strncmp(buf, d1, 4));
509 memset(buf, 0, sizeof(buf));
510 utf16_utf8_strncpy(&pos, c2, 10);
511 ut_asserteq(9, pos - buf);
513 ut_assert(!strncmp(buf, d2, SIZE_MAX));
516 memset(buf, 0, sizeof(buf));
517 utf16_utf8_strncpy(&pos, c3, 2);
518 ut_asserteq(6, pos - buf);
520 ut_assert(!strncmp(buf, d3, 6));
523 memset(buf, 0, sizeof(buf));
524 utf16_utf8_strncpy(&pos, c4, 2);
525 ut_asserteq(8, pos - buf);
527 ut_assert(!strncmp(buf, d4, 8));
530 memset(buf, 0, sizeof(buf));
531 utf16_utf8_strncpy(&pos, c4, 10);
532 ut_asserteq(12, pos - buf);
534 ut_assert(!strncmp(buf, d4, SIZE_MAX));
538 UNICODE_TEST(unicode_test_utf16_utf8_strncpy);
540 static int unicode_test_utf_to_lower(struct unit_test_state *uts)
542 ut_asserteq('@', utf_to_lower('@'));
543 ut_asserteq('a', utf_to_lower('A'));
544 ut_asserteq('z', utf_to_lower('Z'));
545 ut_asserteq('[', utf_to_lower('['));
546 ut_asserteq('m', utf_to_lower('m'));
547 /* Latin letter O with diaresis (umlaut) */
548 ut_asserteq(0x00f6, utf_to_lower(0x00d6));
549 #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
550 /* Cyrillic letter I*/
551 ut_asserteq(0x0438, utf_to_lower(0x0418));
555 UNICODE_TEST(unicode_test_utf_to_lower);
557 static int unicode_test_utf_to_upper(struct unit_test_state *uts)
559 ut_asserteq('`', utf_to_upper('`'));
560 ut_asserteq('A', utf_to_upper('a'));
561 ut_asserteq('Z', utf_to_upper('z'));
562 ut_asserteq('{', utf_to_upper('{'));
563 ut_asserteq('M', utf_to_upper('M'));
564 /* Latin letter O with diaresis (umlaut) */
565 ut_asserteq(0x00d6, utf_to_upper(0x00f6));
566 #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
567 /* Cyrillic letter I */
568 ut_asserteq(0x0418, utf_to_upper(0x0438));
572 UNICODE_TEST(unicode_test_utf_to_upper);
574 static int unicode_test_u16_strncmp(struct unit_test_state *uts)
576 ut_assert(u16_strncmp(L"abc", L"abc", 3) == 0);
577 ut_assert(u16_strncmp(L"abcdef", L"abcghi", 3) == 0);
578 ut_assert(u16_strncmp(L"abcdef", L"abcghi", 6) < 0);
579 ut_assert(u16_strncmp(L"abcghi", L"abcdef", 6) > 0);
580 ut_assert(u16_strcmp(L"abc", L"abc") == 0);
581 ut_assert(u16_strcmp(L"abcdef", L"deghi") < 0);
582 ut_assert(u16_strcmp(L"deghi", L"abcdef") > 0);
585 UNICODE_TEST(unicode_test_u16_strncmp);
587 static int unicode_test_u16_strsize(struct unit_test_state *uts)
589 ut_asserteq_64(u16_strsize(c1), 14);
590 ut_asserteq_64(u16_strsize(c2), 18);
591 ut_asserteq_64(u16_strsize(c3), 8);
592 ut_asserteq_64(u16_strsize(c4), 14);
595 UNICODE_TEST(unicode_test_u16_strsize);
597 int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
599 struct unit_test *tests = ll_entry_start(struct unit_test, unicode_test);
600 const int n_ents = ll_entry_count(struct unit_test, unicode_test);
602 return cmd_ut_category("Unicode", "unicode_test_",
603 tests, n_ents, argc, argv);