forked from unicode-org/icu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvtest.cpp
1866 lines (1657 loc) · 68.3 KB
/
convtest.cpp
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) 2003-2014, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
* file name: convtest.cpp
* encoding: UTF-8
* tab size: 8 (not used)
* indentation:4
*
* created on: 2003jul15
* created by: Markus W. Scherer
*
* Test file for data-driven conversion tests.
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_LEGACY_CONVERSION
/*
* Note: Turning off all of convtest.cpp if !UCONFIG_NO_LEGACY_CONVERSION
* is slightly unnecessary - it removes tests for Unicode charsets
* like UTF-8 that should work.
* However, there is no easy way for the test to detect whether a test case
* is for a Unicode charset, so it would be difficult to only exclude those.
* Also, regular testing of ICU is done with all modules on, therefore
* not testing conversion for a custom configuration like this should be ok.
*/
#include "unicode/ucnv.h"
#include "unicode/unistr.h"
#include "unicode/parsepos.h"
#include "unicode/uniset.h"
#include "unicode/usetiter.h"
#include "unicode/ustring.h"
#include "unicode/ures.h"
#include "unicode/utf16.h"
#include "convtest.h"
#include "cmemory.h"
#include "unicode/tstdtmod.h"
#include <string.h>
#include <stdlib.h>
enum {
// characters used in test data for callbacks
SUB_CB='?',
SKIP_CB='0',
STOP_CB='.',
ESC_CB='&'
};
ConversionTest::ConversionTest() {
UErrorCode errorCode=U_ZERO_ERROR;
utf8Cnv=ucnv_open("UTF-8", &errorCode);
ucnv_setToUCallBack(utf8Cnv, UCNV_TO_U_CALLBACK_STOP, nullptr, nullptr, nullptr, &errorCode);
if(U_FAILURE(errorCode)) {
errln("unable to open UTF-8 converter");
}
}
ConversionTest::~ConversionTest() {
ucnv_close(utf8Cnv);
}
void
ConversionTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
if (exec) logln("TestSuite ConversionTest: ");
TESTCASE_AUTO_BEGIN;
#if !UCONFIG_NO_FILE_IO
TESTCASE_AUTO(TestToUnicode);
TESTCASE_AUTO(TestFromUnicode);
TESTCASE_AUTO(TestGetUnicodeSet);
#endif
TESTCASE_AUTO(TestGetUnicodeSet2);
TESTCASE_AUTO(TestDefaultIgnorableCallback);
TESTCASE_AUTO(TestUTF8ToUTF8Overflow);
TESTCASE_AUTO(TestUTF8ToUTF8Streaming);
TESTCASE_AUTO_END;
}
// test data interface ----------------------------------------------------- ***
void
ConversionTest::TestToUnicode() {
ConversionCase cc;
char charset[100], cbopt[4];
const char *option;
UnicodeString s, unicode;
int32_t offsetsLength;
UConverterToUCallback callback;
TestDataModule *dataModule;
TestData *testData;
const DataMap *testCase;
UErrorCode errorCode;
int32_t i;
errorCode=U_ZERO_ERROR;
dataModule=TestDataModule::getTestDataModule("conversion", *this, errorCode);
if(U_SUCCESS(errorCode)) {
testData=dataModule->createTestData("toUnicode", errorCode);
if(U_SUCCESS(errorCode)) {
for(i=0; testData->nextCase(testCase, errorCode); ++i) {
if(U_FAILURE(errorCode)) {
errln("error retrieving conversion/toUnicode test case %d - %s",
i, u_errorName(errorCode));
errorCode=U_ZERO_ERROR;
continue;
}
cc.caseNr=i;
s=testCase->getString("charset", errorCode);
s.extract(0, 0x7fffffff, charset, sizeof(charset), "");
cc.charset=charset;
cc.bytes=testCase->getBinary(cc.bytesLength, "bytes", errorCode);
unicode=testCase->getString("unicode", errorCode);
cc.unicode=unicode.getBuffer();
cc.unicodeLength=unicode.length();
offsetsLength=0;
cc.offsets=testCase->getIntVector(offsetsLength, "offsets", errorCode);
if(offsetsLength==0) {
cc.offsets=nullptr;
} else if(offsetsLength!=unicode.length()) {
errln("toUnicode[%d] unicode[%d] and offsets[%d] must have the same length",
i, unicode.length(), offsetsLength);
errorCode=U_ILLEGAL_ARGUMENT_ERROR;
}
cc.finalFlush= 0!=testCase->getInt28("flush", errorCode);
cc.fallbacks= 0!=testCase->getInt28("fallbacks", errorCode);
s=testCase->getString("errorCode", errorCode);
if(s==UNICODE_STRING("invalid", 7)) {
cc.outErrorCode=U_INVALID_CHAR_FOUND;
} else if(s==UNICODE_STRING("illegal", 7)) {
cc.outErrorCode=U_ILLEGAL_CHAR_FOUND;
} else if(s==UNICODE_STRING("truncated", 9)) {
cc.outErrorCode=U_TRUNCATED_CHAR_FOUND;
} else if(s==UNICODE_STRING("illesc", 6)) {
cc.outErrorCode=U_ILLEGAL_ESCAPE_SEQUENCE;
} else if(s==UNICODE_STRING("unsuppesc", 9)) {
cc.outErrorCode=U_UNSUPPORTED_ESCAPE_SEQUENCE;
} else {
cc.outErrorCode=U_ZERO_ERROR;
}
s=testCase->getString("callback", errorCode);
s.extract(0, 0x7fffffff, cbopt, sizeof(cbopt), "");
cc.cbopt=cbopt;
switch(cbopt[0]) {
case SUB_CB:
callback=UCNV_TO_U_CALLBACK_SUBSTITUTE;
break;
case SKIP_CB:
callback=UCNV_TO_U_CALLBACK_SKIP;
break;
case STOP_CB:
callback=UCNV_TO_U_CALLBACK_STOP;
break;
case ESC_CB:
callback=UCNV_TO_U_CALLBACK_ESCAPE;
break;
default:
callback=nullptr;
break;
}
option=callback==nullptr ? cbopt : cbopt+1;
if(*option==0) {
option=nullptr;
}
cc.invalidChars=testCase->getBinary(cc.invalidLength, "invalidChars", errorCode);
if(U_FAILURE(errorCode)) {
errln("error parsing conversion/toUnicode test case %d - %s",
i, u_errorName(errorCode));
errorCode=U_ZERO_ERROR;
} else {
logln("TestToUnicode[%d] %s", i, charset);
ToUnicodeCase(cc, callback, option);
}
}
delete testData;
}
delete dataModule;
}
else {
dataerrln("Could not load test conversion data");
}
}
void
ConversionTest::TestFromUnicode() {
ConversionCase cc;
char charset[100], cbopt[4];
const char *option;
UnicodeString s, unicode, invalidUChars;
int32_t offsetsLength, index;
UConverterFromUCallback callback;
TestDataModule *dataModule;
TestData *testData;
const DataMap *testCase;
const char16_t *p;
UErrorCode errorCode;
int32_t i, length;
errorCode=U_ZERO_ERROR;
dataModule=TestDataModule::getTestDataModule("conversion", *this, errorCode);
if(U_SUCCESS(errorCode)) {
testData=dataModule->createTestData("fromUnicode", errorCode);
if(U_SUCCESS(errorCode)) {
for(i=0; testData->nextCase(testCase, errorCode); ++i) {
if(U_FAILURE(errorCode)) {
errln("error retrieving conversion/fromUnicode test case %d - %s",
i, u_errorName(errorCode));
errorCode=U_ZERO_ERROR;
continue;
}
cc.caseNr=i;
s=testCase->getString("charset", errorCode);
s.extract(0, 0x7fffffff, charset, sizeof(charset), "");
cc.charset=charset;
unicode=testCase->getString("unicode", errorCode);
cc.unicode=unicode.getBuffer();
cc.unicodeLength=unicode.length();
cc.bytes=testCase->getBinary(cc.bytesLength, "bytes", errorCode);
offsetsLength=0;
cc.offsets=testCase->getIntVector(offsetsLength, "offsets", errorCode);
if(offsetsLength==0) {
cc.offsets=nullptr;
} else if(offsetsLength!=cc.bytesLength) {
errln("fromUnicode[%d] bytes[%d] and offsets[%d] must have the same length",
i, cc.bytesLength, offsetsLength);
errorCode=U_ILLEGAL_ARGUMENT_ERROR;
}
cc.finalFlush= 0!=testCase->getInt28("flush", errorCode);
cc.fallbacks= 0!=testCase->getInt28("fallbacks", errorCode);
s=testCase->getString("errorCode", errorCode);
if(s==UNICODE_STRING("invalid", 7)) {
cc.outErrorCode=U_INVALID_CHAR_FOUND;
} else if(s==UNICODE_STRING("illegal", 7)) {
cc.outErrorCode=U_ILLEGAL_CHAR_FOUND;
} else if(s==UNICODE_STRING("truncated", 9)) {
cc.outErrorCode=U_TRUNCATED_CHAR_FOUND;
} else {
cc.outErrorCode=U_ZERO_ERROR;
}
s=testCase->getString("callback", errorCode);
cc.setSub=0; // default: no subchar
if((index=s.indexOf((char16_t)0))>0) {
// read NUL-separated subchar first, if any
// copy the subchar from Latin-1 characters
// start after the NUL
p=s.getTerminatedBuffer();
length=index+1;
p+=length;
length=s.length()-length;
if(length<=0 || length>=(int32_t)sizeof(cc.subchar)) {
errorCode=U_ILLEGAL_ARGUMENT_ERROR;
} else {
int32_t j;
for(j=0; j<length; ++j) {
cc.subchar[j]=(char)p[j];
}
// NUL-terminate the subchar
cc.subchar[j]=0;
cc.setSub=1;
}
// remove the NUL and subchar from s
s.truncate(index);
} else if((index=s.indexOf((char16_t)0x3d))>0) /* '=' */ {
// read a substitution string, separated by an equal sign
p=s.getBuffer()+index+1;
length=s.length()-(index+1);
if(length<0 || length>=UPRV_LENGTHOF(cc.subString)) {
errorCode=U_ILLEGAL_ARGUMENT_ERROR;
} else {
u_memcpy(cc.subString, p, length);
// NUL-terminate the subString
cc.subString[length]=0;
cc.setSub=-1;
}
// remove the equal sign and subString from s
s.truncate(index);
}
s.extract(0, 0x7fffffff, cbopt, sizeof(cbopt), "");
cc.cbopt=cbopt;
switch(cbopt[0]) {
case SUB_CB:
callback=UCNV_FROM_U_CALLBACK_SUBSTITUTE;
break;
case SKIP_CB:
callback=UCNV_FROM_U_CALLBACK_SKIP;
break;
case STOP_CB:
callback=UCNV_FROM_U_CALLBACK_STOP;
break;
case ESC_CB:
callback=UCNV_FROM_U_CALLBACK_ESCAPE;
break;
default:
callback=nullptr;
break;
}
option=callback==nullptr ? cbopt : cbopt+1;
if(*option==0) {
option=nullptr;
}
invalidUChars=testCase->getString("invalidUChars", errorCode);
cc.invalidUChars=invalidUChars.getBuffer();
cc.invalidLength=invalidUChars.length();
if(U_FAILURE(errorCode)) {
errln("error parsing conversion/fromUnicode test case %d - %s",
i, u_errorName(errorCode));
errorCode=U_ZERO_ERROR;
} else {
logln("TestFromUnicode[%d] %s", i, charset);
FromUnicodeCase(cc, callback, option);
}
}
delete testData;
}
delete dataModule;
}
else {
dataerrln("Could not load test conversion data");
}
}
static const char16_t ellipsis[]={ 0x2e, 0x2e, 0x2e };
void
ConversionTest::TestGetUnicodeSet() {
char charset[100];
UnicodeString s, map, mapnot;
int32_t which;
ParsePosition pos;
UnicodeSet cnvSet, mapSet, mapnotSet, diffSet;
UnicodeSet *cnvSetPtr = &cnvSet;
LocalUConverterPointer cnv;
TestDataModule *dataModule;
TestData *testData;
const DataMap *testCase;
UErrorCode errorCode;
int32_t i;
errorCode=U_ZERO_ERROR;
dataModule=TestDataModule::getTestDataModule("conversion", *this, errorCode);
if(U_SUCCESS(errorCode)) {
testData=dataModule->createTestData("getUnicodeSet", errorCode);
if(U_SUCCESS(errorCode)) {
for(i=0; testData->nextCase(testCase, errorCode); ++i) {
if(U_FAILURE(errorCode)) {
errln("error retrieving conversion/getUnicodeSet test case %d - %s",
i, u_errorName(errorCode));
errorCode=U_ZERO_ERROR;
continue;
}
s=testCase->getString("charset", errorCode);
s.extract(0, 0x7fffffff, charset, sizeof(charset), "");
map=testCase->getString("map", errorCode);
mapnot=testCase->getString("mapnot", errorCode);
which=testCase->getInt28("which", errorCode);
if(U_FAILURE(errorCode)) {
errln("error parsing conversion/getUnicodeSet test case %d - %s",
i, u_errorName(errorCode));
errorCode=U_ZERO_ERROR;
continue;
}
// test this test case
mapSet.clear();
mapnotSet.clear();
pos.setIndex(0);
mapSet.applyPattern(map, pos, 0, nullptr, errorCode);
if(U_FAILURE(errorCode) || pos.getIndex()!=map.length()) {
errln("error creating the map set for conversion/getUnicodeSet test case %d - %s\n"
" error index %d index %d U+%04x",
i, u_errorName(errorCode), pos.getErrorIndex(), pos.getIndex(), map.char32At(pos.getIndex()));
errorCode=U_ZERO_ERROR;
continue;
}
pos.setIndex(0);
mapnotSet.applyPattern(mapnot, pos, 0, nullptr, errorCode);
if(U_FAILURE(errorCode) || pos.getIndex()!=mapnot.length()) {
errln("error creating the mapnot set for conversion/getUnicodeSet test case %d - %s\n"
" error index %d index %d U+%04x",
i, u_errorName(errorCode), pos.getErrorIndex(), pos.getIndex(), mapnot.char32At(pos.getIndex()));
errorCode=U_ZERO_ERROR;
continue;
}
logln("TestGetUnicodeSet[%d] %s", i, charset);
cnv.adoptInstead(cnv_open(charset, errorCode));
if(U_FAILURE(errorCode)) {
errcheckln(errorCode, "error opening \"%s\" for conversion/getUnicodeSet test case %d - %s",
charset, i, u_errorName(errorCode));
errorCode=U_ZERO_ERROR;
continue;
}
ucnv_getUnicodeSet(cnv.getAlias(), cnvSetPtr->toUSet(), (UConverterUnicodeSet)which, &errorCode);
if(U_FAILURE(errorCode)) {
errln("error in ucnv_getUnicodeSet(\"%s\") for conversion/getUnicodeSet test case %d - %s",
charset, i, u_errorName(errorCode));
errorCode=U_ZERO_ERROR;
continue;
}
// are there items that must be in cnvSet but are not?
(diffSet=mapSet).removeAll(cnvSet);
if(!diffSet.isEmpty()) {
diffSet.toPattern(s, true);
if(s.length()>100) {
s.replace(100, 0x7fffffff, ellipsis, UPRV_LENGTHOF(ellipsis));
}
errln("error: ucnv_getUnicodeSet(\"%s\") is missing items - conversion/getUnicodeSet test case %d",
charset, i);
errln(s);
}
// are there items that must not be in cnvSet but are?
(diffSet=mapnotSet).retainAll(cnvSet);
if(!diffSet.isEmpty()) {
diffSet.toPattern(s, true);
if(s.length()>100) {
s.replace(100, 0x7fffffff, ellipsis, UPRV_LENGTHOF(ellipsis));
}
errln("error: ucnv_getUnicodeSet(\"%s\") contains unexpected items - conversion/getUnicodeSet test case %d",
charset, i);
errln(s);
}
}
delete testData;
}
delete dataModule;
}
else {
dataerrln("Could not load test conversion data");
}
}
U_CDECL_BEGIN
static void U_CALLCONV
getUnicodeSetCallback(const void *context,
UConverterFromUnicodeArgs * /*fromUArgs*/,
const char16_t* /*codeUnits*/,
int32_t /*length*/,
UChar32 codePoint,
UConverterCallbackReason reason,
UErrorCode *pErrorCode) {
if(reason<=UCNV_IRREGULAR) {
static_cast<UnicodeSet *>(const_cast<void*>(context))->remove(codePoint); // the converter cannot convert this code point
*pErrorCode=U_ZERO_ERROR; // skip
} // else ignore the reset, close and clone calls.
}
U_CDECL_END
// Compare ucnv_getUnicodeSet() with the set of characters that can be converted.
void
ConversionTest::TestGetUnicodeSet2() {
// Build a string with all code points.
UChar32 cpLimit;
int32_t s0Length;
if(quick) {
cpLimit=s0Length=0x10000; // BMP only
} else {
cpLimit=0x110000;
s0Length=0x10000+0x200000; // BMP + surrogate pairs
}
char16_t *s0=new char16_t[s0Length];
if(s0==nullptr) {
return;
}
char16_t *s=s0;
UChar32 c;
char16_t c2;
// low BMP
for(c=0; c<=0xd7ff; ++c) {
*s++=(char16_t)c;
}
// trail surrogates
for(c=0xdc00; c<=0xdfff; ++c) {
*s++=(char16_t)c;
}
// lead surrogates
// (after trails so that there is not even one surrogate pair in between)
for(c=0xd800; c<=0xdbff; ++c) {
*s++=(char16_t)c;
}
// high BMP
for(c=0xe000; c<=0xffff; ++c) {
*s++=(char16_t)c;
}
// supplementary code points = surrogate pairs
if(cpLimit==0x110000) {
for(c=0xd800; c<=0xdbff; ++c) {
for(c2=0xdc00; c2<=0xdfff; ++c2) {
*s++=(char16_t)c;
*s++=c2;
}
}
}
static const char *const cnvNames[]={
"UTF-8",
"UTF-7",
"UTF-16",
"US-ASCII",
"ISO-8859-1",
"windows-1252",
"Shift-JIS",
"ibm-1390", // EBCDIC_STATEFUL table
"ibm-16684", // DBCS-only extension table based on EBCDIC_STATEFUL table
"HZ",
"ISO-2022-JP",
"JIS7",
"ISO-2022-CN",
"ISO-2022-CN-EXT",
"LMBCS"
};
LocalUConverterPointer cnv;
char buffer[1024];
int32_t i;
for(i=0; i<UPRV_LENGTHOF(cnvNames); ++i) {
UErrorCode errorCode=U_ZERO_ERROR;
cnv.adoptInstead(cnv_open(cnvNames[i], errorCode));
if(U_FAILURE(errorCode)) {
errcheckln(errorCode, "failed to open converter %s - %s", cnvNames[i], u_errorName(errorCode));
continue;
}
UnicodeSet expected;
ucnv_setFromUCallBack(cnv.getAlias(), getUnicodeSetCallback, &expected, nullptr, nullptr, &errorCode);
if(U_FAILURE(errorCode)) {
errln("failed to set the callback on converter %s - %s", cnvNames[i], u_errorName(errorCode));
continue;
}
UConverterUnicodeSet which;
for(which=UCNV_ROUNDTRIP_SET; which<UCNV_SET_COUNT; which=(UConverterUnicodeSet)((int)which+1)) {
if(which==UCNV_ROUNDTRIP_AND_FALLBACK_SET) {
ucnv_setFallback(cnv.getAlias(), true);
}
expected.add(0, cpLimit-1);
s=s0;
UBool flush;
do {
char *t=buffer;
flush=(UBool)(s==s0+s0Length);
ucnv_fromUnicode(cnv.getAlias(), &t, buffer+sizeof(buffer), (const char16_t **)&s, s0+s0Length, nullptr, flush, &errorCode);
if(U_FAILURE(errorCode)) {
if(errorCode==U_BUFFER_OVERFLOW_ERROR) {
errorCode=U_ZERO_ERROR;
continue;
} else {
break; // unexpected error, should not occur
}
}
} while(!flush);
UnicodeSet set;
ucnv_getUnicodeSet(cnv.getAlias(), set.toUSet(), which, &errorCode);
if(cpLimit<0x110000) {
set.remove(cpLimit, 0x10ffff);
}
if(which==UCNV_ROUNDTRIP_SET) {
// ignore PUA code points because they will be converted even if they
// are fallbacks and when other fallbacks are turned off,
// but ucnv_getUnicodeSet(UCNV_ROUNDTRIP_SET) delivers true roundtrips
expected.remove(0xe000, 0xf8ff);
expected.remove(0xf0000, 0xffffd);
expected.remove(0x100000, 0x10fffd);
set.remove(0xe000, 0xf8ff);
set.remove(0xf0000, 0xffffd);
set.remove(0x100000, 0x10fffd);
}
if(set!=expected) {
// First try to see if we have different sets because ucnv_getUnicodeSet()
// added strings: The above conversion method does not tell us what strings might be convertible.
// Remove strings from the set and compare again.
set.removeAllStrings();
}
if(set!=expected) {
UnicodeSet diffSet;
UnicodeString out;
// are there items that must be in the set but are not?
(diffSet=expected).removeAll(set);
if(!diffSet.isEmpty()) {
diffSet.toPattern(out, true);
if(out.length()>100) {
out.replace(100, 0x7fffffff, ellipsis, UPRV_LENGTHOF(ellipsis));
}
errln("error: ucnv_getUnicodeSet(\"%s\") is missing items - which set: %d",
cnvNames[i], which);
errln(out);
}
// are there items that must not be in the set but are?
(diffSet=set).removeAll(expected);
if(!diffSet.isEmpty()) {
diffSet.toPattern(out, true);
if(out.length()>100) {
out.replace(100, 0x7fffffff, ellipsis, UPRV_LENGTHOF(ellipsis));
}
errln("error: ucnv_getUnicodeSet(\"%s\") contains unexpected items - which set: %d",
cnvNames[i], which);
errln(out);
}
}
}
}
delete [] s0;
}
// Test that all code points which have the default ignorable Unicode property
// are ignored if they have no mapping.
// If there are any failures, the hard coded list (IS_DEFAULT_IGNORABLE_CODE_POINT)
// in ucnv_err.cpp should be updated.
void
ConversionTest::TestDefaultIgnorableCallback() {
UErrorCode status = U_ZERO_ERROR;
const char *cnv_name = "euc-jp-2007";
const char *pattern_ignorable = "[:Default_Ignorable_Code_Point:]";
const char *pattern_not_ignorable =
"[[:^Default_Ignorable_Code_Point:]"
// For test performance, skip large ranges that will likely remain unassigned
// for a long time, and private use code points.
"-[\\U00040000-\\U000DFFFF]-[:Co:]"
"]";
LocalPointer<UnicodeSet> set_ignorable(new UnicodeSet(pattern_ignorable, status));
if (U_FAILURE(status)) {
dataerrln("Unable to create Unicodeset: %s - %s\n", pattern_ignorable, u_errorName(status));
return;
}
LocalPointer<UnicodeSet> set_not_ignorable(new UnicodeSet(pattern_not_ignorable, status));
if (U_FAILURE(status)) {
dataerrln("Unable to create Unicodeset: %s - %s\n", pattern_not_ignorable, u_errorName(status));
return;
}
LocalUConverterPointer cnv(cnv_open(cnv_name, status));
if (U_FAILURE(status)) {
dataerrln("Unable to open converter: %s - %s\n", cnv_name, u_errorName(status));
return;
}
// set callback for the converter
ucnv_setFromUCallBack(cnv.getAlias(), UCNV_FROM_U_CALLBACK_SUBSTITUTE, nullptr, nullptr, nullptr, &status);
UChar32 input[1];
char output[10];
int32_t outputLength;
// test default ignorables are ignored
UnicodeSetIterator iter(*set_ignorable);
while (iter.next()) {
status = U_ZERO_ERROR;
outputLength= 0;
input[0] = iter.getCodepoint();
outputLength = ucnv_fromUChars(cnv.getAlias(), output, 10, UnicodeString::fromUTF32(input, 1).getTerminatedBuffer(), -1, &status);
if (U_FAILURE(status) || outputLength != 0) {
errln("Ignorable code point: U+%04X not skipped as expected - %s", input[0], u_errorName(status));
}
}
// test non-ignorables are not ignored
iter.reset(*set_not_ignorable);
while (iter.next()) {
status = U_ZERO_ERROR;
outputLength= 0;
input[0] = iter.getCodepoint();
if (input[0] == 0) {
continue;
}
outputLength = ucnv_fromUChars(cnv.getAlias(), output, 10, UnicodeString::fromUTF32(input, 1).getTerminatedBuffer(), -1, &status);
if (U_FAILURE(status) || outputLength <= 0) {
errln("Non-ignorable code point: U+%04X skipped unexpectedly - %s", input[0], u_errorName(status));
}
}
}
void
ConversionTest::TestUTF8ToUTF8Overflow() {
IcuTestErrorCode errorCode(*this, "TestUTF8ToUTF8Overflow");
LocalUConverterPointer cnv1(ucnv_open("UTF-8", errorCode));
LocalUConverterPointer cnv2(ucnv_open("UTF-8", errorCode));
static const char *text = "aä"; // ä: 2 bytes
const char *source = text;
const char *sourceLimit = text + strlen(text);
char result[20];
char *target = result;
const char *targetLimit = result + sizeof(result);
char16_t buffer16[20];
char16_t *pivotSource = buffer16;
char16_t *pivotTarget = buffer16;
const char16_t *pivotLimit = buffer16 + UPRV_LENGTHOF(buffer16);
int32_t length;
// Convert with insufficient target capacity.
result[2] = 5;
ucnv_convertEx(cnv2.getAlias(), cnv1.getAlias(),
&target, result + 2, &source, sourceLimit,
buffer16, &pivotSource, &pivotTarget, pivotLimit,
false, false, errorCode);
assertEquals("overflow", U_BUFFER_OVERFLOW_ERROR, errorCode.reset());
length = (int32_t)(target - result);
assertEquals("number of bytes written", 2, length);
assertEquals("next byte not clobbered", 5, result[2]);
// Convert the rest and flush.
ucnv_convertEx(cnv2.getAlias(), cnv1.getAlias(),
&target, targetLimit, &source, sourceLimit,
buffer16, &pivotSource, &pivotTarget, pivotLimit,
false, true, errorCode);
assertSuccess("UTF-8->UTF-8", errorCode);
length = (int32_t)(target - result);
assertEquals("3 bytes", 3, length);
if (length == 3) {
assertTrue("result same as input", memcmp(text, result, length) == 0);
}
ucnv_reset(cnv1.getAlias());
ucnv_reset(cnv2.getAlias());
memset(result, 0, sizeof(result));
static const char *text2 = "a🚲"; // U+1F6B2 bicycle: 4 bytes
source = text2;
sourceLimit = text2 + strlen(text2);
target = result;
pivotSource = pivotTarget = buffer16;
// Convert with insufficient target capacity.
result[3] = 5;
ucnv_convertEx(cnv2.getAlias(), cnv1.getAlias(),
&target, result + 3, &source, sourceLimit,
buffer16, &pivotSource, &pivotTarget, pivotLimit,
false, false, errorCode);
assertEquals("text2 overflow", U_BUFFER_OVERFLOW_ERROR, errorCode.reset());
length = (int32_t)(target - result);
assertEquals("text2 number of bytes written", 3, length);
assertEquals("text2 next byte not clobbered", 5, result[3]);
// Convert the rest and flush.
ucnv_convertEx(cnv2.getAlias(), cnv1.getAlias(),
&target, targetLimit, &source, sourceLimit,
buffer16, &pivotSource, &pivotTarget, pivotLimit,
false, true, errorCode);
assertSuccess("text2 UTF-8->UTF-8", errorCode);
length = (int32_t)(target - result);
assertEquals("text2 5 bytes", 5, length);
if (length == 5) {
assertTrue("text2 result same as input", memcmp(text2, result, length) == 0);
}
ucnv_reset(cnv1.getAlias());
ucnv_reset(cnv2.getAlias());
memset(result, 0, sizeof(result));
static const char *illFormed = "\xf1\x91\x93\x96\x91\x94"; // U+514D6 + two more trail bytes
source = illFormed;
sourceLimit = illFormed + strlen(illFormed);
target = result;
pivotSource = pivotTarget = buffer16;
ucnv_setToUCallBack(cnv1.getAlias(), UCNV_TO_U_CALLBACK_STOP, nullptr, nullptr, nullptr, errorCode);
// Convert only two bytes and flush (but expect failure).
char errorBytes[10];
int8_t errorLength;
result[0] = 5;
ucnv_convertEx(cnv2.getAlias(), cnv1.getAlias(),
&target, targetLimit, &source, source + 2,
buffer16, &pivotSource, &pivotTarget, pivotLimit,
false, true, errorCode);
assertEquals("illFormed truncated", U_TRUNCATED_CHAR_FOUND, errorCode.reset());
length = (int32_t)(target - result);
assertEquals("illFormed number of bytes written", 0, length);
errorLength = UPRV_LENGTHOF(errorBytes);
ucnv_getInvalidChars(cnv1.getAlias(), errorBytes, &errorLength, errorCode);
assertEquals("illFormed truncated errorLength", 2, (int32_t)errorLength);
if (errorLength == 2) {
assertEquals("illFormed truncated errorBytes", 0xf191,
((int32_t)(uint8_t)errorBytes[0] << 8) | (uint8_t)errorBytes[1]);
}
// Continue conversion starting with a trail byte.
ucnv_convertEx(cnv2.getAlias(), cnv1.getAlias(),
&target, targetLimit, &source, sourceLimit,
buffer16, &pivotSource, &pivotTarget, pivotLimit,
false, true, errorCode);
assertEquals("illFormed trail byte", U_ILLEGAL_CHAR_FOUND, errorCode.reset());
length = (int32_t)(target - result);
assertEquals("illFormed trail byte number of bytes written", 0, length);
errorLength = UPRV_LENGTHOF(errorBytes);
ucnv_getInvalidChars(cnv1.getAlias(), errorBytes, &errorLength, errorCode);
assertEquals("illFormed trail byte errorLength", 1, (int32_t)errorLength);
if (errorLength == 1) {
assertEquals("illFormed trail byte errorBytes", 0x93, (int32_t)(uint8_t)errorBytes[0]);
}
}
void
ConversionTest::TestUTF8ToUTF8Streaming() {
IcuTestErrorCode errorCode(*this, "TestUTF8ToUTF8Streaming");
LocalUConverterPointer cnv1(ucnv_open("UTF-8", errorCode));
LocalUConverterPointer cnv2(ucnv_open("UTF-8", errorCode));
// UTF8 encoded cyrillic part of 'Lorem ipsum'
static const char* text =
"\xd0\xb5\xd1\x82\x20\xd1\x81\xd1\x86\xd0\xb0\xd0\xb5\xd0\xb2\xd0"
"\xbe\xd0\xbb\xd0\xb0\x20\xd1\x81\xd0\xb0\xd0\xb4\xd0\xb8\xd0\xbf"
"\xd1\x81\xd1\x86\xd0\xb8\xd0\xbd\xd0\xb3\x20\xd0\xb0\xd1\x86\xd1"
"\x86\xd0\xbe\xd0\xbc\xd0\xbc\xd0\xbe\xd0\xb4\xd0\xb0\xd1\x80\xd0"
"\xb5\x20\xd1\x85\xd0\xb0\xd1\x81";
int32_t chunk1 = 25; // partial lead at the end: 0xd0
int32_t chunk2 = 47; // partial tail at the beginning: 0xb0
char result[128];
int32_t sourceLen = (int32_t)strlen(text);
const char* source = text;
const char* sourceLimit = text + chunk1;
int32_t targetLen = sizeof(result);
char* target = result;
const char* targetLimit = result + targetLen;
char16_t buffer16[20];
char16_t* pivotSource = buffer16;
char16_t* pivotTarget = buffer16;
const char16_t* pivotLimit = buffer16 + UPRV_LENGTHOF(buffer16);
int32_t length;
ucnv_convertEx(cnv2.getAlias(), cnv1.getAlias(),
&target, result + targetLen, &source, sourceLimit,
buffer16, &pivotSource, &pivotTarget, pivotLimit,
false, false, errorCode);
length = (int32_t)(target - result);
targetLen -= length;
assertEquals("First chunk -1 doesn't match converted length", chunk1 - 1, length);
source = text + chunk1;
sourceLimit = source + chunk2;
// Convert the rest and flush.
ucnv_convertEx(cnv2.getAlias(), cnv1.getAlias(),
&target, targetLimit, &source, sourceLimit,
buffer16, &pivotSource, &pivotTarget, pivotLimit,
false, true, errorCode);
length = (int32_t)(target - result - length);
targetLen -= length;
assertEquals("Second chunk + 2 doesn't match converted length", chunk2 + 1, length);
assertEquals("Full text length match", sourceLen, sizeof(result) - targetLen);
assertSuccess("UTF-8->UTF-8", errorCode);
}
// open testdata or ICU data converter ------------------------------------- ***
UConverter *
ConversionTest::cnv_open(const char *name, UErrorCode &errorCode) {
if(name!=nullptr && *name=='+') {
// Converter names that start with '+' are ignored in ICU4J tests.
++name;
}
if(name!=nullptr && *name=='*') {
/* loadTestData(): set the data directory */
return ucnv_openPackage(loadTestData(errorCode), name+1, &errorCode);
} else {
return ucnv_open(name, &errorCode);
}
}
// output helpers ---------------------------------------------------------- ***
static inline char
hexDigit(uint8_t digit) {
return digit<=9 ? (char)('0'+digit) : (char)('a'-10+digit);
}
static char *
printBytes(const uint8_t *bytes, int32_t length, char *out) {
uint8_t b;
if(length>0) {
b=*bytes++;
--length;
*out++=hexDigit((uint8_t)(b>>4));
*out++=hexDigit((uint8_t)(b&0xf));
}
while(length>0) {
b=*bytes++;
--length;
*out++=' ';
*out++=hexDigit((uint8_t)(b>>4));
*out++=hexDigit((uint8_t)(b&0xf));
}
*out++=0;
return out;
}
static char *
printUnicode(const char16_t *unicode, int32_t length, char *out) {
UChar32 c;
int32_t i;
for(i=0; i<length;) {
if(i>0) {
*out++=' ';
}
U16_NEXT(unicode, i, length, c);
// write 4..6 digits
if(c>=0x100000) {
*out++='1';
}
if(c>=0x10000) {
*out++=hexDigit((uint8_t)((c>>16)&0xf));
}
*out++=hexDigit((uint8_t)((c>>12)&0xf));
*out++=hexDigit((uint8_t)((c>>8)&0xf));
*out++=hexDigit((uint8_t)((c>>4)&0xf));
*out++=hexDigit((uint8_t)(c&0xf));
}
*out++=0;
return out;
}
static char *
printOffsets(const int32_t *offsets, int32_t length, char *out) {
int32_t i, o, d;
if(offsets==nullptr) {
length=0;
}
for(i=0; i<length; ++i) {
if(i>0) {
*out++=' ';
}
o=offsets[i];
// print all offsets with 2 characters each (-x, -9..99, xx)
if(o<-9) {
*out++='-';
*out++='x';
} else if(o<0) {
*out++='-';
*out++=(char)('0'-o);
} else if(o<=99) {
*out++=(d=o/10)==0 ? ' ' : (char)('0'+d);
*out++=(char)('0'+o%10);
} else /* o>99 */ {
*out++='x';
*out++='x';
}
}