Blame view

test/unicode_ut.c 14.9 KB
f11a164b5   Heinrich Schuchardt   test: unit tests ...
1
2
3
4
5
6
7
8
9
10
11
  // SPDX-License-Identifier: GPL-2.0+
  /*
   * Unit tests for Unicode functions
   *
   * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
   */
  
  #include <common.h>
  #include <charset.h>
  #include <command.h>
  #include <errno.h>
336d4615f   Simon Glass   dm: core: Create ...
12
  #include <malloc.h>
f11a164b5   Heinrich Schuchardt   test: unit tests ...
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  #include <test/test.h>
  #include <test/suites.h>
  #include <test/ut.h>
  
  /* Linker list entry for a Unicode test */
  #define UNICODE_TEST(_name) UNIT_TEST(_name, 0, unicode_test)
  
  /* Constants c1-c4 and d1-d4 encode the same letters */
  
  /* Six characters translating to one utf-8 byte each. */
  static const u16 c1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
  /* One character translating to two utf-8 bytes */
  static const u16 c2[] = {0x6b, 0x61, 0x66, 0x62, 0xe1, 0x74, 0x75, 0x72, 0x00};
  /* Three characters translating to three utf-8 bytes each */
  static const u16 c3[] = {0x6f5c, 0x6c34, 0x8266, 0x00};
  /* Three letters translating to four utf-8 bytes each */
  static const u16 c4[] = {0xd801, 0xdc8d, 0xd801, 0xdc96, 0xd801, 0xdc87,
  			 0x0000};
  
  /* Illegal utf-16 strings */
  static const u16 i1[] = {0x69, 0x31, 0xdc87, 0x6c, 0x00};
  static const u16 i2[] = {0x69, 0x32, 0xd801, 0xd801, 0x6c, 0x00};
  static const u16 i3[] = {0x69, 0x33, 0xd801, 0x00};
  
  /* Six characters translating to one utf-16 word each. */
  static const char d1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
  /* Eight characters translating to one utf-16 word each */
  static const char d2[] = {0x6b, 0x61, 0x66, 0x62, 0xc3, 0xa1, 0x74, 0x75,
  			  0x72, 0x00};
  /* Three characters translating to one utf-16 word each */
  static const char d3[] = {0xe6, 0xbd, 0x9c, 0xe6, 0xb0, 0xb4, 0xe8, 0x89,
  			  0xa6, 0x00};
  /* Three letters translating to two utf-16 word each */
  static const char d4[] = {0xf0, 0x90, 0x92, 0x8d, 0xf0, 0x90, 0x92, 0x96,
  			  0xf0, 0x90, 0x92, 0x87, 0x00};
  
  /* Illegal utf-8 strings */
  static const char j1[] = {0x6a, 0x31, 0xa1, 0x6c, 0x00};
  static const char j2[] = {0x6a, 0x32, 0xc3, 0xc3, 0x6c, 0x00};
  static const char j3[] = {0x6a, 0x33, 0xf0, 0x90, 0xf0, 0x00};
02b31dce9   Heinrich Schuchardt   test: unit test f...
53
54
55
56
57
58
59
60
61
  static int unicode_test_u16_strlen(struct unit_test_state *uts)
  {
  	ut_asserteq(6, u16_strlen(c1));
  	ut_asserteq(8, u16_strlen(c2));
  	ut_asserteq(3, u16_strlen(c3));
  	ut_asserteq(6, u16_strlen(c4));
  	return 0;
  }
  UNICODE_TEST(unicode_test_u16_strlen);
bc19681ac   Heinrich Schuchardt   test: adjust name...
62
  static int unicode_test_u16_strdup(struct unit_test_state *uts)
