Heinrich Schuchardt | b8b6c81 | 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> |
| 12 | #include <test/test.h> |
| 13 | #include <test/suites.h> |
| 14 | #include <test/ut.h> |
| 15 | |
| 16 | /* Linker list entry for a Unicode test */ |
| 17 | #define UNICODE_TEST(_name) UNIT_TEST(_name, 0, unicode_test) |
| 18 | |
| 19 | /* Constants c1-c4 and d1-d4 encode the same letters */ |
| 20 | |
| 21 | /* Six characters translating to one utf-8 byte each. */ |
| 22 | static const u16 c1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00}; |
| 23 | /* One character translating to two utf-8 bytes */ |
| 24 | static const u16 c2[] = {0x6b, 0x61, 0x66, 0x62, 0xe1, 0x74, 0x75, 0x72, 0x00}; |
| 25 | /* Three characters translating to three utf-8 bytes each */ |
| 26 | static const u16 c3[] = {0x6f5c, 0x6c34, 0x8266, 0x00}; |
| 27 | /* Three letters translating to four utf-8 bytes each */ |
| 28 | static const u16 c4[] = {0xd801, 0xdc8d, 0xd801, 0xdc96, 0xd801, 0xdc87, |
| 29 | 0x0000}; |
| 30 | |
| 31 | /* Illegal utf-16 strings */ |
| 32 | static const u16 i1[] = {0x69, 0x31, 0xdc87, 0x6c, 0x00}; |
| 33 | static const u16 i2[] = {0x69, 0x32, 0xd801, 0xd801, 0x6c, 0x00}; |
| 34 | static const u16 i3[] = {0x69, 0x33, 0xd801, 0x00}; |
| 35 | |
| 36 | /* Six characters translating to one utf-16 word each. */ |
| 37 | static const char d1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00}; |
| 38 | /* Eight characters translating to one utf-16 word each */ |
| 39 | static const char d2[] = {0x6b, 0x61, 0x66, 0x62, 0xc3, 0xa1, 0x74, 0x75, |
| 40 | 0x72, 0x00}; |
| 41 | /* Three characters translating to one utf-16 word each */ |
| 42 | static const char d3[] = {0xe6, 0xbd, 0x9c, 0xe6, 0xb0, 0xb4, 0xe8, 0x89, |
| 43 | 0xa6, 0x00}; |
| 44 | /* Three letters translating to two utf-16 word each */ |
| 45 | static const char d4[] = {0xf0, 0x90, 0x92, 0x8d, 0xf0, 0x90, 0x92, 0x96, |
| 46 | 0xf0, 0x90, 0x92, 0x87, 0x00}; |
| 47 | |
| 48 | /* Illegal utf-8 strings */ |
| 49 | static const char j1[] = {0x6a, 0x31, 0xa1, 0x6c, 0x00}; |
| 50 | static const char j2[] = {0x6a, 0x32, 0xc3, 0xc3, 0x6c, 0x00}; |
| 51 | static const char j3[] = {0x6a, 0x33, 0xf0, 0x90, 0xf0, 0x00}; |
| 52 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 53 | static int unicode_test_u16_strdup(struct unit_test_state *uts) |
Heinrich Schuchardt | 45fceca | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 54 | { |
| 55 | u16 *copy = u16_strdup(c4); |
| 56 | |
| 57 | ut_assert(copy != c4); |
| 58 | ut_assert(!memcmp(copy, c4, sizeof(c4))); |
| 59 | free(copy); |
| 60 | return 0; |
| 61 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 62 | UNICODE_TEST(unicode_test_u16_strdup); |
Heinrich Schuchardt | 45fceca | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 63 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 64 | static int unicode_test_u16_strcpy(struct unit_test_state *uts) |
Heinrich Schuchardt | 45fceca | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 65 | { |
| 66 | u16 *r; |
| 67 | u16 copy[10]; |
| 68 | |
| 69 | r = u16_strcpy(copy, c1); |
| 70 | ut_assert(r == copy); |
| 71 | ut_assert(!memcmp(copy, c1, sizeof(c1))); |
| 72 | return 0; |
| 73 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 74 | UNICODE_TEST(unicode_test_u16_strcpy); |
Heinrich Schuchardt | 45fceca | 2018-12-14 22:00:37 +0100 | [diff] [blame] | 75 | |
Heinrich Schuchardt | 485599d | 2018-08-31 21:31:30 +0200 | [diff] [blame] | 76 | /* U-Boot uses UTF-16 strings in the EFI context only. */ |
| 77 | #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD) |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 78 | static int unicode_test_string16(struct unit_test_state *uts) |
Heinrich Schuchardt | 485599d | 2018-08-31 21:31:30 +0200 | [diff] [blame] | 79 | { |
| 80 | char buf[20]; |
| 81 | |
| 82 | /* Test length and precision */ |
| 83 | memset(buf, 0xff, sizeof(buf)); |
| 84 | sprintf(buf, "%8.6ls", c2); |
| 85 | ut_asserteq(' ', buf[1]); |
| 86 | ut_assert(!strncmp(&buf[2], d2, 7)); |
| 87 | ut_assert(!buf[9]); |
| 88 | |
| 89 | memset(buf, 0xff, sizeof(buf)); |
| 90 | sprintf(buf, "%8.6ls", c4); |
| 91 | ut_asserteq(' ', buf[4]); |
| 92 | ut_assert(!strncmp(&buf[5], d4, 12)); |
| 93 | ut_assert(!buf[17]); |
| 94 | |
| 95 | memset(buf, 0xff, sizeof(buf)); |
| 96 | sprintf(buf, "%-8.2ls", c4); |
| 97 | ut_asserteq(' ', buf[8]); |
| 98 | ut_assert(!strncmp(buf, d4, 8)); |
| 99 | ut_assert(!buf[14]); |
| 100 | |
| 101 | /* Test handling of illegal utf-16 sequences */ |
| 102 | memset(buf, 0xff, sizeof(buf)); |
| 103 | sprintf(buf, "%ls", i1); |
| 104 | ut_asserteq_str("i1?l", buf); |
| 105 | |
| 106 | memset(buf, 0xff, sizeof(buf)); |
| 107 | sprintf(buf, "%ls", i2); |
| 108 | ut_asserteq_str("i2?l", buf); |
| 109 | |
| 110 | memset(buf, 0xff, sizeof(buf)); |
| 111 | sprintf(buf, "%ls", i3); |
| 112 | ut_asserteq_str("i3?", buf); |
| 113 | |
| 114 | return 0; |
| 115 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 116 | UNICODE_TEST(unicode_test_string16); |
Heinrich Schuchardt | 485599d | 2018-08-31 21:31:30 +0200 | [diff] [blame] | 117 | #endif |
| 118 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 119 | static int unicode_test_utf8_get(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 120 | { |
| 121 | const char *s; |
| 122 | s32 code; |
| 123 | int i; |
| 124 | |
| 125 | /* Check characters less than 0x800 */ |
| 126 | s = d2; |
| 127 | for (i = 0; i < 8; ++i) { |
| 128 | code = utf8_get((const char **)&s); |
| 129 | /* c2 is the utf-8 encoding of d2 */ |
| 130 | ut_asserteq(c2[i], code); |
| 131 | if (!code) |
| 132 | break; |
| 133 | } |
| 134 | ut_asserteq_ptr(s, d2 + 9) |
| 135 | |
| 136 | /* Check characters less than 0x10000 */ |
| 137 | s = d3; |
| 138 | for (i = 0; i < 4; ++i) { |
| 139 | code = utf8_get((const char **)&s); |
| 140 | /* c3 is the utf-8 encoding of d3 */ |
| 141 | ut_asserteq(c3[i], code); |
| 142 | if (!code) |
| 143 | break; |
| 144 | } |
| 145 | ut_asserteq_ptr(s, d3 + 9) |
| 146 | |
| 147 | /* Check character greater 0xffff */ |
| 148 | s = d4; |
| 149 | code = utf8_get((const char **)&s); |
| 150 | ut_asserteq(0x0001048d, code); |
| 151 | ut_asserteq_ptr(s, d4 + 4); |
| 152 | |
| 153 | return 0; |
| 154 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 155 | UNICODE_TEST(unicode_test_utf8_get); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 156 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 157 | static int unicode_test_utf8_put(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 158 | { |
| 159 | char buffer[8] = { 0, }; |
| 160 | char *pos; |
| 161 | |
| 162 | /* Commercial at, translates to one character */ |
| 163 | pos = buffer; |
| 164 | ut_assert(!utf8_put('@', &pos)) |
| 165 | ut_asserteq(1, pos - buffer); |
| 166 | ut_asserteq('@', buffer[0]); |
| 167 | ut_assert(!buffer[1]); |
| 168 | |
| 169 | /* Latin letter G with acute, translates to two charactes */ |
| 170 | pos = buffer; |
| 171 | ut_assert(!utf8_put(0x1f4, &pos)); |
| 172 | ut_asserteq(2, pos - buffer); |
| 173 | ut_asserteq_str("\xc7\xb4", buffer); |
| 174 | |
| 175 | /* Tagalog letter i, translates to three characters */ |
| 176 | pos = buffer; |
| 177 | ut_assert(!utf8_put(0x1701, &pos)); |
| 178 | ut_asserteq(3, pos - buffer); |
| 179 | ut_asserteq_str("\xe1\x9c\x81", buffer); |
| 180 | |
| 181 | /* Hamster face, translates to four characters */ |
| 182 | pos = buffer; |
| 183 | ut_assert(!utf8_put(0x1f439, &pos)); |
| 184 | ut_asserteq(4, pos - buffer); |
| 185 | ut_asserteq_str("\xf0\x9f\x90\xb9", buffer); |
| 186 | |
| 187 | /* Illegal code */ |
| 188 | pos = buffer; |
| 189 | ut_asserteq(-1, utf8_put(0xd888, &pos)); |
| 190 | |
| 191 | return 0; |
| 192 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 193 | UNICODE_TEST(unicode_test_utf8_put); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 194 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 195 | static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 196 | { |
| 197 | ut_asserteq(6, utf8_utf16_strlen(d1)); |
| 198 | ut_asserteq(8, utf8_utf16_strlen(d2)); |
| 199 | ut_asserteq(3, utf8_utf16_strlen(d3)); |
| 200 | ut_asserteq(6, utf8_utf16_strlen(d4)); |
| 201 | |
| 202 | /* illegal utf-8 sequences */ |
| 203 | ut_asserteq(4, utf8_utf16_strlen(j1)); |
Heinrich Schuchardt | fc5f1a1 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 204 | ut_asserteq(4, utf8_utf16_strlen(j2)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 205 | ut_asserteq(3, utf8_utf16_strlen(j3)); |
| 206 | |
| 207 | return 0; |
| 208 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 209 | UNICODE_TEST(unicode_test_utf8_utf16_strlen); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 210 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 211 | static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 212 | { |
| 213 | ut_asserteq(3, utf8_utf16_strnlen(d1, 3)); |
| 214 | ut_asserteq(6, utf8_utf16_strnlen(d1, 13)); |
| 215 | ut_asserteq(6, utf8_utf16_strnlen(d2, 6)); |
| 216 | ut_asserteq(2, utf8_utf16_strnlen(d3, 2)); |
| 217 | ut_asserteq(4, utf8_utf16_strnlen(d4, 2)); |
| 218 | ut_asserteq(6, utf8_utf16_strnlen(d4, 3)); |
| 219 | |
| 220 | /* illegal utf-8 sequences */ |
| 221 | ut_asserteq(4, utf8_utf16_strnlen(j1, 16)); |
Heinrich Schuchardt | fc5f1a1 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 222 | ut_asserteq(4, utf8_utf16_strnlen(j2, 16)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 223 | ut_asserteq(3, utf8_utf16_strnlen(j3, 16)); |
| 224 | |
| 225 | return 0; |
| 226 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 227 | UNICODE_TEST(unicode_test_utf8_utf16_strnlen); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 228 | |
| 229 | /** |
| 230 | * ut_u16_strcmp() - Compare to u16 strings. |
| 231 | * |
| 232 | * @a1: first string |
| 233 | * @a2: second string |
| 234 | * @count: number of u16 to compare |
| 235 | * Return: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2 |
| 236 | */ |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 237 | static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 238 | { |
| 239 | for (; (*a1 || *a2) && count; ++a1, ++a2, --count) { |
| 240 | if (*a1 < *a2) |
| 241 | return -1; |
| 242 | if (*a1 > *a2) |
| 243 | return 1; |
| 244 | } |
| 245 | return 0; |
| 246 | } |
| 247 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 248 | static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 249 | { |
| 250 | u16 buf[16]; |
| 251 | u16 *pos; |
| 252 | |
| 253 | pos = buf; |
| 254 | utf8_utf16_strcpy(&pos, d1); |
| 255 | ut_asserteq(6, pos - buf); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 256 | ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 257 | |
| 258 | pos = buf; |
| 259 | utf8_utf16_strcpy(&pos, d2); |
| 260 | ut_asserteq(8, pos - buf); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 261 | ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 262 | |
| 263 | pos = buf; |
| 264 | utf8_utf16_strcpy(&pos, d3); |
| 265 | ut_asserteq(3, pos - buf); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 266 | ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 267 | |
| 268 | pos = buf; |
| 269 | utf8_utf16_strcpy(&pos, d4); |
| 270 | ut_asserteq(6, pos - buf); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 271 | ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 272 | |
| 273 | /* Illegal utf-8 strings */ |
| 274 | pos = buf; |
| 275 | utf8_utf16_strcpy(&pos, j1); |
| 276 | ut_asserteq(4, pos - buf); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 277 | ut_assert(!unicode_test_u16_strcmp(buf, L"j1?l", SIZE_MAX)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 278 | |
| 279 | pos = buf; |
| 280 | utf8_utf16_strcpy(&pos, j2); |
Heinrich Schuchardt | fc5f1a1 | 2018-09-12 00:05:32 +0200 | [diff] [blame] | 281 | ut_asserteq(4, pos - buf); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 282 | ut_assert(!unicode_test_u16_strcmp(buf, L"j2?l", SIZE_MAX)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 283 | |
| 284 | pos = buf; |
| 285 | utf8_utf16_strcpy(&pos, j3); |
| 286 | ut_asserteq(3, pos - buf); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 287 | ut_assert(!unicode_test_u16_strcmp(buf, L"j3?", SIZE_MAX)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 288 | |
| 289 | return 0; |
| 290 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 291 | UNICODE_TEST(unicode_test_utf8_utf16_strcpy); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 292 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 293 | static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 294 | { |
| 295 | u16 buf[16]; |
| 296 | u16 *pos; |
| 297 | |
| 298 | pos = buf; |
| 299 | memset(buf, 0, sizeof(buf)); |
| 300 | utf8_utf16_strncpy(&pos, d1, 4); |
| 301 | ut_asserteq(4, pos - buf); |
| 302 | ut_assert(!buf[4]); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 303 | ut_assert(!unicode_test_u16_strcmp(buf, c1, 4)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 304 | |
| 305 | pos = buf; |
| 306 | memset(buf, 0, sizeof(buf)); |
| 307 | utf8_utf16_strncpy(&pos, d2, 10); |
| 308 | ut_asserteq(8, pos - buf); |
| 309 | ut_assert(buf[4]); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 310 | ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 311 | |
| 312 | pos = buf; |
| 313 | memset(buf, 0, sizeof(buf)); |
| 314 | utf8_utf16_strncpy(&pos, d3, 2); |
| 315 | ut_asserteq(2, pos - buf); |
| 316 | ut_assert(!buf[2]); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 317 | ut_assert(!unicode_test_u16_strcmp(buf, c3, 2)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 318 | |
| 319 | pos = buf; |
| 320 | memset(buf, 0, sizeof(buf)); |
| 321 | utf8_utf16_strncpy(&pos, d4, 2); |
| 322 | ut_asserteq(4, pos - buf); |
| 323 | ut_assert(!buf[4]); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 324 | ut_assert(!unicode_test_u16_strcmp(buf, c4, 4)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 325 | |
| 326 | pos = buf; |
| 327 | memset(buf, 0, sizeof(buf)); |
| 328 | utf8_utf16_strncpy(&pos, d4, 10); |
| 329 | ut_asserteq(6, pos - buf); |
| 330 | ut_assert(buf[5]); |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 331 | ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX)); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 332 | |
| 333 | return 0; |
| 334 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 335 | UNICODE_TEST(unicode_test_utf8_utf16_strncpy); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 336 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 337 | static int unicode_test_utf16_get(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 338 | { |
| 339 | const u16 *s; |
| 340 | s32 code; |
| 341 | int i; |
| 342 | |
| 343 | /* Check characters less than 0x10000 */ |
| 344 | s = c2; |
| 345 | for (i = 0; i < 9; ++i) { |
| 346 | code = utf16_get((const u16 **)&s); |
| 347 | ut_asserteq(c2[i], code); |
| 348 | if (!code) |
| 349 | break; |
| 350 | } |
| 351 | ut_asserteq_ptr(c2 + 8, s); |
| 352 | |
| 353 | /* Check character greater 0xffff */ |
| 354 | s = c4; |
| 355 | code = utf16_get((const u16 **)&s); |
| 356 | ut_asserteq(0x0001048d, code); |
| 357 | ut_asserteq_ptr(c4 + 2, s); |
| 358 | |
| 359 | return 0; |
| 360 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 361 | UNICODE_TEST(unicode_test_utf16_get); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 362 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 363 | static int unicode_test_utf16_put(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 364 | { |
| 365 | u16 buffer[4] = { 0, }; |
| 366 | u16 *pos; |
| 367 | |
| 368 | /* Commercial at, translates to one word */ |
| 369 | pos = buffer; |
| 370 | ut_assert(!utf16_put('@', &pos)); |
| 371 | ut_asserteq(1, pos - buffer); |
| 372 | ut_asserteq((u16)'@', buffer[0]); |
| 373 | ut_assert(!buffer[1]); |
| 374 | |
| 375 | /* Hamster face, translates to two words */ |
| 376 | pos = buffer; |
| 377 | ut_assert(!utf16_put(0x1f439, &pos)); |
| 378 | ut_asserteq(2, pos - buffer); |
| 379 | ut_asserteq((u16)0xd83d, buffer[0]); |
| 380 | ut_asserteq((u16)0xdc39, buffer[1]); |
| 381 | ut_assert(!buffer[2]); |
| 382 | |
| 383 | /* Illegal code */ |
| 384 | pos = buffer; |
| 385 | ut_asserteq(-1, utf16_put(0xd888, &pos)); |
| 386 | |
| 387 | return 0; |
| 388 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 389 | UNICODE_TEST(unicode_test_utf16_put); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 390 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 391 | static int unicode_test_utf16_strnlen(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 392 | { |
| 393 | ut_asserteq(3, utf16_strnlen(c1, 3)); |
| 394 | ut_asserteq(6, utf16_strnlen(c1, 13)); |
| 395 | ut_asserteq(6, utf16_strnlen(c2, 6)); |
| 396 | ut_asserteq(2, utf16_strnlen(c3, 2)); |
| 397 | ut_asserteq(2, utf16_strnlen(c4, 2)); |
| 398 | ut_asserteq(3, utf16_strnlen(c4, 3)); |
| 399 | |
| 400 | /* illegal utf-16 word sequences */ |
| 401 | ut_asserteq(4, utf16_strnlen(i1, 16)); |
| 402 | ut_asserteq(4, utf16_strnlen(i2, 16)); |
| 403 | ut_asserteq(3, utf16_strnlen(i3, 16)); |
| 404 | |
| 405 | return 0; |
| 406 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 407 | UNICODE_TEST(unicode_test_utf16_strnlen); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 408 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 409 | static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 410 | { |
| 411 | ut_asserteq(6, utf16_utf8_strlen(c1)); |
| 412 | ut_asserteq(9, utf16_utf8_strlen(c2)); |
| 413 | ut_asserteq(9, utf16_utf8_strlen(c3)); |
| 414 | ut_asserteq(12, utf16_utf8_strlen(c4)); |
| 415 | |
| 416 | /* illegal utf-16 word sequences */ |
| 417 | ut_asserteq(4, utf16_utf8_strlen(i1)); |
| 418 | ut_asserteq(4, utf16_utf8_strlen(i2)); |
| 419 | ut_asserteq(3, utf16_utf8_strlen(i3)); |
| 420 | |
| 421 | return 0; |
| 422 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 423 | UNICODE_TEST(unicode_test_utf16_utf8_strlen); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 424 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 425 | static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 426 | { |
| 427 | ut_asserteq(3, utf16_utf8_strnlen(c1, 3)); |
| 428 | ut_asserteq(6, utf16_utf8_strnlen(c1, 13)); |
| 429 | ut_asserteq(7, utf16_utf8_strnlen(c2, 6)); |
| 430 | ut_asserteq(6, utf16_utf8_strnlen(c3, 2)); |
| 431 | ut_asserteq(8, utf16_utf8_strnlen(c4, 2)); |
| 432 | ut_asserteq(12, utf16_utf8_strnlen(c4, 3)); |
| 433 | return 0; |
| 434 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 435 | UNICODE_TEST(unicode_test_utf16_utf8_strnlen); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 436 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 437 | static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 438 | { |
| 439 | char buf[16]; |
| 440 | char *pos; |
| 441 | |
| 442 | pos = buf; |
| 443 | utf16_utf8_strcpy(&pos, c1); |
| 444 | ut_asserteq(6, pos - buf); |
| 445 | ut_asserteq_str(d1, buf); |
| 446 | |
| 447 | pos = buf; |
| 448 | utf16_utf8_strcpy(&pos, c2); |
| 449 | ut_asserteq(9, pos - buf); |
| 450 | ut_asserteq_str(d2, buf); |
| 451 | |
| 452 | pos = buf; |
| 453 | utf16_utf8_strcpy(&pos, c3); |
| 454 | ut_asserteq(9, pos - buf); |
| 455 | ut_asserteq_str(d3, buf); |
| 456 | |
| 457 | pos = buf; |
| 458 | utf16_utf8_strcpy(&pos, c4); |
| 459 | ut_asserteq(12, pos - buf); |
| 460 | ut_asserteq_str(d4, buf); |
| 461 | |
| 462 | /* Illegal utf-16 strings */ |
| 463 | pos = buf; |
| 464 | utf16_utf8_strcpy(&pos, i1); |
| 465 | ut_asserteq(4, pos - buf); |
| 466 | ut_asserteq_str("i1?l", buf); |
| 467 | |
| 468 | pos = buf; |
| 469 | utf16_utf8_strcpy(&pos, i2); |
| 470 | ut_asserteq(4, pos - buf); |
| 471 | ut_asserteq_str("i2?l", buf); |
| 472 | |
| 473 | pos = buf; |
| 474 | utf16_utf8_strcpy(&pos, i3); |
| 475 | ut_asserteq(3, pos - buf); |
| 476 | ut_asserteq_str("i3?", buf); |
| 477 | |
| 478 | return 0; |
| 479 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 480 | UNICODE_TEST(unicode_test_utf16_utf8_strcpy); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 481 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 482 | static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts) |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 483 | { |
| 484 | char buf[16]; |
| 485 | char *pos; |
| 486 | |
| 487 | pos = buf; |
| 488 | memset(buf, 0, sizeof(buf)); |
| 489 | utf16_utf8_strncpy(&pos, c1, 4); |
| 490 | ut_asserteq(4, pos - buf); |
| 491 | ut_assert(!buf[4]); |
| 492 | ut_assert(!strncmp(buf, d1, 4)); |
| 493 | |
| 494 | pos = buf; |
| 495 | memset(buf, 0, sizeof(buf)); |
| 496 | utf16_utf8_strncpy(&pos, c2, 10); |
| 497 | ut_asserteq(9, pos - buf); |
| 498 | ut_assert(buf[4]); |
| 499 | ut_assert(!strncmp(buf, d2, SIZE_MAX)); |
| 500 | |
| 501 | pos = buf; |
| 502 | memset(buf, 0, sizeof(buf)); |
| 503 | utf16_utf8_strncpy(&pos, c3, 2); |
| 504 | ut_asserteq(6, pos - buf); |
| 505 | ut_assert(!buf[6]); |
| 506 | ut_assert(!strncmp(buf, d3, 6)); |
| 507 | |
| 508 | pos = buf; |
| 509 | memset(buf, 0, sizeof(buf)); |
| 510 | utf16_utf8_strncpy(&pos, c4, 2); |
| 511 | ut_asserteq(8, pos - buf); |
| 512 | ut_assert(!buf[8]); |
| 513 | ut_assert(!strncmp(buf, d4, 8)); |
| 514 | |
| 515 | pos = buf; |
| 516 | memset(buf, 0, sizeof(buf)); |
| 517 | utf16_utf8_strncpy(&pos, c4, 10); |
| 518 | ut_asserteq(12, pos - buf); |
| 519 | ut_assert(buf[5]); |
| 520 | ut_assert(!strncmp(buf, d4, SIZE_MAX)); |
| 521 | |
| 522 | return 0; |
| 523 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 524 | UNICODE_TEST(unicode_test_utf16_utf8_strncpy); |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 525 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 526 | static int unicode_test_utf_to_lower(struct unit_test_state *uts) |
Heinrich Schuchardt | 15552f7 | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 527 | { |
| 528 | ut_asserteq('@', utf_to_lower('@')); |
| 529 | ut_asserteq('a', utf_to_lower('A')); |
| 530 | ut_asserteq('z', utf_to_lower('Z')); |
| 531 | ut_asserteq('[', utf_to_lower('[')); |
| 532 | ut_asserteq('m', utf_to_lower('m')); |
| 533 | /* Latin letter O with diaresis (umlaut) */ |
| 534 | ut_asserteq(0x00f6, utf_to_lower(0x00d6)); |
| 535 | #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION |
| 536 | /* Cyrillic letter I*/ |
| 537 | ut_asserteq(0x0438, utf_to_lower(0x0418)); |
| 538 | #endif |
| 539 | return 0; |
| 540 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 541 | UNICODE_TEST(unicode_test_utf_to_lower); |
Heinrich Schuchardt | 15552f7 | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 542 | |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 543 | static int unicode_test_utf_to_upper(struct unit_test_state *uts) |
Heinrich Schuchardt | 15552f7 | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 544 | { |
| 545 | ut_asserteq('`', utf_to_upper('`')); |
| 546 | ut_asserteq('A', utf_to_upper('a')); |
| 547 | ut_asserteq('Z', utf_to_upper('z')); |
| 548 | ut_asserteq('{', utf_to_upper('{')); |
| 549 | ut_asserteq('M', utf_to_upper('M')); |
| 550 | /* Latin letter O with diaresis (umlaut) */ |
| 551 | ut_asserteq(0x00d6, utf_to_upper(0x00f6)); |
| 552 | #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION |
| 553 | /* Cyrillic letter I */ |
| 554 | ut_asserteq(0x0418, utf_to_upper(0x0438)); |
| 555 | #endif |
| 556 | return 0; |
| 557 | } |
Heinrich Schuchardt | 46da93c | 2019-02-15 23:12:50 +0100 | [diff] [blame] | 558 | UNICODE_TEST(unicode_test_utf_to_upper); |
Heinrich Schuchardt | 15552f7 | 2018-09-04 19:34:57 +0200 | [diff] [blame] | 559 | |
Heinrich Schuchardt | b8b6c81 | 2018-08-31 21:31:28 +0200 | [diff] [blame] | 560 | int do_ut_unicode(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 561 | { |
| 562 | struct unit_test *tests = ll_entry_start(struct unit_test, unicode_test); |
| 563 | const int n_ents = ll_entry_count(struct unit_test, unicode_test); |
| 564 | |
| 565 | return cmd_ut_category("Unicode", tests, n_ents, argc, argv); |
| 566 | } |