1 // SPDX-License-Identifier: GPL-2.0+
3 * Unit tests for Unicode functions
5 * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
13 #include <test/test.h>
14 #include <test/suites.h>
17 /* Linker list entry for a Unicode test */
18 #define UNICODE_TEST(_name) UNIT_TEST(_name, 0, unicode_test)
20 /* Constants c1-c4 and d1-d4 encode the same letters */
22 /* Six characters translating to one utf-8 byte each. */
23 static const u16 c1
[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
24 /* One character translating to two utf-8 bytes */
25 static const u16 c2
[] = {0x6b, 0x61, 0x66, 0x62, 0xe1, 0x74, 0x75, 0x72, 0x00};
26 /* Three characters translating to three utf-8 bytes each */
27 static const u16 c3
[] = {0x6f5c, 0x6c34, 0x8266, 0x00};
28 /* Three letters translating to four utf-8 bytes each */
29 static const u16 c4
[] = {0xd801, 0xdc8d, 0xd801, 0xdc96, 0xd801, 0xdc87,
32 /* Illegal utf-16 strings */
33 static const u16 i1
[] = {0x69, 0x31, 0xdc87, 0x6c, 0x00};
34 static const u16 i2
[] = {0x69, 0x32, 0xd801, 0xd801, 0x6c, 0x00};
35 static const u16 i3
[] = {0x69, 0x33, 0xd801, 0x00};
37 /* Six characters translating to one utf-16 word each. */
38 static const char d1
[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
39 /* Eight characters translating to one utf-16 word each */
40 static const char d2
[] = {0x6b, 0x61, 0x66, 0x62, 0xc3, 0xa1, 0x74, 0x75,
42 /* Three characters translating to one utf-16 word each */
43 static const char d3
[] = {0xe6, 0xbd, 0x9c, 0xe6, 0xb0, 0xb4, 0xe8, 0x89,
45 /* Three letters translating to two utf-16 word each */
46 static const char d4
[] = {0xf0, 0x90, 0x92, 0x8d, 0xf0, 0x90, 0x92, 0x96,
47 0xf0, 0x90, 0x92, 0x87, 0x00};
49 /* Illegal utf-8 strings */
50 static const char j1
[] = {0x6a, 0x31, 0xa1, 0x6c, 0x00};
51 static const char j2
[] = {0x6a, 0x32, 0xc3, 0xc3, 0x6c, 0x00};
52 static const char j3
[] = {0x6a, 0x33, 0xf0, 0x90, 0xf0, 0x00};
54 static int unicode_test_u16_strlen(struct unit_test_state
*uts
)
56 ut_asserteq(6, u16_strlen(c1
));
57 ut_asserteq(8, u16_strlen(c2
));
58 ut_asserteq(3, u16_strlen(c3
));
59 ut_asserteq(6, u16_strlen(c4
));
62 UNICODE_TEST(unicode_test_u16_strlen
);
64 static int unicode_test_u16_strdup(struct unit_test_state
*uts
)
66 u16
*copy
= u16_strdup(c4
);
68 ut_assert(copy
!= c4
);
69 ut_assert(!memcmp(copy
, c4
, sizeof(c4
)));
73 UNICODE_TEST(unicode_test_u16_strdup
);
75 static int unicode_test_u16_strcpy(struct unit_test_state
*uts
)
80 r
= u16_strcpy(copy
, c1
);
82 ut_assert(!memcmp(copy
, c1
, sizeof(c1
)));
85 UNICODE_TEST(unicode_test_u16_strcpy
);
87 /* U-Boot uses UTF-16 strings in the EFI context only. */
88 #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
89 static int unicode_test_string16(struct unit_test_state
*uts
)
93 /* Test length and precision */
94 memset(buf
, 0xff, sizeof(buf
));
95 sprintf(buf
, "%8.6ls", c2
);
96 ut_asserteq(' ', buf
[1]);
97 ut_assert(!strncmp(&buf
[2], d2
, 7));
100 memset(buf
, 0xff, sizeof(buf
));
101 sprintf(buf
, "%8.6ls", c4
);
102 ut_asserteq(' ', buf
[4]);
103 ut_assert(!strncmp(&buf
[5], d4
, 12));
106 memset(buf
, 0xff, sizeof(buf
));
107 sprintf(buf
, "%-8.2ls", c4
);
108 ut_asserteq(' ', buf
[8]);
109 ut_assert(!strncmp(buf
, d4
, 8));
112 /* Test handling of illegal utf-16 sequences */
113 memset(buf
, 0xff, sizeof(buf
));
114 sprintf(buf
, "%ls", i1
);
115 ut_asserteq_str("i1?l", buf
);
117 memset(buf
, 0xff, sizeof(buf
));
118 sprintf(buf
, "%ls", i2
);
119 ut_asserteq_str("i2?l", buf
);
121 memset(buf
, 0xff, sizeof(buf
));
122 sprintf(buf
, "%ls", i3
);
123 ut_asserteq_str("i3?", buf
);
127 UNICODE_TEST(unicode_test_string16
);
130 static int unicode_test_utf8_get(struct unit_test_state
*uts
)
136 /* Check characters less than 0x800 */
138 for (i
= 0; i
< 8; ++i
) {
139 code
= utf8_get((const char **)&s
);
140 /* c2 is the utf-8 encoding of d2 */
141 ut_asserteq(c2
[i
], code
);
145 ut_asserteq_ptr(s
, d2
+ 9)
147 /* Check characters less than 0x10000 */
149 for (i
= 0; i
< 4; ++i
) {
150 code
= utf8_get((const char **)&s
);
151 /* c3 is the utf-8 encoding of d3 */
152 ut_asserteq(c3
[i
], code
);
156 ut_asserteq_ptr(s
, d3
+ 9)
158 /* Check character greater 0xffff */
160 code
= utf8_get((const char **)&s
);
161 ut_asserteq(0x0001048d, code
);
162 ut_asserteq_ptr(s
, d4
+ 4);
166 UNICODE_TEST(unicode_test_utf8_get
);
168 static int unicode_test_utf8_put(struct unit_test_state
*uts
)
170 char buffer
[8] = { 0, };
173 /* Commercial at, translates to one character */
175 ut_assert(!utf8_put('@', &pos
))
176 ut_asserteq(1, pos
- buffer
);
177 ut_asserteq('@', buffer
[0]);
178 ut_assert(!buffer
[1]);
180 /* Latin letter G with acute, translates to two charactes */
182 ut_assert(!utf8_put(0x1f4, &pos
));
183 ut_asserteq(2, pos
- buffer
);
184 ut_asserteq_str("\xc7\xb4", buffer
);
186 /* Tagalog letter i, translates to three characters */
188 ut_assert(!utf8_put(0x1701, &pos
));
189 ut_asserteq(3, pos
- buffer
);
190 ut_asserteq_str("\xe1\x9c\x81", buffer
);
192 /* Hamster face, translates to four characters */
194 ut_assert(!utf8_put(0x1f439, &pos
));
195 ut_asserteq(4, pos
- buffer
);
196 ut_asserteq_str("\xf0\x9f\x90\xb9", buffer
);
200 ut_asserteq(-1, utf8_put(0xd888, &pos
));
204 UNICODE_TEST(unicode_test_utf8_put
);
206 static int unicode_test_utf8_utf16_strlen(struct unit_test_state
*uts
)
208 ut_asserteq(6, utf8_utf16_strlen(d1
));
209 ut_asserteq(8, utf8_utf16_strlen(d2
));
210 ut_asserteq(3, utf8_utf16_strlen(d3
));
211 ut_asserteq(6, utf8_utf16_strlen(d4
));
213 /* illegal utf-8 sequences */
214 ut_asserteq(4, utf8_utf16_strlen(j1
));
215 ut_asserteq(4, utf8_utf16_strlen(j2
));
216 ut_asserteq(3, utf8_utf16_strlen(j3
));
220 UNICODE_TEST(unicode_test_utf8_utf16_strlen
);
222 static int unicode_test_utf8_utf16_strnlen(struct unit_test_state
*uts
)
224 ut_asserteq(3, utf8_utf16_strnlen(d1
, 3));
225 ut_asserteq(6, utf8_utf16_strnlen(d1
, 13));
226 ut_asserteq(6, utf8_utf16_strnlen(d2
, 6));
227 ut_asserteq(2, utf8_utf16_strnlen(d3
, 2));
228 ut_asserteq(4, utf8_utf16_strnlen(d4
, 2));
229 ut_asserteq(6, utf8_utf16_strnlen(d4
, 3));
231 /* illegal utf-8 sequences */
232 ut_asserteq(4, utf8_utf16_strnlen(j1
, 16));
233 ut_asserteq(4, utf8_utf16_strnlen(j2
, 16));
234 ut_asserteq(3, utf8_utf16_strnlen(j3
, 16));
238 UNICODE_TEST(unicode_test_utf8_utf16_strnlen
);
241 * ut_u16_strcmp() - Compare to u16 strings.
245 * @count: number of u16 to compare
246 * Return: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
248 static int unicode_test_u16_strcmp(const u16
*a1
, const u16
*a2
, size_t count
)
250 for (; (*a1
|| *a2
) && count
; ++a1
, ++a2
, --count
) {
259 static int unicode_test_utf8_utf16_strcpy(struct unit_test_state
*uts
)
265 utf8_utf16_strcpy(&pos
, d1
);
266 ut_asserteq(6, pos
- buf
);
267 ut_assert(!unicode_test_u16_strcmp(buf
, c1
, SIZE_MAX
));
270 utf8_utf16_strcpy(&pos
, d2
);
271 ut_asserteq(8, pos
- buf
);
272 ut_assert(!unicode_test_u16_strcmp(buf
, c2
, SIZE_MAX
));
275 utf8_utf16_strcpy(&pos
, d3
);
276 ut_asserteq(3, pos
- buf
);
277 ut_assert(!unicode_test_u16_strcmp(buf
, c3
, SIZE_MAX
));
280 utf8_utf16_strcpy(&pos
, d4
);
281 ut_asserteq(6, pos
- buf
);
282 ut_assert(!unicode_test_u16_strcmp(buf
, c4
, SIZE_MAX
));
284 /* Illegal utf-8 strings */
286 utf8_utf16_strcpy(&pos
, j1
);
287 ut_asserteq(4, pos
- buf
);
288 ut_assert(!unicode_test_u16_strcmp(buf
, L
"j1?l", SIZE_MAX
));
291 utf8_utf16_strcpy(&pos
, j2
);
292 ut_asserteq(4, pos
- buf
);
293 ut_assert(!unicode_test_u16_strcmp(buf
, L
"j2?l", SIZE_MAX
));
296 utf8_utf16_strcpy(&pos
, j3
);
297 ut_asserteq(3, pos
- buf
);
298 ut_assert(!unicode_test_u16_strcmp(buf
, L
"j3?", SIZE_MAX
));
302 UNICODE_TEST(unicode_test_utf8_utf16_strcpy
);
304 static int unicode_test_utf8_utf16_strncpy(struct unit_test_state
*uts
)
310 memset(buf
, 0, sizeof(buf
));
311 utf8_utf16_strncpy(&pos
, d1
, 4);
312 ut_asserteq(4, pos
- buf
);
314 ut_assert(!unicode_test_u16_strcmp(buf
, c1
, 4));
317 memset(buf
, 0, sizeof(buf
));
318 utf8_utf16_strncpy(&pos
, d2
, 10);
319 ut_asserteq(8, pos
- buf
);
321 ut_assert(!unicode_test_u16_strcmp(buf
, c2
, SIZE_MAX
));
324 memset(buf
, 0, sizeof(buf
));
325 utf8_utf16_strncpy(&pos
, d3
, 2);
326 ut_asserteq(2, pos
- buf
);
328 ut_assert(!unicode_test_u16_strcmp(buf
, c3
, 2));
331 memset(buf
, 0, sizeof(buf
));
332 utf8_utf16_strncpy(&pos
, d4
, 2);
333 ut_asserteq(4, pos
- buf
);
335 ut_assert(!unicode_test_u16_strcmp(buf
, c4
, 4));
338 memset(buf
, 0, sizeof(buf
));
339 utf8_utf16_strncpy(&pos
, d4
, 10);
340 ut_asserteq(6, pos
- buf
);
342 ut_assert(!unicode_test_u16_strcmp(buf
, c4
, SIZE_MAX
));
346 UNICODE_TEST(unicode_test_utf8_utf16_strncpy
);
348 static int unicode_test_utf16_get(struct unit_test_state
*uts
)
354 /* Check characters less than 0x10000 */
356 for (i
= 0; i
< 9; ++i
) {
357 code
= utf16_get((const u16
**)&s
);
358 ut_asserteq(c2
[i
], code
);
362 ut_asserteq_ptr(c2
+ 8, s
);
364 /* Check character greater 0xffff */
366 code
= utf16_get((const u16
**)&s
);
367 ut_asserteq(0x0001048d, code
);
368 ut_asserteq_ptr(c4
+ 2, s
);
372 UNICODE_TEST(unicode_test_utf16_get
);
374 static int unicode_test_utf16_put(struct unit_test_state
*uts
)
376 u16 buffer
[4] = { 0, };
379 /* Commercial at, translates to one word */
381 ut_assert(!utf16_put('@', &pos
));
382 ut_asserteq(1, pos
- buffer
);
383 ut_asserteq((u16
)'@', buffer
[0]);
384 ut_assert(!buffer
[1]);
386 /* Hamster face, translates to two words */
388 ut_assert(!utf16_put(0x1f439, &pos
));
389 ut_asserteq(2, pos
- buffer
);
390 ut_asserteq((u16
)0xd83d, buffer
[0]);
391 ut_asserteq((u16
)0xdc39, buffer
[1]);
392 ut_assert(!buffer
[2]);
396 ut_asserteq(-1, utf16_put(0xd888, &pos
));
400 UNICODE_TEST(unicode_test_utf16_put
);
402 static int unicode_test_utf16_strnlen(struct unit_test_state
*uts
)
404 ut_asserteq(3, utf16_strnlen(c1
, 3));
405 ut_asserteq(6, utf16_strnlen(c1
, 13));
406 ut_asserteq(6, utf16_strnlen(c2
, 6));
407 ut_asserteq(2, utf16_strnlen(c3
, 2));
408 ut_asserteq(2, utf16_strnlen(c4
, 2));
409 ut_asserteq(3, utf16_strnlen(c4
, 3));
411 /* illegal utf-16 word sequences */
412 ut_asserteq(4, utf16_strnlen(i1
, 16));
413 ut_asserteq(4, utf16_strnlen(i2
, 16));
414 ut_asserteq(3, utf16_strnlen(i3
, 16));
418 UNICODE_TEST(unicode_test_utf16_strnlen
);
420 static int unicode_test_utf16_utf8_strlen(struct unit_test_state
*uts
)
422 ut_asserteq(6, utf16_utf8_strlen(c1
));
423 ut_asserteq(9, utf16_utf8_strlen(c2
));
424 ut_asserteq(9, utf16_utf8_strlen(c3
));
425 ut_asserteq(12, utf16_utf8_strlen(c4
));
427 /* illegal utf-16 word sequences */
428 ut_asserteq(4, utf16_utf8_strlen(i1
));
429 ut_asserteq(4, utf16_utf8_strlen(i2
));
430 ut_asserteq(3, utf16_utf8_strlen(i3
));
434 UNICODE_TEST(unicode_test_utf16_utf8_strlen
);
436 static int unicode_test_utf16_utf8_strnlen(struct unit_test_state
*uts
)
438 ut_asserteq(3, utf16_utf8_strnlen(c1
, 3));
439 ut_asserteq(6, utf16_utf8_strnlen(c1
, 13));
440 ut_asserteq(7, utf16_utf8_strnlen(c2
, 6));
441 ut_asserteq(6, utf16_utf8_strnlen(c3
, 2));
442 ut_asserteq(8, utf16_utf8_strnlen(c4
, 2));
443 ut_asserteq(12, utf16_utf8_strnlen(c4
, 3));
446 UNICODE_TEST(unicode_test_utf16_utf8_strnlen
);
448 static int unicode_test_utf16_utf8_strcpy(struct unit_test_state
*uts
)
454 utf16_utf8_strcpy(&pos
, c1
);
455 ut_asserteq(6, pos
- buf
);
456 ut_asserteq_str(d1
, buf
);
459 utf16_utf8_strcpy(&pos
, c2
);
460 ut_asserteq(9, pos
- buf
);
461 ut_asserteq_str(d2
, buf
);
464 utf16_utf8_strcpy(&pos
, c3
);
465 ut_asserteq(9, pos
- buf
);
466 ut_asserteq_str(d3
, buf
);
469 utf16_utf8_strcpy(&pos
, c4
);
470 ut_asserteq(12, pos
- buf
);
471 ut_asserteq_str(d4
, buf
);
473 /* Illegal utf-16 strings */
475 utf16_utf8_strcpy(&pos
, i1
);
476 ut_asserteq(4, pos
- buf
);
477 ut_asserteq_str("i1?l", buf
);
480 utf16_utf8_strcpy(&pos
, i2
);
481 ut_asserteq(4, pos
- buf
);
482 ut_asserteq_str("i2?l", buf
);
485 utf16_utf8_strcpy(&pos
, i3
);
486 ut_asserteq(3, pos
- buf
);
487 ut_asserteq_str("i3?", buf
);
491 UNICODE_TEST(unicode_test_utf16_utf8_strcpy
);
493 static int unicode_test_utf16_utf8_strncpy(struct unit_test_state
*uts
)
499 memset(buf
, 0, sizeof(buf
));
500 utf16_utf8_strncpy(&pos
, c1
, 4);
501 ut_asserteq(4, pos
- buf
);
503 ut_assert(!strncmp(buf
, d1
, 4));
506 memset(buf
, 0, sizeof(buf
));
507 utf16_utf8_strncpy(&pos
, c2
, 10);
508 ut_asserteq(9, pos
- buf
);
510 ut_assert(!strncmp(buf
, d2
, SIZE_MAX
));
513 memset(buf
, 0, sizeof(buf
));
514 utf16_utf8_strncpy(&pos
, c3
, 2);
515 ut_asserteq(6, pos
- buf
);
517 ut_assert(!strncmp(buf
, d3
, 6));
520 memset(buf
, 0, sizeof(buf
));
521 utf16_utf8_strncpy(&pos
, c4
, 2);
522 ut_asserteq(8, pos
- buf
);
524 ut_assert(!strncmp(buf
, d4
, 8));
527 memset(buf
, 0, sizeof(buf
));
528 utf16_utf8_strncpy(&pos
, c4
, 10);
529 ut_asserteq(12, pos
- buf
);
531 ut_assert(!strncmp(buf
, d4
, SIZE_MAX
));
535 UNICODE_TEST(unicode_test_utf16_utf8_strncpy
);
537 static int unicode_test_utf_to_lower(struct unit_test_state
*uts
)
539 ut_asserteq('@', utf_to_lower('@'));
540 ut_asserteq('a', utf_to_lower('A'));
541 ut_asserteq('z', utf_to_lower('Z'));
542 ut_asserteq('[', utf_to_lower('['));
543 ut_asserteq('m', utf_to_lower('m'));
544 /* Latin letter O with diaresis (umlaut) */
545 ut_asserteq(0x00f6, utf_to_lower(0x00d6));
546 #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
547 /* Cyrillic letter I*/
548 ut_asserteq(0x0438, utf_to_lower(0x0418));
552 UNICODE_TEST(unicode_test_utf_to_lower
);
554 static int unicode_test_utf_to_upper(struct unit_test_state
*uts
)
556 ut_asserteq('`', utf_to_upper('`'));
557 ut_asserteq('A', utf_to_upper('a'));
558 ut_asserteq('Z', utf_to_upper('z'));
559 ut_asserteq('{', utf_to_upper('{'));
560 ut_asserteq('M', utf_to_upper('M'));
561 /* Latin letter O with diaresis (umlaut) */
562 ut_asserteq(0x00d6, utf_to_upper(0x00f6));
563 #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
564 /* Cyrillic letter I */
565 ut_asserteq(0x0418, utf_to_upper(0x0438));
569 UNICODE_TEST(unicode_test_utf_to_upper
);
571 static int unicode_test_u16_strncmp(struct unit_test_state
*uts
)
573 ut_assert(u16_strncmp(L
"abc", L
"abc", 3) == 0);
574 ut_assert(u16_strncmp(L
"abcdef", L
"abcghi", 3) == 0);
575 ut_assert(u16_strncmp(L
"abcdef", L
"abcghi", 6) < 0);
576 ut_assert(u16_strncmp(L
"abcghi", L
"abcdef", 6) > 0);
577 ut_assert(u16_strcmp(L
"abc", L
"abc") == 0);
578 ut_assert(u16_strcmp(L
"abcdef", L
"deghi") < 0);
579 ut_assert(u16_strcmp(L
"deghi", L
"abcdef") > 0);
582 UNICODE_TEST(unicode_test_u16_strncmp
);
584 int do_ut_unicode(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
586 struct unit_test
*tests
= ll_entry_start(struct unit_test
, unicode_test
);
587 const int n_ents
= ll_entry_count(struct unit_test
, unicode_test
);
589 return cmd_ut_category("Unicode", "unicode_test_",
590 tests
, n_ents
, argc
, argv
);