1 // SPDX-License-Identifier: GPL-2.0+
3 * Unit tests for Unicode functions
5 * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
11 #include <efi_loader.h>
15 #include <test/test.h>
16 #include <test/suites.h>
19 /* Linker list entry for a Unicode test */
20 #define UNICODE_TEST(_name) UNIT_TEST(_name, 0, unicode_test)
22 /* Constants c1-c4 and d1-d4 encode the same letters */
24 /* Six characters translating to one utf-8 byte each. */
25 static const u16 c1
[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
26 /* One character translating to two utf-8 bytes */
27 static const u16 c2
[] = {0x6b, 0x61, 0x66, 0x62, 0xe1, 0x74, 0x75, 0x72, 0x00};
28 /* Three characters translating to three utf-8 bytes each */
29 static const u16 c3
[] = {0x6f5c, 0x6c34, 0x8266, 0x00};
30 /* Three letters translating to four utf-8 bytes each */
31 static const u16 c4
[] = {0xd801, 0xdc8d, 0xd801, 0xdc96, 0xd801, 0xdc87,
34 /* Illegal utf-16 strings */
35 static const u16 i1
[] = {0x69, 0x31, 0xdc87, 0x6c, 0x00};
36 static const u16 i2
[] = {0x69, 0x32, 0xd801, 0xd801, 0x6c, 0x00};
37 static const u16 i3
[] = {0x69, 0x33, 0xd801, 0x00};
39 /* Six characters translating to one utf-16 word each. */
40 static const char d1
[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
41 /* Eight characters translating to one utf-16 word each */
42 static const char d2
[] = {0x6b, 0x61, 0x66, 0x62, 0xc3, 0xa1, 0x74, 0x75,
44 /* Three characters translating to one utf-16 word each */
45 static const char d3
[] = {0xe6, 0xbd, 0x9c, 0xe6, 0xb0, 0xb4, 0xe8, 0x89,
47 /* Three letters translating to two utf-16 word each */
48 static const char d4
[] = {0xf0, 0x90, 0x92, 0x8d, 0xf0, 0x90, 0x92, 0x96,
49 0xf0, 0x90, 0x92, 0x87, 0x00};
50 /* Letter not in code page 437 */
51 static const char d5
[] = {0xCE, 0x92, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F,
52 0x74, 0x20, 0x42, 0x00};
54 /* Illegal utf-8 strings */
55 static const char j1
[] = {0x6a, 0x31, 0xa1, 0x6c, 0x00};
56 static const char j2
[] = {0x6a, 0x32, 0xc3, 0xc3, 0x6c, 0x00};
57 static const char j3
[] = {0x6a, 0x33, 0xf0, 0x90, 0xf0, 0x00};
58 static const char j4
[] = {0xa1, 0x00};
60 static int unicode_test_u16_strlen(struct unit_test_state
*uts
)
62 ut_asserteq(6, u16_strlen(c1
));
63 ut_asserteq(8, u16_strlen(c2
));
64 ut_asserteq(3, u16_strlen(c3
));
65 ut_asserteq(6, u16_strlen(c4
));
68 UNICODE_TEST(unicode_test_u16_strlen
);
70 static int unicode_test_u16_strnlen(struct unit_test_state
*uts
)
72 ut_asserteq(0, u16_strnlen(c1
, 0));
73 ut_asserteq(4, u16_strnlen(c1
, 4));
74 ut_asserteq(6, u16_strnlen(c1
, 6));
75 ut_asserteq(6, u16_strnlen(c1
, 7));
79 UNICODE_TEST(unicode_test_u16_strnlen
);
81 static int unicode_test_u16_strdup(struct unit_test_state
*uts
)
83 u16
*copy
= u16_strdup(c4
);
85 ut_assert(copy
!= c4
);
86 ut_asserteq_mem(copy
, c4
, sizeof(c4
));
91 UNICODE_TEST(unicode_test_u16_strdup
);
93 static int unicode_test_u16_strcpy(struct unit_test_state
*uts
)
98 r
= u16_strcpy(copy
, c1
);
100 ut_asserteq_mem(copy
, c1
, sizeof(c1
));
104 UNICODE_TEST(unicode_test_u16_strcpy
);
106 /* U-Boot uses UTF-16 strings in the EFI context only. */
107 #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
108 static int unicode_test_string16(struct unit_test_state
*uts
)
113 /* Test length and precision */
114 memset(buf
, 0xff, sizeof(buf
));
115 sprintf(buf
, "%8.6ls", c2
);
116 ut_asserteq(' ', buf
[1]);
117 ut_assert(!strncmp(&buf
[2], d2
, 7));
120 memset(buf
, 0xff, sizeof(buf
));
121 sprintf(buf
, "%8.6ls", c4
);
122 ut_asserteq(' ', buf
[4]);
123 ut_assert(!strncmp(&buf
[5], d4
, 12));
126 memset(buf
, 0xff, sizeof(buf
));
127 sprintf(buf
, "%-8.2ls", c4
);
128 ut_asserteq(' ', buf
[8]);
129 ut_assert(!strncmp(buf
, d4
, 8));
132 /* Test handling of illegal utf-16 sequences */
133 memset(buf
, 0xff, sizeof(buf
));
134 sprintf(buf
, "%ls", i1
);
135 ut_asserteq_str("i1?l", buf
);
137 memset(buf
, 0xff, sizeof(buf
));
138 sprintf(buf
, "%ls", i2
);
139 ut_asserteq_str("i2?l", buf
);
141 memset(buf
, 0xff, sizeof(buf
));
142 sprintf(buf
, "%ls", i3
);
143 ut_asserteq_str("i3?", buf
);
145 memset(buf
, 0xff, sizeof(buf
));
146 ret
= snprintf(buf
, 4, "%ls", c1
);
148 ut_asserteq_str("U-B", buf
);
150 memset(buf
, 0xff, sizeof(buf
));
151 ret
= snprintf(buf
, 6, "%ls", c2
);
152 ut_asserteq_str("kafb", buf
);
155 memset(buf
, 0xff, sizeof(buf
));
156 ret
= snprintf(buf
, 7, "%ls", c2
);
157 ut_asserteq_str("kafb\xC3\xA1", buf
);
160 memset(buf
, 0xff, sizeof(buf
));
161 ret
= snprintf(buf
, 8, "%ls", c3
);
162 ut_asserteq_str("\xE6\xBD\x9C\xE6\xB0\xB4", buf
);
165 memset(buf
, 0xff, sizeof(buf
));
166 ret
= snprintf(buf
, 11, "%ls", c4
);
167 ut_asserteq_str("\xF0\x90\x92\x8D\xF0\x90\x92\x96", buf
);
168 ut_asserteq(12, ret
);
170 memset(buf
, 0xff, sizeof(buf
));
171 ret
= snprintf(buf
, 4, "%ls", c4
);
172 ut_asserteq_str("", buf
);
173 ut_asserteq(12, ret
);
177 UNICODE_TEST(unicode_test_string16
);
180 static int unicode_test_utf8_get(struct unit_test_state
*uts
)
186 /* Check characters less than 0x800 */
188 for (i
= 0; i
< 8; ++i
) {
189 code
= utf8_get((const char **)&s
);
190 /* c2 is the utf-8 encoding of d2 */
191 ut_asserteq(c2
[i
], code
);
195 ut_asserteq_ptr(s
, d2
+ 9);
197 /* Check characters less than 0x10000 */
199 for (i
= 0; i
< 4; ++i
) {
200 code
= utf8_get((const char **)&s
);
201 /* c3 is the utf-8 encoding of d3 */
202 ut_asserteq(c3
[i
], code
);
206 ut_asserteq_ptr(s
, d3
+ 9);
208 /* Check character greater 0xffff */
210 code
= utf8_get((const char **)&s
);
211 ut_asserteq(0x0001048d, code
);
212 ut_asserteq_ptr(s
, d4
+ 4);
214 /* Check illegal character */
216 code
= utf8_get((const char **)&s
);
217 ut_asserteq(-1, code
);
218 ut_asserteq_ptr(j4
+ 1, s
);
222 UNICODE_TEST(unicode_test_utf8_get
);
224 static int unicode_test_utf8_put(struct unit_test_state
*uts
)
226 char buffer
[8] = { 0, };
229 /* Commercial at, translates to one character */
231 ut_assert(!utf8_put('@', &pos
));
232 ut_asserteq(1, pos
- buffer
);
233 ut_asserteq('@', buffer
[0]);
234 ut_assert(!buffer
[1]);
236 /* Latin letter G with acute, translates to two charactes */
238 ut_assert(!utf8_put(0x1f4, &pos
));
239 ut_asserteq(2, pos
- buffer
);
240 ut_asserteq_str("\xc7\xb4", buffer
);
242 /* Tagalog letter i, translates to three characters */
244 ut_assert(!utf8_put(0x1701, &pos
));
245 ut_asserteq(3, pos
- buffer
);
246 ut_asserteq_str("\xe1\x9c\x81", buffer
);
248 /* Hamster face, translates to four characters */
250 ut_assert(!utf8_put(0x1f439, &pos
));
251 ut_asserteq(4, pos
- buffer
);
252 ut_asserteq_str("\xf0\x9f\x90\xb9", buffer
);
256 ut_asserteq(-1, utf8_put(0xd888, &pos
));
260 UNICODE_TEST(unicode_test_utf8_put
);
262 static int unicode_test_utf8_utf16_strlen(struct unit_test_state
*uts
)
264 ut_asserteq(6, utf8_utf16_strlen(d1
));
265 ut_asserteq(8, utf8_utf16_strlen(d2
));
266 ut_asserteq(3, utf8_utf16_strlen(d3
));
267 ut_asserteq(6, utf8_utf16_strlen(d4
));
269 /* illegal utf-8 sequences */
270 ut_asserteq(4, utf8_utf16_strlen(j1
));
271 ut_asserteq(4, utf8_utf16_strlen(j2
));
272 ut_asserteq(3, utf8_utf16_strlen(j3
));
276 UNICODE_TEST(unicode_test_utf8_utf16_strlen
);
278 static int unicode_test_utf8_utf16_strnlen(struct unit_test_state
*uts
)
280 ut_asserteq(3, utf8_utf16_strnlen(d1
, 3));
281 ut_asserteq(6, utf8_utf16_strnlen(d1
, 13));
282 ut_asserteq(6, utf8_utf16_strnlen(d2
, 6));
283 ut_asserteq(2, utf8_utf16_strnlen(d3
, 2));
284 ut_asserteq(4, utf8_utf16_strnlen(d4
, 2));
285 ut_asserteq(6, utf8_utf16_strnlen(d4
, 3));
287 /* illegal utf-8 sequences */
288 ut_asserteq(4, utf8_utf16_strnlen(j1
, 16));
289 ut_asserteq(4, utf8_utf16_strnlen(j2
, 16));
290 ut_asserteq(3, utf8_utf16_strnlen(j3
, 16));
294 UNICODE_TEST(unicode_test_utf8_utf16_strnlen
);
297 * ut_u16_strcmp() - Compare to u16 strings.
301 * @count: number of u16 to compare
302 * Return: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
304 static int unicode_test_u16_strcmp(const u16
*a1
, const u16
*a2
, size_t count
)
306 for (; (*a1
|| *a2
) && count
; ++a1
, ++a2
, --count
) {
315 static int unicode_test_utf8_utf16_strcpy(struct unit_test_state
*uts
)
321 utf8_utf16_strcpy(&pos
, d1
);
322 ut_asserteq(6, pos
- buf
);
323 ut_assert(!unicode_test_u16_strcmp(buf
, c1
, SIZE_MAX
));
326 utf8_utf16_strcpy(&pos
, d2
);
327 ut_asserteq(8, pos
- buf
);
328 ut_assert(!unicode_test_u16_strcmp(buf
, c2
, SIZE_MAX
));
331 utf8_utf16_strcpy(&pos
, d3
);
332 ut_asserteq(3, pos
- buf
);
333 ut_assert(!unicode_test_u16_strcmp(buf
, c3
, SIZE_MAX
));
336 utf8_utf16_strcpy(&pos
, d4
);
337 ut_asserteq(6, pos
- buf
);
338 ut_assert(!unicode_test_u16_strcmp(buf
, c4
, SIZE_MAX
));
340 /* Illegal utf-8 strings */
342 utf8_utf16_strcpy(&pos
, j1
);
343 ut_asserteq(4, pos
- buf
);
344 ut_assert(!unicode_test_u16_strcmp(buf
, u
"j1?l", SIZE_MAX
));
347 utf8_utf16_strcpy(&pos
, j2
);
348 ut_asserteq(4, pos
- buf
);
349 ut_assert(!unicode_test_u16_strcmp(buf
, u
"j2?l", SIZE_MAX
));
352 utf8_utf16_strcpy(&pos
, j3
);
353 ut_asserteq(3, pos
- buf
);
354 ut_assert(!unicode_test_u16_strcmp(buf
, u
"j3?", SIZE_MAX
));
358 UNICODE_TEST(unicode_test_utf8_utf16_strcpy
);
360 static int unicode_test_utf8_utf16_strncpy(struct unit_test_state
*uts
)
366 memset(buf
, 0, sizeof(buf
));
367 utf8_utf16_strncpy(&pos
, d1
, 4);
368 ut_asserteq(4, pos
- buf
);
370 ut_assert(!unicode_test_u16_strcmp(buf
, c1
, 4));
373 memset(buf
, 0, sizeof(buf
));
374 utf8_utf16_strncpy(&pos
, d2
, 10);
375 ut_asserteq(8, pos
- buf
);
377 ut_assert(!unicode_test_u16_strcmp(buf
, c2
, SIZE_MAX
));
380 memset(buf
, 0, sizeof(buf
));
381 utf8_utf16_strncpy(&pos
, d3
, 2);
382 ut_asserteq(2, pos
- buf
);
384 ut_assert(!unicode_test_u16_strcmp(buf
, c3
, 2));
387 memset(buf
, 0, sizeof(buf
));
388 utf8_utf16_strncpy(&pos
, d4
, 2);
389 ut_asserteq(4, pos
- buf
);
391 ut_assert(!unicode_test_u16_strcmp(buf
, c4
, 4));
394 memset(buf
, 0, sizeof(buf
));
395 utf8_utf16_strncpy(&pos
, d4
, 10);
396 ut_asserteq(6, pos
- buf
);
398 ut_assert(!unicode_test_u16_strcmp(buf
, c4
, SIZE_MAX
));
402 UNICODE_TEST(unicode_test_utf8_utf16_strncpy
);
404 static int unicode_test_utf16_get(struct unit_test_state
*uts
)
410 /* Check characters less than 0x10000 */
412 for (i
= 0; i
< 9; ++i
) {
413 code
= utf16_get((const u16
**)&s
);
414 ut_asserteq(c2
[i
], code
);
418 ut_asserteq_ptr(c2
+ 8, s
);
420 /* Check character greater 0xffff */
422 code
= utf16_get((const u16
**)&s
);
423 ut_asserteq(0x0001048d, code
);
424 ut_asserteq_ptr(c4
+ 2, s
);
428 UNICODE_TEST(unicode_test_utf16_get
);
430 static int unicode_test_utf16_put(struct unit_test_state
*uts
)
432 u16 buffer
[4] = { 0, };
435 /* Commercial at, translates to one word */
437 ut_assert(!utf16_put('@', &pos
));
438 ut_asserteq(1, pos
- buffer
);
439 ut_asserteq((u16
)'@', buffer
[0]);
440 ut_assert(!buffer
[1]);
442 /* Hamster face, translates to two words */
444 ut_assert(!utf16_put(0x1f439, &pos
));
445 ut_asserteq(2, pos
- buffer
);
446 ut_asserteq((u16
)0xd83d, buffer
[0]);
447 ut_asserteq((u16
)0xdc39, buffer
[1]);
448 ut_assert(!buffer
[2]);
452 ut_asserteq(-1, utf16_put(0xd888, &pos
));
456 UNICODE_TEST(unicode_test_utf16_put
);
458 static int unicode_test_utf16_strnlen(struct unit_test_state
*uts
)
460 ut_asserteq(3, utf16_strnlen(c1
, 3));
461 ut_asserteq(6, utf16_strnlen(c1
, 13));
462 ut_asserteq(6, utf16_strnlen(c2
, 6));
463 ut_asserteq(2, utf16_strnlen(c3
, 2));
464 ut_asserteq(2, utf16_strnlen(c4
, 2));
465 ut_asserteq(3, utf16_strnlen(c4
, 3));
467 /* illegal utf-16 word sequences */
468 ut_asserteq(4, utf16_strnlen(i1
, 16));
469 ut_asserteq(4, utf16_strnlen(i2
, 16));
470 ut_asserteq(3, utf16_strnlen(i3
, 16));
474 UNICODE_TEST(unicode_test_utf16_strnlen
);
476 static int unicode_test_utf16_utf8_strlen(struct unit_test_state
*uts
)
478 ut_asserteq(6, utf16_utf8_strlen(c1
));
479 ut_asserteq(9, utf16_utf8_strlen(c2
));
480 ut_asserteq(9, utf16_utf8_strlen(c3
));
481 ut_asserteq(12, utf16_utf8_strlen(c4
));
483 /* illegal utf-16 word sequences */
484 ut_asserteq(4, utf16_utf8_strlen(i1
));
485 ut_asserteq(4, utf16_utf8_strlen(i2
));
486 ut_asserteq(3, utf16_utf8_strlen(i3
));
490 UNICODE_TEST(unicode_test_utf16_utf8_strlen
);
492 static int unicode_test_utf16_utf8_strnlen(struct unit_test_state
*uts
)
494 ut_asserteq(3, utf16_utf8_strnlen(c1
, 3));
495 ut_asserteq(6, utf16_utf8_strnlen(c1
, 13));
496 ut_asserteq(7, utf16_utf8_strnlen(c2
, 6));
497 ut_asserteq(6, utf16_utf8_strnlen(c3
, 2));
498 ut_asserteq(8, utf16_utf8_strnlen(c4
, 2));
499 ut_asserteq(12, utf16_utf8_strnlen(c4
, 3));
502 UNICODE_TEST(unicode_test_utf16_utf8_strnlen
);
504 static int unicode_test_utf16_utf8_strcpy(struct unit_test_state
*uts
)
510 utf16_utf8_strcpy(&pos
, c1
);
511 ut_asserteq(6, pos
- buf
);
512 ut_asserteq_str(d1
, buf
);
515 utf16_utf8_strcpy(&pos
, c2
);
516 ut_asserteq(9, pos
- buf
);
517 ut_asserteq_str(d2
, buf
);
520 utf16_utf8_strcpy(&pos
, c3
);
521 ut_asserteq(9, pos
- buf
);
522 ut_asserteq_str(d3
, buf
);
525 utf16_utf8_strcpy(&pos
, c4
);
526 ut_asserteq(12, pos
- buf
);
527 ut_asserteq_str(d4
, buf
);
529 /* Illegal utf-16 strings */
531 utf16_utf8_strcpy(&pos
, i1
);
532 ut_asserteq(4, pos
- buf
);
533 ut_asserteq_str("i1?l", buf
);
536 utf16_utf8_strcpy(&pos
, i2
);
537 ut_asserteq(4, pos
- buf
);
538 ut_asserteq_str("i2?l", buf
);
541 utf16_utf8_strcpy(&pos
, i3
);
542 ut_asserteq(3, pos
- buf
);
543 ut_asserteq_str("i3?", buf
);
547 UNICODE_TEST(unicode_test_utf16_utf8_strcpy
);
549 static int unicode_test_utf16_utf8_strncpy(struct unit_test_state
*uts
)
555 memset(buf
, 0, sizeof(buf
));
556 utf16_utf8_strncpy(&pos
, c1
, 4);
557 ut_asserteq(4, pos
- buf
);
559 ut_assert(!strncmp(buf
, d1
, 4));
562 memset(buf
, 0, sizeof(buf
));
563 utf16_utf8_strncpy(&pos
, c2
, 10);
564 ut_asserteq(9, pos
- buf
);
566 ut_assert(!strncmp(buf
, d2
, SIZE_MAX
));
569 memset(buf
, 0, sizeof(buf
));
570 utf16_utf8_strncpy(&pos
, c3
, 2);
571 ut_asserteq(6, pos
- buf
);
573 ut_assert(!strncmp(buf
, d3
, 6));
576 memset(buf
, 0, sizeof(buf
));
577 utf16_utf8_strncpy(&pos
, c4
, 2);
578 ut_asserteq(8, pos
- buf
);
580 ut_assert(!strncmp(buf
, d4
, 8));
583 memset(buf
, 0, sizeof(buf
));
584 utf16_utf8_strncpy(&pos
, c4
, 10);
585 ut_asserteq(12, pos
- buf
);
587 ut_assert(!strncmp(buf
, d4
, SIZE_MAX
));
591 UNICODE_TEST(unicode_test_utf16_utf8_strncpy
);
593 static int unicode_test_utf_to_lower(struct unit_test_state
*uts
)
595 ut_asserteq('@', utf_to_lower('@'));
596 ut_asserteq('a', utf_to_lower('A'));
597 ut_asserteq('z', utf_to_lower('Z'));
598 ut_asserteq('[', utf_to_lower('['));
599 ut_asserteq('m', utf_to_lower('m'));
600 /* Latin letter O with diaresis (umlaut) */
601 ut_asserteq(0x00f6, utf_to_lower(0x00d6));
602 #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
603 /* Cyrillic letter I*/
604 ut_asserteq(0x0438, utf_to_lower(0x0418));
608 UNICODE_TEST(unicode_test_utf_to_lower
);
610 static int unicode_test_utf_to_upper(struct unit_test_state
*uts
)
612 ut_asserteq('`', utf_to_upper('`'));
613 ut_asserteq('A', utf_to_upper('a'));
614 ut_asserteq('Z', utf_to_upper('z'));
615 ut_asserteq('{', utf_to_upper('{'));
616 ut_asserteq('M', utf_to_upper('M'));
617 /* Latin letter O with diaresis (umlaut) */
618 ut_asserteq(0x00d6, utf_to_upper(0x00f6));
619 #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
620 /* Cyrillic letter I */
621 ut_asserteq(0x0418, utf_to_upper(0x0438));
625 UNICODE_TEST(unicode_test_utf_to_upper
);
627 static int unicode_test_u16_strcasecmp(struct unit_test_state
*uts
)
629 ut_assert(u16_strcasecmp(u
"abcd", u
"abcd") == 0);
630 ut_assert(u16_strcasecmp(u
"aBcd", u
"abcd") == 0);
631 ut_assert(u16_strcasecmp(u
"abcd", u
"abCd") == 0);
632 ut_assert(u16_strcasecmp(u
"abcdE", u
"abcd") > 0);
633 ut_assert(u16_strcasecmp(u
"abcd", u
"abcdE") < 0);
634 ut_assert(u16_strcasecmp(u
"abcE", u
"abcd") > 0);
635 ut_assert(u16_strcasecmp(u
"abcd", u
"abcE") < 0);
636 ut_assert(u16_strcasecmp(u
"abcd", u
"abcd") == 0);
637 ut_assert(u16_strcasecmp(u
"abcd", u
"abcd") == 0);
638 if (CONFIG_IS_ENABLED(EFI_UNICODE_CAPITALIZATION
)) {
639 /* Cyrillic letters */
640 ut_assert(u16_strcasecmp(u
"\x043a\x043d\x0438\x0433\x0430",
641 u
"\x041a\x041d\x0418\x0413\x0410") == 0);
642 ut_assert(u16_strcasecmp(u
"\x043a\x043d\x0438\x0433\x0430",
643 u
"\x041a\x041d\x0418\x0413\x0411") < 0);
644 ut_assert(u16_strcasecmp(u
"\x043a\x043d\x0438\x0433\x0431",
645 u
"\x041a\x041d\x0418\x0413\x0410") > 0);
650 UNICODE_TEST(unicode_test_u16_strcasecmp
);
652 static int unicode_test_u16_strncmp(struct unit_test_state
*uts
)
654 ut_assert(u16_strncmp(u
"abc", u
"abc", 3) == 0);
655 ut_assert(u16_strncmp(u
"abcdef", u
"abcghi", 3) == 0);
656 ut_assert(u16_strncmp(u
"abcdef", u
"abcghi", 6) < 0);
657 ut_assert(u16_strncmp(u
"abcghi", u
"abcdef", 6) > 0);
658 ut_assert(u16_strcmp(u
"abc", u
"abc") == 0);
659 ut_assert(u16_strcmp(u
"abcdef", u
"deghi") < 0);
660 ut_assert(u16_strcmp(u
"deghi", u
"abcdef") > 0);
663 UNICODE_TEST(unicode_test_u16_strncmp
);
665 static int unicode_test_u16_strsize(struct unit_test_state
*uts
)
667 ut_asserteq_64(u16_strsize(c1
), 14);
668 ut_asserteq_64(u16_strsize(c2
), 18);
669 ut_asserteq_64(u16_strsize(c3
), 8);
670 ut_asserteq_64(u16_strsize(c4
), 14);
673 UNICODE_TEST(unicode_test_u16_strsize
);
675 static int unicode_test_utf_to_cp(struct unit_test_state
*uts
)
681 ret
= utf_to_cp(&c
, codepage_437
);
683 ut_asserteq('\n', c
);
686 ret
= utf_to_cp(&c
, codepage_437
);
690 c
= 0x03c4; /* Greek small letter tau */
691 ret
= utf_to_cp(&c
, codepage_437
);
693 ut_asserteq(0xe7, c
);
695 c
= 0x03a4; /* Greek capital letter tau */
696 ret
= utf_to_cp(&c
, codepage_437
);
697 ut_asserteq(-ENOENT
, ret
);
702 UNICODE_TEST(unicode_test_utf_to_cp
);
704 static void utf8_to_cp437_stream_helper(const char *in
, char *out
)
711 ret
= utf8_to_cp437_stream(*in
, buffer
);
718 static int unicode_test_utf8_to_cp437_stream(struct unit_test_state
*uts
)
722 utf8_to_cp437_stream_helper(d1
, buf
);
723 ut_asserteq_str("U-Boot", buf
);
724 utf8_to_cp437_stream_helper(d2
, buf
);
725 ut_asserteq_str("kafb\xa0tur", buf
);
726 utf8_to_cp437_stream_helper(d5
, buf
);
727 ut_asserteq_str("? is not B", buf
);
728 utf8_to_cp437_stream_helper(j2
, buf
);
729 ut_asserteq_str("j2l", buf
);
733 UNICODE_TEST(unicode_test_utf8_to_cp437_stream
);
735 static void utf8_to_utf32_stream_helper(const char *in
, s32
*out
)
742 ret
= utf8_to_utf32_stream(*in
, buffer
);
749 static int unicode_test_utf8_to_utf32_stream(struct unit_test_state
*uts
)
753 const u32 u1
[] = {0x55, 0x2D, 0x42, 0x6F, 0x6F, 0x74, 0x0000};
754 const u32 u2
[] = {0x6B, 0x61, 0x66, 0x62, 0xE1, 0x74, 0x75, 0x72, 0x00};
755 const u32 u3
[] = {0x6f5c, 0x6c34, 0x8266};
756 const u32 u4
[] = {0x6A, 0x32, 0x6C, 0x00};
757 const u32 u5
[] = {0x0392, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F, 0x74,
760 memset(buf
, 0, sizeof(buf
));
761 utf8_to_utf32_stream_helper(d1
, buf
);
762 ut_asserteq_mem(u1
, buf
, sizeof(u1
));
764 memset(buf
, 0, sizeof(buf
));
765 utf8_to_utf32_stream_helper(d2
, buf
);
766 ut_asserteq_mem(u2
, buf
, sizeof(u2
));
768 memset(buf
, 0, sizeof(buf
));
769 utf8_to_utf32_stream_helper(d3
, buf
);
770 ut_asserteq_mem(u3
, buf
, sizeof(u3
));
772 memset(buf
, 0, sizeof(buf
));
773 utf8_to_utf32_stream_helper(d5
, buf
);
774 ut_asserteq_mem(u5
, buf
, sizeof(u5
));
776 memset(buf
, 0, sizeof(buf
));
777 utf8_to_utf32_stream_helper(j2
, buf
);
778 ut_asserteq_mem(u4
, buf
, sizeof(u4
));
782 UNICODE_TEST(unicode_test_utf8_to_utf32_stream
);
784 #ifdef CONFIG_EFI_LOADER
785 static int unicode_test_efi_create_indexed_name(struct unit_test_state
*uts
)
788 u16
const expected
[] = u
"Capsule0AF9";
791 memset(buf
, 0xeb, sizeof(buf
));
792 pos
= efi_create_indexed_name(buf
, sizeof(buf
), "Capsule", 0x0af9);
794 ut_asserteq_mem(expected
, buf
, sizeof(expected
));
795 ut_asserteq(pos
- buf
, u16_strnlen(buf
, SIZE_MAX
));
799 UNICODE_TEST(unicode_test_efi_create_indexed_name
);
802 static int unicode_test_u16_strlcat(struct unit_test_state
*uts
)
805 u16 dest
[] = {0x3053, 0x3093, 0x306b, 0x3061, 0x306f, 0};
806 u16 src
[] = {0x03B1, 0x2172, 0x6F5C, 0x8247, 0};
807 u16 concat_str
[] = {0x3053, 0x3093, 0x306b, 0x3061, 0x306f,
808 0x03B1, 0x2172, 0x6F5C, 0x8247, 0};
809 u16 null_src
= u
'\0';
810 size_t ret
, expected
;
813 /* dest and src are empty string */
814 memset(buf
, 0, sizeof(buf
));
815 ret
= u16_strlcat(buf
, &null_src
, ARRAY_SIZE(buf
));
818 /* dest is empty string */
819 memset(buf
, 0, sizeof(buf
));
820 ret
= u16_strlcat(buf
, src
, ARRAY_SIZE(buf
));
822 ut_assert(!unicode_test_u16_strcmp(buf
, src
, 40));
824 /* src is empty string */
825 memset(buf
, 0xCD, (sizeof(buf
) - sizeof(u16
)));
827 memcpy(buf
, dest
, sizeof(dest
));
828 ret
= u16_strlcat(buf
, &null_src
, ARRAY_SIZE(buf
));
830 ut_assert(!unicode_test_u16_strcmp(buf
, dest
, 40));
832 for (i
= 0; i
<= 40; i
++) {
833 memset(buf
, 0xCD, (sizeof(buf
) - sizeof(u16
)));
835 memcpy(buf
, dest
, sizeof(dest
));
836 expected
= min(5, i
) + 4;
837 ret
= u16_strlcat(buf
, src
, i
);
838 ut_asserteq(expected
, ret
);
840 ut_assert(!unicode_test_u16_strcmp(buf
, dest
, 40));
842 ut_assert(!unicode_test_u16_strcmp(buf
, concat_str
, i
- 1));
844 ut_assert(!unicode_test_u16_strcmp(buf
, concat_str
, 40));
850 UNICODE_TEST(unicode_test_u16_strlcat
);
852 int do_ut_unicode(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[])
854 struct unit_test
*tests
= UNIT_TEST_SUITE_START(unicode_test
);
855 const int n_ents
= UNIT_TEST_SUITE_COUNT(unicode_test
);
857 return cmd_ut_category("Unicode", "unicode_test_",
858 tests
, n_ents
, argc
, argv
);