forked from unicode-org/icu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustrtst.c
1577 lines (1398 loc) · 55.5 KB
/
custrtst.c
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
******************************************************************************
*
* Copyright (C) 2002-2016, International Business Machines
* Corporation and others. All Rights Reserved.
*
******************************************************************************
* file name: custrtst.c
* encoding: UTF-8
* tab size: 8 (not used)
* indentation:4
*
* created on: 2002oct09
* created by: Markus W. Scherer
*
* Tests of ustring.h Unicode string API functions.
*/
#include "unicode/ustring.h"
#include "unicode/ucnv.h"
#include "unicode/uiter.h"
#include "cintltst.h"
#include "cstring.h"
#include "cmemory.h"
#include <stdbool.h>
#include <string.h>
/* get the sign of an integer */
#define _SIGN(value) ((value)==0 ? 0 : ((int32_t)(value)>>31)|1)
/* test setup --------------------------------------------------------------- */
static void setUpDataTable(void);
static void TestStringCopy(void);
static void TestStringFunctions(void);
static void TestStringSearching(void);
static void TestSurrogateSearching(void);
static void TestUnescape(void);
static void TestUnescapeRepeatedSurrogateLead20725(void);
static void TestCountChar32(void);
static void TestUCharIterator(void);
void addUStringTest(TestNode** root);
void addUStringTest(TestNode** root)
{
addTest(root, &TestStringCopy, "tsutil/custrtst/TestStringCopy");
addTest(root, &TestStringFunctions, "tsutil/custrtst/TestStringFunctions");
addTest(root, &TestStringSearching, "tsutil/custrtst/TestStringSearching");
addTest(root, &TestSurrogateSearching, "tsutil/custrtst/TestSurrogateSearching");
addTest(root, &TestUnescape, "tsutil/custrtst/TestUnescape");
addTest(root, &TestUnescapeRepeatedSurrogateLead20725,
"tsutil/custrtst/TestUnescapeRepeatedSurrogateLead20725");
addTest(root, &TestCountChar32, "tsutil/custrtst/TestCountChar32");
addTest(root, &TestUCharIterator, "tsutil/custrtst/TestUCharIterator");
}
/* test data for TestStringFunctions ---------------------------------------- */
UChar*** dataTable = NULL;
static const char* raw[3][4] = {
/* First String */
{ "English_", "French_", "Croatian_", "English_"},
/* Second String */
{ "United States", "France", "Croatia", "Unites States"},
/* Concatenated string */
{ "English_United States", "French_France", "Croatian_Croatia", "English_United States"}
};
static void setUpDataTable()
{
int32_t i,j;
if(dataTable == NULL) {
dataTable = (UChar***)calloc(sizeof(UChar**),3);
for (i = 0; i < 3; i++) {
dataTable[i] = (UChar**)calloc(sizeof(UChar*),4);
for (j = 0; j < 4; j++){
dataTable[i][j] = (UChar*) malloc(sizeof(UChar)*(strlen(raw[i][j])+1));
u_uastrcpy(dataTable[i][j],raw[i][j]);
}
}
}
}
static void cleanUpDataTable()
{
int32_t i,j;
if(dataTable != NULL) {
for (i=0; i<3; i++) {
for(j = 0; j<4; j++) {
free(dataTable[i][j]);
}
free(dataTable[i]);
}
free(dataTable);
}
dataTable = NULL;
}
/*Tests for u_strcat(),u_strcmp(), u_strlen(), u_strcpy(),u_strncat(),u_strncmp(),u_strncpy, u_uastrcpy(),u_austrcpy(), u_uastrncpy(); */
static void TestStringFunctions()
{
int32_t i,j,k;
UChar temp[512];
UChar nullTemp[512];
char test[512];
char tempOut[512];
setUpDataTable();
log_verbose("Testing u_strlen()\n");
if( u_strlen(dataTable[0][0])!= u_strlen(dataTable[0][3]) || u_strlen(dataTable[0][0]) == u_strlen(dataTable[0][2]))
log_err("There is an error in u_strlen()");
log_verbose("Testing u_memcpy() and u_memcmp()\n");
for(i=0;i<3;++i)
{
for(j=0;j<4;++j)
{
log_verbose("Testing %s\n", u_austrcpy(tempOut, dataTable[i][j]));
temp[0] = 0;
temp[7] = 0xA4; /* Mark the end */
u_memcpy(temp,dataTable[i][j], 7);
if(temp[7] != 0xA4)
log_err("an error occurred in u_memcpy()\n");
if(u_memcmp(temp, dataTable[i][j], 7)!=0)
log_err("an error occurred in u_memcpy() or u_memcmp()\n");
}
}
if(u_memcmp(dataTable[0][0], dataTable[1][1], 7)==0)
log_err("an error occurred in u_memcmp()\n");
log_verbose("Testing u_memset()\n");
nullTemp[0] = 0;
nullTemp[7] = 0;
u_memset(nullTemp, 0xa4, 7);
for (i = 0; i < 7; i++) {
if(nullTemp[i] != 0xa4) {
log_err("an error occurred in u_memset()\n");
}
}
if(nullTemp[7] != 0) {
log_err("u_memset() went too far\n");
}
u_memset(nullTemp, 0, 7);
nullTemp[7] = 0xa4;
temp[7] = 0;
u_memcpy(temp,nullTemp, 7);
if(u_memcmp(temp, nullTemp, 7)!=0 || temp[7]!=0)
log_err("an error occurred in u_memcpy() or u_memcmp()\n");
log_verbose("Testing u_memmove()\n");
for (i = 0; i < 7; i++) {
temp[i] = (UChar)i;
}
u_memmove(temp + 1, temp, 7);
if(temp[0] != 0) {
log_err("an error occurred in u_memmove()\n");
}
for (i = 1; i <= 7; i++) {
if(temp[i] != (i - 1)) {
log_err("an error occurred in u_memmove()\n");
}
}
log_verbose("Testing u_strcpy() and u_strcmp()\n");
for(i=0;i<3;++i)
{
for(j=0;j<4;++j)
{
log_verbose("Testing %s\n", u_austrcpy(tempOut, dataTable[i][j]));
temp[0] = 0;
u_strcpy(temp,dataTable[i][j]);
if(u_strcmp(temp,dataTable[i][j])!=0)
log_err("something threw an error in u_strcpy() or u_strcmp()\n");
}
}
if(u_strcmp(dataTable[0][0], dataTable[1][1])==0)
log_err("an error occurred in u_memcmp()\n");
log_verbose("testing u_strcat()\n");
i=0;
for(j=0; j<2;++j)
{
u_uastrcpy(temp, "");
u_strcpy(temp,dataTable[i][j]);
u_strcat(temp,dataTable[i+1][j]);
if(u_strcmp(temp,dataTable[i+2][j])!=0)
log_err("something threw an error in u_strcat()\n");
}
log_verbose("Testing u_strncmp()\n");
for(i=0,j=0;j<4; ++j)
{
k=u_strlen(dataTable[i][j]);
if(u_strncmp(dataTable[i][j],dataTable[i+2][j],k)!=0)
log_err("Something threw an error in u_strncmp\n");
}
if(u_strncmp(dataTable[0][0], dataTable[1][1], 7)==0)
log_err("an error occurred in u_memcmp()\n");
log_verbose("Testing u_strncat\n");
for(i=0,j=0;j<4; ++j)
{
k=u_strlen(dataTable[i][j]);
u_uastrcpy(temp,"");
if(u_strcmp(u_strncat(temp,dataTable[i+2][j],k),dataTable[i][j])!=0)
log_err("something threw an error in u_strncat or u_uastrcpy()\n");
}
log_verbose("Testing u_strncpy() and u_uastrcpy()\n");
for(i=2,j=0;j<4; ++j)
{
k=u_strlen(dataTable[i][j]);
u_strncpy(temp, dataTable[i][j],k);
temp[k] = 0xa4;
if(u_strncmp(temp, dataTable[i][j],k)!=0)
log_err("something threw an error in u_strncpy()\n");
if(temp[k] != 0xa4)
log_err("something threw an error in u_strncpy()\n");
u_memset(temp, 0x3F, UPRV_LENGTHOF(temp) - 1);
u_uastrncpy(temp, raw[i][j], k-1);
if(u_strncmp(temp, dataTable[i][j],k-1)!=0)
log_err("something threw an error in u_uastrncpy(k-1)\n");
if(temp[k-1] != 0x3F)
log_err("something threw an error in u_uastrncpy(k-1)\n");
u_memset(temp, 0x3F, UPRV_LENGTHOF(temp) - 1);
u_uastrncpy(temp, raw[i][j], k+1);
if(u_strcmp(temp, dataTable[i][j])!=0)
log_err("something threw an error in u_uastrncpy(k+1)\n");
if(temp[k] != 0)
log_err("something threw an error in u_uastrncpy(k+1)\n");
u_memset(temp, 0x3F, UPRV_LENGTHOF(temp) - 1);
u_uastrncpy(temp, raw[i][j], k);
if(u_strncmp(temp, dataTable[i][j], k)!=0)
log_err("something threw an error in u_uastrncpy(k)\n");
if(temp[k] != 0x3F)
log_err("something threw an error in u_uastrncpy(k)\n");
}
log_verbose("Testing u_strchr() and u_memchr()\n");
for(i=2,j=0;j<4;j++)
{
UChar saveVal = dataTable[i][j][0];
UChar *findPtr = u_strchr(dataTable[i][j], 0x005F);
int32_t dataSize = (int32_t)(u_strlen(dataTable[i][j]) + 1);
log_verbose("%s ", u_austrcpy(tempOut, findPtr));
if (findPtr == NULL || *findPtr != 0x005F) {
log_err("u_strchr can't find '_' in the string\n");
}
findPtr = u_strchr32(dataTable[i][j], 0x005F);
if (findPtr == NULL || *findPtr != 0x005F) {
log_err("u_strchr32 can't find '_' in the string\n");
}
findPtr = u_strchr(dataTable[i][j], 0);
if (findPtr != (&(dataTable[i][j][dataSize - 1]))) {
log_err("u_strchr can't find NULL in the string\n");
}
findPtr = u_strchr32(dataTable[i][j], 0);
if (findPtr != (&(dataTable[i][j][dataSize - 1]))) {
log_err("u_strchr32 can't find NULL in the string\n");
}
findPtr = u_memchr(dataTable[i][j], 0, dataSize);
if (findPtr != (&(dataTable[i][j][dataSize - 1]))) {
log_err("u_memchr can't find NULL in the string\n");
}
findPtr = u_memchr32(dataTable[i][j], 0, dataSize);
if (findPtr != (&(dataTable[i][j][dataSize - 1]))) {
log_err("u_memchr32 can't find NULL in the string\n");
}
dataTable[i][j][0] = 0;
/* Make sure we skip over the NULL termination */
findPtr = u_memchr(dataTable[i][j], 0x005F, dataSize);
if (findPtr == NULL || *findPtr != 0x005F) {
log_err("u_memchr can't find '_' in the string\n");
}
findPtr = u_memchr32(dataTable[i][j], 0x005F, dataSize);
if (findPtr == NULL || *findPtr != 0x005F) {
log_err("u_memchr32 can't find '_' in the string\n");
}
findPtr = u_memchr32(dataTable[i][j], 0xFFFD, dataSize);
if (findPtr != NULL) {
log_err("Should have found NULL when the character is not there.\n");
}
dataTable[i][j][0] = saveVal; /* Put it back for the other tests */
}
/*
* test that u_strchr32()
* does not find surrogate code points when they are part of matched pairs
* (= part of supplementary code points)
* Jitterbug 1542
*/
{
static const UChar s[]={
/* 0 1 2 3 4 5 6 7 8 9 */
0x0061, 0xd841, 0xdc02, 0xd841, 0x0062, 0xdc02, 0xd841, 0xdc02, 0x0063, 0
};
if(u_strchr32(s, 0xd841)!=(s+3) || u_strchr32(s, 0xdc02)!=(s+5)) {
log_err("error: u_strchr32(surrogate) finds a partial supplementary code point\n");
}
if(u_memchr32(s, 0xd841, 9)!=(s+3) || u_memchr32(s, 0xdc02, 9)!=(s+5)) {
log_err("error: u_memchr32(surrogate) finds a partial supplementary code point\n");
}
}
log_verbose("Testing u_austrcpy()");
u_austrcpy(test,dataTable[0][0]);
if(strcmp(test,raw[0][0])!=0)
log_err("There is an error in u_austrcpy()");
log_verbose("Testing u_strtok_r()");
{
const char tokString[] = " , 1 2 3 AHHHHH! 5.5 6 7 , 8\n";
const char *tokens[] = {",", "1", "2", "3", "AHHHHH!", "5.5", "6", "7", "8\n"};
UChar delimBuf[sizeof(test)];
UChar currTokenBuf[sizeof(tokString)];
UChar *state;
uint32_t currToken = 0;
UChar *ptr;
u_uastrcpy(temp, tokString);
u_uastrcpy(delimBuf, " ");
ptr = u_strtok_r(temp, delimBuf, &state);
u_uastrcpy(delimBuf, " ,");
while (ptr != NULL) {
u_uastrcpy(currTokenBuf, tokens[currToken]);
if (u_strcmp(ptr, currTokenBuf) != 0) {
log_err("u_strtok_r mismatch at %d. Got: %s, Expected: %s\n", currToken, ptr, tokens[currToken]);
}
ptr = u_strtok_r(NULL, delimBuf, &state);
currToken++;
}
if (currToken != UPRV_LENGTHOF(tokens)) {
log_err("Didn't get correct number of tokens\n");
}
state = delimBuf; /* Give it an "invalid" saveState */
u_uastrcpy(currTokenBuf, "");
if (u_strtok_r(currTokenBuf, delimBuf, &state) != NULL) {
log_err("Didn't get NULL for empty string\n");
}
if (state != NULL) {
log_err("State should be NULL for empty string\n");
}
state = delimBuf; /* Give it an "invalid" saveState */
u_uastrcpy(currTokenBuf, ", ,");
if (u_strtok_r(currTokenBuf, delimBuf, &state) != NULL) {
log_err("Didn't get NULL for a string of delimiters\n");
}
if (state != NULL) {
log_err("State should be NULL for a string of delimiters\n");
}
state = delimBuf; /* Give it an "invalid" saveState */
u_uastrcpy(currTokenBuf, "q, ,");
if (u_strtok_r(currTokenBuf, delimBuf, &state) == NULL) {
log_err("Got NULL for a string that does not begin with delimiters\n");
}
if (u_strtok_r(NULL, delimBuf, &state) != NULL) {
log_err("Didn't get NULL for a string that ends in delimiters\n");
}
if (state != NULL) {
log_err("State should be NULL for empty string\n");
}
state = delimBuf; /* Give it an "invalid" saveState */
u_uastrcpy(currTokenBuf, tokString);
u_uastrcpy(temp, tokString);
u_uastrcpy(delimBuf, "q"); /* Give it a delimiter that it can't find. */
ptr = u_strtok_r(currTokenBuf, delimBuf, &state);
if (ptr == NULL || u_strcmp(ptr, temp) != 0) {
log_err("Should have received the same string when there are no delimiters\n");
}
if (u_strtok_r(NULL, delimBuf, &state) != NULL) {
log_err("Should not have found another token in a one token string\n");
}
}
/* test u_strcmpCodePointOrder() */
{
/* these strings are in ascending order */
static const UChar strings[][4]={
{ 0x61, 0 }, /* U+0061 */
{ 0x20ac, 0xd801, 0 }, /* U+20ac U+d801 */
{ 0x20ac, 0xd800, 0xdc00, 0 }, /* U+20ac U+10000 */
{ 0xd800, 0 }, /* U+d800 */
{ 0xd800, 0xff61, 0 }, /* U+d800 U+ff61 */
{ 0xdfff, 0 }, /* U+dfff */
{ 0xff61, 0xdfff, 0 }, /* U+ff61 U+dfff */
{ 0xff61, 0xd800, 0xdc02, 0 }, /* U+ff61 U+10002 */
{ 0xd800, 0xdc02, 0 }, /* U+10002 */
{ 0xd84d, 0xdc56, 0 } /* U+23456 */
};
UCharIterator iter1, iter2;
int32_t len1, len2, r1, r2;
for(i=0; i<(UPRV_LENGTHOF(strings)-1); ++i) {
if(u_strcmpCodePointOrder(strings[i], strings[i+1])>=0) {
log_err("error: u_strcmpCodePointOrder() fails for string %d and the following one\n", i);
}
if(u_strncmpCodePointOrder(strings[i], strings[i+1], 10)>=0) {
log_err("error: u_strncmpCodePointOrder() fails for string %d and the following one\n", i);
}
/* There are at least 2 UChars in each string - verify that strncmp()==memcmp(). */
if(u_strncmpCodePointOrder(strings[i], strings[i+1], 2)!=u_memcmpCodePointOrder(strings[i], strings[i+1], 2)) {
log_err("error: u_strncmpCodePointOrder(2)!=u_memcmpCodePointOrder(2) for string %d and the following one\n", i);
}
/* test u_strCompare(true) */
len1=u_strlen(strings[i]);
len2=u_strlen(strings[i+1]);
if( u_strCompare(strings[i], -1, strings[i+1], -1, true)>=0 ||
u_strCompare(strings[i], -1, strings[i+1], len2, true)>=0 ||
u_strCompare(strings[i], len1, strings[i+1], -1, true)>=0 ||
u_strCompare(strings[i], len1, strings[i+1], len2, true)>=0
) {
log_err("error: u_strCompare(code point order) fails for string %d and the following one\n", i);
}
/* test u_strCompare(false) */
r1=u_strCompare(strings[i], -1, strings[i+1], -1, false);
r2=u_strcmp(strings[i], strings[i+1]);
if(_SIGN(r1)!=_SIGN(r2)) {
log_err("error: u_strCompare(code unit order)!=u_strcmp() for string %d and the following one\n", i);
}
/* test u_strCompareIter() */
uiter_setString(&iter1, strings[i], len1);
uiter_setString(&iter2, strings[i+1], len2);
if(u_strCompareIter(&iter1, &iter2, true)>=0) {
log_err("error: u_strCompareIter(code point order) fails for string %d and the following one\n", i);
}
r1=u_strCompareIter(&iter1, &iter2, false);
if(_SIGN(r1)!=_SIGN(u_strcmp(strings[i], strings[i+1]))) {
log_err("error: u_strCompareIter(code unit order)!=u_strcmp() for string %d and the following one\n", i);
}
}
}
cleanUpDataTable();
}
static void TestStringSearching()
{
const UChar testString[] = {0x0061, 0x0062, 0x0063, 0x0064, 0x0064, 0x0061, 0};
const UChar testSurrogateString[] = {0xdbff, 0x0061, 0x0062, 0xdbff, 0xdfff, 0x0063, 0x0064, 0x0064, 0xdbff, 0xdfff, 0xdb00, 0xdf00, 0x0061, 0};
const UChar surrMatchSet1[] = {0xdbff, 0xdfff, 0};
const UChar surrMatchSet2[] = {0x0061, 0x0062, 0xdbff, 0xdfff, 0};
const UChar surrMatchSet3[] = {0xdb00, 0xdf00, 0xdbff, 0xdfff, 0};
const UChar surrMatchSet4[] = {0x0000};
const UChar surrMatchSetBad[] = {0xdbff, 0x0061, 0};
const UChar surrMatchSetBad2[] = {0x0061, 0xdbff, 0};
const UChar surrMatchSetBad3[] = {0xdbff, 0x0061, 0x0062, 0xdbff, 0xdfff, 0}; /* has partial surrogate */
const UChar
empty[] = { 0 },
a[] = { 0x61, 0 },
ab[] = { 0x61, 0x62, 0 },
ba[] = { 0x62, 0x61, 0 },
abcd[] = { 0x61, 0x62, 0x63, 0x64, 0 },
cd[] = { 0x63, 0x64, 0 },
dc[] = { 0x64, 0x63, 0 },
cdh[] = { 0x63, 0x64, 0x68, 0 },
f[] = { 0x66, 0 },
fg[] = { 0x66, 0x67, 0 },
gf[] = { 0x67, 0x66, 0 };
log_verbose("Testing u_strpbrk()");
if (u_strpbrk(testString, a) != &testString[0]) {
log_err("u_strpbrk couldn't find first letter a.\n");
}
if (u_strpbrk(testString, dc) != &testString[2]) {
log_err("u_strpbrk couldn't find d or c.\n");
}
if (u_strpbrk(testString, cd) != &testString[2]) {
log_err("u_strpbrk couldn't find c or d.\n");
}
if (u_strpbrk(testString, cdh) != &testString[2]) {
log_err("u_strpbrk couldn't find c, d or h.\n");
}
if (u_strpbrk(testString, f) != NULL) {
log_err("u_strpbrk didn't return NULL for \"f\".\n");
}
if (u_strpbrk(testString, fg) != NULL) {
log_err("u_strpbrk didn't return NULL for \"fg\".\n");
}
if (u_strpbrk(testString, gf) != NULL) {
log_err("u_strpbrk didn't return NULL for \"gf\".\n");
}
if (u_strpbrk(testString, empty) != NULL) {
log_err("u_strpbrk didn't return NULL for \"\".\n");
}
log_verbose("Testing u_strpbrk() with surrogates");
if (u_strpbrk(testSurrogateString, a) != &testSurrogateString[1]) {
log_err("u_strpbrk couldn't find first letter a.\n");
}
if (u_strpbrk(testSurrogateString, dc) != &testSurrogateString[5]) {
log_err("u_strpbrk couldn't find d or c.\n");
}
if (u_strpbrk(testSurrogateString, cd) != &testSurrogateString[5]) {
log_err("u_strpbrk couldn't find c or d.\n");
}
if (u_strpbrk(testSurrogateString, cdh) != &testSurrogateString[5]) {
log_err("u_strpbrk couldn't find c, d or h.\n");
}
if (u_strpbrk(testSurrogateString, f) != NULL) {
log_err("u_strpbrk didn't return NULL for \"f\".\n");
}
if (u_strpbrk(testSurrogateString, fg) != NULL) {
log_err("u_strpbrk didn't return NULL for \"fg\".\n");
}
if (u_strpbrk(testSurrogateString, gf) != NULL) {
log_err("u_strpbrk didn't return NULL for \"gf\".\n");
}
if (u_strpbrk(testSurrogateString, surrMatchSet1) != &testSurrogateString[3]) {
log_err("u_strpbrk couldn't find \"0xdbff, 0xdfff\".\n");
}
if (u_strpbrk(testSurrogateString, surrMatchSet2) != &testSurrogateString[1]) {
log_err("u_strpbrk couldn't find \"0xdbff, a, b, 0xdbff, 0xdfff\".\n");
}
if (u_strpbrk(testSurrogateString, surrMatchSet3) != &testSurrogateString[3]) {
log_err("u_strpbrk couldn't find \"0xdb00, 0xdf00, 0xdbff, 0xdfff\".\n");
}
if (u_strpbrk(testSurrogateString, surrMatchSet4) != NULL) {
log_err("u_strpbrk should have returned NULL for empty string.\n");
}
if (u_strpbrk(testSurrogateString, surrMatchSetBad) != &testSurrogateString[0]) {
log_err("u_strpbrk should have found bad surrogate.\n");
}
log_verbose("Testing u_strcspn()");
if (u_strcspn(testString, a) != 0) {
log_err("u_strcspn couldn't find first letter a.\n");
}
if (u_strcspn(testString, dc) != 2) {
log_err("u_strcspn couldn't find d or c.\n");
}
if (u_strcspn(testString, cd) != 2) {
log_err("u_strcspn couldn't find c or d.\n");
}
if (u_strcspn(testString, cdh) != 2) {
log_err("u_strcspn couldn't find c, d or h.\n");
}
if (u_strcspn(testString, f) != u_strlen(testString)) {
log_err("u_strcspn didn't return NULL for \"f\".\n");
}
if (u_strcspn(testString, fg) != u_strlen(testString)) {
log_err("u_strcspn didn't return NULL for \"fg\".\n");
}
if (u_strcspn(testString, gf) != u_strlen(testString)) {
log_err("u_strcspn didn't return NULL for \"gf\".\n");
}
log_verbose("Testing u_strcspn() with surrogates");
if (u_strcspn(testSurrogateString, a) != 1) {
log_err("u_strcspn couldn't find first letter a.\n");
}
if (u_strcspn(testSurrogateString, dc) != 5) {
log_err("u_strcspn couldn't find d or c.\n");
}
if (u_strcspn(testSurrogateString, cd) != 5) {
log_err("u_strcspn couldn't find c or d.\n");
}
if (u_strcspn(testSurrogateString, cdh) != 5) {
log_err("u_strcspn couldn't find c, d or h.\n");
}
if (u_strcspn(testSurrogateString, f) != u_strlen(testSurrogateString)) {
log_err("u_strcspn didn't return NULL for \"f\".\n");
}
if (u_strcspn(testSurrogateString, fg) != u_strlen(testSurrogateString)) {
log_err("u_strcspn didn't return NULL for \"fg\".\n");
}
if (u_strcspn(testSurrogateString, gf) != u_strlen(testSurrogateString)) {
log_err("u_strcspn didn't return NULL for \"gf\".\n");
}
if (u_strcspn(testSurrogateString, surrMatchSet1) != 3) {
log_err("u_strcspn couldn't find \"0xdbff, 0xdfff\".\n");
}
if (u_strcspn(testSurrogateString, surrMatchSet2) != 1) {
log_err("u_strcspn couldn't find \"a, b, 0xdbff, 0xdfff\".\n");
}
if (u_strcspn(testSurrogateString, surrMatchSet3) != 3) {
log_err("u_strcspn couldn't find \"0xdb00, 0xdf00, 0xdbff, 0xdfff\".\n");
}
if (u_strcspn(testSurrogateString, surrMatchSet4) != u_strlen(testSurrogateString)) {
log_err("u_strcspn should have returned strlen for empty string.\n");
}
log_verbose("Testing u_strspn()");
if (u_strspn(testString, a) != 1) {
log_err("u_strspn couldn't skip first letter a.\n");
}
if (u_strspn(testString, ab) != 2) {
log_err("u_strspn couldn't skip a or b.\n");
}
if (u_strspn(testString, ba) != 2) {
log_err("u_strspn couldn't skip a or b.\n");
}
if (u_strspn(testString, f) != 0) {
log_err("u_strspn didn't return 0 for \"f\".\n");
}
if (u_strspn(testString, dc) != 0) {
log_err("u_strspn couldn't find first letter a (skip d or c).\n");
}
if (u_strspn(testString, abcd) != u_strlen(testString)) {
log_err("u_strspn couldn't skip over the whole string.\n");
}
if (u_strspn(testString, empty) != 0) {
log_err("u_strspn should have returned 0 for empty string.\n");
}
log_verbose("Testing u_strspn() with surrogates");
if (u_strspn(testSurrogateString, surrMatchSetBad) != 2) {
log_err("u_strspn couldn't skip 0xdbff or a.\n");
}
if (u_strspn(testSurrogateString, surrMatchSetBad2) != 2) {
log_err("u_strspn couldn't skip 0xdbff or a.\n");
}
if (u_strspn(testSurrogateString, f) != 0) {
log_err("u_strspn couldn't skip d or c (skip first letter).\n");
}
if (u_strspn(testSurrogateString, dc) != 0) {
log_err("u_strspn couldn't skip d or c (skip first letter).\n");
}
if (u_strspn(testSurrogateString, cd) != 0) {
log_err("u_strspn couldn't skip d or c (skip first letter).\n");
}
if (u_strspn(testSurrogateString, testSurrogateString) != u_strlen(testSurrogateString)) {
log_err("u_strspn couldn't skip whole string.\n");
}
if (u_strspn(testSurrogateString, surrMatchSet1) != 0) {
log_err("u_strspn couldn't skip \"0xdbff, 0xdfff\" (get first letter).\n");
}
if (u_strspn(testSurrogateString, surrMatchSetBad3) != 5) {
log_err("u_strspn couldn't skip \"0xdbff, a, b, 0xdbff, 0xdfff\".\n");
}
if (u_strspn(testSurrogateString, surrMatchSet4) != 0) {
log_err("u_strspn should have returned 0 for empty string.\n");
}
}
/*
* All binary Unicode string searches should behave the same for equivalent input.
* See Jitterbug 2145.
* There are some new functions, too - just test them all.
*/
static void
TestSurrogateSearching() {
static const UChar s[]={
/* 0 1 2 3 4 5 6 7 8 9 10 11 */
0x61, 0xd801, 0xdc02, 0x61, 0xdc02, 0x61, 0xd801, 0x61, 0xd801, 0xdc02, 0x61, 0
}, sub_a[]={
0x61, 0
}, sub_b[]={
0x62, 0
}, sub_lead[]={
0xd801, 0
}, sub_trail[]={
0xdc02, 0
}, sub_supp[]={
0xd801, 0xdc02, 0
}, sub_supp2[]={
0xd801, 0xdc03, 0
}, sub_a_lead[]={
0x61, 0xd801, 0
}, sub_trail_a[]={
0xdc02, 0x61, 0
}, sub_aba[]={
0x61, 0x62, 0x61, 0
};
static const UChar a=0x61, b=0x62, lead=0xd801, trail=0xdc02, nul=0;
static const UChar32 supp=0x10402, supp2=0x10403, ill=0x123456;
const UChar *first, *last;
/* search for NUL code point: find end of string */
first=s+u_strlen(s);
if(
first!=u_strchr(s, nul) ||
first!=u_strchr32(s, nul) ||
first!=u_memchr(s, nul, UPRV_LENGTHOF(s)) ||
first!=u_memchr32(s, nul, UPRV_LENGTHOF(s)) ||
first!=u_strrchr(s, nul) ||
first!=u_strrchr32(s, nul) ||
first!=u_memrchr(s, nul, UPRV_LENGTHOF(s)) ||
first!=u_memrchr32(s, nul, UPRV_LENGTHOF(s))
) {
log_err("error: one of the u_str[|mem][r]chr[32](s, nul) does not find the terminator of s\n");
}
/* search for empty substring: find beginning of string */
if(
s!=u_strstr(s, &nul) ||
s!=u_strFindFirst(s, -1, &nul, -1) ||
s!=u_strFindFirst(s, -1, &nul, 0) ||
s!=u_strFindFirst(s, UPRV_LENGTHOF(s), &nul, -1) ||
s!=u_strFindFirst(s, UPRV_LENGTHOF(s), &nul, 0) ||
s!=u_strrstr(s, &nul) ||
s!=u_strFindLast(s, -1, &nul, -1) ||
s!=u_strFindLast(s, -1, &nul, 0) ||
s!=u_strFindLast(s, UPRV_LENGTHOF(s), &nul, -1) ||
s!=u_strFindLast(s, UPRV_LENGTHOF(s), &nul, 0)
) {
log_err("error: one of the u_str[str etc](s, \"\") does not find s itself\n");
}
/* find 'a' in s[1..10[ */
first=s+3;
last=s+7;
if(
first!=u_strchr(s+1, a) ||
first!=u_strchr32(s+1, a) ||
first!=u_memchr(s+1, a, 9) ||
first!=u_memchr32(s+1, a, 9) ||
first!=u_strstr(s+1, sub_a) ||
first!=u_strFindFirst(s+1, -1, sub_a, -1) ||
first!=u_strFindFirst(s+1, -1, &a, 1) ||
first!=u_strFindFirst(s+1, 9, sub_a, -1) ||
first!=u_strFindFirst(s+1, 9, &a, 1) ||
(s+10)!=u_strrchr(s+1, a) ||
(s+10)!=u_strrchr32(s+1, a) ||
last!=u_memrchr(s+1, a, 9) ||
last!=u_memrchr32(s+1, a, 9) ||
(s+10)!=u_strrstr(s+1, sub_a) ||
(s+10)!=u_strFindLast(s+1, -1, sub_a, -1) ||
(s+10)!=u_strFindLast(s+1, -1, &a, 1) ||
last!=u_strFindLast(s+1, 9, sub_a, -1) ||
last!=u_strFindLast(s+1, 9, &a, 1)
) {
log_err("error: one of the u_str[chr etc]('a') does not find the correct place\n");
}
/* do not find 'b' in s[1..10[ */
if(
NULL!=u_strchr(s+1, b) ||
NULL!=u_strchr32(s+1, b) ||
NULL!=u_memchr(s+1, b, 9) ||
NULL!=u_memchr32(s+1, b, 9) ||
NULL!=u_strstr(s+1, sub_b) ||
NULL!=u_strFindFirst(s+1, -1, sub_b, -1) ||
NULL!=u_strFindFirst(s+1, -1, &b, 1) ||
NULL!=u_strFindFirst(s+1, 9, sub_b, -1) ||
NULL!=u_strFindFirst(s+1, 9, &b, 1) ||
NULL!=u_strrchr(s+1, b) ||
NULL!=u_strrchr32(s+1, b) ||
NULL!=u_memrchr(s+1, b, 9) ||
NULL!=u_memrchr32(s+1, b, 9) ||
NULL!=u_strrstr(s+1, sub_b) ||
NULL!=u_strFindLast(s+1, -1, sub_b, -1) ||
NULL!=u_strFindLast(s+1, -1, &b, 1) ||
NULL!=u_strFindLast(s+1, 9, sub_b, -1) ||
NULL!=u_strFindLast(s+1, 9, &b, 1)
) {
log_err("error: one of the u_str[chr etc]('b') incorrectly finds something\n");
}
/* do not find a non-code point in s[1..10[ */
if(
NULL!=u_strchr32(s+1, ill) ||
NULL!=u_memchr32(s+1, ill, 9) ||
NULL!=u_strrchr32(s+1, ill) ||
NULL!=u_memrchr32(s+1, ill, 9)
) {
log_err("error: one of the u_str[chr etc](illegal code point) incorrectly finds something\n");
}
/* find U+d801 in s[1..10[ */
first=s+6;
if(
first!=u_strchr(s+1, lead) ||
first!=u_strchr32(s+1, lead) ||
first!=u_memchr(s+1, lead, 9) ||
first!=u_memchr32(s+1, lead, 9) ||
first!=u_strstr(s+1, sub_lead) ||
first!=u_strFindFirst(s+1, -1, sub_lead, -1) ||
first!=u_strFindFirst(s+1, -1, &lead, 1) ||
first!=u_strFindFirst(s+1, 9, sub_lead, -1) ||
first!=u_strFindFirst(s+1, 9, &lead, 1) ||
first!=u_strrchr(s+1, lead) ||
first!=u_strrchr32(s+1, lead) ||
first!=u_memrchr(s+1, lead, 9) ||
first!=u_memrchr32(s+1, lead, 9) ||
first!=u_strrstr(s+1, sub_lead) ||
first!=u_strFindLast(s+1, -1, sub_lead, -1) ||
first!=u_strFindLast(s+1, -1, &lead, 1) ||
first!=u_strFindLast(s+1, 9, sub_lead, -1) ||
first!=u_strFindLast(s+1, 9, &lead, 1)
) {
log_err("error: one of the u_str[chr etc](U+d801) does not find the correct place\n");
}
/* find U+dc02 in s[1..10[ */
first=s+4;
if(
first!=u_strchr(s+1, trail) ||
first!=u_strchr32(s+1, trail) ||
first!=u_memchr(s+1, trail, 9) ||
first!=u_memchr32(s+1, trail, 9) ||
first!=u_strstr(s+1, sub_trail) ||
first!=u_strFindFirst(s+1, -1, sub_trail, -1) ||
first!=u_strFindFirst(s+1, -1, &trail, 1) ||
first!=u_strFindFirst(s+1, 9, sub_trail, -1) ||
first!=u_strFindFirst(s+1, 9, &trail, 1) ||
first!=u_strrchr(s+1, trail) ||
first!=u_strrchr32(s+1, trail) ||
first!=u_memrchr(s+1, trail, 9) ||
first!=u_memrchr32(s+1, trail, 9) ||
first!=u_strrstr(s+1, sub_trail) ||
first!=u_strFindLast(s+1, -1, sub_trail, -1) ||
first!=u_strFindLast(s+1, -1, &trail, 1) ||
first!=u_strFindLast(s+1, 9, sub_trail, -1) ||
first!=u_strFindLast(s+1, 9, &trail, 1)
) {
log_err("error: one of the u_str[chr etc](U+dc02) does not find the correct place\n");
}
/* find U+10402 in s[1..10[ */
first=s+1;
last=s+8;
if(
first!=u_strchr32(s+1, supp) ||
first!=u_memchr32(s+1, supp, 9) ||
first!=u_strstr(s+1, sub_supp) ||
first!=u_strFindFirst(s+1, -1, sub_supp, -1) ||
first!=u_strFindFirst(s+1, -1, sub_supp, 2) ||
first!=u_strFindFirst(s+1, 9, sub_supp, -1) ||
first!=u_strFindFirst(s+1, 9, sub_supp, 2) ||
last!=u_strrchr32(s+1, supp) ||
last!=u_memrchr32(s+1, supp, 9) ||
last!=u_strrstr(s+1, sub_supp) ||
last!=u_strFindLast(s+1, -1, sub_supp, -1) ||
last!=u_strFindLast(s+1, -1, sub_supp, 2) ||
last!=u_strFindLast(s+1, 9, sub_supp, -1) ||
last!=u_strFindLast(s+1, 9, sub_supp, 2)
) {
log_err("error: one of the u_str[chr etc](U+10402) does not find the correct place\n");
}
/* do not find U+10402 in a single UChar */
if(
NULL!=u_memchr32(s+1, supp, 1) ||
NULL!=u_strFindFirst(s+1, 1, sub_supp, -1) ||
NULL!=u_strFindFirst(s+1, 1, sub_supp, 2) ||
NULL!=u_memrchr32(s+1, supp, 1) ||
NULL!=u_strFindLast(s+1, 1, sub_supp, -1) ||
NULL!=u_strFindLast(s+1, 1, sub_supp, 2) ||
NULL!=u_memrchr32(s+2, supp, 1) ||
NULL!=u_strFindLast(s+2, 1, sub_supp, -1) ||
NULL!=u_strFindLast(s+2, 1, sub_supp, 2)
) {
log_err("error: one of the u_str[chr etc](U+10402) incorrectly finds a supplementary c.p. in a single UChar\n");
}
/* do not find U+10403 in s[1..10[ */
if(
NULL!=u_strchr32(s+1, supp2) ||
NULL!=u_memchr32(s+1, supp2, 9) ||
NULL!=u_strstr(s+1, sub_supp2) ||
NULL!=u_strFindFirst(s+1, -1, sub_supp2, -1) ||
NULL!=u_strFindFirst(s+1, -1, sub_supp2, 2) ||
NULL!=u_strFindFirst(s+1, 9, sub_supp2, -1) ||
NULL!=u_strFindFirst(s+1, 9, sub_supp2, 2) ||
NULL!=u_strrchr32(s+1, supp2) ||
NULL!=u_memrchr32(s+1, supp2, 9) ||
NULL!=u_strrstr(s+1, sub_supp2) ||
NULL!=u_strFindLast(s+1, -1, sub_supp2, -1) ||
NULL!=u_strFindLast(s+1, -1, sub_supp2, 2) ||
NULL!=u_strFindLast(s+1, 9, sub_supp2, -1) ||
NULL!=u_strFindLast(s+1, 9, sub_supp2, 2)
) {
log_err("error: one of the u_str[chr etc](U+10403) incorrectly finds something\n");
}
/* find <0061 d801> in s[1..10[ */
first=s+5;
if(
first!=u_strstr(s+1, sub_a_lead) ||
first!=u_strFindFirst(s+1, -1, sub_a_lead, -1) ||
first!=u_strFindFirst(s+1, -1, sub_a_lead, 2) ||
first!=u_strFindFirst(s+1, 9, sub_a_lead, -1) ||
first!=u_strFindFirst(s+1, 9, sub_a_lead, 2) ||
first!=u_strrstr(s+1, sub_a_lead) ||
first!=u_strFindLast(s+1, -1, sub_a_lead, -1) ||
first!=u_strFindLast(s+1, -1, sub_a_lead, 2) ||
first!=u_strFindLast(s+1, 9, sub_a_lead, -1) ||
first!=u_strFindLast(s+1, 9, sub_a_lead, 2)
) {
log_err("error: one of the u_str[str etc](<0061 d801>) does not find the correct place\n");
}
/* find <dc02 0061> in s[1..10[ */
first=s+4;
if(
first!=u_strstr(s+1, sub_trail_a) ||
first!=u_strFindFirst(s+1, -1, sub_trail_a, -1) ||
first!=u_strFindFirst(s+1, -1, sub_trail_a, 2) ||
first!=u_strFindFirst(s+1, 9, sub_trail_a, -1) ||
first!=u_strFindFirst(s+1, 9, sub_trail_a, 2) ||
first!=u_strrstr(s+1, sub_trail_a) ||
first!=u_strFindLast(s+1, -1, sub_trail_a, -1) ||
first!=u_strFindLast(s+1, -1, sub_trail_a, 2) ||
first!=u_strFindLast(s+1, 9, sub_trail_a, -1) ||
first!=u_strFindLast(s+1, 9, sub_trail_a, 2)
) {
log_err("error: one of the u_str[str etc](<dc02 0061>) does not find the correct place\n");
}
/* do not find "aba" in s[1..10[ */
if(
NULL!=u_strstr(s+1, sub_aba) ||
NULL!=u_strFindFirst(s+1, -1, sub_aba, -1) ||
NULL!=u_strFindFirst(s+1, -1, sub_aba, 3) ||
NULL!=u_strFindFirst(s+1, 9, sub_aba, -1) ||
NULL!=u_strFindFirst(s+1, 9, sub_aba, 3) ||
NULL!=u_strrstr(s+1, sub_aba) ||
NULL!=u_strFindLast(s+1, -1, sub_aba, -1) ||
NULL!=u_strFindLast(s+1, -1, sub_aba, 3) ||
NULL!=u_strFindLast(s+1, 9, sub_aba, -1) ||
NULL!=u_strFindLast(s+1, 9, sub_aba, 3)
) {
log_err("error: one of the u_str[str etc](\"aba\") incorrectly finds something\n");
}
/* Regression test for ICU-20684 Use-of-uninitialized-value in isMatchAtCPBoundary
* Condition: search the same string while the first char is not an
* surrogate and the last char is the leading surragte.
*/
{
static const UChar s[]={ 0x0020, 0xD9C1 };
if (u_strFindFirst(s, 2, s, 2) != s) {
log_err("error: ending with a partial supplementary code point should match\n");
}
}
}
static void TestStringCopy()
{
UChar temp[40];
UChar *result=0;
UChar subString[5];
UChar uchars[]={0x61, 0x62, 0x63, 0x00};
char charOut[40];
char chars[]="abc"; /* needs default codepage */
log_verbose("Testing u_uastrncpy() and u_uastrcpy()");
u_uastrcpy(temp, "abc");
if(u_strcmp(temp, uchars) != 0) {
log_err("There is an error in u_uastrcpy() Expected %s Got %s\n", austrdup(uchars), austrdup(temp));
}
temp[0] = 0xFB; /* load garbage into it */
temp[1] = 0xFB;
temp[2] = 0xFB;