abb93cb0e   Heinrich Schuchardt   test: tests for u...
63
64
65
66
67
68
69
70
  {
  	u16 *copy = u16_strdup(c4);
  
  	ut_assert(copy != c4);
  	ut_assert(!memcmp(copy, c4, sizeof(c4)));
  	free(copy);
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
71
  UNICODE_TEST(unicode_test_u16_strdup);
abb93cb0e   Heinrich Schuchardt   test: tests for u...
72

bc19681ac   Heinrich Schuchardt   test: adjust name...
73
  static int unicode_test_u16_strcpy(struct unit_test_state *uts)
abb93cb0e   Heinrich Schuchardt   test: tests for u...
74
75
76
77
78
79
80
81
82
  {
  	u16 *r;
  	u16 copy[10];
  
  	r = u16_strcpy(copy, c1);
  	ut_assert(r == copy);
  	ut_assert(!memcmp(copy, c1, sizeof(c1)));
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
83
  UNICODE_TEST(unicode_test_u16_strcpy);
abb93cb0e   Heinrich Schuchardt   test: tests for u...
84

fbba2f677   Heinrich Schuchardt   test: test printi...
85
86
  /* U-Boot uses UTF-16 strings in the EFI context only. */
  #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
bc19681ac   Heinrich Schuchardt   test: adjust name...
87
  static int unicode_test_string16(struct unit_test_state *uts)
fbba2f677   Heinrich Schuchardt   test: test printi...
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  {
  	char buf[20];
  
  	/* Test length and precision */
  	memset(buf, 0xff, sizeof(buf));
  	sprintf(buf, "%8.6ls", c2);
  	ut_asserteq(' ', buf[1]);
  	ut_assert(!strncmp(&buf[2], d2, 7));
  	ut_assert(!buf[9]);
  
  	memset(buf, 0xff, sizeof(buf));
  	sprintf(buf, "%8.6ls", c4);
  	ut_asserteq(' ', buf[4]);
  	ut_assert(!strncmp(&buf[5], d4, 12));
  	ut_assert(!buf[17]);
  
  	memset(buf, 0xff, sizeof(buf));
  	sprintf(buf, "%-8.2ls", c4);
  	ut_asserteq(' ', buf[8]);
  	ut_assert(!strncmp(buf, d4, 8));
  	ut_assert(!buf[14]);
  
  	/* Test handling of illegal utf-16 sequences */
  	memset(buf, 0xff, sizeof(buf));
  	sprintf(buf, "%ls", i1);
  	ut_asserteq_str("i1?l", buf);
  
  	memset(buf, 0xff, sizeof(buf));
  	sprintf(buf, "%ls", i2);
  	ut_asserteq_str("i2?l", buf);
  
  	memset(buf, 0xff, sizeof(buf));
  	sprintf(buf, "%ls", i3);
  	ut_asserteq_str("i3?", buf);
  
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
125
  UNICODE_TEST(unicode_test_string16);
fbba2f677   Heinrich Schuchardt   test: test printi...
126
  #endif
bc19681ac   Heinrich Schuchardt   test: adjust name...
127
  static int unicode_test_utf8_get(struct unit_test_state *uts)
f11a164b5   Heinrich Schuchardt   test: unit tests ...
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  {
  	const char *s;
  	s32 code;
  	int i;
  
  	/* Check characters less than 0x800 */
  	s = d2;
  	for (i = 0; i < 8; ++i) {
  		code = utf8_get((const char **)&s);
  		/* c2 is the utf-8 encoding of d2 */
  		ut_asserteq(c2[i], code);
  		if (!code)
  			break;
  	}
  	ut_asserteq_ptr(s, d2 + 9)
  
  	/* Check characters less than 0x10000 */
  	s = d3;
  	for (i = 0; i < 4; ++i) {
  		code = utf8_get((const char **)&s);
  		/* c3 is the utf-8 encoding of d3 */
  		ut_asserteq(c3[i], code);
  		if (!code)
  			break;
  	}
  	ut_asserteq_ptr(s, d3 + 9)
  
  	/* Check character greater 0xffff */
  	s = d4;
  	code = utf8_get((const char **)&s);
  	ut_asserteq(0x0001048d, code);
  	ut_asserteq_ptr(s, d4 + 4);
  
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
163
  UNICODE_TEST(unicode_test_utf8_get);
f11a164b5   Heinrich Schuchardt   test: unit tests ...
164

bc19681ac   Heinrich Schuchardt   test: adjust name...
165
  static int unicode_test_utf8_put(struct unit_test_state *uts)
f11a164b5   Heinrich Schuchardt   test: unit tests ...
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  {
  	char buffer[8] = { 0, };
  	char *pos;
  
  	/* Commercial at, translates to one character */
  	pos = buffer;
  	ut_assert(!utf8_put('@', &pos))
  	ut_asserteq(1, pos - buffer);
  	ut_asserteq('@', buffer[0]);
  	ut_assert(!buffer[1]);
  
  	/* Latin letter G with acute, translates to two charactes */
  	pos = buffer;
  	ut_assert(!utf8_put(0x1f4, &pos));
  	ut_asserteq(2, pos - buffer);
  	ut_asserteq_str("\xc7\xb4", buffer);
  
  	/* Tagalog letter i, translates to three characters */
  	pos = buffer;
  	ut_assert(!utf8_put(0x1701, &pos));
  	ut_asserteq(3, pos - buffer);
  	ut_asserteq_str("\xe1\x9c\x81", buffer);
  
  	/* Hamster face, translates to four characters */
  	pos = buffer;
  	ut_assert(!utf8_put(0x1f439, &pos));
  	ut_asserteq(4, pos - buffer);
  	ut_asserteq_str("\xf0\x9f\x90\xb9", buffer);
  
  	/* Illegal code */
  	pos = buffer;
  	ut_asserteq(-1, utf8_put(0xd888, &pos));
  
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
201
  UNICODE_TEST(unicode_test_utf8_put);
f11a164b5   Heinrich Schuchardt   test: unit tests ...
202

bc19681ac   Heinrich Schuchardt   test: adjust name...
203
  static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts)
f11a164b5   Heinrich Schuchardt   test: unit tests ...
204
205
206
207
208
209
210
211
  {
  	ut_asserteq(6, utf8_utf16_strlen(d1));
  	ut_asserteq(8, utf8_utf16_strlen(d2));
  	ut_asserteq(3, utf8_utf16_strlen(d3));
  	ut_asserteq(6, utf8_utf16_strlen(d4));
  
  	/* illegal utf-8 sequences */
  	ut_asserteq(4, utf8_utf16_strlen(j1));
35cbb796a   Heinrich Schuchardt   efi_loader: suppo...
212
  	ut_asserteq(4, utf8_utf16_strlen(j2));
f11a164b5   Heinrich Schuchardt   test: unit tests ...
213
214
215
216
  	ut_asserteq(3, utf8_utf16_strlen(j3));
  
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
217
  UNICODE_TEST(unicode_test_utf8_utf16_strlen);
f11a164b5   Heinrich Schuchardt   test: unit tests ...
218

bc19681ac   Heinrich Schuchardt   test: adjust name...
219
  static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts)
f11a164b5   Heinrich Schuchardt   test: unit tests ...
220
221
222
223
224
225
226
227
228
229
  {
  	ut_asserteq(3, utf8_utf16_strnlen(d1, 3));
  	ut_asserteq(6, utf8_utf16_strnlen(d1, 13));
  	ut_asserteq(6, utf8_utf16_strnlen(d2, 6));
  	ut_asserteq(2, utf8_utf16_strnlen(d3, 2));
  	ut_asserteq(4, utf8_utf16_strnlen(d4, 2));
  	ut_asserteq(6, utf8_utf16_strnlen(d4, 3));
  
  	/* illegal utf-8 sequences */
  	ut_asserteq(4, utf8_utf16_strnlen(j1, 16));
35cbb796a   Heinrich Schuchardt   efi_loader: suppo...
230
  	ut_asserteq(4, utf8_utf16_strnlen(j2, 16));
f11a164b5   Heinrich Schuchardt   test: unit tests ...
231
232
233
234
  	ut_asserteq(3, utf8_utf16_strnlen(j3, 16));
  
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
235
  UNICODE_TEST(unicode_test_utf8_utf16_strnlen);
f11a164b5   Heinrich Schuchardt   test: unit tests ...
236
237
238
239
240
241
242
243
244
  
  /**
   * ut_u16_strcmp() - Compare to u16 strings.
   *
   * @a1:		first string
   * @a2:		second string
   * @count:	number of u16 to compare
   * Return:	-1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
   */
bc19681ac   Heinrich Schuchardt   test: adjust name...
245
  static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count)
f11a164b5   Heinrich Schuchardt   test: unit tests ...
246
247
248
249
250
251
252
253
254
  {
  	for (; (*a1 || *a2) && count; ++a1, ++a2, --count) {
  		if (*a1 < *a2)
  			return -1;
  		if (*a1 > *a2)
  			return 1;
  	}
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
255
  static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts)
f11a164b5   Heinrich Schuchardt   test: unit tests ...
256
257
258
259
260
261
262
  {
  	u16 buf[16];
  	u16 *pos;
  
  	pos = buf;
  	utf8_utf16_strcpy(&pos, d1);
  	ut_asserteq(6, pos - buf);
bc19681ac   Heinrich Schuchardt   test: adjust name...
263
  	ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX));
f11a164b5   Heinrich Schuchardt   test: unit tests ...
264
265
266
267
  
  	pos = buf;
  	utf8_utf16_strcpy(&pos, d2);
  	ut_asserteq(8, pos - buf);
bc19681ac   Heinrich Schuchardt   test: adjust name...
268
  	ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
f11a164b5   Heinrich Schuchardt   test: unit tests ...
269
270
271
272
  
  	pos = buf;
  	utf8_utf16_strcpy(&pos, d3);
  	ut_asserteq(3, pos - buf);
bc19681ac   Heinrich Schuchardt   test: adjust name...
273
  	ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX));
f11a164b5   Heinrich Schuchardt   test: unit tests ...
274
275
276
277
  
  	pos = buf;
  	utf8_utf16_strcpy(&pos, d4);
  	ut_asserteq(6, pos - buf);
bc19681ac   Heinrich Schuchardt   test: adjust name...
278
  	ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
f11a164b5   Heinrich Schuchardt   test: unit tests ...
279
280
281
282
283
  
  	/* Illegal utf-8 strings */
  	pos = buf;
  	utf8_utf16_strcpy(&pos, j1);
  	ut_asserteq(4, pos - buf);
bc19681ac   Heinrich Schuchardt   test: adjust name...
284
  	ut_assert(!unicode_test_u16_strcmp(buf, L"j1?l", SIZE_MAX));
f11a164b5   Heinrich Schuchardt   test: unit tests ...
285
286
287
  
  	pos = buf;
  	utf8_utf16_strcpy(&pos, j2);
35cbb796a   Heinrich Schuchardt   efi_loader: suppo...
288
  	ut_asserteq(4, pos - buf);
bc19681ac   Heinrich Schuchardt   test: adjust name...
289
  	ut_assert(!unicode_test_u16_strcmp(buf, L"j2?l", SIZE_MAX));
f11a164b5   Heinrich Schuchardt   test: unit tests ...
290
291
292
293
  
  	pos = buf;
  	utf8_utf16_strcpy(&pos, j3);
  	ut_asserteq(3, pos - buf);
bc19681ac   Heinrich Schuchardt   test: adjust name...
294
  	ut_assert(!unicode_test_u16_strcmp(buf, L"j3?", SIZE_MAX));
f11a164b5   Heinrich Schuchardt   test: unit tests ...
295
296
297
  
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
298
  UNICODE_TEST(unicode_test_utf8_utf16_strcpy);
f11a164b5   Heinrich Schuchardt   test: unit tests ...
299

bc19681ac   Heinrich Schuchardt   test: adjust name...
300
  static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts)
