Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Unit tests for Unicode functions |
| 4 | * |
| 5 | * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de> |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <charset.h> |
| 10 | #include <command.h> |
Heinrich Schuchardt | af11423 | 2020-10-30 12:23:59 +0100 | [diff] [blame] | 11 | #include <efi_loader.h> |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 12 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <malloc.h> |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 15 | #include <test/test.h> |
| 16 | #include <test/suites.h> |
| 17 | #include <test/ut.h> |
| 18 | |
| 19 | /* Linker list entry for a Unicode test */ |
| 20 | #define UNICODE_TEST(_name) UNIT_TEST(_name, 0, unicode_test) |
| 21 | |
| 22 | /* Constants c1-c4 and d1-d4 encode the same letters */ |
| 23 | |
| 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, |
| 32 | 0x0000}; |
| 33 | |
| 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}; |
| 38 | |
| 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, |
| 43 | 0x72, 0x00}; |
| 44 | /* Three characters translating to one utf-16 word each */ |
| 45 | static const char d3[] = {0xe6, 0xbd, 0x9c, 0xe6, 0xb0, 0xb4, 0xe8, 0x89, |
| 46 | 0xa6, 0x00}; |
| 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}; |
Heinrich Schuchardt | e91789e | 2021-02-27 14:08:38 +0100 | [diff] [blame] | 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}; |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 53 | |
| 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}; |
Heinrich Schuchardt | ddbaff5 | 2021-02-27 14:08:37 +0100 | [diff] [blame] | 58 | static const char j4[] = {0xa1, 0x00}; |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 59 | |
Heinrich Schuchardt | 02b31dc | 2019-07-14 17:47:46 +0200 | [diff] [blame] | 60 | static int unicode_test_u16_strlen(struct unit_test_state *uts) |
| 61 | { |
| 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)); |
| 66 | return 0; |
| 67 | } |
| 68 | UNICODE_TEST(unicode_test_u16_strlen); |
| 69 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 70 | static int unicode_test_u16_strdup(struct unit_test_state *uts) |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 71 | { |
| 72 | u16 *copy = u16_strdup(c4); |
| 73 | |
| 74 | ut_assert(copy != c4); |
Simon Glass | f91f366 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 75 | ut_asserteq_mem(copy, c4, sizeof(c4)); |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 76 | free(copy); |
Simon Glass | f91f366 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 77 | |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 78 | return 0; |
| 79 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 80 | UNICODE_TEST(unicode_test_u16_strdup); |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 81 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 82 | static int unicode_test_u16_strcpy(struct unit_test_state *uts) |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 83 | { |
| 84 | u16 *r; |
| 85 | u16 copy[10]; |
| 86 | |
| 87 | r = u16_strcpy(copy, c1); |
| 88 | ut_assert(r == copy); |
Simon Glass | f91f366 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 89 | ut_asserteq_mem(copy, c1, sizeof(c1)); |
| 90 | |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 91 | return 0; |
| 92 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 93 | UNICODE_TEST(unicode_test_u16_strcpy); |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 94 | |
Heinrich Schuchardt | fbba2f6 | 2018-08-31 21:31:30 +0200 | [diff] [blame] | 95 | /* U-Boot uses UTF-16 strings in the EFI context only. */ |
| 96 | #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD) |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 97 | static int unicode_test_string16(struct unit_test_state *uts) |
Heinrich Schuchardt | fbba2f6 | 2018-08-31 21:31:30 +0200 | [diff] [blame] | 98 | { |
| 99 | char buf[20]; |
| 100 | |
| 101 | /* Test length and precision */ |
| 102 | memset(buf, 0xff, sizeof(buf)); |
| 103 | sprintf(buf, "%8.6ls", c2); |
| 104 | ut_asserteq(' ', buf[1]); |
| 105 | ut_assert(!strncmp(&buf[2], d2, 7)); |
| 106 | ut_assert(!buf[9]); |
| 107 | |
| 108 | memset(buf, 0xff, sizeof(buf)); |
| 109 | sprintf(buf, "%8.6ls", c4); |
| 110 | ut_asserteq(' ', buf[4]); |
| 111 | ut_assert(!strncmp(&buf[5], d4, 12)); |
| 112 | ut_assert(!buf[17]); |
| 113 | |
| 114 | memset(buf, 0xff, sizeof(buf)); |
| 115 | sprintf(buf, "%-8.2ls", c4); |
| 116 | ut_asserteq(' ', buf[8]); |
| 117 | ut_assert(!strncmp(buf, d4, 8)); |
| 118 | ut_assert(!buf[14]); |
| 119 | |
| 120 | /* Test handling of illegal utf-16 sequences */ |
| 121 | memset(buf, 0xff, sizeof(buf)); |
| 122 | sprintf(buf, "%ls", i1); |
| 123 | ut_asserteq_str("i1?l", buf); |
| 124 | |
| 125 | memset(buf, 0xff, sizeof(buf)); |
| 126 | sprintf(buf, "%ls", i2); |
| 127 | ut_asserteq_str("i2?l", buf); |
| 128 | |
| 129 | memset(buf, 0xff, sizeof(buf)); |
| 130 | sprintf(buf, "%ls", i3); |
| 131 | ut_asserteq_str("i3?", buf); |
| 132 | |
| 133 | return 0; |
| 134 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 135 | UNICODE_TEST(unicode_test_string16); |
Heinrich Schuchardt | fbba2f6 | 2018-08-31 21:31:30 +0200 | [diff] [blame] | 136 | #endif |
| 137 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 138 | static int unicode_test_utf8_get(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 139 | { |
| 140 | const char *s; |
| 141 | s32 code; |
| 142 | int i; |
| 143 | |
| 144 | /* Check characters less than 0x800 */ |
| 145 | s = d2; |
| 146 | for (i = 0; i < 8; ++i) { |
| 147 | code = utf8_get((const char **)&s); |
| 148 | /* c2 is the utf-8 encoding of d2 */ |
| 149 | ut_asserteq(c2[i], code); |
| 150 | if (!code) |
| 151 | break; |
| 152 | } |
| 153 | ut_asserteq_ptr(s, d2 + 9) |
| 154 | |
| 155 | /* Check characters less than 0x10000 */ |
| 156 | s = d3; |
| 157 | for (i = 0; i < 4; ++i) { |
| 158 | code = utf8_get((const char **)&s); |
| 159 | /* c3 is the utf-8 encoding of d3 */ |
| 160 | ut_asserteq(c3[i], code); |
| 161 | if (!code) |
| 162 | break; |
| 163 | } |
| 164 | ut_asserteq_ptr(s, d3 + 9) |
| 165 | |
| 166 | /* Check character greater 0xffff */ |
| 167 | s = d4; |
| 168 | code = utf8_get((const char **)&s); |
| 169 | ut_asserteq(0x0001048d, code); |
| 170 | ut_asserteq_ptr(s, d4 + 4); |
| 171 | |
Heinrich Schuchardt | ddbaff5 | 2021-02-27 14:08:37 +0100 | [diff] [blame] | 172 | /* Check illegal character */ |
| 173 | s = j4; |
| 174 | code = utf8_get((const char **)&s); |
| 175 | ut_asserteq(-1, code); |
| 176 | ut_asserteq_ptr(j4 + 1, s); |
| 177 | |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 178 | return 0; |
| 179 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 180 | UNICODE_TEST(unicode_test_utf8_get); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 181 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 182 | static int unicode_test_utf8_put(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 183 | { |
| 184 | char buffer[8] = { 0, }; |
| 185 | char *pos; |
| 186 | |
| 187 | /* Commercial at, translates to one character */ |
| 188 | pos = buffer; |
| 189 | ut_assert(!utf8_put('@', &pos)) |
| 190 | ut_asserteq(1, pos - buffer); |
| 191 | ut_asserteq('@', buffer[0]); |
| 192 | ut_assert(!buffer[1]); |
| 193 | |
| 194 | /* Latin letter G with acute, translates to two charactes */ |
| 195 | pos = buffer; |
| 196 | ut_assert(!utf8_put(0x1f4, &pos)); |
| 197 | ut_asserteq(2, pos - buffer); |
| 198 | ut_asserteq_str("\xc7\xb4", buffer); |
| 199 | |
| 200 | /* Tagalog letter i, translates to three characters */ |
| 201 | pos = buffer; |
| 202 | ut_assert(!utf8_put(0x1701, &pos)); |
| 203 | ut_asserteq(3, pos - buffer); |
| 204 | ut_asserteq_str("\xe1\x9c\x81", buffer); |
| 205 | |
| 206 | /* Hamster face, translates to four characters */ |
| 207 | pos = buffer; |
| 208 | ut_assert(!utf8_put(0x1f439, &pos)); |
| 209 | ut_asserteq(4, pos - buffer); |
| 210 | ut_asserteq_str("\xf0\x9f\x90\xb9", buffer); |
| 211 | |
| 212 | /* Illegal code */ |
| 213 | pos = buffer; |
| 214 | ut_asserteq(-1, utf8_put(0xd888, &pos)); |
| 215 | |
| 216 | return 0; |
| 217 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 218 | UNICODE_TEST(unicode_test_utf8_put); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 219 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 220 | static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 221 | { |
| 222 | ut_asserteq(6, utf8_utf16_strlen(d1)); |
| 223 | ut_asserteq(8, utf8_utf16_strlen(d2)); |
| 224 | ut_asserteq(3, utf8_utf16_strlen(d3)); |
| 225 | ut_asserteq(6, utf8_utf16_strlen(d4)); |
| 226 | |
| 227 | /* illegal utf-8 sequences */ |
| 228 | ut_asserteq(4, utf8_utf16_strlen(j1)); |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 229 | ut_asserteq(4, utf8_utf16_strlen(j2)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 230 | ut_asserteq(3, utf8_utf16_strlen(j3)); |
| 231 | |
| 232 | return 0; |
| 233 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 234 | UNICODE_TEST(unicode_test_utf8_utf16_strlen); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 235 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 236 | static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 237 | { |
| 238 | ut_asserteq(3, utf8_utf16_strnlen(d1, 3)); |
| 239 | ut_asserteq(6, utf8_utf16_strnlen(d1, 13)); |
| 240 | ut_asserteq(6, utf8_utf16_strnlen(d2, 6)); |
| 241 | ut_asserteq(2, utf8_utf16_strnlen(d3, 2)); |
| 242 | ut_asserteq(4, utf8_utf16_strnlen(d4, 2)); |
| 243 | ut_asserteq(6, utf8_utf16_strnlen(d4, 3)); |
| 244 | |
| 245 | /* illegal utf-8 sequences */ |
| 246 | ut_asserteq(4, utf8_utf16_strnlen(j1, 16)); |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 247 | ut_asserteq(4, utf8_utf16_strnlen(j2, 16)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 248 | ut_asserteq(3, utf8_utf16_strnlen(j3, 16)); |
| 249 | |
| 250 | return 0; |
| 251 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 252 | UNICODE_TEST(unicode_test_utf8_utf16_strnlen); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 253 | |
| 254 | /** |
| 255 | * ut_u16_strcmp() - Compare to u16 strings. |
| 256 | * |
| 257 | * @a1: first string |
| 258 | * @a2: second string |
| 259 | * @count: number of u16 to compare |
| 260 | * Return: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2 |
| 261 | */ |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 262 | static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 263 | { |
| 264 | for (; (*a1 || *a2) && count; ++a1, ++a2, --count) { |
| 265 | if (*a1 < *a2) |
| 266 | return -1; |
| 267 | if (*a1 > *a2) |
| 268 | return 1; |
| 269 | } |
| 270 | return 0; |
| 271 | } |
| 272 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 273 | static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 274 | { |
| 275 | u16 buf[16]; |
| 276 | u16 *pos; |
| 277 | |
| 278 | pos = buf; |
| 279 | utf8_utf16_strcpy(&pos, d1); |
| 280 | ut_asserteq(6, pos - buf); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 281 | ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 282 | |
| 283 | pos = buf; |
| 284 | utf8_utf16_strcpy(&pos, d2); |
| 285 | ut_asserteq(8, pos - buf); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 286 | ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 287 | |
| 288 | pos = buf; |
| 289 | utf8_utf16_strcpy(&pos, d3); |
| 290 | ut_asserteq(3, pos - buf); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 291 | ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 292 | |
| 293 | pos = buf; |
| 294 | utf8_utf16_strcpy(&pos, d4); |
| 295 | ut_asserteq(6, pos - buf); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 296 | ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 297 | |
| 298 | /* Illegal utf-8 strings */ |
| 299 | pos = buf; |
| 300 | utf8_utf16_strcpy(&pos, j1); |
| 301 | ut_asserteq(4, pos - buf); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 302 | ut_assert(!unicode_test_u16_strcmp(buf, L"j1?l", SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 303 | |
| 304 | pos = buf; |
| 305 | utf8_utf16_strcpy(&pos, j2); |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 306 | ut_asserteq(4, pos - buf); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 307 | ut_assert(!unicode_test_u16_strcmp(buf, L"j2?l", SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 308 | |
| 309 | pos = buf; |
| 310 | utf8_utf16_strcpy(&pos, j3); |
| 311 | ut_asserteq(3, pos - buf); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 312 | ut_assert(!unicode_test_u16_strcmp(buf, L"j3?", SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 313 | |
| 314 | return 0; |
| 315 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 316 | UNICODE_TEST(unicode_test_utf8_utf16_strcpy); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 317 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 318 | static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 319 | { |
| 320 | u16 buf[16]; |
| 321 | u16 *pos; |
| 322 | |
| 323 | pos = buf; |
| 324 | memset(buf, 0, sizeof(buf)); |
| 325 | utf8_utf16_strncpy(&pos, d1, 4); |
| 326 | ut_asserteq(4, pos - buf); |
| 327 | ut_assert(!buf[4]); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 328 | ut_assert(!unicode_test_u16_strcmp(buf, c1, 4)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 329 | |
| 330 | pos = buf; |
| 331 | memset(buf, 0, sizeof(buf)); |
| 332 | utf8_utf16_strncpy(&pos, d2, 10); |
| 333 | ut_asserteq(8, pos - buf); |
| 334 | ut_assert(buf[4]); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 335 | ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 336 | |
| 337 | pos = buf; |
| 338 | memset(buf, 0, sizeof(buf)); |
| 339 | utf8_utf16_strncpy(&pos, d3, 2); |
| 340 | ut_asserteq(2, pos - buf); |
| 341 | ut_assert(!buf[2]); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 342 | ut_assert(!unicode_test_u16_strcmp(buf, c3, 2)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 343 | |
| 344 | pos = buf; |
| 345 | memset(buf, 0, sizeof(buf)); |
| 346 | utf8_utf16_strncpy(&pos, d4, 2); |
| 347 | ut_asserteq(4, pos - buf); |
| 348 | ut_assert(!buf[4]); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 349 | ut_assert(!unicode_test_u16_strcmp(buf, c4, 4)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 350 | |
| 351 | pos = buf; |
| 352 | memset(buf, 0, sizeof(buf)); |
| 353 | utf8_utf16_strncpy(&pos, d4, 10); |
| 354 | ut_asserteq(6, pos - buf); |
| 355 | ut_assert(buf[5]); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 356 | ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 357 | |
| 358 | return 0; |
| 359 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 360 | UNICODE_TEST(unicode_test_utf8_utf16_strncpy); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 361 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 362 | static int unicode_test_utf16_get(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 363 | { |
| 364 | const u16 *s; |
| 365 | s32 code; |
| 366 | int i; |
| 367 | |
| 368 | /* Check characters less than 0x10000 */ |
| 369 | s = c2; |
| 370 | for (i = 0; i < 9; ++i) { |
| 371 | code = utf16_get((const u16 **)&s); |
| 372 | ut_asserteq(c2[i], code); |
| 373 | if (!code) |
| 374 | break; |
| 375 | } |
| 376 | ut_asserteq_ptr(c2 + 8, s); |
| 377 | |
| 378 | /* Check character greater 0xffff */ |
| 379 | s = c4; |
| 380 | code = utf16_get((const u16 **)&s); |
| 381 | ut_asserteq(0x0001048d, code); |
| 382 | ut_asserteq_ptr(c4 + 2, s); |
| 383 | |
| 384 | return 0; |
| 385 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 386 | UNICODE_TEST(unicode_test_utf16_get); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 387 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 388 | static int unicode_test_utf16_put(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 389 | { |
| 390 | u16 buffer[4] = { 0, }; |
| 391 | u16 *pos; |
| 392 | |
| 393 | /* Commercial at, translates to one word */ |
| 394 | pos = buffer; |
| 395 | ut_assert(!utf16_put('@', &pos)); |
| 396 | ut_asserteq(1, pos - buffer); |
| 397 | ut_asserteq((u16)'@', buffer[0]); |
| 398 | ut_assert(!buffer[1]); |
| 399 | |
| 400 | /* Hamster face, translates to two words */ |
| 401 | pos = buffer; |
| 402 | ut_assert(!utf16_put(0x1f439, &pos)); |
| 403 | ut_asserteq(2, pos - buffer); |
| 404 | ut_asserteq((u16)0xd83d, buffer[0]); |
| 405 | ut_asserteq((u16)0xdc39, buffer[1]); |
| 406 | ut_assert(!buffer[2]); |
| 407 | |
| 408 | /* Illegal code */ |
| 409 | pos = buffer; |
| 410 | ut_asserteq(-1, utf16_put(0xd888, &pos)); |
| 411 | |
| 412 | return 0; |
| 413 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 414 | UNICODE_TEST(unicode_test_utf16_put); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 415 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 416 | static int unicode_test_utf16_strnlen(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 417 | { |
| 418 | ut_asserteq(3, utf16_strnlen(c1, 3)); |
| 419 | ut_asserteq(6, utf16_strnlen(c1, 13)); |
| 420 | ut_asserteq(6, utf16_strnlen(c2, 6)); |
| 421 | ut_asserteq(2, utf16_strnlen(c3, 2)); |
| 422 | ut_asserteq(2, utf16_strnlen(c4, 2)); |
| 423 | ut_asserteq(3, utf16_strnlen(c4, 3)); |
| 424 | |
| 425 | /* illegal utf-16 word sequences */ |
| 426 | ut_asserteq(4, utf16_strnlen(i1, 16)); |
| 427 | ut_asserteq(4, utf16_strnlen(i2, 16)); |
| 428 | ut_asserteq(3, utf16_strnlen(i3, 16)); |
| 429 | |
| 430 | return 0; |
| 431 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 432 | UNICODE_TEST(unicode_test_utf16_strnlen); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 433 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 434 | static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 435 | { |
| 436 | ut_asserteq(6, utf16_utf8_strlen(c1)); |
| 437 | ut_asserteq(9, utf16_utf8_strlen(c2)); |
| 438 | ut_asserteq(9, utf16_utf8_strlen(c3)); |
| 439 | ut_asserteq(12, utf16_utf8_strlen(c4)); |
| 440 | |
| 441 | /* illegal utf-16 word sequences */ |
| 442 | ut_asserteq(4, utf16_utf8_strlen(i1)); |
| 443 | ut_asserteq(4, utf16_utf8_strlen(i2)); |
| 444 | ut_asserteq(3, utf16_utf8_strlen(i3)); |
| 445 | |
| 446 | return 0; |
| 447 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 448 | UNICODE_TEST(unicode_test_utf16_utf8_strlen); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 449 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 450 | static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 451 | { |
| 452 | ut_asserteq(3, utf16_utf8_strnlen(c1, 3)); |
| 453 | ut_asserteq(6, utf16_utf8_strnlen(c1, 13)); |
| 454 | ut_asserteq(7, utf16_utf8_strnlen(c2, 6)); |
| 455 | ut_asserteq(6, utf16_utf8_strnlen(c3, 2)); |
| 456 | ut_asserteq(8, utf16_utf8_strnlen(c4, 2)); |
| 457 | ut_asserteq(12, utf16_utf8_strnlen(c4, 3)); |
| 458 | return 0; |
| 459 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 460 | UNICODE_TEST(unicode_test_utf16_utf8_strnlen); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 461 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 462 | static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 463 | { |
| 464 | char buf[16]; |
| 465 | char *pos; |
| 466 | |
| 467 | pos = buf; |
| 468 | utf16_utf8_strcpy(&pos, c1); |
| 469 | ut_asserteq(6, pos - buf); |
| 470 | ut_asserteq_str(d1, buf); |
| 471 | |
| 472 | pos = buf; |
| 473 | utf16_utf8_strcpy(&pos, c2); |
| 474 | ut_asserteq(9, pos - buf); |
| 475 | ut_asserteq_str(d2, buf); |
| 476 | |
| 477 | pos = buf; |
| 478 | utf16_utf8_strcpy(&pos, c3); |
| 479 | ut_asserteq(9, pos - buf); |
| 480 | ut_asserteq_str(d3, buf); |
| 481 | |
| 482 | pos = buf; |
| 483 | utf16_utf8_strcpy(&pos, c4); |
| 484 | ut_asserteq(12, pos - buf); |
| 485 | ut_asserteq_str(d4, buf); |
| 486 | |
| 487 | /* Illegal utf-16 strings */ |
| 488 | pos = buf; |
| 489 | utf16_utf8_strcpy(&pos, i1); |
| 490 | ut_asserteq(4, pos - buf); |
| 491 | ut_asserteq_str("i1?l", buf); |
| 492 | |
| 493 | pos = buf; |
| 494 | utf16_utf8_strcpy(&pos, i2); |
| 495 | ut_asserteq(4, pos - buf); |
| 496 | ut_asserteq_str("i2?l", buf); |
| 497 | |
| 498 | pos = buf; |
| 499 | utf16_utf8_strcpy(&pos, i3); |
| 500 | ut_asserteq(3, pos - buf); |
| 501 | ut_asserteq_str("i3?", buf); |
| 502 | |
| 503 | return 0; |
| 504 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 505 | UNICODE_TEST(unicode_test_utf16_utf8_strcpy); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 506 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 507 | static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 508 | { |
| 509 | char buf[16]; |
| 510 | char *pos; |
| 511 | |
| 512 | pos = buf; |
| 513 | memset(buf, 0, sizeof(buf)); |
| 514 | utf16_utf8_strncpy(&pos, c1, 4); |
| 515 | ut_asserteq(4, pos - buf); |
| 516 | ut_assert(!buf[4]); |
| 517 | ut_assert(!strncmp(buf, d1, 4)); |
| 518 | |
| 519 | pos = buf; |
| 520 | memset(buf, 0, sizeof(buf)); |
| 521 | utf16_utf8_strncpy(&pos, c2, 10); |
| 522 | ut_asserteq(9, pos - buf); |
| 523 | ut_assert(buf[4]); |
| 524 | ut_assert(!strncmp(buf, d2, SIZE_MAX)); |
| 525 | |
| 526 | pos = buf; |
| 527 | memset(buf, 0, sizeof(buf)); |
| 528 | utf16_utf8_strncpy(&pos, c3, 2); |
| 529 | ut_asserteq(6, pos - buf); |
| 530 | ut_assert(!buf[6]); |
| 531 | ut_assert(!strncmp(buf, d3, 6)); |
| 532 | |
| 533 | pos = buf; |
| 534 | memset(buf, 0, sizeof(buf)); |
| 535 | utf16_utf8_strncpy(&pos, c4, 2); |
| 536 | ut_asserteq(8, pos - buf); |
| 537 | ut_assert(!buf[8]); |
| 538 | ut_assert(!strncmp(buf, d4, 8)); |
| 539 | |
| 540 | pos = buf; |
| 541 | memset(buf, 0, sizeof(buf)); |
| 542 | utf16_utf8_strncpy(&pos, c4, 10); |
| 543 | ut_asserteq(12, pos - buf); |
| 544 | ut_assert(buf[5]); |
| 545 | ut_assert(!strncmp(buf, d4, SIZE_MAX)); |
| 546 | |
| 547 | return 0; |
| 548 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 549 | UNICODE_TEST(unicode_test_utf16_utf8_strncpy); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 550 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 551 | static int unicode_test_utf_to_lower(struct unit_test_state *uts) |
Heinrich Schuchardt | 1a1012a | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 552 | { |
| 553 | ut_asserteq('@', utf_to_lower('@')); |
| 554 | ut_asserteq('a', utf_to_lower('A')); |
| 555 | ut_asserteq('z', utf_to_lower('Z')); |
| 556 | ut_asserteq('[', utf_to_lower('[')); |
| 557 | ut_asserteq('m', utf_to_lower('m')); |
| 558 | /* Latin letter O with diaresis (umlaut) */ |
| 559 | ut_asserteq(0x00f6, utf_to_lower(0x00d6)); |
| 560 | #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION |
| 561 | /* Cyrillic letter I*/ |
| 562 | ut_asserteq(0x0438, utf_to_lower(0x0418)); |
| 563 | #endif |
| 564 | return 0; |
| 565 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 566 | UNICODE_TEST(unicode_test_utf_to_lower); |
Heinrich Schuchardt | 1a1012a | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 567 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 568 | static int unicode_test_utf_to_upper(struct unit_test_state *uts) |
Heinrich Schuchardt | 1a1012a | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 569 | { |
| 570 | ut_asserteq('`', utf_to_upper('`')); |
| 571 | ut_asserteq('A', utf_to_upper('a')); |
| 572 | ut_asserteq('Z', utf_to_upper('z')); |
| 573 | ut_asserteq('{', utf_to_upper('{')); |
| 574 | ut_asserteq('M', utf_to_upper('M')); |
| 575 | /* Latin letter O with diaresis (umlaut) */ |
| 576 | ut_asserteq(0x00d6, utf_to_upper(0x00f6)); |
| 577 | #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION |
| 578 | /* Cyrillic letter I */ |
| 579 | ut_asserteq(0x0418, utf_to_upper(0x0438)); |
| 580 | #endif |
| 581 | return 0; |
| 582 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 583 | UNICODE_TEST(unicode_test_utf_to_upper); |
Heinrich Schuchardt | 1a1012a | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 584 | |
AKASHI Takahiro | 79907a4 | 2019-09-18 10:26:30 +0900 | [diff] [blame] | 585 | static int unicode_test_u16_strncmp(struct unit_test_state *uts) |
| 586 | { |
| 587 | ut_assert(u16_strncmp(L"abc", L"abc", 3) == 0); |
| 588 | ut_assert(u16_strncmp(L"abcdef", L"abcghi", 3) == 0); |
| 589 | ut_assert(u16_strncmp(L"abcdef", L"abcghi", 6) < 0); |
| 590 | ut_assert(u16_strncmp(L"abcghi", L"abcdef", 6) > 0); |
| 591 | ut_assert(u16_strcmp(L"abc", L"abc") == 0); |
| 592 | ut_assert(u16_strcmp(L"abcdef", L"deghi") < 0); |
| 593 | ut_assert(u16_strcmp(L"deghi", L"abcdef") > 0); |
| 594 | return 0; |
| 595 | } |
| 596 | UNICODE_TEST(unicode_test_u16_strncmp); |
| 597 | |
Heinrich Schuchardt | efe3b5c | 2020-05-09 09:16:49 +0200 | [diff] [blame] | 598 | static int unicode_test_u16_strsize(struct unit_test_state *uts) |
| 599 | { |
| 600 | ut_asserteq_64(u16_strsize(c1), 14); |
| 601 | ut_asserteq_64(u16_strsize(c2), 18); |
| 602 | ut_asserteq_64(u16_strsize(c3), 8); |
| 603 | ut_asserteq_64(u16_strsize(c4), 14); |
| 604 | return 0; |
| 605 | } |
| 606 | UNICODE_TEST(unicode_test_u16_strsize); |
| 607 | |
Heinrich Schuchardt | 73bb90c | 2021-02-27 14:08:36 +0100 | [diff] [blame] | 608 | static int unicode_test_utf_to_cp(struct unit_test_state *uts) |
| 609 | { |
| 610 | int ret; |
| 611 | s32 c; |
| 612 | |
| 613 | c = '\n'; |
| 614 | ret = utf_to_cp(&c, codepage_437); |
| 615 | ut_asserteq(0, ret); |
| 616 | ut_asserteq('\n', c); |
| 617 | |
| 618 | c = 'a'; |
| 619 | ret = utf_to_cp(&c, codepage_437); |
| 620 | ut_asserteq(0, ret); |
| 621 | ut_asserteq('a', c); |
| 622 | |
| 623 | c = 0x03c4; /* Greek small letter tau */ |
| 624 | ret = utf_to_cp(&c, codepage_437); |
| 625 | ut_asserteq(0, ret); |
| 626 | ut_asserteq(0xe7, c); |
| 627 | |
| 628 | c = 0x03a4; /* Greek capital letter tau */ |
| 629 | ret = utf_to_cp(&c, codepage_437); |
| 630 | ut_asserteq(-ENOENT, ret); |
| 631 | ut_asserteq('?', c); |
| 632 | |
| 633 | return 0; |
| 634 | } |
| 635 | UNICODE_TEST(unicode_test_utf_to_cp); |
| 636 | |
Heinrich Schuchardt | e91789e | 2021-02-27 14:08:38 +0100 | [diff] [blame] | 637 | static void utf8_to_cp437_stream_helper(const char *in, char *out) |
| 638 | { |
| 639 | char buffer[5]; |
| 640 | int ret; |
| 641 | |
| 642 | *buffer = 0; |
| 643 | for (; *in; ++in) { |
| 644 | ret = utf8_to_cp437_stream(*in, buffer); |
| 645 | if (ret) |
| 646 | *out++ = ret; |
| 647 | } |
| 648 | *out = 0; |
| 649 | } |
| 650 | |
| 651 | static int unicode_test_utf8_to_cp437_stream(struct unit_test_state *uts) |
| 652 | { |
| 653 | char buf[16]; |
| 654 | |
| 655 | utf8_to_cp437_stream_helper(d1, buf); |
| 656 | ut_asserteq_str("U-Boot", buf); |
| 657 | utf8_to_cp437_stream_helper(d2, buf); |
| 658 | ut_asserteq_str("kafb\xa0tur", buf); |
| 659 | utf8_to_cp437_stream_helper(d5, buf); |
| 660 | ut_asserteq_str("? is not B", buf); |
| 661 | utf8_to_cp437_stream_helper(j2, buf); |
| 662 | ut_asserteq_str("j2l", buf); |
| 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | UNICODE_TEST(unicode_test_utf8_to_cp437_stream); |
| 667 | |
| 668 | static void utf8_to_utf32_stream_helper(const char *in, s32 *out) |
| 669 | { |
| 670 | char buffer[5]; |
| 671 | int ret; |
| 672 | |
| 673 | *buffer = 0; |
| 674 | for (; *in; ++in) { |
| 675 | ret = utf8_to_utf32_stream(*in, buffer); |
| 676 | if (ret) |
| 677 | *out++ = ret; |
| 678 | } |
| 679 | *out = 0; |
| 680 | } |
| 681 | |
| 682 | static int unicode_test_utf8_to_utf32_stream(struct unit_test_state *uts) |
| 683 | { |
| 684 | s32 buf[16]; |
| 685 | |
| 686 | const u32 u1[] = {0x55, 0x2D, 0x42, 0x6F, 0x6F, 0x74, 0x0000}; |
| 687 | const u32 u2[] = {0x6B, 0x61, 0x66, 0x62, 0xE1, 0x74, 0x75, 0x72, 0x00}; |
| 688 | const u32 u3[] = {0x0392, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F, 0x74, |
| 689 | 0x20, 0x42, 0x00}; |
| 690 | const u32 u4[] = {0x6A, 0x32, 0x6C, 0x00}; |
| 691 | |
| 692 | memset(buf, 0, sizeof(buf)); |
| 693 | utf8_to_utf32_stream_helper(d1, buf); |
| 694 | ut_asserteq_mem(u1, buf, sizeof(u1)); |
| 695 | |
| 696 | memset(buf, 0, sizeof(buf)); |
| 697 | utf8_to_utf32_stream_helper(d2, buf); |
| 698 | ut_asserteq_mem(u2, buf, sizeof(u2)); |
| 699 | |
| 700 | memset(buf, 0, sizeof(buf)); |
| 701 | utf8_to_utf32_stream_helper(d5, buf); |
| 702 | ut_asserteq_mem(u3, buf, sizeof(u3)); |
| 703 | |
| 704 | memset(buf, 0, sizeof(buf)); |
| 705 | utf8_to_utf32_stream_helper(j2, buf); |
| 706 | ut_asserteq_mem(u4, buf, sizeof(u4)); |
| 707 | |
| 708 | return 0; |
| 709 | } |
| 710 | UNICODE_TEST(unicode_test_utf8_to_utf32_stream); |
| 711 | |
Heinrich Schuchardt | af11423 | 2020-10-30 12:23:59 +0100 | [diff] [blame] | 712 | #ifdef CONFIG_EFI_LOADER |
| 713 | static int unicode_test_efi_create_indexed_name(struct unit_test_state *uts) |
| 714 | { |
| 715 | u16 buf[16]; |
| 716 | u16 const expected[] = L"Capsule0AF9"; |
| 717 | u16 *pos; |
| 718 | |
| 719 | memset(buf, 0xeb, sizeof(buf)); |
Ilias Apalodimas | fe179d7 | 2020-12-31 12:26:46 +0200 | [diff] [blame] | 720 | pos = efi_create_indexed_name(buf, sizeof(buf), "Capsule", 0x0af9); |
Heinrich Schuchardt | af11423 | 2020-10-30 12:23:59 +0100 | [diff] [blame] | 721 | |
| 722 | ut_asserteq_mem(expected, buf, sizeof(expected)); |
| 723 | ut_asserteq(pos - buf, u16_strnlen(buf, SIZE_MAX)); |
| 724 | |
| 725 | return 0; |
| 726 | } |
| 727 | UNICODE_TEST(unicode_test_efi_create_indexed_name); |
| 728 | #endif |
| 729 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 730 | int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 731 | { |
| 732 | struct unit_test *tests = ll_entry_start(struct unit_test, unicode_test); |
| 733 | const int n_ents = ll_entry_count(struct unit_test, unicode_test); |
| 734 | |
Philippe Reynes | 4ad4edf | 2019-12-17 19:07:04 +0100 | [diff] [blame] | 735 | return cmd_ut_category("Unicode", "unicode_test_", |
| 736 | tests, n_ents, argc, argv); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 737 | } |