forked from unicode-org/icu
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapitst.c
More file actions
2703 lines (2336 loc) · 105 KB
/
capitst.c
File metadata and controls
2703 lines (2336 loc) · 105 KB
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) 1997-2016, International Business Machines
* Corporation and others. All Rights Reserved.
********************************************************************/
/*****************************************************************************
*
* File CAPITEST.C
*
* Modification History:
* Name Description
* Madhu Katragadda Ported for C API
* Brian Rower Added TestOpenVsOpenRules
******************************************************************************
*//* C API TEST For COLLATOR */
#include "unicode/utypes.h"
#if !UCONFIG_NO_COLLATION
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "unicode/uloc.h"
#include "unicode/ulocdata.h"
#include "unicode/ustring.h"
#include "unicode/ures.h"
#include "unicode/ucoleitr.h"
#include "cintltst.h"
#include "capitst.h"
#include "ccolltst.h"
#include "putilimp.h"
#include "cmemory.h"
#include "cstring.h"
#include "ucol_imp.h"
static void TestAttribute(void);
static void TestDefault(void);
static void TestDefaultKeyword(void);
static void TestBengaliSortKey(void);
static char* U_EXPORT2 ucol_sortKeyToString(const UCollator *coll, const uint8_t *sortkey, char *buffer, uint32_t len) {
(void)coll; // suppress compiler warnings about unused variable
uint32_t position = 0;
uint8_t b;
if (position + 1 < len)
position += snprintf(buffer + position, len-position, "[");
while ((b = *sortkey++) != 0) {
if (b == 1 && position + 5 < len) {
position += snprintf(buffer + position, len-position, "%02X . ", b);
} else if (b != 1 && position + 3 < len) {
position += snprintf(buffer + position, len-position, "%02X ", b);
}
}
if (position + 3 < len)
position += snprintf(buffer + position, len-position, "%02X]", b);
return buffer;
}
void addCollAPITest(TestNode** root)
{
/* WEIVTODO: return tests here */
addTest(root, &TestProperty, "tscoll/capitst/TestProperty");
addTest(root, &TestRuleBasedColl, "tscoll/capitst/TestRuleBasedColl");
addTest(root, &TestCompare, "tscoll/capitst/TestCompare");
addTest(root, &TestSortKey, "tscoll/capitst/TestSortKey");
addTest(root, &TestHashCode, "tscoll/capitst/TestHashCode");
addTest(root, &TestElemIter, "tscoll/capitst/TestElemIter");
addTest(root, &TestGetAll, "tscoll/capitst/TestGetAll");
/*addTest(root, &TestGetDefaultRules, "tscoll/capitst/TestGetDefaultRules");*/
addTest(root, &TestDecomposition, "tscoll/capitst/TestDecomposition");
addTest(root, &TestSafeClone, "tscoll/capitst/TestSafeClone");
addTest(root, &TestClone, "tscoll/capitst/TestClone");
addTest(root, &TestCloneBinary, "tscoll/capitst/TestCloneBinary");
addTest(root, &TestGetSetAttr, "tscoll/capitst/TestGetSetAttr");
addTest(root, &TestBounds, "tscoll/capitst/TestBounds");
addTest(root, &TestGetLocale, "tscoll/capitst/TestGetLocale");
addTest(root, &TestSortKeyBufferOverrun, "tscoll/capitst/TestSortKeyBufferOverrun");
addTest(root, &TestAttribute, "tscoll/capitst/TestAttribute");
addTest(root, &TestGetTailoredSet, "tscoll/capitst/TestGetTailoredSet");
addTest(root, &TestMergeSortKeys, "tscoll/capitst/TestMergeSortKeys");
addTest(root, &TestShortString, "tscoll/capitst/TestShortString");
addTest(root, &TestGetContractionsAndUnsafes, "tscoll/capitst/TestGetContractionsAndUnsafes");
addTest(root, &TestOpenBinary, "tscoll/capitst/TestOpenBinary");
addTest(root, &TestDefault, "tscoll/capitst/TestDefault");
addTest(root, &TestDefaultKeyword, "tscoll/capitst/TestDefaultKeyword");
addTest(root, &TestOpenVsOpenRules, "tscoll/capitst/TestOpenVsOpenRules");
addTest(root, &TestBengaliSortKey, "tscoll/capitst/TestBengaliSortKey");
addTest(root, &TestGetKeywordValuesForLocale, "tscoll/capitst/TestGetKeywordValuesForLocale");
addTest(root, &TestStrcollNull, "tscoll/capitst/TestStrcollNull");
addTest(root, &TestLocaleIDWithUnderscoreAndExtension, "tscoll/capitst/TestLocaleIDWithUnderscoreAndExtension");
}
void TestGetSetAttr(void) {
UErrorCode status = U_ZERO_ERROR;
UCollator *coll = ucol_open(NULL, &status);
struct attrTest {
UColAttribute att;
UColAttributeValue val[5];
uint32_t valueSize;
UColAttributeValue nonValue;
} attrs[] = {
{UCOL_FRENCH_COLLATION, {UCOL_ON, UCOL_OFF}, 2, UCOL_SHIFTED},
{UCOL_ALTERNATE_HANDLING, {UCOL_NON_IGNORABLE, UCOL_SHIFTED}, 2, UCOL_OFF},/* attribute for handling variable elements*/
{UCOL_CASE_FIRST, {UCOL_OFF, UCOL_LOWER_FIRST, UCOL_UPPER_FIRST}, 3, UCOL_SHIFTED},/* who goes first, lower case or uppercase */
{UCOL_CASE_LEVEL, {UCOL_ON, UCOL_OFF}, 2, UCOL_SHIFTED},/* do we have an extra case level */
{UCOL_NORMALIZATION_MODE, {UCOL_ON, UCOL_OFF}, 2, UCOL_SHIFTED},/* attribute for normalization */
{UCOL_DECOMPOSITION_MODE, {UCOL_ON, UCOL_OFF}, 2, UCOL_SHIFTED},
{UCOL_STRENGTH, {UCOL_PRIMARY, UCOL_SECONDARY, UCOL_TERTIARY, UCOL_QUATERNARY, UCOL_IDENTICAL}, 5, UCOL_SHIFTED},/* attribute for strength */
{UCOL_HIRAGANA_QUATERNARY_MODE, {UCOL_ON, UCOL_OFF}, 2, UCOL_SHIFTED},/* when turned on, this attribute */
};
UColAttribute currAttr;
UColAttributeValue value;
uint32_t i = 0, j = 0;
if (coll == NULL) {
log_err_status(status, "Unable to open collator. %s\n", u_errorName(status));
return;
}
for(i = 0; i<UPRV_LENGTHOF(attrs); i++) {
currAttr = attrs[i].att;
ucol_setAttribute(coll, currAttr, UCOL_DEFAULT, &status);
if(U_FAILURE(status)) {
log_err_status(status, "ucol_setAttribute with the default value returned error: %s\n", u_errorName(status));
break;
}
value = ucol_getAttribute(coll, currAttr, &status);
if(U_FAILURE(status)) {
log_err("ucol_getAttribute returned error: %s\n", u_errorName(status));
break;
}
for(j = 0; j<attrs[i].valueSize; j++) {
ucol_setAttribute(coll, currAttr, attrs[i].val[j], &status);
if(U_FAILURE(status)) {
log_err("ucol_setAttribute with the value %i returned error: %s\n", attrs[i].val[j], u_errorName(status));
break;
}
}
status = U_ZERO_ERROR;
ucol_setAttribute(coll, currAttr, attrs[i].nonValue, &status);
if(U_SUCCESS(status)) {
log_err("ucol_setAttribute with the bad value didn't return an error\n");
break;
}
status = U_ZERO_ERROR;
ucol_setAttribute(coll, currAttr, value, &status);
if(U_FAILURE(status)) {
log_err("ucol_setAttribute with the default valuereturned error: %s\n", u_errorName(status));
break;
}
}
status = U_ZERO_ERROR;
value = ucol_getAttribute(coll, UCOL_ATTRIBUTE_COUNT, &status);
if(U_SUCCESS(status)) {
log_err("ucol_getAttribute for UCOL_ATTRIBUTE_COUNT didn't return an error\n");
}
status = U_ZERO_ERROR;
ucol_setAttribute(coll, UCOL_ATTRIBUTE_COUNT, UCOL_DEFAULT, &status);
if(U_SUCCESS(status)) {
log_err("ucol_setAttribute for UCOL_ATTRIBUTE_COUNT didn't return an error\n");
}
status = U_ZERO_ERROR;
ucol_close(coll);
}
static void doAssert(int condition, const char *message)
{
if (condition==0) {
log_err("ERROR : %s\n", message);
}
}
#define UTF8_BUF_SIZE 128
static void doStrcoll(const UCollator* coll, const UChar* src, int32_t srcLen, const UChar* tgt, int32_t tgtLen,
UCollationResult expected, const char *message) {
UErrorCode err = U_ZERO_ERROR;
char srcU8[UTF8_BUF_SIZE], tgtU8[UTF8_BUF_SIZE];
int32_t srcU8Len = -1, tgtU8Len = -1;
int32_t len = 0;
if (ucol_strcoll(coll, src, srcLen, tgt, tgtLen) != expected) {
log_err("ERROR : %s\n", message);
}
u_strToUTF8(srcU8, UTF8_BUF_SIZE, &len, src, srcLen, &err);
if (U_FAILURE(err) || len >= UTF8_BUF_SIZE) {
log_err("ERROR : UTF-8 conversion error\n");
return;
}
if (srcLen >= 0) {
srcU8Len = len;
}
u_strToUTF8(tgtU8, UTF8_BUF_SIZE, &len, tgt, tgtLen, &err);
if (U_FAILURE(err) || len >= UTF8_BUF_SIZE) {
log_err("ERROR : UTF-8 conversion error\n");
return;
}
if (tgtLen >= 0) {
tgtU8Len = len;
}
if (ucol_strcollUTF8(coll, srcU8, srcU8Len, tgtU8, tgtU8Len, &err) != expected
|| U_FAILURE(err)) {
log_err("ERROR: %s (strcollUTF8)\n", message);
}
}
#if 0
/* We don't have default rules, at least not in the previous sense */
void TestGetDefaultRules(){
uint32_t size=0;
UErrorCode status=U_ZERO_ERROR;
UCollator *coll=NULL;
int32_t len1 = 0, len2=0;
uint8_t *binColData = NULL;
UResourceBundle *res = NULL;
UResourceBundle *binColl = NULL;
uint8_t *binResult = NULL;
const UChar * defaultRulesArray=ucol_getDefaultRulesArray(&size);
log_verbose("Test the function ucol_getDefaultRulesArray()\n");
coll = ucol_openRules(defaultRulesArray, size, UCOL_ON, UCOL_PRIMARY, &status);
if(U_SUCCESS(status) && coll !=NULL) {
binColData = (uint8_t*)ucol_cloneRuleData(coll, &len1, &status);
}
status=U_ZERO_ERROR;
res=ures_open(NULL, "root", &status);
if(U_FAILURE(status)){
log_err("ERROR: Failed to get resource for \"root Locale\" with %s", myErrorName(status));
return;
}
binColl=ures_getByKey(res, "%%Collation", binColl, &status);
if(U_SUCCESS(status)){
binResult=(uint8_t*)ures_getBinary(binColl, &len2, &status);
if(U_FAILURE(status)){
log_err("ERROR: ures_getBinary() failed\n");
}
}else{
log_err("ERROR: ures_getByKey(locale(default), %%Collation) failed");
}
if(len1 != len2){
log_err("Error: ucol_getDefaultRulesArray() failed to return the correct length.\n");
}
if(memcmp(binColData, binResult, len1) != 0){
log_err("Error: ucol_getDefaultRulesArray() failed\n");
}
free(binColData);
ures_close(binColl);
ures_close(res);
ucol_close(coll);
}
#endif
/* Collator Properties
ucol_open, ucol_strcoll, getStrength/setStrength
getDecomposition/setDecomposition, getDisplayName*/
void TestProperty(void)
{
UCollator *col, *ruled;
const UChar *rules;
UChar *disName;
int32_t len = 0;
UChar source[12], target[12];
int32_t tempLength;
UErrorCode status = U_ZERO_ERROR;
/*
* Expected version of the English collator.
* Currently, the major/minor version numbers change when the builder code
* changes,
* number 2 is from the tailoring data version and
* number 3 is the UCA version.
* This changes with every UCA version change, and the expected value
* needs to be adjusted.
* Same in intltest/apicoll.cpp.
*/
UVersionInfo currVersionArray = {0x31, 0xC0, 0x05, 0x2A}; /* from ICU 4.4/UCA 5.2 */
UVersionInfo versionArray = {0, 0, 0, 0};
UVersionInfo versionUCAArray = {0, 0, 0, 0};
UVersionInfo versionUCDArray = {0, 0, 0, 0};
log_verbose("The property tests begin : \n");
log_verbose("Test ucol_strcoll : \n");
col = ucol_open("en_US", &status);
if (U_FAILURE(status)) {
log_err_status(status, "Default Collator creation failed.: %s\n", myErrorName(status));
return;
}
ucol_getVersion(col, versionArray);
/* Check for a version greater than some value rather than equality
* so that we need not update the expected version each time. */
if (uprv_memcmp(versionArray, currVersionArray, 4)<0) {
log_err("Testing ucol_getVersion() - unexpected result: %02x.%02x.%02x.%02x\n",
versionArray[0], versionArray[1], versionArray[2], versionArray[3]);
} else {
log_verbose("ucol_getVersion() result: %02x.%02x.%02x.%02x\n",
versionArray[0], versionArray[1], versionArray[2], versionArray[3]);
}
/* Assume that the UCD and UCA versions are the same,
* rather than hardcoding (and updating each time) a particular UCA version. */
u_getUnicodeVersion(versionUCDArray);
ucol_getUCAVersion(col, versionUCAArray);
if (0!=uprv_memcmp(versionUCAArray, versionUCDArray, 4)) {
log_err("Testing ucol_getUCAVersion() - unexpected result: %hu.%hu.%hu.%hu\n",
versionUCAArray[0], versionUCAArray[1], versionUCAArray[2], versionUCAArray[3]);
}
u_uastrcpy(source, "ab");
u_uastrcpy(target, "abc");
doStrcoll(col, source, u_strlen(source), target, u_strlen(target), UCOL_LESS, "ab < abc comparison failed");
u_uastrcpy(source, "ab");
u_uastrcpy(target, "AB");
doStrcoll(col, source, u_strlen(source), target, u_strlen(target), UCOL_LESS, "ab < AB comparison failed");
u_uastrcpy(source, "blackbird");
u_uastrcpy(target, "black-bird");
doStrcoll(col, source, u_strlen(source), target, u_strlen(target), UCOL_GREATER, "black-bird > blackbird comparison failed");
u_uastrcpy(source, "black bird");
u_uastrcpy(target, "black-bird");
doStrcoll(col, source, u_strlen(source), target, u_strlen(target), UCOL_LESS, "black bird < black-bird comparison failed");
u_uastrcpy(source, "Hello");
u_uastrcpy(target, "hello");
doStrcoll(col, source, u_strlen(source), target, u_strlen(target), UCOL_GREATER, "Hello > hello comparison failed");
log_verbose("Test ucol_strcoll ends.\n");
log_verbose("testing ucol_getStrength() method ...\n");
doAssert( (ucol_getStrength(col) == UCOL_TERTIARY), "collation object has the wrong strength");
doAssert( (ucol_getStrength(col) != UCOL_PRIMARY), "collation object's strength is primary difference");
log_verbose("testing ucol_setStrength() method ...\n");
ucol_setStrength(col, UCOL_SECONDARY);
doAssert( (ucol_getStrength(col) != UCOL_TERTIARY), "collation object's strength is secondary difference");
doAssert( (ucol_getStrength(col) != UCOL_PRIMARY), "collation object's strength is primary difference");
doAssert( (ucol_getStrength(col) == UCOL_SECONDARY), "collation object has the wrong strength");
log_verbose("Get display name for the default collation in German : \n");
len=ucol_getDisplayName("en_US", "de_DE", NULL, 0, &status);
if(status==U_BUFFER_OVERFLOW_ERROR){
status=U_ZERO_ERROR;
disName=(UChar*)malloc(sizeof(UChar) * (len+1));
ucol_getDisplayName("en_US", "de_DE", disName, len+1, &status);
log_verbose("the display name for default collation in german: %s\n", austrdup(disName) );
free(disName);
}
if(U_FAILURE(status)){
log_err("ERROR: in getDisplayName: %s\n", myErrorName(status));
return;
}
log_verbose("Default collation getDisplayName ended.\n");
ruled = ucol_open("da_DK", &status);
if(U_FAILURE(status)) {
log_data_err("ucol_open(\"da_DK\") failed - %s\n", u_errorName(status));
ucol_close(col);
return;
}
log_verbose("ucol_getRules() testing ...\n");
rules = ucol_getRules(ruled, &tempLength);
if(tempLength == 0) {
log_data_err("missing da_DK tailoring rule string\n");
} else {
UChar aa[2] = { 0x61, 0x61 };
doAssert(u_strFindFirst(rules, tempLength, aa, 2) != NULL,
"da_DK rules do not contain 'aa'");
}
log_verbose("getRules tests end.\n");
{
UChar *buffer = (UChar *)malloc(200000*sizeof(UChar));
int32_t bufLen = 200000;
buffer[0] = '\0';
log_verbose("ucol_getRulesEx() testing ...\n");
tempLength = ucol_getRulesEx(col,UCOL_TAILORING_ONLY,buffer,bufLen );
doAssert( tempLength == 0x00, "getRulesEx() result incorrect" );
log_verbose("getRules tests end.\n");
log_verbose("ucol_getRulesEx() testing ...\n");
tempLength=ucol_getRulesEx(col,UCOL_FULL_RULES,buffer,bufLen );
if(tempLength == 0) {
log_data_err("missing *full* rule string\n");
}
log_verbose("getRulesEx tests end.\n");
free(buffer);
}
ucol_close(ruled);
ucol_close(col);
log_verbose("open an collator for french locale");
col = ucol_open("fr_FR", &status);
if (U_FAILURE(status)) {
log_err("ERROR: Creating French collation failed.: %s\n", myErrorName(status));
return;
}
ucol_setStrength(col, UCOL_PRIMARY);
log_verbose("testing ucol_getStrength() method again ...\n");
doAssert( (ucol_getStrength(col) != UCOL_TERTIARY), "collation object has the wrong strength");
doAssert( (ucol_getStrength(col) == UCOL_PRIMARY), "collation object's strength is not primary difference");
log_verbose("testing French ucol_setStrength() method ...\n");
ucol_setStrength(col, UCOL_TERTIARY);
doAssert( (ucol_getStrength(col) == UCOL_TERTIARY), "collation object's strength is not tertiary difference");
doAssert( (ucol_getStrength(col) != UCOL_PRIMARY), "collation object's strength is primary difference");
doAssert( (ucol_getStrength(col) != UCOL_SECONDARY), "collation object's strength is secondary difference");
ucol_close(col);
log_verbose("Get display name for the french collation in english : \n");
len=ucol_getDisplayName("fr_FR", "en_US", NULL, 0, &status);
if(status==U_BUFFER_OVERFLOW_ERROR){
status=U_ZERO_ERROR;
disName=(UChar*)malloc(sizeof(UChar) * (len+1));
ucol_getDisplayName("fr_FR", "en_US", disName, len+1, &status);
log_verbose("the display name for french collation in english: %s\n", austrdup(disName) );
free(disName);
}
if(U_FAILURE(status)){
log_err("ERROR: in getDisplayName: %s\n", myErrorName(status));
return;
}
log_verbose("Default collation getDisplayName ended.\n");
}
/* Test RuleBasedCollator and getRules*/
void TestRuleBasedColl(void)
{
UCollator *col1, *col2, *col3, *col4;
UCollationElements *iter1, *iter2;
UChar ruleset1[60];
UChar ruleset2[50];
UChar teststr[10];
const UChar *rule1, *rule2, *rule3, *rule4;
int32_t tempLength;
UErrorCode status = U_ZERO_ERROR;
u_uastrcpy(ruleset1, "&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
u_uastrcpy(ruleset2, "&9 < a, A < b, B < c, C < d, D, e, E");
col1 = ucol_openRules(ruleset1, u_strlen(ruleset1), UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL,&status);
if (U_FAILURE(status)) {
log_err_status(status, "RuleBased Collator creation failed.: %s\n", myErrorName(status));
return;
}
else
log_verbose("PASS: RuleBased Collator creation passed\n");
status = U_ZERO_ERROR;
col2 = ucol_openRules(ruleset2, u_strlen(ruleset2), UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL, &status);
if (U_FAILURE(status)) {
log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status));
return;
}
else
log_verbose("PASS: RuleBased Collator creation passed\n");
status = U_ZERO_ERROR;
col3= ucol_open(NULL, &status);
if (U_FAILURE(status)) {
log_err("Default Collator creation failed.: %s\n", myErrorName(status));
return;
}
else
log_verbose("PASS: Default Collator creation passed\n");
rule1 = ucol_getRules(col1, &tempLength);
rule2 = ucol_getRules(col2, &tempLength);
rule3 = ucol_getRules(col3, &tempLength);
doAssert((u_strcmp(rule1, rule2) != 0), "Default collator getRules failed");
doAssert((u_strcmp(rule2, rule3) != 0), "Default collator getRules failed");
doAssert((u_strcmp(rule1, rule3) != 0), "Default collator getRules failed");
col4=ucol_openRules(rule2, u_strlen(rule2), UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL, &status);
if (U_FAILURE(status)) {
log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status));
return;
}
rule4= ucol_getRules(col4, &tempLength);
doAssert((u_strcmp(rule2, rule4) == 0), "Default collator getRules failed");
ucol_close(col1);
ucol_close(col2);
ucol_close(col3);
ucol_close(col4);
/* tests that modifier ! is always ignored */
u_uastrcpy(ruleset1, "!&a<b");
teststr[0] = 0x0e40;
teststr[1] = 0x0e01;
teststr[2] = 0x0e2d;
col1 = ucol_openRules(ruleset1, u_strlen(ruleset1), UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL, &status);
if (U_FAILURE(status)) {
log_err("RuleBased Collator creation failed.: %s\n", myErrorName(status));
return;
}
col2 = ucol_open("en_US", &status);
if (U_FAILURE(status)) {
log_err("en_US Collator creation failed.: %s\n", myErrorName(status));
return;
}
iter1 = ucol_openElements(col1, teststr, 3, &status);
iter2 = ucol_openElements(col2, teststr, 3, &status);
if(U_FAILURE(status)) {
log_err("ERROR: CollationElement iterator creation failed.: %s\n", myErrorName(status));
return;
}
while (true) {
/* testing with en since thai has its own tailoring */
int32_t ce = ucol_next(iter1, &status);
int32_t ce2 = ucol_next(iter2, &status);
if(U_FAILURE(status)) {
log_err("ERROR: CollationElement iterator creation failed.: %s\n", myErrorName(status));
return;
}
if (ce2 != ce) {
log_err("! modifier test failed");
}
if (ce == UCOL_NULLORDER) {
break;
}
}
ucol_closeElements(iter1);
ucol_closeElements(iter2);
ucol_close(col1);
ucol_close(col2);
/* CLDR 24+ requires a reset before the first relation */
u_uastrcpy(ruleset1, "< z < a");
col1 = ucol_openRules(ruleset1, u_strlen(ruleset1), UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL, &status);
if (status != U_PARSE_ERROR && status != U_INVALID_FORMAT_ERROR) {
log_err("ucol_openRules(without initial reset: '< z < a') "
"should fail with U_PARSE_ERROR or U_INVALID_FORMAT_ERROR but yielded %s\n",
myErrorName(status));
}
ucol_close(col1);
}
void TestCompare(void)
{
UErrorCode status = U_ZERO_ERROR;
UCollator *col;
UChar* test1;
UChar* test2;
log_verbose("The compare tests begin : \n");
status=U_ZERO_ERROR;
col = ucol_open("en_US", &status);
if(U_FAILURE(status)) {
log_err_status(status, "ucal_open() collation creation failed.: %s\n", myErrorName(status));
return;
}
test1=(UChar*)malloc(sizeof(UChar) * 6);
test2=(UChar*)malloc(sizeof(UChar) * 6);
u_uastrcpy(test1, "Abcda");
u_uastrcpy(test2, "abcda");
log_verbose("Use tertiary comparison level testing ....\n");
doAssert( (!ucol_equal(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" != \"abcda\" ");
doAssert( (ucol_greater(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" >>> \"abcda\" ");
doAssert( (ucol_greaterOrEqual(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" >>> \"abcda\"");
ucol_setStrength(col, UCOL_SECONDARY);
log_verbose("Use secondary comparison level testing ....\n");
doAssert( (ucol_equal(col, test1, u_strlen(test1), test2, u_strlen(test2) )), "Result should be \"Abcda\" == \"abcda\"");
doAssert( (!ucol_greater(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" == \"abcda\"");
doAssert( (ucol_greaterOrEqual(col, test1, u_strlen(test1), test2, u_strlen(test2) )), "Result should be \"Abcda\" == \"abcda\"");
ucol_setStrength(col, UCOL_PRIMARY);
log_verbose("Use primary comparison level testing ....\n");
doAssert( (ucol_equal(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" == \"abcda\"");
doAssert( (!ucol_greater(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" == \"abcda\"");
doAssert( (ucol_greaterOrEqual(col, test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"Abcda\" == \"abcda\"");
log_verbose("The compare tests end.\n");
ucol_close(col);
free(test1);
free(test2);
}
/*
---------------------------------------------
tests decomposition setting
*/
void TestDecomposition(void) {
UErrorCode status = U_ZERO_ERROR;
UCollator *en_US, *el_GR, *vi_VN;
en_US = ucol_open("en_US", &status);
el_GR = ucol_open("el_GR", &status);
vi_VN = ucol_open("vi_VN", &status);
if (U_FAILURE(status)) {
log_err_status(status, "ERROR: collation creation failed.: %s\n", myErrorName(status));
return;
}
if (ucol_getAttribute(vi_VN, UCOL_NORMALIZATION_MODE, &status) != UCOL_ON ||
U_FAILURE(status))
{
log_err("ERROR: vi_VN collation did not have canonical decomposition for normalization!\n");
}
status = U_ZERO_ERROR;
if (ucol_getAttribute(el_GR, UCOL_NORMALIZATION_MODE, &status) != UCOL_ON ||
U_FAILURE(status))
{
log_err("ERROR: el_GR collation did not have canonical decomposition for normalization!\n");
}
status = U_ZERO_ERROR;
if (ucol_getAttribute(en_US, UCOL_NORMALIZATION_MODE, &status) != UCOL_OFF ||
U_FAILURE(status))
{
log_err("ERROR: en_US collation had canonical decomposition for normalization!\n");
}
ucol_close(en_US);
ucol_close(el_GR);
ucol_close(vi_VN);
}
#define CLONETEST_COLLATOR_COUNT 4
void TestSafeClone(void) {
UChar test1[6];
UChar test2[6];
static const UChar umlautUStr[] = {0x00DC, 0};
static const UChar oeStr[] = {0x0055, 0x0045, 0};
UCollator * someCollators [CLONETEST_COLLATOR_COUNT];
UCollator * someClonedCollators [CLONETEST_COLLATOR_COUNT];
UCollator * col;
UErrorCode err = U_ZERO_ERROR;
int8_t idx = 6; /* Leave this here to test buffer alignment in memory*/
uint8_t buffer [CLONETEST_COLLATOR_COUNT] [U_COL_SAFECLONE_BUFFERSIZE];
int32_t bufferSize = U_COL_SAFECLONE_BUFFERSIZE;
const char sampleRuleChars[] = "&Z < CH";
UChar sampleRule[sizeof(sampleRuleChars)];
u_uastrcpy(test1, "abCda");
u_uastrcpy(test2, "abcda");
u_uastrcpy(sampleRule, sampleRuleChars);
/* one default collator & two complex ones */
someCollators[0] = ucol_open("en_US", &err);
someCollators[1] = ucol_open("ko", &err);
someCollators[2] = ucol_open("ja_JP", &err);
someCollators[3] = ucol_openRules(sampleRule, -1, UCOL_ON, UCOL_TERTIARY, NULL, &err);
if(U_FAILURE(err)) {
for (idx = 0; idx < CLONETEST_COLLATOR_COUNT; idx++) {
ucol_close(someCollators[idx]);
}
log_data_err("Couldn't open one or more collators\n");
return;
}
/* Check the various error & informational states: */
/* Null status - just returns NULL */
if (NULL != ucol_safeClone(someCollators[0], buffer[0], &bufferSize, NULL))
{
log_err("FAIL: Cloned Collator failed to deal correctly with null status\n");
}
/* error status - should return 0 & keep error the same */
err = U_MEMORY_ALLOCATION_ERROR;
if (NULL != ucol_safeClone(someCollators[0], buffer[0], &bufferSize, &err) || err != U_MEMORY_ALLOCATION_ERROR)
{
log_err("FAIL: Cloned Collator failed to deal correctly with incoming error status\n");
}
err = U_ZERO_ERROR;
/* Null buffer size pointer is ok */
if (NULL == (col = ucol_safeClone(someCollators[0], buffer[0], NULL, &err)) || U_FAILURE(err))
{
log_err("FAIL: Cloned Collator failed to deal correctly with null bufferSize pointer\n");
}
ucol_close(col);
err = U_ZERO_ERROR;
/* buffer size pointer is 0 - fill in pbufferSize with a size */
bufferSize = 0;
if (NULL != ucol_safeClone(someCollators[0], buffer[0], &bufferSize, &err) ||
U_FAILURE(err) || bufferSize <= 0)
{
log_err("FAIL: Cloned Collator failed a sizing request ('preflighting')\n");
}
/* Verify our define is large enough */
if (U_COL_SAFECLONE_BUFFERSIZE < bufferSize)
{
log_err("FAIL: Pre-calculated buffer size is too small\n");
}
/* Verify we can use this run-time calculated size */
if (NULL == (col = ucol_safeClone(someCollators[0], buffer[0], &bufferSize, &err)) || U_FAILURE(err))
{
log_err("FAIL: Collator can't be cloned with run-time size\n");
}
if (col) ucol_close(col);
/* size one byte too small - should allocate & let us know */
if (bufferSize > 1) {
--bufferSize;
}
if (NULL == (col = ucol_safeClone(someCollators[0], 0, &bufferSize, &err)) || err != U_SAFECLONE_ALLOCATED_WARNING)
{
log_err("FAIL: Cloned Collator failed to deal correctly with too-small buffer size\n");
}
if (col) ucol_close(col);
err = U_ZERO_ERROR;
bufferSize = U_COL_SAFECLONE_BUFFERSIZE;
/* Null buffer pointer - return Collator & set error to U_SAFECLONE_ALLOCATED_ERROR */
if (NULL == (col = ucol_safeClone(someCollators[0], 0, &bufferSize, &err)) || err != U_SAFECLONE_ALLOCATED_WARNING)
{
log_err("FAIL: Cloned Collator failed to deal correctly with null buffer pointer\n");
}
if (col) ucol_close(col);
err = U_ZERO_ERROR;
/* Null Collator - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */
if (NULL != ucol_safeClone(NULL, buffer[0], &bufferSize, &err) || err != U_ILLEGAL_ARGUMENT_ERROR)
{
log_err("FAIL: Cloned Collator failed to deal correctly with null Collator pointer\n");
}
err = U_ZERO_ERROR;
/* Test that a cloned collator doesn't accidentally use UCA. */
col=ucol_open("de@collation=phonebook", &err);
bufferSize = U_COL_SAFECLONE_BUFFERSIZE;
someClonedCollators[0] = ucol_safeClone(col, buffer[0], &bufferSize, &err);
doAssert( (ucol_greater(col, umlautUStr, u_strlen(umlautUStr), oeStr, u_strlen(oeStr))), "Original German phonebook collation sorts differently than expected");
doAssert( (ucol_greater(someClonedCollators[0], umlautUStr, u_strlen(umlautUStr), oeStr, u_strlen(oeStr))), "Cloned German phonebook collation sorts differently than expected");
if (!ucol_equals(someClonedCollators[0], col)) {
log_err("FAIL: Cloned German phonebook collator is not equal to original.\n");
}
ucol_close(col);
ucol_close(someClonedCollators[0]);
err = U_ZERO_ERROR;
/* change orig & clone & make sure they are independent */
for (idx = 0; idx < CLONETEST_COLLATOR_COUNT; idx++)
{
ucol_setStrength(someCollators[idx], UCOL_IDENTICAL);
bufferSize = 1;
err = U_ZERO_ERROR;
ucol_close(ucol_safeClone(someCollators[idx], buffer[idx], &bufferSize, &err));
if (err != U_SAFECLONE_ALLOCATED_WARNING) {
log_err("FAIL: collator number %d was not allocated.\n", idx);
log_err("FAIL: status of Collator[%d] is %d (hex: %x).\n", idx, err, err);
}
bufferSize = U_COL_SAFECLONE_BUFFERSIZE;
err = U_ZERO_ERROR;
someClonedCollators[idx] = ucol_safeClone(someCollators[idx], buffer[idx], &bufferSize, &err);
if (U_FAILURE(err)) {
log_err("FAIL: Unable to clone collator %d - %s\n", idx, u_errorName(err));
continue;
}
if (!ucol_equals(someClonedCollators[idx], someCollators[idx])) {
log_err("FAIL: Cloned collator is not equal to original at index = %d.\n", idx);
}
/* Check the usability */
ucol_setStrength(someCollators[idx], UCOL_PRIMARY);
ucol_setAttribute(someCollators[idx], UCOL_CASE_LEVEL, UCOL_OFF, &err);
doAssert( (ucol_equal(someCollators[idx], test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"abcda\" == \"abCda\"");
/* Close the original to make sure that the clone is usable. */
ucol_close(someCollators[idx]);
ucol_setStrength(someClonedCollators[idx], UCOL_TERTIARY);
ucol_setAttribute(someClonedCollators[idx], UCOL_CASE_LEVEL, UCOL_OFF, &err);
doAssert( (ucol_greater(someClonedCollators[idx], test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"abCda\" >>> \"abcda\" ");
ucol_close(someClonedCollators[idx]);
}
}
void TestClone(void) {
UChar test1[6];
UChar test2[6];
static const UChar umlautUStr[] = {0x00DC, 0};
static const UChar oeStr[] = {0x0055, 0x0045, 0};
UCollator * someCollators [CLONETEST_COLLATOR_COUNT];
UCollator * someClonedCollators [CLONETEST_COLLATOR_COUNT];
UCollator * col = NULL;
UErrorCode err = U_ZERO_ERROR;
int8_t idx = 6; /* Leave this here to test buffer alignment in memory*/
const char sampleRuleChars[] = "&Z < CH";
UChar sampleRule[sizeof(sampleRuleChars)];
u_uastrcpy(test1, "abCda");
u_uastrcpy(test2, "abcda");
u_uastrcpy(sampleRule, sampleRuleChars);
/* one default collator & two complex ones */
someCollators[0] = ucol_open("en_US", &err);
someCollators[1] = ucol_open("ko", &err);
someCollators[2] = ucol_open("ja_JP", &err);
someCollators[3] = ucol_openRules(sampleRule, -1, UCOL_ON, UCOL_TERTIARY, NULL, &err);
if(U_FAILURE(err)) {
for (idx = 0; idx < CLONETEST_COLLATOR_COUNT; idx++) {
ucol_close(someCollators[idx]);
}
log_data_err("Couldn't open one or more collators\n");
return;
}
/* Check the various error & informational states: */
/* Null status - just returns NULL */
if (NULL != ucol_clone(someCollators[0], NULL))
{
log_err("FAIL: Cloned Collator failed to deal correctly with null status\n");
}
/* error status - should return 0 & keep error the same */
err = U_MEMORY_ALLOCATION_ERROR;
if (NULL != ucol_clone(someCollators[0], &err) || err != U_MEMORY_ALLOCATION_ERROR)
{
log_err("FAIL: Cloned Collator failed to deal correctly with incoming error status\n");
}
err = U_ZERO_ERROR;
/* Verify we can use this run-time calculated size */
if (NULL == (col = ucol_clone(someCollators[0], &err)) || U_FAILURE(err))
{
log_err("FAIL: Collator can't be cloned.\n");
}
if (col) ucol_close(col);
if (NULL == (col = ucol_clone(someCollators[0], &err)) || err != U_ZERO_ERROR)
{
log_err("FAIL: Cloned Collator failed to deal correctly\n");
}
if (col) ucol_close(col);
err = U_ZERO_ERROR;
/* Null Collator - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */
if (NULL != ucol_clone(NULL, &err) || err != U_ILLEGAL_ARGUMENT_ERROR)
{
log_err("FAIL: Cloned Collator failed to deal correctly with null Collator pointer\n");
}
err = U_ZERO_ERROR;
/* Test that a cloned collator doesn't accidentally use UCA. */
col=ucol_open("de@collation=phonebook", &err);
someClonedCollators[0] = ucol_clone(col, &err);
doAssert( (ucol_greater(col, umlautUStr, u_strlen(umlautUStr), oeStr, u_strlen(oeStr))), "Original German phonebook collation sorts differently than expected");
doAssert( (ucol_greater(someClonedCollators[0], umlautUStr, u_strlen(umlautUStr), oeStr, u_strlen(oeStr))), "Cloned German phonebook collation sorts differently than expected");
if (!ucol_equals(someClonedCollators[0], col)) {
log_err("FAIL: Cloned German phonebook collator is not equal to original.\n");
}
ucol_close(col);
ucol_close(someClonedCollators[0]);
err = U_ZERO_ERROR;
/* change orig & clone & make sure they are independent */
for (idx = 0; idx < CLONETEST_COLLATOR_COUNT; idx++)
{
ucol_setStrength(someCollators[idx], UCOL_IDENTICAL);
err = U_ZERO_ERROR;
ucol_close(ucol_clone(someCollators[idx], &err));
if (err != U_ZERO_ERROR) {
log_err("FAIL: collator number %d was not allocated.\n", idx);
log_err("FAIL: status of Collator[%d] is %d (hex: %x).\n", idx, err, err);
}
err = U_ZERO_ERROR;
someClonedCollators[idx] = ucol_clone(someCollators[idx], &err);
if (U_FAILURE(err)) {
log_err("FAIL: Unable to clone collator %d - %s\n", idx, u_errorName(err));
continue;
}
if (!ucol_equals(someClonedCollators[idx], someCollators[idx])) {
log_err("FAIL: Cloned collator is not equal to original at index = %d.\n", idx);
}
/* Check the usability */
ucol_setStrength(someCollators[idx], UCOL_PRIMARY);
ucol_setAttribute(someCollators[idx], UCOL_CASE_LEVEL, UCOL_OFF, &err);
doAssert( (ucol_equal(someCollators[idx], test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"abcda\" == \"abCda\"");
/* Close the original to make sure that the clone is usable. */
ucol_close(someCollators[idx]);
ucol_setStrength(someClonedCollators[idx], UCOL_TERTIARY);
ucol_setAttribute(someClonedCollators[idx], UCOL_CASE_LEVEL, UCOL_OFF, &err);
doAssert( (ucol_greater(someClonedCollators[idx], test1, u_strlen(test1), test2, u_strlen(test2))), "Result should be \"abCda\" >>> \"abcda\" ");
ucol_close(someClonedCollators[idx]);
}
}
void TestCloneBinary(void){
UErrorCode err = U_ZERO_ERROR;
UCollator * col = ucol_open("en_US", &err);
UCollator * c;
int32_t size;
uint8_t * buffer;
if (U_FAILURE(err)) {
log_data_err("Couldn't open collator. Error: %s\n", u_errorName(err));
return;
}
size = ucol_cloneBinary(col, NULL, 0, &err);
if(size==0 || err!=U_BUFFER_OVERFLOW_ERROR) {
log_err("ucol_cloneBinary - couldn't check size. Error: %s\n", u_errorName(err));
return;
}
err = U_ZERO_ERROR;
buffer = (uint8_t *) malloc(size);
ucol_cloneBinary(col, buffer, size, &err);
if(U_FAILURE(err)) {
log_err("ucol_cloneBinary - couldn't clone.. Error: %s\n", u_errorName(err));
free(buffer);
return;
}
/* how to check binary result ? */
c = ucol_openBinary(buffer, size, col, &err);
if(U_FAILURE(err)) {
log_err("ucol_openBinary failed. Error: %s\n", u_errorName(err));
} else {
UChar t[] = {0x41, 0x42, 0x43, 0}; /* ABC */
uint8_t *k1, *k2;
int l1, l2;
l1 = ucol_getSortKey(col, t, -1, NULL,0);
l2 = ucol_getSortKey(c, t, -1, NULL,0);
k1 = (uint8_t *) malloc(sizeof(uint8_t) * l1);
k2 = (uint8_t *) malloc(sizeof(uint8_t) * l2);
ucol_getSortKey(col, t, -1, k1, l1);
ucol_getSortKey(col, t, -1, k2, l2);
if (strcmp((char *)k1,(char *)k2) != 0){
log_err("ucol_openBinary - new collator should equal to old one\n");
}
free(k1);
free(k2);
}
free(buffer);
ucol_close(c);
ucol_close(col);
}
static void TestBengaliSortKey(void)
{
const char *curLoc = "bn";
UChar str1[] = { 0x09BE, 0 };
UChar str2[] = { 0x0B70, 0 };
UCollator *c2 = NULL;
const UChar *rules;
int32_t rulesLength=-1;
uint8_t *sortKey1;
int32_t sortKeyLen1 = 0;
uint8_t *sortKey2;
int32_t sortKeyLen2 = 0;
UErrorCode status = U_ZERO_ERROR;
char sortKeyStr1[2048];
uint32_t sortKeyStrLen1 = UPRV_LENGTHOF(sortKeyStr1);
char sortKeyStr2[2048];
uint32_t sortKeyStrLen2 = UPRV_LENGTHOF(sortKeyStr2);
UCollationResult result;