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> |
| 11 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 14 | #include <test/test.h> |
| 15 | #include <test/suites.h> |
| 16 | #include <test/ut.h> |
| 17 | |
| 18 | /* Linker list entry for a Unicode test */ |
| 19 | #define UNICODE_TEST(_name) UNIT_TEST(_name, 0, unicode_test) |
| 20 | |
| 21 | /* Constants c1-c4 and d1-d4 encode the same letters */ |
| 22 | |
| 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, |
| 31 | 0x0000}; |
| 32 | |
| 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}; |
| 37 | |
| 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, |
| 42 | 0x72, 0x00}; |
| 43 | /* Three characters translating to one utf-16 word each */ |
| 44 | static const char d3[] = {0xe6, 0xbd, 0x9c, 0xe6, 0xb0, 0xb4, 0xe8, 0x89, |
| 45 | 0xa6, 0x00}; |
| 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}; |
| 49 | |
| 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}; |
| 54 | |
Heinrich Schuchardt | 02b31dc | 2019-07-14 17:47:46 +0200 | [diff] [blame] | 55 | static int unicode_test_u16_strlen(struct unit_test_state *uts) |
| 56 | { |
| 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)); |
| 61 | return 0; |
| 62 | } |
| 63 | UNICODE_TEST(unicode_test_u16_strlen); |
| 64 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 65 | static int unicode_test_u16_strdup(struct unit_test_state *uts) |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 66 | { |
| 67 | u16 *copy = u16_strdup(c4); |
| 68 | |
| 69 | ut_assert(copy != c4); |
Simon Glass | f91f366 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 70 | ut_asserteq_mem(copy, c4, sizeof(c4)); |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 71 | free(copy); |
Simon Glass | f91f366 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 72 | |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 73 | return 0; |
| 74 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 75 | UNICODE_TEST(unicode_test_u16_strdup); |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 76 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 77 | static int unicode_test_u16_strcpy(struct unit_test_state *uts) |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 78 | { |
| 79 | u16 *r; |
| 80 | u16 copy[10]; |
| 81 | |
| 82 | r = u16_strcpy(copy, c1); |
| 83 | ut_assert(r == copy); |
Simon Glass | f91f366 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 84 | ut_asserteq_mem(copy, c1, sizeof(c1)); |
| 85 | |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 86 | return 0; |
| 87 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 88 | UNICODE_TEST(unicode_test_u16_strcpy); |
Heinrich Schuchardt | abb93cb | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 89 | |
Heinrich Schuchardt | fbba2f6 | 2018-08-31 21:31:30 +0200 | [diff] [blame] | 90 | /* U-Boot uses UTF-16 strings in the EFI context only. */ |
| 91 | #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD) |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 92 | static int unicode_test_string16(struct unit_test_state *uts) |
Heinrich Schuchardt | fbba2f6 | 2018-08-31 21:31:30 +0200 | [diff] [blame] | 93 | { |
| 94 | char buf[20]; |
| 95 | |
| 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)); |
| 101 | ut_assert(!buf[9]); |
| 102 | |
| 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)); |
| 107 | ut_assert(!buf[17]); |
| 108 | |
| 109 | memset(buf, 0xff, sizeof(buf)); |
| 110 | sprintf(buf, "%-8.2ls", c4); |
| 111 | ut_asserteq(' ', buf[8]); |
| 112 | ut_assert(!strncmp(buf, d4, 8)); |
| 113 | ut_assert(!buf[14]); |
| 114 | |
| 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); |
| 119 | |
| 120 | memset(buf, 0xff, sizeof(buf)); |
| 121 | sprintf(buf, "%ls", i2); |
| 122 | ut_asserteq_str("i2?l", buf); |
| 123 | |
| 124 | memset(buf, 0xff, sizeof(buf)); |
| 125 | sprintf(buf, "%ls", i3); |
| 126 | ut_asserteq_str("i3?", buf); |
| 127 | |
| 128 | return 0; |
| 129 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 130 | UNICODE_TEST(unicode_test_string16); |
Heinrich Schuchardt | fbba2f6 | 2018-08-31 21:31:30 +0200 | [diff] [blame] | 131 | #endif |
| 132 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 133 | static int unicode_test_utf8_get(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 134 | { |
| 135 | const char *s; |
| 136 | s32 code; |
| 137 | int i; |
| 138 | |
| 139 | /* Check characters less than 0x800 */ |
| 140 | s = d2; |
| 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); |
| 145 | if (!code) |
| 146 | break; |
| 147 | } |
| 148 | ut_asserteq_ptr(s, d2 + 9) |
| 149 | |
| 150 | /* Check characters less than 0x10000 */ |
| 151 | s = d3; |
| 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); |
| 156 | if (!code) |
| 157 | break; |
| 158 | } |
| 159 | ut_asserteq_ptr(s, d3 + 9) |
| 160 | |
| 161 | /* Check character greater 0xffff */ |
| 162 | s = d4; |
| 163 | code = utf8_get((const char **)&s); |
| 164 | ut_asserteq(0x0001048d, code); |
| 165 | ut_asserteq_ptr(s, d4 + 4); |
| 166 | |
| 167 | return 0; |
| 168 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 169 | UNICODE_TEST(unicode_test_utf8_get); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 170 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 171 | static int unicode_test_utf8_put(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 172 | { |
| 173 | char buffer[8] = { 0, }; |
| 174 | char *pos; |
| 175 | |
| 176 | /* Commercial at, translates to one character */ |
| 177 | pos = buffer; |
| 178 | ut_assert(!utf8_put('@', &pos)) |
| 179 | ut_asserteq(1, pos - buffer); |
| 180 | ut_asserteq('@', buffer[0]); |
| 181 | ut_assert(!buffer[1]); |
| 182 | |
| 183 | /* Latin letter G with acute, translates to two charactes */ |
| 184 | pos = buffer; |
| 185 | ut_assert(!utf8_put(0x1f4, &pos)); |
| 186 | ut_asserteq(2, pos - buffer); |
| 187 | ut_asserteq_str("\xc7\xb4", buffer); |
| 188 | |
| 189 | /* Tagalog letter i, translates to three characters */ |
| 190 | pos = buffer; |
| 191 | ut_assert(!utf8_put(0x1701, &pos)); |
| 192 | ut_asserteq(3, pos - buffer); |
| 193 | ut_asserteq_str("\xe1\x9c\x81", buffer); |
| 194 | |
| 195 | /* Hamster face, translates to four characters */ |
| 196 | pos = buffer; |
| 197 | ut_assert(!utf8_put(0x1f439, &pos)); |
| 198 | ut_asserteq(4, pos - buffer); |
| 199 | ut_asserteq_str("\xf0\x9f\x90\xb9", buffer); |
| 200 | |
| 201 | /* Illegal code */ |
| 202 | pos = buffer; |
| 203 | ut_asserteq(-1, utf8_put(0xd888, &pos)); |
| 204 | |
| 205 | return 0; |
| 206 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 207 | UNICODE_TEST(unicode_test_utf8_put); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 208 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 209 | static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 210 | { |
| 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)); |
| 215 | |
| 216 | /* illegal utf-8 sequences */ |
| 217 | ut_asserteq(4, utf8_utf16_strlen(j1)); |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 218 | ut_asserteq(4, utf8_utf16_strlen(j2)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 219 | ut_asserteq(3, utf8_utf16_strlen(j3)); |
| 220 | |
| 221 | return 0; |
| 222 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 223 | UNICODE_TEST(unicode_test_utf8_utf16_strlen); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 224 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 225 | static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 226 | { |
| 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)); |
| 233 | |
| 234 | /* illegal utf-8 sequences */ |
| 235 | ut_asserteq(4, utf8_utf16_strnlen(j1, 16)); |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 236 | ut_asserteq(4, utf8_utf16_strnlen(j2, 16)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 237 | ut_asserteq(3, utf8_utf16_strnlen(j3, 16)); |
| 238 | |
| 239 | return 0; |
| 240 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 241 | UNICODE_TEST(unicode_test_utf8_utf16_strnlen); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 242 | |
| 243 | /** |
| 244 | * ut_u16_strcmp() - Compare to u16 strings. |
| 245 | * |
| 246 | * @a1: first string |
| 247 | * @a2: second string |
| 248 | * @count: number of u16 to compare |
| 249 | * Return: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2 |
| 250 | */ |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 251 | 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] | 252 | { |
| 253 | for (; (*a1 || *a2) && count; ++a1, ++a2, --count) { |
| 254 | if (*a1 < *a2) |
| 255 | return -1; |
| 256 | if (*a1 > *a2) |
| 257 | return 1; |
| 258 | } |
| 259 | return 0; |
| 260 | } |
| 261 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 262 | static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 263 | { |
| 264 | u16 buf[16]; |
| 265 | u16 *pos; |
| 266 | |
| 267 | pos = buf; |
| 268 | utf8_utf16_strcpy(&pos, d1); |
| 269 | ut_asserteq(6, pos - buf); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 270 | ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 271 | |
| 272 | pos = buf; |
| 273 | utf8_utf16_strcpy(&pos, d2); |
| 274 | ut_asserteq(8, pos - buf); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 275 | ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 276 | |
| 277 | pos = buf; |
| 278 | utf8_utf16_strcpy(&pos, d3); |
| 279 | ut_asserteq(3, pos - buf); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 280 | ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 281 | |
| 282 | pos = buf; |
| 283 | utf8_utf16_strcpy(&pos, d4); |
| 284 | ut_asserteq(6, pos - buf); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 285 | ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 286 | |
| 287 | /* Illegal utf-8 strings */ |
| 288 | pos = buf; |
| 289 | utf8_utf16_strcpy(&pos, j1); |
| 290 | ut_asserteq(4, pos - buf); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 291 | ut_assert(!unicode_test_u16_strcmp(buf, L"j1?l", SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 292 | |
| 293 | pos = buf; |
| 294 | utf8_utf16_strcpy(&pos, j2); |
Heinrich Schuchardt | 35cbb79 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 295 | ut_asserteq(4, pos - buf); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 296 | ut_assert(!unicode_test_u16_strcmp(buf, L"j2?l", SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 297 | |
| 298 | pos = buf; |
| 299 | utf8_utf16_strcpy(&pos, j3); |
| 300 | ut_asserteq(3, pos - buf); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 301 | ut_assert(!unicode_test_u16_strcmp(buf, L"j3?", SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 302 | |
| 303 | return 0; |
| 304 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 305 | UNICODE_TEST(unicode_test_utf8_utf16_strcpy); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 306 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 307 | static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 308 | { |
| 309 | u16 buf[16]; |
| 310 | u16 *pos; |
| 311 | |
| 312 | pos = buf; |
| 313 | memset(buf, 0, sizeof(buf)); |
| 314 | utf8_utf16_strncpy(&pos, d1, 4); |
| 315 | ut_asserteq(4, pos - buf); |
| 316 | ut_assert(!buf[4]); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 317 | ut_assert(!unicode_test_u16_strcmp(buf, c1, 4)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 318 | |
| 319 | pos = buf; |
| 320 | memset(buf, 0, sizeof(buf)); |
| 321 | utf8_utf16_strncpy(&pos, d2, 10); |
| 322 | ut_asserteq(8, pos - buf); |
| 323 | ut_assert(buf[4]); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 324 | ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 325 | |
| 326 | pos = buf; |
| 327 | memset(buf, 0, sizeof(buf)); |
| 328 | utf8_utf16_strncpy(&pos, d3, 2); |
| 329 | ut_asserteq(2, pos - buf); |
| 330 | ut_assert(!buf[2]); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 331 | ut_assert(!unicode_test_u16_strcmp(buf, c3, 2)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 332 | |
| 333 | pos = buf; |
| 334 | memset(buf, 0, sizeof(buf)); |
| 335 | utf8_utf16_strncpy(&pos, d4, 2); |
| 336 | ut_asserteq(4, pos - buf); |
| 337 | ut_assert(!buf[4]); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 338 | ut_assert(!unicode_test_u16_strcmp(buf, c4, 4)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 339 | |
| 340 | pos = buf; |
| 341 | memset(buf, 0, sizeof(buf)); |
| 342 | utf8_utf16_strncpy(&pos, d4, 10); |
| 343 | ut_asserteq(6, pos - buf); |
| 344 | ut_assert(buf[5]); |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 345 | ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX)); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 346 | |
| 347 | return 0; |
| 348 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 349 | UNICODE_TEST(unicode_test_utf8_utf16_strncpy); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 350 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 351 | static int unicode_test_utf16_get(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 352 | { |
| 353 | const u16 *s; |
| 354 | s32 code; |
| 355 | int i; |
| 356 | |
| 357 | /* Check characters less than 0x10000 */ |
| 358 | s = c2; |
| 359 | for (i = 0; i < 9; ++i) { |
| 360 | code = utf16_get((const u16 **)&s); |
| 361 | ut_asserteq(c2[i], code); |
| 362 | if (!code) |
| 363 | break; |
| 364 | } |
| 365 | ut_asserteq_ptr(c2 + 8, s); |
| 366 | |
| 367 | /* Check character greater 0xffff */ |
| 368 | s = c4; |
| 369 | code = utf16_get((const u16 **)&s); |
| 370 | ut_asserteq(0x0001048d, code); |
| 371 | ut_asserteq_ptr(c4 + 2, s); |
| 372 | |
| 373 | return 0; |
| 374 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 375 | UNICODE_TEST(unicode_test_utf16_get); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 376 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 377 | static int unicode_test_utf16_put(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 378 | { |
| 379 | u16 buffer[4] = { 0, }; |
| 380 | u16 *pos; |
| 381 | |
| 382 | /* Commercial at, translates to one word */ |
| 383 | pos = buffer; |
| 384 | ut_assert(!utf16_put('@', &pos)); |
| 385 | ut_asserteq(1, pos - buffer); |
| 386 | ut_asserteq((u16)'@', buffer[0]); |
| 387 | ut_assert(!buffer[1]); |
| 388 | |
| 389 | /* Hamster face, translates to two words */ |
| 390 | pos = buffer; |
| 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]); |
| 396 | |
| 397 | /* Illegal code */ |
| 398 | pos = buffer; |
| 399 | ut_asserteq(-1, utf16_put(0xd888, &pos)); |
| 400 | |
| 401 | return 0; |
| 402 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 403 | UNICODE_TEST(unicode_test_utf16_put); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 404 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 405 | static int unicode_test_utf16_strnlen(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 406 | { |
| 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)); |
| 413 | |
| 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)); |
| 418 | |
| 419 | return 0; |
| 420 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 421 | UNICODE_TEST(unicode_test_utf16_strnlen); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 422 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 423 | static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 424 | { |
| 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)); |
| 429 | |
| 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)); |
| 434 | |
| 435 | return 0; |
| 436 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 437 | UNICODE_TEST(unicode_test_utf16_utf8_strlen); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 438 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 439 | static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 440 | { |
| 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)); |
| 447 | return 0; |
| 448 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 449 | UNICODE_TEST(unicode_test_utf16_utf8_strnlen); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 450 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 451 | static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 452 | { |
| 453 | char buf[16]; |
| 454 | char *pos; |
| 455 | |
| 456 | pos = buf; |
| 457 | utf16_utf8_strcpy(&pos, c1); |
| 458 | ut_asserteq(6, pos - buf); |
| 459 | ut_asserteq_str(d1, buf); |
| 460 | |
| 461 | pos = buf; |
| 462 | utf16_utf8_strcpy(&pos, c2); |
| 463 | ut_asserteq(9, pos - buf); |
| 464 | ut_asserteq_str(d2, buf); |
| 465 | |
| 466 | pos = buf; |
| 467 | utf16_utf8_strcpy(&pos, c3); |
| 468 | ut_asserteq(9, pos - buf); |
| 469 | ut_asserteq_str(d3, buf); |
| 470 | |
| 471 | pos = buf; |
| 472 | utf16_utf8_strcpy(&pos, c4); |
| 473 | ut_asserteq(12, pos - buf); |
| 474 | ut_asserteq_str(d4, buf); |
| 475 | |
| 476 | /* Illegal utf-16 strings */ |
| 477 | pos = buf; |
| 478 | utf16_utf8_strcpy(&pos, i1); |
| 479 | ut_asserteq(4, pos - buf); |
| 480 | ut_asserteq_str("i1?l", buf); |
| 481 | |
| 482 | pos = buf; |
| 483 | utf16_utf8_strcpy(&pos, i2); |
| 484 | ut_asserteq(4, pos - buf); |
| 485 | ut_asserteq_str("i2?l", buf); |
| 486 | |
| 487 | pos = buf; |
| 488 | utf16_utf8_strcpy(&pos, i3); |
| 489 | ut_asserteq(3, pos - buf); |
| 490 | ut_asserteq_str("i3?", buf); |
| 491 | |
| 492 | return 0; |
| 493 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 494 | UNICODE_TEST(unicode_test_utf16_utf8_strcpy); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 495 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 496 | static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts) |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 497 | { |
| 498 | char buf[16]; |
| 499 | char *pos; |
| 500 | |
| 501 | pos = buf; |
| 502 | memset(buf, 0, sizeof(buf)); |
| 503 | utf16_utf8_strncpy(&pos, c1, 4); |
| 504 | ut_asserteq(4, pos - buf); |
| 505 | ut_assert(!buf[4]); |
| 506 | ut_assert(!strncmp(buf, d1, 4)); |
| 507 | |
| 508 | pos = buf; |
| 509 | memset(buf, 0, sizeof(buf)); |
| 510 | utf16_utf8_strncpy(&pos, c2, 10); |
| 511 | ut_asserteq(9, pos - buf); |
| 512 | ut_assert(buf[4]); |
| 513 | ut_assert(!strncmp(buf, d2, SIZE_MAX)); |
| 514 | |
| 515 | pos = buf; |
| 516 | memset(buf, 0, sizeof(buf)); |
| 517 | utf16_utf8_strncpy(&pos, c3, 2); |
| 518 | ut_asserteq(6, pos - buf); |
| 519 | ut_assert(!buf[6]); |
| 520 | ut_assert(!strncmp(buf, d3, 6)); |
| 521 | |
| 522 | pos = buf; |
| 523 | memset(buf, 0, sizeof(buf)); |
| 524 | utf16_utf8_strncpy(&pos, c4, 2); |
| 525 | ut_asserteq(8, pos - buf); |
| 526 | ut_assert(!buf[8]); |
| 527 | ut_assert(!strncmp(buf, d4, 8)); |
| 528 | |
| 529 | pos = buf; |
| 530 | memset(buf, 0, sizeof(buf)); |
| 531 | utf16_utf8_strncpy(&pos, c4, 10); |
| 532 | ut_asserteq(12, pos - buf); |
| 533 | ut_assert(buf[5]); |
| 534 | ut_assert(!strncmp(buf, d4, SIZE_MAX)); |
| 535 | |
| 536 | return 0; |
| 537 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 538 | UNICODE_TEST(unicode_test_utf16_utf8_strncpy); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 539 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 540 | static int unicode_test_utf_to_lower(struct unit_test_state *uts) |
Heinrich Schuchardt | 1a1012a | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 541 | { |
| 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)); |
| 552 | #endif |
| 553 | return 0; |
| 554 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 555 | UNICODE_TEST(unicode_test_utf_to_lower); |
Heinrich Schuchardt | 1a1012a | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 556 | |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 557 | static int unicode_test_utf_to_upper(struct unit_test_state *uts) |
Heinrich Schuchardt | 1a1012a | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 558 | { |
| 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)); |
| 569 | #endif |
| 570 | return 0; |
| 571 | } |
Heinrich Schuchardt | bc19681 | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 572 | UNICODE_TEST(unicode_test_utf_to_upper); |
Heinrich Schuchardt | 1a1012a | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 573 | |
AKASHI Takahiro | 79907a4 | 2019-09-18 10:26:30 +0900 | [diff] [blame] | 574 | static int unicode_test_u16_strncmp(struct unit_test_state *uts) |
| 575 | { |
| 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); |
| 583 | return 0; |
| 584 | } |
| 585 | UNICODE_TEST(unicode_test_u16_strncmp); |
| 586 | |
Heinrich Schuchardt | efe3b5c | 2020-05-09 09:16:49 +0200 | [diff] [blame] | 587 | static int unicode_test_u16_strsize(struct unit_test_state *uts) |
| 588 | { |
| 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); |
| 593 | return 0; |
| 594 | } |
| 595 | UNICODE_TEST(unicode_test_u16_strsize); |
| 596 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 597 | 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] | 598 | { |
| 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); |
| 601 | |
Philippe Reynes | 4ad4edf | 2019-12-17 19:07:04 +0100 | [diff] [blame] | 602 | return cmd_ut_category("Unicode", "unicode_test_", |
| 603 | tests, n_ents, argc, argv); |
Heinrich Schuchardt | f11a164 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 604 | } |