forked from unicode-org/icu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapicoll.cpp
2566 lines (2244 loc) · 99.5 KB
/
apicoll.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:
* Copyright (c) 1997-2016, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
//===============================================================================
//
// File apicoll.cpp
//
//
//
// Created by: Helena Shih
//
// Modification History:
//
// Date Name Description
// 2/5/97 aliu Added streamIn and streamOut methods. Added
// constructor which reads RuleBasedCollator object from
// a binary file. Added writeToFile method which streams
// RuleBasedCollator out to a binary file. The streamIn
// and streamOut methods use istream and ostream objects
// in binary mode.
// 6/30/97 helena Added tests for CollationElementIterator::setText, getOffset
// setOffset and DecompositionIterator::getOffset, setOffset.
// DecompositionIterator is made public so add class scope
// testing.
// 02/10/98 damiba Added test for compare(UnicodeString&, UnicodeString&, int32_t)
//===============================================================================
#include "unicode/utypes.h"
#if !UCONFIG_NO_COLLATION
#include "unicode/localpointer.h"
#include "unicode/coll.h"
#include "unicode/tblcoll.h"
#include "unicode/coleitr.h"
#include "unicode/sortkey.h"
#include "apicoll.h"
#include "unicode/chariter.h"
#include "unicode/schriter.h"
#include "unicode/strenum.h"
#include "unicode/ustring.h"
#include "unicode/ucol.h"
#include "sfwdchit.h"
#include "cmemory.h"
#include <stdlib.h>
void
CollationAPITest::doAssert(UBool condition, const char *message)
{
if (!condition) {
errln(UnicodeString("ERROR : ") + message);
}
}
// Collator Class Properties
// ctor, dtor, createInstance, compare, getStrength/setStrength
// getDecomposition/setDecomposition, getDisplayName
void
CollationAPITest::TestProperty(/* char* par */)
{
UErrorCode success = U_ZERO_ERROR;
Collator *col = 0;
/*
* 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 cintltst/capitst.c.
*/
UVersionInfo currVersionArray = {0x31, 0xC0, 0x05, 0x2A}; // from ICU 4.4/UCA 5.2
UVersionInfo versionArray;
logln("The property tests begin : ");
logln("Test ctors : ");
col = Collator::createInstance(Locale::getEnglish(), success);
if (U_FAILURE(success)){
errcheckln(success, "English Collator creation failed. - %s", u_errorName(success));
return;
}
col->getVersion(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) {
errln("Testing Collator::getVersion() - unexpected result: %02x.%02x.%02x.%02x",
versionArray[0], versionArray[1], versionArray[2], versionArray[3]);
} else {
logln("Collator::getVersion() result: %02x.%02x.%02x.%02x",
versionArray[0], versionArray[1], versionArray[2], versionArray[3]);
}
doAssert((col->compare("ab", "abc") == Collator::LESS), "ab < abc comparison failed");
doAssert((col->compare("ab", "AB") == Collator::LESS), "ab < AB comparison failed");
doAssert((col->compare("blackbird", "black-bird") == Collator::GREATER), "black-bird > blackbird comparison failed");
doAssert((col->compare("black bird", "black-bird") == Collator::LESS), "black bird > black-bird comparison failed");
doAssert((col->compare("Hello", "hello") == Collator::GREATER), "Hello > hello comparison failed");
doAssert((col->compare("","",success) == UCOL_EQUAL), "Comparison between empty strings failed");
doAssert((col->compareUTF8("\x61\x62\xc3\xa4", "\x61\x62\xc3\x9f", success) == UCOL_LESS), "ab a-umlaut < ab sharp-s UTF-8 comparison failed");
success = U_ZERO_ERROR;
{
UnicodeString abau=UNICODE_STRING_SIMPLE("\\x61\\x62\\xe4").unescape();
UnicodeString abss=UNICODE_STRING_SIMPLE("\\x61\\x62\\xdf").unescape();
UCharIterator abauIter, abssIter;
uiter_setReplaceable(&abauIter, &abau);
uiter_setReplaceable(&abssIter, &abss);
doAssert((col->compare(abauIter, abssIter, success) == UCOL_LESS), "ab a-umlaut < ab sharp-s UCharIterator comparison failed");
success = U_ZERO_ERROR;
}
/*start of update [Bertrand A. D. 02/10/98]*/
doAssert((col->compare("ab", "abc", 2) == Collator::EQUAL), "ab = abc with length 2 comparison failed");
doAssert((col->compare("ab", "AB", 2) == Collator::LESS), "ab < AB with length 2 comparison failed");
doAssert((col->compare("ab", "Aa", 1) == Collator::LESS), "ab < Aa with length 1 comparison failed");
doAssert((col->compare("ab", "Aa", 2) == Collator::GREATER), "ab > Aa with length 2 comparison failed");
doAssert((col->compare("black-bird", "blackbird", 5) == Collator::EQUAL), "black-bird = blackbird with length of 5 comparison failed");
doAssert((col->compare("black bird", "black-bird", 10) == Collator::LESS), "black bird < black-bird with length 10 comparison failed");
doAssert((col->compare("Hello", "hello", 5) == Collator::GREATER), "Hello > hello with length 5 comparison failed");
/*end of update [Bertrand A. D. 02/10/98]*/
logln("Test ctors ends.");
logln("testing Collator::getStrength() method ...");
doAssert((col->getStrength() == Collator::TERTIARY), "collation object has the wrong strength");
doAssert((col->getStrength() != Collator::PRIMARY), "collation object's strength is primary difference");
logln("testing Collator::setStrength() method ...");
col->setStrength(Collator::SECONDARY);
doAssert((col->getStrength() != Collator::TERTIARY), "collation object's strength is secondary difference");
doAssert((col->getStrength() != Collator::PRIMARY), "collation object's strength is primary difference");
doAssert((col->getStrength() == Collator::SECONDARY), "collation object has the wrong strength");
UnicodeString name;
logln("Get display name for the US English collation in German : ");
logln(Collator::getDisplayName(Locale::getUS(), Locale::getGerman(), name));
doAssert((name == UnicodeString("Englisch (Vereinigte Staaten)")), "getDisplayName failed");
logln("Get display name for the US English collation in English : ");
logln(Collator::getDisplayName(Locale::getUS(), Locale::getEnglish(), name));
doAssert((name == UnicodeString("English (United States)")), "getDisplayName failed");
#if 0
// weiv : this test is bogus if we're running on any machine that has different default locale than English.
// Therefore, it is banned!
logln("Get display name for the US English in default locale language : ");
logln(Collator::getDisplayName(Locale::US, name));
doAssert((name == UnicodeString("English (United States)")), "getDisplayName failed if this is an English machine");
#endif
delete col; col = 0;
RuleBasedCollator *rcol = dynamic_cast<RuleBasedCollator*>(Collator::createInstance("da_DK",
success));
if (U_FAILURE(success)) {
errcheckln(success, "Collator::createInstance(\"da_DK\") failed - %s", u_errorName(success));
return;
}
const UnicodeString &daRules = rcol->getRules();
if(daRules.isEmpty()) {
dataerrln("missing da_DK tailoring rule string");
} else {
doAssert(daRules.indexOf("aa") >= 0, "da_DK rules do not contain 'aa'");
}
delete rcol;
col = Collator::createInstance(Locale::getFrench(), success);
if (U_FAILURE(success))
{
errln("Creating French collation failed.");
return;
}
col->setStrength(Collator::PRIMARY);
logln("testing Collator::getStrength() method again ...");
doAssert((col->getStrength() != Collator::TERTIARY), "collation object has the wrong strength");
doAssert((col->getStrength() == Collator::PRIMARY), "collation object's strength is not primary difference");
logln("testing French Collator::setStrength() method ...");
col->setStrength(Collator::TERTIARY);
doAssert((col->getStrength() == Collator::TERTIARY), "collation object's strength is not tertiary difference");
doAssert((col->getStrength() != Collator::PRIMARY), "collation object's strength is primary difference");
doAssert((col->getStrength() != Collator::SECONDARY), "collation object's strength is secondary difference");
delete col;
logln("Create junk collation: ");
Locale abcd("ab", "CD", "");
success = U_ZERO_ERROR;
Collator *junk = 0;
junk = Collator::createInstance(abcd, success);
if (U_FAILURE(success))
{
errln("Junk collation creation failed, should at least return default.");
return;
}
doAssert((dynamic_cast<RuleBasedCollator*>(junk))->getRules().isEmpty(),
"The root collation should be returned for an unsupported language.");
Collator *frCol = Collator::createInstance(Locale::getCanadaFrench(), success);
if (U_FAILURE(success))
{
errln("Creating fr_CA collator failed.");
delete junk;
return;
}
// If the default locale isn't French, the French and non-French collators
// should be different
if (frCol->getLocale(ULOC_ACTUAL_LOCALE, success) != Locale::getCanadaFrench()) {
doAssert((*frCol != *junk), "The junk is the same as the fr_CA collator.");
}
Collator *aFrCol = frCol->clone();
doAssert((*frCol == *aFrCol), "The cloning of a fr_CA collator failed.");
logln("Collator property test ended.");
delete frCol;
delete aFrCol;
delete junk;
}
void CollationAPITest::TestKeywordValues() {
IcuTestErrorCode errorCode(*this, "TestKeywordValues");
LocalPointer<Collator> col(Collator::createInstance(Locale::getEnglish(), errorCode));
if (errorCode.errIfFailureAndReset("English Collator creation failed")) {
return;
}
LocalPointer<StringEnumeration> kwEnum(
col->getKeywordValuesForLocale("collation", Locale::getEnglish(), true, errorCode));
if (errorCode.errIfFailureAndReset("Get Keyword Values for English Collator failed")) {
return;
}
assertTrue("expect at least one collation tailoring for English", kwEnum->count(errorCode) > 0);
const char *kw;
UBool hasStandard = false;
while ((kw = kwEnum->next(nullptr, errorCode)) != nullptr) {
if (strcmp(kw, "standard") == 0) {
hasStandard = true;
}
}
assertTrue("expect at least the 'standard' collation tailoring for English", hasStandard);
}
void
CollationAPITest::TestRuleBasedColl()
{
RuleBasedCollator *col1, *col2, *col3, *col4;
UErrorCode status = U_ZERO_ERROR;
UnicodeString ruleset1("&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
UnicodeString ruleset2("&9 < a, A < b, B < c, C < d, D, e, E");
col1 = new RuleBasedCollator(ruleset1, status);
if (U_FAILURE(status)) {
errcheckln(status, "RuleBased Collator creation failed. - %s", u_errorName(status));
return;
}
else {
logln("PASS: RuleBased Collator creation passed\n");
}
status = U_ZERO_ERROR;
col2 = new RuleBasedCollator(ruleset2, status);
if (U_FAILURE(status)) {
errln("RuleBased Collator creation failed.\n");
return;
}
else {
logln("PASS: RuleBased Collator creation passed\n");
}
status = U_ZERO_ERROR;
Locale locale("aa", "AA");
col3 = dynamic_cast<RuleBasedCollator*>(Collator::createInstance(locale, status));
if (U_FAILURE(status)) {
errln("Fallback Collator creation failed.: %s\n");
return;
}
else {
logln("PASS: Fallback Collator creation passed\n");
}
delete col3;
status = U_ZERO_ERROR;
col3 = dynamic_cast<RuleBasedCollator*>(Collator::createInstance(status));
if (U_FAILURE(status)) {
errln("Default Collator creation failed.: %s\n");
return;
}
else {
logln("PASS: Default Collator creation passed\n");
}
UnicodeString rule1 = col1->getRules();
UnicodeString rule2 = col2->getRules();
UnicodeString rule3 = col3->getRules();
doAssert(rule1 != rule2, "Default collator getRules failed");
doAssert(rule2 != rule3, "Default collator getRules failed");
doAssert(rule1 != rule3, "Default collator getRules failed");
col4 = new RuleBasedCollator(rule2, status);
if (U_FAILURE(status)) {
errln("RuleBased Collator creation failed.\n");
return;
}
UnicodeString rule4 = col4->getRules();
doAssert(rule2 == rule4, "Default collator getRules failed");
int32_t length4 = 0;
uint8_t *clonedrule4 = col4->cloneRuleData(length4, status);
if (U_FAILURE(status)) {
errln("Cloned rule data failed.\n");
return;
}
// free(clonedrule4); BAD API!!!!
uprv_free(clonedrule4);
delete col1;
delete col2;
delete col3;
delete col4;
}
void
CollationAPITest::TestRules()
{
RuleBasedCollator *coll;
UErrorCode status = U_ZERO_ERROR;
UnicodeString rules;
coll = dynamic_cast<RuleBasedCollator*>(Collator::createInstance(Locale::getEnglish(), status));
if (U_FAILURE(status)) {
errcheckln(status, "English Collator creation failed. - %s", u_errorName(status));
return;
}
else {
logln("PASS: RuleBased Collator creation passed\n");
}
coll->getRules(UCOL_TAILORING_ONLY, rules);
if (rules.length() != 0x00) {
errln("English tailored rules failed - length is 0x%x expected 0x%x", rules.length(), 0x00);
}
coll->getRules(UCOL_FULL_RULES, rules);
if (rules.length() < 0) {
errln("English full rules failed");
}
delete coll;
}
void
CollationAPITest::TestDecomposition() {
UErrorCode status = U_ZERO_ERROR;
Collator *en_US = Collator::createInstance("en_US", status),
*el_GR = Collator::createInstance("el_GR", status),
*vi_VN = Collator::createInstance("vi_VN", status);
if (U_FAILURE(status)) {
errcheckln(status, "ERROR: collation creation failed. - %s", u_errorName(status));
return;
}
/* there is no reason to have canonical decomposition in en_US OR default locale */
if (vi_VN->getAttribute(UCOL_NORMALIZATION_MODE, status) != UCOL_ON)
{
errln("ERROR: vi_VN collation did not have canonical decomposition for normalization!\n");
}
if (el_GR->getAttribute(UCOL_NORMALIZATION_MODE, status) != UCOL_ON)
{
errln("ERROR: el_GR collation did not have canonical decomposition for normalization!\n");
}
if (en_US->getAttribute(UCOL_NORMALIZATION_MODE, status) != UCOL_OFF)
{
errln("ERROR: en_US collation had canonical decomposition for normalization!\n");
}
delete en_US;
delete el_GR;
delete vi_VN;
}
void
CollationAPITest::TestSafeClone() {
static const int CLONETEST_COLLATOR_COUNT = 3;
Collator *someCollators [CLONETEST_COLLATOR_COUNT];
Collator *col;
UErrorCode err = U_ZERO_ERROR;
int index;
UnicodeString test1("abCda");
UnicodeString test2("abcda");
/* one default collator & two complex ones */
someCollators[0] = Collator::createInstance("en_US", err);
someCollators[1] = Collator::createInstance("ko", err);
someCollators[2] = Collator::createInstance("ja_JP", err);
if(U_FAILURE(err)) {
errcheckln(err, "Couldn't instantiate collators. Error: %s", u_errorName(err));
delete someCollators[0];
delete someCollators[1];
delete someCollators[2];
return;
}
/* change orig & clone & make sure they are independent */
for (index = 0; index < CLONETEST_COLLATOR_COUNT; index++)
{
col = someCollators[index]->safeClone();
if (col == 0) {
errln("SafeClone of collator should not return null\n");
break;
}
col->setStrength(Collator::TERTIARY);
someCollators[index]->setStrength(Collator::PRIMARY);
col->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, err);
someCollators[index]->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, err);
doAssert(col->greater(test1, test2), "Result should be \"abCda\" >>> \"abcda\" ");
doAssert(someCollators[index]->equals(test1, test2), "Result should be \"abcda\" == \"abCda\"");
delete col;
delete someCollators[index];
}
}
void
CollationAPITest::TestHashCode(/* char* par */)
{
logln("hashCode tests begin.");
UErrorCode success = U_ZERO_ERROR;
Collator *col1 = 0;
col1 = Collator::createInstance(Locale::getEnglish(), success);
if (U_FAILURE(success))
{
errcheckln(success, "Default collation creation failed. - %s", u_errorName(success));
return;
}
Collator *col2 = 0;
Locale dk("da", "DK", "");
col2 = Collator::createInstance(dk, success);
if (U_FAILURE(success))
{
errln("Danish collation creation failed.");
return;
}
Collator *col3 = 0;
col3 = Collator::createInstance(Locale::getEnglish(), success);
if (U_FAILURE(success))
{
errln("2nd default collation creation failed.");
return;
}
logln("Collator::hashCode() testing ...");
doAssert(col1->hashCode() != col2->hashCode(), "Hash test1 result incorrect" );
doAssert(!(col1->hashCode() == col2->hashCode()), "Hash test2 result incorrect" );
doAssert(col1->hashCode() == col3->hashCode(), "Hash result not equal" );
logln("hashCode tests end.");
delete col1;
delete col2;
UnicodeString test1("Abcda");
UnicodeString test2("abcda");
CollationKey sortk1, sortk2, sortk3;
UErrorCode status = U_ZERO_ERROR;
col3->getCollationKey(test1, sortk1, status);
col3->getCollationKey(test2, sortk2, status);
col3->getCollationKey(test2, sortk3, status);
doAssert(sortk1.hashCode() != sortk2.hashCode(), "Hash test1 result incorrect");
doAssert(sortk2.hashCode() == sortk3.hashCode(), "Hash result not equal" );
delete col3;
}
//----------------------------------------------------------------------------
// CollationKey -- Tests the CollationKey methods
//
void
CollationAPITest::TestCollationKey(/* char* par */)
{
logln("testing CollationKey begins...");
Collator *col = 0;
UErrorCode success=U_ZERO_ERROR;
col = Collator::createInstance(Locale::getEnglish(), success);
if (U_FAILURE(success))
{
errcheckln(success, "Default collation creation failed. - %s", u_errorName(success));
return;
}
col->setStrength(Collator::TERTIARY);
CollationKey sortk1, sortk2;
UnicodeString test1("Abcda"), test2("abcda");
UErrorCode key1Status = U_ZERO_ERROR, key2Status = U_ZERO_ERROR;
logln("Testing weird arguments");
// No string vs. empty string vs. completely-ignorable string:
// See ICU ticket #10495.
CollationKey sortkNone;
int32_t length;
sortkNone.getByteArray(length);
doAssert(!sortkNone.isBogus() && length == 0,
"Default-constructed collation key should be empty");
CollationKey sortkEmpty;
col->getCollationKey(nullptr, 0, sortkEmpty, key1Status);
// key gets reset here
const uint8_t* byteArrayEmpty = sortkEmpty.getByteArray(length);
doAssert(sortkEmpty.isBogus() == false && length == 3 &&
byteArrayEmpty[0] == 1 && byteArrayEmpty[1] == 1 && byteArrayEmpty[2] == 0,
"Empty string should return a collation key with empty levels");
doAssert(sortkNone.compareTo(sortkEmpty) == Collator::LESS,
"Expected no collation key < collation key for empty string");
doAssert(sortkEmpty.compareTo(sortkNone) == Collator::GREATER,
"Expected collation key for empty string > no collation key");
CollationKey sortkIgnorable;
// Most control codes and CGJ are completely ignorable.
// A string with only completely ignorables must compare equal to an empty string.
col->getCollationKey(UnicodeString((char16_t)1).append((char16_t)0x34f), sortkIgnorable, key1Status);
sortkIgnorable.getByteArray(length);
doAssert(!sortkIgnorable.isBogus() && length == 3,
"Completely ignorable string should return a collation key with empty levels");
doAssert(sortkIgnorable.compareTo(sortkEmpty) == Collator::EQUAL,
"Completely ignorable string should compare equal to empty string");
// bogus key returned here
key1Status = U_ILLEGAL_ARGUMENT_ERROR;
col->getCollationKey(nullptr, 0, sortk1, key1Status);
doAssert(sortk1.isBogus() && (sortk1.getByteArray(length), length) == 0,
"Error code should return bogus collation key");
key1Status = U_ZERO_ERROR;
logln("Use tertiary comparison level testing ....");
col->getCollationKey(test1, sortk1, key1Status);
if (U_FAILURE(key1Status)) {
errln("getCollationKey(Abcda) failed - %s", u_errorName(key1Status));
return;
}
doAssert((sortk1.compareTo(col->getCollationKey(test2, sortk2, key2Status)))
== Collator::GREATER,
"Result should be \"Abcda\" >>> \"abcda\"");
CollationKey sortk3(sortk2), sortkNew;
sortkNew = sortk1;
doAssert((sortk1 != sortk2), "The sort keys should be different");
doAssert((sortk1.hashCode() != sortk2.hashCode()), "sort key hashCode() failed");
doAssert((sortk2 == sortk3), "The sort keys should be the same");
doAssert((sortk1 == sortkNew), "The sort keys assignment failed");
doAssert((sortk1.hashCode() == sortkNew.hashCode()), "sort key hashCode() failed");
doAssert((sortkNew != sortk3), "The sort keys should be different");
doAssert(sortk1.compareTo(sortk3) == Collator::GREATER, "Result should be \"Abcda\" >>> \"abcda\"");
doAssert(sortk2.compareTo(sortk3) == Collator::EQUAL, "Result should be \"abcda\" == \"abcda\"");
doAssert(sortkEmpty.compareTo(sortk1) == Collator::LESS, "Result should be (empty key) <<< \"Abcda\"");
doAssert(sortk1.compareTo(sortkEmpty) == Collator::GREATER, "Result should be \"Abcda\" >>> (empty key)");
doAssert(sortkEmpty.compareTo(sortkEmpty) == Collator::EQUAL, "Result should be (empty key) == (empty key)");
doAssert(sortk1.compareTo(sortk3, success) == UCOL_GREATER, "Result should be \"Abcda\" >>> \"abcda\"");
doAssert(sortk2.compareTo(sortk3, success) == UCOL_EQUAL, "Result should be \"abcda\" == \"abcda\"");
doAssert(sortkEmpty.compareTo(sortk1, success) == UCOL_LESS, "Result should be (empty key) <<< \"Abcda\"");
doAssert(sortk1.compareTo(sortkEmpty, success) == UCOL_GREATER, "Result should be \"Abcda\" >>> (empty key)");
doAssert(sortkEmpty.compareTo(sortkEmpty, success) == UCOL_EQUAL, "Result should be (empty key) == (empty key)");
int32_t cnt1, cnt2, cnt3, cnt4;
const uint8_t* byteArray1 = sortk1.getByteArray(cnt1);
const uint8_t* byteArray2 = sortk2.getByteArray(cnt2);
const uint8_t* byteArray3 = 0;
byteArray3 = sortk1.getByteArray(cnt3);
const uint8_t* byteArray4 = 0;
byteArray4 = sortk2.getByteArray(cnt4);
CollationKey sortk4(byteArray1, cnt1), sortk5(byteArray2, cnt2);
CollationKey sortk6(byteArray3, cnt3), sortk7(byteArray4, cnt4);
doAssert(sortk1.compareTo(sortk4) == Collator::EQUAL, "CollationKey::toByteArray(sortk1) Failed.");
doAssert(sortk2.compareTo(sortk5) == Collator::EQUAL, "CollationKey::toByteArray(sortk2) Failed.");
doAssert(sortk4.compareTo(sortk5) == Collator::GREATER, "sortk4 >>> sortk5 Failed");
doAssert(sortk1.compareTo(sortk6) == Collator::EQUAL, "CollationKey::getByteArray(sortk1) Failed.");
doAssert(sortk2.compareTo(sortk7) == Collator::EQUAL, "CollationKey::getByteArray(sortk2) Failed.");
doAssert(sortk6.compareTo(sortk7) == Collator::GREATER, "sortk6 >>> sortk7 Failed");
logln("Equality tests : ");
doAssert(sortk1 == sortk4, "sortk1 == sortk4 Failed.");
doAssert(sortk2 == sortk5, "sortk2 == sortk5 Failed.");
doAssert(sortk1 != sortk5, "sortk1 != sortk5 Failed.");
doAssert(sortk1 == sortk6, "sortk1 == sortk6 Failed.");
doAssert(sortk2 == sortk7, "sortk2 == sortk7 Failed.");
doAssert(sortk1 != sortk7, "sortk1 != sortk7 Failed.");
byteArray1 = 0;
byteArray2 = 0;
sortk3 = sortk1;
doAssert(sortk1 == sortk3, "sortk1 = sortk3 assignment Failed.");
doAssert(sortk2 != sortk3, "sortk2 != sortk3 Failed.");
logln("testing sortkey ends...");
col->setStrength(Collator::SECONDARY);
doAssert(col->getCollationKey(test1, sortk1, key1Status).compareTo(
col->getCollationKey(test2, sortk2, key2Status))
== Collator::EQUAL,
"Result should be \"Abcda\" == \"abcda\"");
delete col;
}
//----------------------------------------------------------------------------
// Tests the CollatorElementIterator class.
// ctor, RuleBasedCollator::createCollationElementIterator(), operator==, operator!=
//
void
CollationAPITest::TestElemIter(/* char* par */)
{
logln("testing sortkey begins...");
Collator *col = 0;
UErrorCode success = U_ZERO_ERROR;
col = Collator::createInstance(Locale::getEnglish(), success);
if (U_FAILURE(success))
{
errcheckln(success, "Default collation creation failed. - %s", u_errorName(success));
return;
}
UnicodeString testString1("XFILE What subset of all possible test cases has the highest probability of detecting the most errors?");
UnicodeString testString2("Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?");
logln("Constructors and comparison testing....");
CollationElementIterator *iterator1 = (dynamic_cast<RuleBasedCollator*>(col))->createCollationElementIterator(testString1);
CharacterIterator *chariter=new StringCharacterIterator(testString1);
CollationElementIterator *coliter = (dynamic_cast<RuleBasedCollator*>(col))->createCollationElementIterator(*chariter);
// copy ctor
CollationElementIterator *iterator2 = (dynamic_cast<RuleBasedCollator*>(col))->createCollationElementIterator(testString1);
CollationElementIterator *iterator3 = (dynamic_cast<RuleBasedCollator*>(col))->createCollationElementIterator(testString2);
int32_t offset = iterator1->getOffset();
if (offset != 0) {
errln("Error in getOffset for collation element iterator\n");
return;
}
iterator1->setOffset(6, success);
if (U_FAILURE(success)) {
errln("Error in setOffset for collation element iterator\n");
return;
}
iterator1->setOffset(0, success);
int32_t order1, order2, order3;
doAssert((*iterator1 == *iterator2), "The two iterators should be the same");
doAssert((*iterator1 != *iterator3), "The two iterators should be different");
doAssert((*coliter == *iterator1), "The two iterators should be the same");
doAssert((*coliter == *iterator2), "The two iterators should be the same");
doAssert((*coliter != *iterator3), "The two iterators should be different");
order1 = iterator1->next(success);
if (U_FAILURE(success))
{
errln("Somehow ran out of memory stepping through the iterator.");
return;
}
doAssert((*iterator1 != *iterator2), "The first iterator advance failed");
order2 = iterator2->getOffset();
doAssert((order1 != order2), "The order result should not be the same");
order2 = iterator2->next(success);
if (U_FAILURE(success))
{
errln("Somehow ran out of memory stepping through the iterator.");
return;
}
doAssert((*iterator1 == *iterator2), "The second iterator advance failed");
doAssert((order1 == order2), "The order result should be the same");
order3 = iterator3->next(success);
if (U_FAILURE(success))
{
errln("Somehow ran out of memory stepping through the iterator.");
return;
}
doAssert((CollationElementIterator::primaryOrder(order1) ==
CollationElementIterator::primaryOrder(order3)), "The primary orders should be the same");
doAssert((CollationElementIterator::secondaryOrder(order1) ==
CollationElementIterator::secondaryOrder(order3)), "The secondary orders should be the same");
doAssert((CollationElementIterator::tertiaryOrder(order1) ==
CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be the same");
order1 = iterator1->next(success); order3 = iterator3->next(success);
if (U_FAILURE(success))
{
errln("Somehow ran out of memory stepping through the iterator.");
return;
}
doAssert((CollationElementIterator::primaryOrder(order1) ==
CollationElementIterator::primaryOrder(order3)), "The primary orders should be identical");
doAssert((CollationElementIterator::tertiaryOrder(order1) !=
CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be different");
order1 = iterator1->next(success);
order3 = iterator3->next(success);
/* NO! Secondary orders of two CEs are not related, especially in the case of '_' vs 'I' */
/*
doAssert((CollationElementIterator::secondaryOrder(order1) !=
CollationElementIterator::secondaryOrder(order3)), "The secondary orders should not be the same");
*/
doAssert((order1 != CollationElementIterator::NULLORDER), "Unexpected end of iterator reached");
iterator1->reset(); iterator2->reset(); iterator3->reset();
order1 = iterator1->next(success);
if (U_FAILURE(success))
{
errln("Somehow ran out of memory stepping through the iterator.");
return;
}
doAssert((*iterator1 != *iterator2), "The first iterator advance failed");
order2 = iterator2->next(success);
if (U_FAILURE(success))
{
errln("Somehow ran out of memory stepping through the iterator.");
return;
}
doAssert((*iterator1 == *iterator2), "The second iterator advance failed");
doAssert((order1 == order2), "The order result should be the same");
order3 = iterator3->next(success);
if (U_FAILURE(success))
{
errln("Somehow ran out of memory stepping through the iterator.");
return;
}
doAssert((CollationElementIterator::primaryOrder(order1) ==
CollationElementIterator::primaryOrder(order3)), "The primary orders should be the same");
doAssert((CollationElementIterator::secondaryOrder(order1) ==
CollationElementIterator::secondaryOrder(order3)), "The secondary orders should be the same");
doAssert((CollationElementIterator::tertiaryOrder(order1) ==
CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be the same");
order1 = iterator1->next(success); order2 = iterator2->next(success); order3 = iterator3->next(success);
if (U_FAILURE(success))
{
errln("Somehow ran out of memory stepping through the iterator.");
return;
}
doAssert((CollationElementIterator::primaryOrder(order1) ==
CollationElementIterator::primaryOrder(order3)), "The primary orders should be identical");
doAssert((CollationElementIterator::tertiaryOrder(order1) !=
CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be different");
order1 = iterator1->next(success); order3 = iterator3->next(success);
if (U_FAILURE(success))
{
errln("Somehow ran out of memory stepping through the iterator.");
return;
}
/* NO! Secondary orders of two CEs are not related, especially in the case of '_' vs 'I' */
/*
doAssert((CollationElementIterator::secondaryOrder(order1) !=
CollationElementIterator::secondaryOrder(order3)), "The secondary orders should not be the same");
*/
doAssert((order1 != CollationElementIterator::NULLORDER), "Unexpected end of iterator reached");
doAssert((*iterator2 != *iterator3), "The iterators should be different");
//test error values
success=U_UNSUPPORTED_ERROR;
Collator *colerror=nullptr;
colerror=Collator::createInstance(Locale::getEnglish(), success);
if (colerror != 0 || success == U_ZERO_ERROR){
errln("Error: createInstance(UErrorCode != U_ZERO_ERROR) should just return and not create an instance\n");
}
int32_t position=coliter->previous(success);
if(position != CollationElementIterator::NULLORDER){
errln((UnicodeString)"Expected NULLORDER got" + position);
}
coliter->reset();
coliter->setText(*chariter, success);
if(!U_FAILURE(success)){
errln("Expected error");
}
iterator1->setText((UnicodeString)"hello there", success);
if(!U_FAILURE(success)){
errln("Expected error");
}
delete chariter;
delete coliter;
delete iterator1;
delete iterator2;
delete iterator3;
delete col;
logln("testing CollationElementIterator ends...");
}
// Test RuleBasedCollator ctor, dtor, operator==, operator!=, clone, copy, and getRules
void
CollationAPITest::TestOperators(/* char* par */)
{
UErrorCode success = U_ZERO_ERROR;
UnicodeString ruleset1("&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
UnicodeString ruleset2("&9 < a, A < b, B < c, C < d, D, e, E");
RuleBasedCollator *col1 = new RuleBasedCollator(ruleset1, success);
if (U_FAILURE(success)) {
errcheckln(success, "RuleBasedCollator creation failed. - %s", u_errorName(success));
return;
}
success = U_ZERO_ERROR;
RuleBasedCollator *col2 = new RuleBasedCollator(ruleset2, success);
if (U_FAILURE(success)) {
errln("The RuleBasedCollator constructor failed when building with the 2nd rule set.");
return;
}
logln("The operator tests begin : ");
logln("testing operator==, operator!=, clone methods ...");
doAssert((*col1 != *col2), "The two different table collations compared equal");
*col1 = *col2;
doAssert((*col1 == *col2), "Collator objects not equal after assignment (operator=)");
success = U_ZERO_ERROR;
Collator *col3 = Collator::createInstance(Locale::getEnglish(), success);
if (U_FAILURE(success)) {
errln("Default collation creation failed.");
return;
}
doAssert((*col1 != *col3), "The two different table collations compared equal");
Collator* col4 = col1->clone();
Collator* col5 = col3->clone();
doAssert((*col1 == *col4), "Cloned collation objects not equal");
doAssert((*col3 != *col4), "Two different table collations compared equal");
doAssert((*col3 == *col5), "Cloned collation objects not equal");
doAssert((*col4 != *col5), "Two cloned collations compared equal");
const UnicodeString& defRules = (dynamic_cast<RuleBasedCollator*>(col3))->getRules();
RuleBasedCollator* col6 = new RuleBasedCollator(defRules, success);
if (U_FAILURE(success)) {
errln("Creating default collation with rules failed.");
return;
}
doAssert(((dynamic_cast<RuleBasedCollator*>(col3))->getRules() == col6->getRules()), "Default collator getRules failed");
success = U_ZERO_ERROR;
RuleBasedCollator *col7 = new RuleBasedCollator(ruleset2, Collator::TERTIARY, success);
if (U_FAILURE(success)) {
errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with tertiary strength.");
return;
}
success = U_ZERO_ERROR;
RuleBasedCollator *col8 = new RuleBasedCollator(ruleset2, UCOL_OFF, success);
if (U_FAILURE(success)) {
errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with Normalizer::NO_OP.");
return;
}
success = U_ZERO_ERROR;
RuleBasedCollator *col9 = new RuleBasedCollator(ruleset2, Collator::PRIMARY, UCOL_ON, success);
if (U_FAILURE(success)) {
errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with tertiary strength and Normalizer::NO_OP.");
return;
}
// doAssert((*col7 == *col8), "The two equal table collations compared different");
doAssert((*col7 != *col9), "The two different table collations compared equal");
doAssert((*col8 != *col9), "The two different table collations compared equal");
logln("operator tests ended.");
delete col1;
delete col2;
delete col3;
delete col4;
delete col5;
delete col6;
delete col7;
delete col8;
delete col9;
}
// test clone and copy
void
CollationAPITest::TestDuplicate(/* char* par */)
{
UErrorCode status = U_ZERO_ERROR;
Collator *col1 = Collator::createInstance(Locale::getEnglish(), status);
if (U_FAILURE(status)) {
logln("Default collator creation failed.");
return;
}
Collator *col2 = col1->clone();
doAssert((*col1 == *col2), "Cloned object is not equal to the original");
UnicodeString ruleset("&9 < a, A < b, B < c, C < d, D, e, E");
RuleBasedCollator *col3 = new RuleBasedCollator(ruleset, status);
if (U_FAILURE(status)) {
logln("Collation tailoring failed.");
return;
}
doAssert((*col1 != *col3), "Cloned object is equal to some dummy");
*col3 = *(dynamic_cast<RuleBasedCollator*>(col1));
doAssert((*col1 == *col3), "Copied object is not equal to the original");
UCollationResult res;
UnicodeString first((char16_t)0x0061);
UnicodeString second((char16_t)0x0062);
UnicodeString copiedEnglishRules((dynamic_cast<RuleBasedCollator*>(col1))->getRules());
delete col1;
// Try using the cloned collators after deleting the original data
res = col2->compare(first, second, status);
if(res != UCOL_LESS) {
errln("a should be less then b after tailoring");
}
if ((dynamic_cast<RuleBasedCollator*>(col2))->getRules() != copiedEnglishRules) {
errln(UnicodeString("English rule difference. ")
+ copiedEnglishRules + UnicodeString("\ngetRules=") + (dynamic_cast<RuleBasedCollator*>(col2))->getRules());
}
res = col3->compare(first, second, status);
if(res != UCOL_LESS) {
errln("a should be less then b after tailoring");
}
if (col3->getRules() != copiedEnglishRules) {
errln(UnicodeString("English rule difference. ")
+ copiedEnglishRules + UnicodeString("\ngetRules=") + col3->getRules());
}
delete col2;
delete col3;
}
void
CollationAPITest::TestCompare(/* char* par */)
{
logln("The compare tests begin : ");
Collator *col = 0;
UErrorCode success = U_ZERO_ERROR;
col = Collator::createInstance(Locale::getEnglish(), success);
if (U_FAILURE(success)) {
errcheckln(success, "Default collation creation failed. - %s", u_errorName(success));
return;
}
UnicodeString test1("Abcda"), test2("abcda");
logln("Use tertiary comparison level testing ....");
doAssert((!col->equals(test1, test2) ), "Result should be \"Abcda\" != \"abcda\"");
doAssert((col->greater(test1, test2) ), "Result should be \"Abcda\" >>> \"abcda\"");
doAssert((col->greaterOrEqual(test1, test2) ), "Result should be \"Abcda\" >>> \"abcda\"");
col->setStrength(Collator::SECONDARY);
logln("Use secondary comparison level testing ....");
doAssert((col->equals(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
doAssert((!col->greater(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
doAssert((col->greaterOrEqual(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
col->setStrength(Collator::PRIMARY);
logln("Use primary comparison level testing ....");
doAssert((col->equals(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
doAssert((!col->greater(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
doAssert((col->greaterOrEqual(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
// Test different APIs
const char16_t* t1 = test1.getBuffer();
int32_t t1Len = test1.length();
const char16_t* t2 = test2.getBuffer();
int32_t t2Len = test2.length();
doAssert((col->compare(test1, test2) == Collator::EQUAL), "Problem");
doAssert((col->compare(test1, test2, success) == UCOL_EQUAL), "Problem");
doAssert((col->compare(t1, t1Len, t2, t2Len) == Collator::EQUAL), "Problem");
doAssert((col->compare(t1, t1Len, t2, t2Len, success) == UCOL_EQUAL), "Problem");
doAssert((col->compare(test1, test2, t1Len) == Collator::EQUAL), "Problem");
doAssert((col->compare(test1, test2, t1Len, success) == UCOL_EQUAL), "Problem");