f11a164b5   Heinrich Schuchardt   test: unit tests ...
301
302
303
304
305
306
307
308
309
  {
  	u16 buf[16];
  	u16 *pos;
  
  	pos = buf;
  	memset(buf, 0, sizeof(buf));
  	utf8_utf16_strncpy(&pos, d1, 4);
  	ut_asserteq(4, pos - buf);
  	ut_assert(!buf[4]);
bc19681ac   Heinrich Schuchardt   test: adjust name...
310
  	ut_assert(!unicode_test_u16_strcmp(buf, c1, 4));
f11a164b5   Heinrich Schuchardt   test: unit tests ...
311
312
313
314
315
316
  
  	pos = buf;
  	memset(buf, 0, sizeof(buf));
  	utf8_utf16_strncpy(&pos, d2, 10);
  	ut_asserteq(8, pos - buf);
  	ut_assert(buf[4]);
bc19681ac   Heinrich Schuchardt   test: adjust name...
317
  	ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
f11a164b5   Heinrich Schuchardt   test: unit tests ...
318
319
320
321
322
323
  
  	pos = buf;
  	memset(buf, 0, sizeof(buf));
  	utf8_utf16_strncpy(&pos, d3, 2);
  	ut_asserteq(2, pos - buf);
  	ut_assert(!buf[2]);
bc19681ac   Heinrich Schuchardt   test: adjust name...
324
  	ut_assert(!unicode_test_u16_strcmp(buf, c3, 2));
f11a164b5   Heinrich Schuchardt   test: unit tests ...
325
326
327
328
329
330
  
  	pos = buf;
  	memset(buf, 0, sizeof(buf));
  	utf8_utf16_strncpy(&pos, d4, 2);
  	ut_asserteq(4, pos - buf);
  	ut_assert(!buf[4]);
bc19681ac   Heinrich Schuchardt   test: adjust name...
331
  	ut_assert(!unicode_test_u16_strcmp(buf, c4, 4));
f11a164b5   Heinrich Schuchardt   test: unit tests ...
332
333
334
335
336
337
  
  	pos = buf;
  	memset(buf, 0, sizeof(buf));
  	utf8_utf16_strncpy(&pos, d4, 10);
  	ut_asserteq(6, pos - buf);
  	ut_assert(buf[5]);
bc19681ac   Heinrich Schuchardt   test: adjust name...
338
  	ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
f11a164b5   Heinrich Schuchardt   test: unit tests ...
339
340
341
  
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
342
  UNICODE_TEST(unicode_test_utf8_utf16_strncpy);
f11a164b5   Heinrich Schuchardt   test: unit tests ...
343

bc19681ac   Heinrich Schuchardt   test: adjust name...
344
  static int unicode_test_utf16_get(struct unit_test_state *uts)
f11a164b5   Heinrich Schuchardt   test: unit tests ...
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
  {
  	const u16 *s;
  	s32 code;
  	int i;
  
  	/* Check characters less than 0x10000 */
  	s = c2;
  	for (i = 0; i < 9; ++i) {
  		code = utf16_get((const u16 **)&s);
  		ut_asserteq(c2[i], code);
  		if (!code)
  			break;
  	}
  	ut_asserteq_ptr(c2 + 8, s);
  
  	/* Check character greater 0xffff */
  	s = c4;
  	code = utf16_get((const u16 **)&s);
  	ut_asserteq(0x0001048d, code);
  	ut_asserteq_ptr(c4 + 2, s);
  
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
368
  UNICODE_TEST(unicode_test_utf16_get);
f11a164b5   Heinrich Schuchardt   test: unit tests ...
369

bc19681ac   Heinrich Schuchardt   test: adjust name...
370
  static int unicode_test_utf16_put(struct unit_test_state *uts)
f11a164b5   Heinrich Schuchardt   test: unit tests ...
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
  {
  	u16 buffer[4] = { 0, };
  	u16 *pos;
  
  	/* Commercial at, translates to one word */
  	pos = buffer;
  	ut_assert(!utf16_put('@', &pos));
  	ut_asserteq(1, pos - buffer);
  	ut_asserteq((u16)'@', buffer[0]);
  	ut_assert(!buffer[1]);
  
  	/* Hamster face, translates to two words */
  	pos = buffer;
  	ut_assert(!utf16_put(0x1f439, &pos));
  	ut_asserteq(2, pos - buffer);
  	ut_asserteq((u16)0xd83d, buffer[0]);
  	ut_asserteq((u16)0xdc39, buffer[1]);
  	ut_assert(!buffer[2]);
  
  	/* Illegal code */
  	pos = buffer;
  	ut_asserteq(-1, utf16_put(0xd888, &pos));
  
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
396
  UNICODE_TEST(unicode_test_utf16_put);
f11a164b5   Heinrich Schuchardt   test: unit tests ...
397

bc19681ac   Heinrich Schuchardt   test: adjust name...
398
  static int unicode_test_utf16_strnlen(struct unit_test_state *uts)
f11a164b5   Heinrich Schuchardt   test: unit tests ...
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
  {
  	ut_asserteq(3, utf16_strnlen(c1, 3));
  	ut_asserteq(6, utf16_strnlen(c1, 13));
  	ut_asserteq(6, utf16_strnlen(c2, 6));
  	ut_asserteq(2, utf16_strnlen(c3, 2));
  	ut_asserteq(2, utf16_strnlen(c4, 2));
  	ut_asserteq(3, utf16_strnlen(c4, 3));
  
  	/* illegal utf-16 word sequences */
  	ut_asserteq(4, utf16_strnlen(i1, 16));
  	ut_asserteq(4, utf16_strnlen(i2, 16));
  	ut_asserteq(3, utf16_strnlen(i3, 16));
  
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
414
  UNICODE_TEST(unicode_test_utf16_strnlen);
f11a164b5   Heinrich Schuchardt   test: unit tests ...
415

bc19681ac   Heinrich Schuchardt   test: adjust name...
416
  static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts)
f11a164b5   Heinrich Schuchardt   test: unit tests ...
417
418
419
420
421
422
423
424
425
426
427
428
429
  {
  	ut_asserteq(6, utf16_utf8_strlen(c1));
  	ut_asserteq(9, utf16_utf8_strlen(c2));
  	ut_asserteq(9, utf16_utf8_strlen(c3));
  	ut_asserteq(12, utf16_utf8_strlen(c4));
  
  	/* illegal utf-16 word sequences */
  	ut_asserteq(4, utf16_utf8_strlen(i1));
  	ut_asserteq(4, utf16_utf8_strlen(i2));
  	ut_asserteq(3, utf16_utf8_strlen(i3));
  
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
430
  UNICODE_TEST(unicode_test_utf16_utf8_strlen);
f11a164b5   Heinrich Schuchardt   test: unit tests ...
431

bc19681ac   Heinrich Schuchardt   test: adjust name...
432
  static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts)
f11a164b5   Heinrich Schuchardt   test: unit tests ...
433
434
435
436
437
438
439
440
441
  {
  	ut_asserteq(3, utf16_utf8_strnlen(c1, 3));
  	ut_asserteq(6, utf16_utf8_strnlen(c1, 13));
  	ut_asserteq(7, utf16_utf8_strnlen(c2, 6));
  	ut_asserteq(6, utf16_utf8_strnlen(c3, 2));
  	ut_asserteq(8, utf16_utf8_strnlen(c4, 2));
  	ut_asserteq(12, utf16_utf8_strnlen(c4, 3));
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
442
  UNICODE_TEST(unicode_test_utf16_utf8_strnlen);
f11a164b5   Heinrich Schuchardt   test: unit tests ...
443

bc19681ac   Heinrich Schuchardt   test: adjust name...
444
  static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts)
f11a164b5   Heinrich Schuchardt   test: unit tests ...
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
  {
  	char buf[16];
  	char *pos;
  
  	pos = buf;
  	utf16_utf8_strcpy(&pos, c1);
  	ut_asserteq(6, pos - buf);
  	ut_asserteq_str(d1, buf);
  
  	pos = buf;
  	utf16_utf8_strcpy(&pos, c2);
  	ut_asserteq(9, pos - buf);
  	ut_asserteq_str(d2, buf);
  
  	pos = buf;
  	utf16_utf8_strcpy(&pos, c3);
  	ut_asserteq(9, pos - buf);
  	ut_asserteq_str(d3, buf);
  
  	pos = buf;
  	utf16_utf8_strcpy(&pos, c4);
  	ut_asserteq(12, pos - buf);
  	ut_asserteq_str(d4, buf);
  
  	/* Illegal utf-16 strings */
  	pos = buf;
  	utf16_utf8_strcpy(&pos, i1);
  	ut_asserteq(4, pos - buf);
  	ut_asserteq_str("i1?l", buf);
  
  	pos = buf;
  	utf16_utf8_strcpy(&pos, i2);
  	ut_asserteq(4, pos - buf);
  	ut_asserteq_str("i2?l", buf);
  
  	pos = buf;
  	utf16_utf8_strcpy(&pos, i3);
  	ut_asserteq(3, pos - buf);
  	ut_asserteq_str("i3?", buf);
  
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
487
  UNICODE_TEST(unicode_test_utf16_utf8_strcpy);
f11a164b5   Heinrich Schuchardt   test: unit tests ...
488

bc19681ac   Heinrich Schuchardt   test: adjust name...
489
  static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts)
f11a164b5   Heinrich Schuchardt   test: unit tests ...
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
  {
  	char buf[16];
  	char *pos;
  
  	pos = buf;
  	memset(buf, 0, sizeof(buf));
  	utf16_utf8_strncpy(&pos, c1, 4);
  	ut_asserteq(4, pos - buf);
  	ut_assert(!buf[4]);
  	ut_assert(!strncmp(buf, d1, 4));
  
  	pos = buf;
  	memset(buf, 0, sizeof(buf));
  	utf16_utf8_strncpy(&pos, c2, 10);
  	ut_asserteq(9, pos - buf);
  	ut_assert(buf[4]);
  	ut_assert(!strncmp(buf, d2, SIZE_MAX));
  
  	pos = buf;
  	memset(buf, 0, sizeof(buf));
  	utf16_utf8_strncpy(&pos, c3, 2);
  	ut_asserteq(6, pos - buf);
  	ut_assert(!buf[6]);
  	ut_assert(!strncmp(buf, d3, 6));
  
  	pos = buf;
  	memset(buf, 0, sizeof(buf));
  	utf16_utf8_strncpy(&pos, c4, 2);
  	ut_asserteq(8, pos - buf);
  	ut_assert(!buf[8]);
  	ut_assert(!strncmp(buf, d4, 8));
  
  	pos = buf;
  	memset(buf, 0, sizeof(buf));
  	utf16_utf8_strncpy(&pos, c4, 10);
  	ut_asserteq(12, pos - buf);
  	ut_assert(buf[5]);
  	ut_assert(!strncmp(buf, d4, SIZE_MAX));
  
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
531
  UNICODE_TEST(unicode_test_utf16_utf8_strncpy);
f11a164b5   Heinrich Schuchardt   test: unit tests ...
532

bc19681ac   Heinrich Schuchardt   test: adjust name...
533
  static int unicode_test_utf_to_lower(struct unit_test_state *uts)
1a1012a1c   Heinrich Schuchardt   test: tests for u...
534
535
536
537
538
539
540
541
542
543
544
545
546
547
  {
  	ut_asserteq('@', utf_to_lower('@'));
  	ut_asserteq('a', utf_to_lower('A'));
  	ut_asserteq('z', utf_to_lower('Z'));
  	ut_asserteq('[', utf_to_lower('['));
  	ut_asserteq('m', utf_to_lower('m'));
  	/* Latin letter O with diaresis (umlaut) */
  	ut_asserteq(0x00f6, utf_to_lower(0x00d6));
  #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
  	/* Cyrillic letter I*/
  	ut_asserteq(0x0438, utf_to_lower(0x0418));
  #endif
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
548
  UNICODE_TEST(unicode_test_utf_to_lower);
1a1012a1c   Heinrich Schuchardt   test: tests for u...
549

bc19681ac   Heinrich Schuchardt   test: adjust name...
550
  static int unicode_test_utf_to_upper(struct unit_test_state *uts)
1a1012a1c   Heinrich Schuchardt   test: tests for u...
551
552
553
554
555
556
557
558
559
560
561
562
563
564
  {
  	ut_asserteq('`', utf_to_upper('`'));
  	ut_asserteq('A', utf_to_upper('a'));
  	ut_asserteq('Z', utf_to_upper('z'));
  	ut_asserteq('{', utf_to_upper('{'));
  	ut_asserteq('M', utf_to_upper('M'));
  	/* Latin letter O with diaresis (umlaut) */
  	ut_asserteq(0x00d6, utf_to_upper(0x00f6));
  #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
  	/* Cyrillic letter I */
  	ut_asserteq(0x0418, utf_to_upper(0x0438));
  #endif
  	return 0;
  }
bc19681ac   Heinrich Schuchardt   test: adjust name...
565
  UNICODE_TEST(unicode_test_utf_to_upper);
1a1012a1c   Heinrich Schuchardt   test: tests for u...
566

79907a4f8   AKASHI Takahiro   test: add tests f...
567
568
569
570
571
572
573
574
575
576
577
578
  static int unicode_test_u16_strncmp(struct unit_test_state *uts)
  {
  	ut_assert(u16_strncmp(L"abc", L"abc", 3) == 0);
  	ut_assert(u16_strncmp(L"abcdef", L"abcghi", 3) == 0);
  	ut_assert(u16_strncmp(L"abcdef", L"abcghi", 6) < 0);
  	ut_assert(u16_strncmp(L"abcghi", L"abcdef", 6) > 0);
  	ut_assert(u16_strcmp(L"abc", L"abc") == 0);
  	ut_assert(u16_strcmp(L"abcdef", L"deghi") < 0);
  	ut_assert(u16_strcmp(L"deghi", L"abcdef") > 0);
  	return 0;
  }
  UNICODE_TEST(unicode_test_u16_strncmp);
f11a164b5   Heinrich Schuchardt   test: unit tests ...
579
580
581
582
  int do_ut_unicode(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  {
  	struct unit_test *tests = ll_entry_start(struct unit_test, unicode_test);
  	const int n_ents = ll_entry_count(struct unit_test, unicode_test);
4ad4edfe7   Philippe Reynes   cmd_ut: add a par...
583
584
  	return cmd_ut_category("Unicode", "unicode_test_",
  			       tests, n_ents, argc, argv);
f11a164b5   Heinrich Schuchardt   test: unit tests ...
585
  }