1
2
3
4
5
6
7
8
9
10
11
12
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
125
126
127
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
163
164
165
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
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
487
488
489
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
|
/*
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibTest/TestCase.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <wchar.h>
TEST_CASE(wcspbrk)
{
wchar_t const* input;
wchar_t* ret;
// Test empty haystack.
ret = wcspbrk(L"", L"ab");
EXPECT_EQ(ret, nullptr);
// Test empty needle.
ret = wcspbrk(L"ab", L"");
EXPECT_EQ(ret, nullptr);
// Test search for a single character.
input = L"abcd";
ret = wcspbrk(input, L"a");
EXPECT_EQ(ret, input);
// Test search for multiple characters, none matches.
ret = wcspbrk(input, L"zxy");
EXPECT_EQ(ret, nullptr);
// Test search for multiple characters, last matches.
ret = wcspbrk(input, L"zxyc");
EXPECT_EQ(ret, input + 2);
}
TEST_CASE(wcsstr)
{
wchar_t const* input = L"abcde";
wchar_t* ret;
// Empty needle should return haystack.
ret = wcsstr(input, L"");
EXPECT_EQ(ret, input);
// Test exact match.
ret = wcsstr(input, input);
EXPECT_EQ(ret, input);
// Test match at string start.
ret = wcsstr(input, L"ab");
EXPECT_EQ(ret, input);
// Test match at string end.
ret = wcsstr(input, L"de");
EXPECT_EQ(ret, input + 3);
// Test no match.
ret = wcsstr(input, L"z");
EXPECT_EQ(ret, nullptr);
// Test needle that is longer than the haystack.
ret = wcsstr(input, L"abcdef");
EXPECT_EQ(ret, nullptr);
}
TEST_CASE(wmemchr)
{
wchar_t const* input = L"abcde";
wchar_t* ret;
// Empty haystack returns nothing.
ret = wmemchr(L"", L'c', 0);
EXPECT_EQ(ret, nullptr);
// Not included character returns nothing.
ret = wmemchr(input, L'z', 5);
EXPECT_EQ(ret, nullptr);
// Match at string start.
ret = wmemchr(input, L'a', 5);
EXPECT_EQ(ret, input);
// Match at string end.
ret = wmemchr(input, L'e', 5);
EXPECT_EQ(ret, input + 4);
input = L"abcde\0fg";
// Handle finding null characters.
ret = wmemchr(input, L'\0', 8);
EXPECT_EQ(ret, input + 5);
// Don't stop at null characters.
ret = wmemchr(input, L'f', 8);
EXPECT_EQ(ret, input + 6);
}
TEST_CASE(wmemcpy)
{
wchar_t const* input = L"abc\0def";
auto buf = static_cast<wchar_t*>(malloc(8 * sizeof(wchar_t)));
if (!buf) {
FAIL("Could not allocate space for copy target");
return;
}
wchar_t* ret = wmemcpy(buf, input, 8);
EXPECT_EQ(ret, buf);
EXPECT_EQ(memcmp(buf, input, 8 * sizeof(wchar_t)), 0);
}
TEST_CASE(wmemset)
{
auto buf_length = 8;
auto buf = static_cast<wchar_t*>(calloc(buf_length, sizeof(wchar_t)));
if (!buf) {
FAIL("Could not allocate memory for target buffer");
return;
}
wchar_t* ret = wmemset(buf, L'\U0001f41e', buf_length - 1);
EXPECT_EQ(ret, buf);
for (int i = 0; i < buf_length - 1; i++) {
EXPECT_EQ(buf[i], L'\U0001f41e');
}
EXPECT_EQ(buf[buf_length - 1], L'\0');
free(buf);
}
TEST_CASE(wmemmove)
{
wchar_t* ret;
wchar_t const* string = L"abc\0def";
auto buf = static_cast<wchar_t*>(calloc(32, sizeof(wchar_t)));
if (!buf) {
FAIL("Could not allocate memory for target buffer");
return;
}
// Test moving to smaller addresses.
wmemcpy(buf + 3, string, 8);
ret = wmemmove(buf + 1, buf + 3, 8);
EXPECT_EQ(ret, buf + 1);
EXPECT_EQ(memcmp(string, buf + 1, 8 * sizeof(wchar_t)), 0);
// Test moving to larger addresses.
wmemcpy(buf + 16, string, 8);
ret = wmemmove(buf + 18, buf + 16, 8);
EXPECT_EQ(ret, buf + 18);
EXPECT_EQ(memcmp(string, buf + 18, 8 * sizeof(wchar_t)), 0);
free(buf);
}
TEST_CASE(wcscoll)
{
// Check if wcscoll is sorting correctly. At the moment we are doing raw char comparisons,
// so it's digits, then uppercase letters, then lowercase letters.
// Equalness between equal strings.
EXPECT(wcscoll(L"", L"") == 0);
EXPECT(wcscoll(L"0", L"0") == 0);
// Shorter strings before longer strings.
EXPECT(wcscoll(L"", L"0") < 0);
EXPECT(wcscoll(L"0", L"") > 0);
EXPECT(wcscoll(L"123", L"1234") < 0);
EXPECT(wcscoll(L"1234", L"123") > 0);
// Order within digits.
EXPECT(wcscoll(L"0", L"9") < 0);
EXPECT(wcscoll(L"9", L"0") > 0);
// Digits before uppercase letters.
EXPECT(wcscoll(L"9", L"A") < 0);
EXPECT(wcscoll(L"A", L"9") > 0);
// Order within uppercase letters.
EXPECT(wcscoll(L"A", L"Z") < 0);
EXPECT(wcscoll(L"Z", L"A") > 0);
// Uppercase letters before lowercase letters.
EXPECT(wcscoll(L"Z", L"a") < 0);
EXPECT(wcscoll(L"a", L"Z") > 0);
// Uppercase letters before lowercase letters.
EXPECT(wcscoll(L"a", L"z") < 0);
EXPECT(wcscoll(L"z", L"a") > 0);
}
TEST_CASE(mbsinit)
{
// Ensure that nullptr is considered an initial state.
EXPECT(mbsinit(nullptr) != 0);
// Ensure that a zero-initialized state is recognized as initial state.
mbstate_t state = {};
EXPECT(mbsinit(&state) != 0);
// Read a partial multibyte sequence (0b11011111 / 0xdf).
size_t ret = mbrtowc(nullptr, "\xdf", 1, &state);
if (ret != -2ul)
FAIL(String::formatted("mbrtowc accepted partial multibyte sequence with return code {} (expected -2)", static_cast<ssize_t>(ret)));
// Ensure that we are not in an initial state.
EXPECT(mbsinit(&state) == 0);
// Read the remaining multibyte sequence (0b10111111 / 0xbf).
ret = mbrtowc(nullptr, "\xbf", 1, &state);
if (ret != 1ul)
FAIL(String::formatted("mbrtowc did not consume the expected number of bytes (1), returned {} instead", static_cast<ssize_t>(ret)));
// Ensure that we are in an initial state again.
EXPECT(mbsinit(&state) != 0);
}
TEST_CASE(mbrtowc)
{
size_t ret = 0;
mbstate_t state = {};
wchar_t wc = 0;
// Ensure that we can parse normal ASCII characters.
ret = mbrtowc(&wc, "Hello", 5, &state);
EXPECT_EQ(ret, 1ul);
EXPECT_EQ(wc, 'H');
// Try two three-byte codepoints (™™), only one of which should be consumed.
ret = mbrtowc(&wc, "\xe2\x84\xa2\xe2\x84\xa2", 6, &state);
EXPECT_EQ(ret, 3ul);
EXPECT_EQ(wc, 0x2122);
// Try a null character, which should return 0 and reset the state to the initial state.
ret = mbrtowc(&wc, "\x00\x00", 2, &state);
EXPECT_EQ(ret, 0ul);
EXPECT_EQ(wc, 0);
EXPECT_NE(mbsinit(&state), 0);
// Try an incomplete multibyte character.
ret = mbrtowc(&wc, "\xe2\x84", 2, &state);
EXPECT_EQ(ret, -2ul);
EXPECT_EQ(mbsinit(&state), 0);
mbstate_t incomplete_state = state;
// Finish the previous multibyte character.
ret = mbrtowc(&wc, "\xa2", 1, &state);
EXPECT_EQ(ret, 1ul);
EXPECT_EQ(wc, 0x2122);
// Try an invalid multibyte sequence.
// Reset the state afterwards because the effects are undefined.
ret = mbrtowc(&wc, "\xff", 1, &state);
EXPECT_EQ(ret, -1ul);
EXPECT_EQ(errno, EILSEQ);
state = {};
// Try a successful conversion, but without target address.
ret = mbrtowc(nullptr, "\xe2\x84\xa2\xe2\x84\xa2", 6, &state);
EXPECT_EQ(ret, 3ul);
// Test the "null byte shorthand". Ensure that wc is ignored.
state = {};
wchar_t old_wc = wc;
ret = mbrtowc(&wc, nullptr, 0, &state);
EXPECT_EQ(ret, 0ul);
EXPECT_EQ(wc, old_wc);
// Test recognition of incomplete multibyte sequences.
ret = mbrtowc(nullptr, nullptr, 0, &incomplete_state);
EXPECT_EQ(ret, -1ul);
EXPECT_EQ(errno, EILSEQ);
}
TEST_CASE(wcrtomb)
{
char buf[MB_LEN_MAX];
size_t ret = 0;
// Ensure that `wc` is ignored when buf is a nullptr.
ret = wcrtomb(nullptr, L'a', nullptr);
EXPECT_EQ(ret, 1ul);
ret = wcrtomb(nullptr, L'\U0001F41E', nullptr);
EXPECT_EQ(ret, 1ul);
// When the buffer is non-null, the multibyte representation is written into it.
ret = wcrtomb(buf, L'a', nullptr);
EXPECT_EQ(ret, 1ul);
EXPECT_EQ(memcmp(buf, "a", ret), 0);
ret = wcrtomb(buf, L'\U0001F41E', nullptr);
EXPECT_EQ(ret, 4ul);
EXPECT_EQ(memcmp(buf, "\xf0\x9f\x90\x9e", ret), 0);
// When the wide character is invalid, -1 is returned and errno is set to EILSEQ.
ret = wcrtomb(buf, 0x110000, nullptr);
EXPECT_EQ(ret, (size_t)-1);
EXPECT_EQ(errno, EILSEQ);
// Replacement characters and conversion errors are not confused.
ret = wcrtomb(buf, L'\uFFFD', nullptr);
EXPECT_NE(ret, (size_t)-1);
}
TEST_CASE(wcsrtombs)
{
mbstate_t state = {};
char buf[MB_LEN_MAX * 4];
const wchar_t good_chars[] = { L'\U0001F41E', L'\U0001F41E', L'\0' };
const wchar_t bad_chars[] = { L'\U0001F41E', static_cast<wchar_t>(0x1111F41E), L'\0' };
wchar_t const* src;
size_t ret = 0;
// Convert normal and valid wchar_t values.
src = good_chars;
ret = wcsrtombs(buf, &src, 9, &state);
EXPECT_EQ(ret, 8ul);
EXPECT_EQ(memcmp(buf, "\xf0\x9f\x90\x9e\xf0\x9f\x90\x9e", 9), 0);
EXPECT_EQ(src, nullptr);
EXPECT_NE(mbsinit(&state), 0);
// Stop on invalid wchar values.
src = bad_chars;
ret = wcsrtombs(buf, &src, 9, &state);
EXPECT_EQ(ret, -1ul);
EXPECT_EQ(memcmp(buf, "\xf0\x9f\x90\x9e", 4), 0);
EXPECT_EQ(errno, EILSEQ);
EXPECT_EQ(src, bad_chars + 1);
// Valid characters but not enough space.
src = good_chars;
ret = wcsrtombs(buf, &src, 7, &state);
EXPECT_EQ(ret, 4ul);
EXPECT_EQ(memcmp(buf, "\xf0\x9f\x90\x9e", 4), 0);
EXPECT_EQ(src, good_chars + 1);
// Try a conversion with no destination and too short length.
src = good_chars;
ret = wcsrtombs(nullptr, &src, 2, &state);
EXPECT_EQ(ret, 8ul);
EXPECT_EQ(src, nullptr);
EXPECT_NE(mbsinit(&state), 0);
// Try a conversion using the internal anonymous state.
src = good_chars;
ret = wcsrtombs(buf, &src, 9, nullptr);
EXPECT_EQ(ret, 8ul);
EXPECT_EQ(memcmp(buf, "\xf0\x9f\x90\x9e\xf0\x9f\x90\x9e", 9), 0);
EXPECT_EQ(src, nullptr);
}
TEST_CASE(wcsnrtombs)
{
mbstate_t state = {};
const wchar_t good_chars[] = { L'\U0001F41E', L'\U0001F41E', L'\0' };
wchar_t const* src;
size_t ret = 0;
// Convert nothing.
src = good_chars;
ret = wcsnrtombs(nullptr, &src, 0, 0, &state);
EXPECT_EQ(ret, 0ul);
EXPECT_EQ(src, good_chars);
// Convert one wide char.
src = good_chars;
ret = wcsnrtombs(nullptr, &src, 1, 0, &state);
EXPECT_EQ(ret, 4ul);
EXPECT_EQ(src, good_chars + 1);
// Encounter a null character.
src = good_chars;
ret = wcsnrtombs(nullptr, &src, 4, 0, &state);
EXPECT_EQ(ret, 8ul);
EXPECT_EQ(src, nullptr);
}
TEST_CASE(mbsrtowcs)
{
mbstate_t state = {};
wchar_t buf[4];
char const good_chars[] = "\xf0\x9f\x90\x9e\xf0\x9f\x90\x9e";
char const bad_chars[] = "\xf0\x9f\x90\x9e\xf0\xff\x90\x9e";
char const* src;
size_t ret = 0;
// Convert normal and valid multibyte sequences.
src = good_chars;
ret = mbsrtowcs(buf, &src, 3, &state);
EXPECT_EQ(ret, 2ul);
EXPECT_EQ(buf[0], L'\U0001F41E');
EXPECT_EQ(buf[1], L'\U0001F41E');
EXPECT_EQ(buf[2], L'\0');
EXPECT_EQ(src, nullptr);
EXPECT_NE(mbsinit(&state), 0);
// Stop on invalid multibyte sequences.
src = bad_chars;
ret = mbsrtowcs(buf, &src, 3, &state);
EXPECT_EQ(ret, -1ul);
EXPECT_EQ(buf[0], L'\U0001F41E');
EXPECT_EQ(errno, EILSEQ);
EXPECT_EQ(src, bad_chars + 4);
// Valid sequence but not enough space.
src = good_chars;
ret = mbsrtowcs(buf, &src, 1, &state);
EXPECT_EQ(ret, 1ul);
EXPECT_EQ(buf[0], L'\U0001F41E');
EXPECT_EQ(src, good_chars + 4);
// Try a conversion with no destination and too short length.
src = good_chars;
ret = mbsrtowcs(nullptr, &src, 1, &state);
EXPECT_EQ(ret, 2ul);
EXPECT_EQ(src, nullptr);
EXPECT_NE(mbsinit(&state), 0);
// Try a conversion using the internal anonymous state.
src = good_chars;
ret = mbsrtowcs(buf, &src, 3, nullptr);
EXPECT_EQ(ret, 2ul);
EXPECT_EQ(buf[0], L'\U0001F41E');
EXPECT_EQ(buf[1], L'\U0001F41E');
EXPECT_EQ(buf[2], L'\0');
EXPECT_EQ(src, nullptr);
}
TEST_CASE(mbsnrtowcs)
{
mbstate_t state = {};
char const good_chars[] = "\xf0\x9f\x90\x9e\xf0\x9f\x90\x9e";
char const* src;
size_t ret = 0;
// Convert nothing.
src = good_chars;
ret = mbsnrtowcs(nullptr, &src, 0, 0, &state);
EXPECT_EQ(ret, 0ul);
EXPECT_EQ(src, good_chars);
// Convert one full wide character.
src = good_chars;
ret = mbsnrtowcs(nullptr, &src, 4, 0, &state);
EXPECT_EQ(ret, 1ul);
EXPECT_EQ(src, good_chars + 4);
// Encounter a null character.
src = good_chars;
ret = mbsnrtowcs(nullptr, &src, 10, 0, &state);
EXPECT_EQ(ret, 2ul);
EXPECT_EQ(src, nullptr);
// Convert an incomplete character.
// Make sure that we point past the last processed byte.
src = good_chars;
ret = mbsnrtowcs(nullptr, &src, 6, 0, &state);
EXPECT_EQ(ret, 1ul);
EXPECT_EQ(src, good_chars + 6);
EXPECT_EQ(mbsinit(&state), 0);
// Finish converting the incomplete character.
ret = mbsnrtowcs(nullptr, &src, 2, 0, &state);
EXPECT_EQ(ret, 1ul);
EXPECT_EQ(src, good_chars + 8);
}
TEST_CASE(wcslcpy)
{
auto buf = static_cast<wchar_t*>(malloc(8 * sizeof(wchar_t)));
if (!buf) {
FAIL("Could not allocate space for copy target");
return;
}
size_t ret;
// If buffer is long enough, a straight-forward string copy is performed.
ret = wcslcpy(buf, L"abc", 8);
EXPECT_EQ(ret, 3ul);
EXPECT_EQ(wmemcmp(L"abc", buf, 4), 0);
// If buffer is (supposedly) too small, the string will be truncated.
ret = wcslcpy(buf, L"1234", 4);
EXPECT_EQ(ret, 4ul);
EXPECT_EQ(wmemcmp(L"123", buf, 4), 0);
// If the buffer is null, the length of the input is returned.
ret = wcslcpy(nullptr, L"abc", 0);
EXPECT_EQ(ret, 3ul);
}
TEST_CASE(mbrlen)
{
size_t ret = 0;
mbstate_t state = {};
// Ensure that we can parse normal ASCII characters.
ret = mbrlen("Hello", 5, &state);
EXPECT_EQ(ret, 1ul);
// Try two three-byte codepoints (™™), only one of which should be consumed.
ret = mbrlen("\xe2\x84\xa2\xe2\x84\xa2", 6, &state);
EXPECT_EQ(ret, 3ul);
// Try a null character, which should return 0 and reset the state to the initial state.
ret = mbrlen("\x00\x00", 2, &state);
EXPECT_EQ(ret, 0ul);
EXPECT_NE(mbsinit(&state), 0);
// Try an incomplete multibyte character.
ret = mbrlen("\xe2\x84", 2, &state);
EXPECT_EQ(ret, -2ul);
EXPECT_EQ(mbsinit(&state), 0);
// Finish the previous multibyte character.
ret = mbrlen("\xa2", 1, &state);
EXPECT_EQ(ret, 1ul);
// Try an invalid multibyte sequence.
// Reset the state afterwards because the effects are undefined.
ret = mbrlen("\xff", 1, &state);
EXPECT_EQ(ret, -1ul);
EXPECT_EQ(errno, EILSEQ);
state = {};
}
TEST_CASE(mbtowc)
{
int ret = 0;
wchar_t wc = 0;
// Ensure that we can parse normal ASCII characters.
ret = mbtowc(&wc, "Hello", 5);
EXPECT_EQ(ret, 1);
EXPECT_EQ(wc, 'H');
// Try two three-byte codepoints (™™), only one of which should be consumed.
ret = mbtowc(&wc, "\xe2\x84\xa2\xe2\x84\xa2", 6);
EXPECT_EQ(ret, 3);
EXPECT_EQ(wc, 0x2122);
// Try a null character, which should return 0.
ret = mbtowc(&wc, "\x00\x00", 2);
EXPECT_EQ(ret, 0);
EXPECT_EQ(wc, 0);
// Try an incomplete multibyte character.
ret = mbtowc(&wc, "\xe2\x84", 2);
EXPECT_EQ(ret, -1);
EXPECT_EQ(errno, EILSEQ);
// Ask if we support shift states and reset the internal state in the process.
ret = mbtowc(nullptr, nullptr, 2);
EXPECT_EQ(ret, 0); // We don't support shift states.
ret = mbtowc(nullptr, "\x00", 1);
EXPECT_EQ(ret, 0); // No error likely means that the state is working again.
// Try an invalid multibyte sequence.
ret = mbtowc(&wc, "\xff", 1);
EXPECT_EQ(ret, -1);
EXPECT_EQ(errno, EILSEQ);
// Try a successful conversion, but without target address.
ret = mbtowc(nullptr, "\xe2\x84\xa2\xe2\x84\xa2", 6);
EXPECT_EQ(ret, 3);
}
TEST_CASE(mblen)
{
int ret = 0;
// Ensure that we can parse normal ASCII characters.
ret = mblen("Hello", 5);
EXPECT_EQ(ret, 1);
// Try two three-byte codepoints (™™), only one of which should be consumed.
ret = mblen("\xe2\x84\xa2\xe2\x84\xa2", 6);
EXPECT_EQ(ret, 3);
// Try a null character, which should return 0.
ret = mblen("\x00\x00", 2);
EXPECT_EQ(ret, 0);
// Try an incomplete multibyte character.
ret = mblen("\xe2\x84", 2);
EXPECT_EQ(ret, -1);
EXPECT_EQ(errno, EILSEQ);
// Ask if we support shift states and reset the internal state in the process.
ret = mblen(nullptr, 2);
EXPECT_EQ(ret, 0); // We don't support shift states.
ret = mblen("\x00", 1);
EXPECT_EQ(ret, 0); // No error likely means that the state is working again.
// Try an invalid multibyte sequence.
ret = mblen("\xff", 1);
EXPECT_EQ(ret, -1);
EXPECT_EQ(errno, EILSEQ);
}
|