]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/allcoll.cpp
ICU-57149.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / allcoll.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2016, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7 #include "unicode/utypes.h"
8
9 #if !UCONFIG_NO_COLLATION
10
11 #include "unicode/coll.h"
12 #include "unicode/tblcoll.h"
13 #include "unicode/unistr.h"
14 #include "unicode/sortkey.h"
15 #include "allcoll.h"
16 #include "cmemory.h"
17 #include "sfwdchit.h"
18 #include "../cintltst/calldata.h"
19
20
21 CollationDummyTest::CollationDummyTest()
22 : myCollation(0)
23 {
24 /*UErrorCode status = U_ZERO_ERROR;
25 UnicodeString rules(TRUE, DEFAULTRULEARRAY, UPRV_LENGTHOF(DEFAULTRULEARRAY));
26 UnicodeString newRules("& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ");
27 rules += newRules;
28 myCollation = new RuleBasedCollator(rules, status);
29 */
30
31 UErrorCode status = U_ZERO_ERROR;
32 UnicodeString ruleset("& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ");
33 if (myCollation != NULL)
34 {
35 delete myCollation;
36 }
37 myCollation = new RuleBasedCollator(ruleset, status);
38 if(U_FAILURE(status)){
39 errcheckln(status, "ERROR: in creation of rule based collator from ruleset - %s", u_errorName(status));
40 delete myCollation;
41 myCollation = 0;
42 }
43 }
44
45 CollationDummyTest::~CollationDummyTest()
46 {
47 delete myCollation;
48 }
49
50 const Collator::EComparisonResult CollationDummyTest::results[] = {
51 Collator::LESS,
52 Collator::LESS, /*Collator::GREATER,*/
53 Collator::LESS,
54 Collator::LESS,
55 Collator::LESS,
56 Collator::LESS,
57 Collator::LESS,
58 Collator::GREATER,
59 Collator::GREATER,
60 Collator::LESS, /* 10 */
61 Collator::GREATER,
62 Collator::LESS,
63 Collator::GREATER,
64 Collator::GREATER,
65 Collator::LESS,
66 Collator::LESS,
67 Collator::LESS,
68 /* test primary > 17 */
69 Collator::EQUAL,
70 Collator::EQUAL,
71 Collator::EQUAL, /* 20 */
72 Collator::LESS,
73 Collator::LESS,
74 Collator::EQUAL,
75 Collator::EQUAL,
76 Collator::EQUAL,
77 Collator::LESS,
78 /* test secondary > 26 */
79 Collator::EQUAL,
80 Collator::EQUAL,
81 Collator::EQUAL,
82 Collator::EQUAL,
83 Collator::EQUAL, /* 30 */
84 Collator::EQUAL,
85 Collator::LESS,
86 Collator::EQUAL, /* 34 */
87 Collator::EQUAL,
88 Collator::EQUAL,
89 Collator::LESS
90 };
91
92
93 void CollationDummyTest::TestTertiary(/* char* par */)
94 {
95 int32_t i = 0;
96 myCollation->setStrength(Collator::TERTIARY);
97 for (i = 0; i < 17 ; i++)
98 {
99 doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
100 }
101 }
102 void CollationDummyTest::TestPrimary(/* char* par */)
103 {
104 /* problem in strcollinc for unfinshed contractions */
105 UErrorCode status = U_ZERO_ERROR;
106
107 myCollation->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
108 myCollation->setStrength(Collator::PRIMARY);
109
110 if (U_FAILURE(status))
111 {
112 errln("Failure in setting attribute for normalization mode\n");
113 }
114
115 for (int i = 17; i < 26 ; i++)
116 {
117 doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
118 }
119 }
120
121 void CollationDummyTest::TestSecondary(/* char* par */)
122 {
123 int32_t i;
124 myCollation->setStrength(Collator::SECONDARY);
125 for (i = 26; i < 34; i++)
126 {
127 doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
128 }
129 }
130
131 void CollationDummyTest::TestExtra(/* char* par */)
132 {
133 int32_t i, j;
134 myCollation->setStrength(Collator::TERTIARY);
135 for (i = 0; i < COUNT_TEST_CASES-1; i++)
136 {
137 for (j = i + 1; j < COUNT_TEST_CASES; j += 1)
138 {
139 doTest(myCollation, testCases[i], testCases[j], Collator::LESS);
140 }
141 }
142 }
143
144 void CollationDummyTest::TestIdentical()
145 {
146 int32_t i;
147 myCollation->setStrength(Collator::IDENTICAL);
148 for (i= 34; i<37; i++)
149 {
150 doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
151 }
152 }
153
154 void CollationDummyTest::TestJB581(void)
155 {
156 UErrorCode status = U_ZERO_ERROR;
157
158 UnicodeString source("THISISATEST.");
159 UnicodeString target("Thisisatest.");
160
161 Collator *coll = Collator::createInstance("en_US", status);
162 if (U_FAILURE(status)){
163 errln("ERROR: Failed to create the collator for : en_US\n");
164 return;
165 }
166
167 Collator::EComparisonResult result = coll->compare(source, target);
168 /* result is 1, secondary differences only for ignorable space characters*/
169 if (result != 1)
170 {
171 errln("Comparing two strings with only secondary differences in C failed.\n");
172 }
173 /* To compare them with just primary differences */
174 coll->setStrength(Collator::PRIMARY);
175 result = coll->compare(source, target);
176 /* result is 0 */
177 if (result != 0)
178 {
179 errln("Comparing two strings with no differences in C failed.\n");
180 }
181 /* Now, do the same comparison with keys */
182 CollationKey sourceKeyOut,
183 targetKeyOut;
184 coll->getCollationKey(source, sourceKeyOut, status);
185 coll->getCollationKey(target, targetKeyOut, status);
186 result = sourceKeyOut.compareTo(targetKeyOut);
187 if (result != 0)
188 {
189 errln("Comparing two strings with sort keys in C failed.\n");
190 }
191 delete coll;
192 }
193
194 void CollationDummyTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
195 {
196 if (exec) logln("TestSuite CollationDummyTest: ");
197 if(myCollation) {
198 switch (index) {
199 case 0: name = "TestPrimary"; if (exec) TestPrimary(/* par */); break;
200 case 1: name = "TestSecondary"; if (exec) TestSecondary(/* par */); break;
201 case 2: name = "TestTertiary"; if (exec) TestTertiary(/* par */); break;
202 case 3: name = "TestExtra"; if (exec) TestExtra(/* par */); break;
203 case 4: name = "TestIdentical"; if (exec) TestIdentical(/* par */); break;
204 case 5: name = "TestJB581"; if (exec) TestJB581(/* par */); break;
205 default: name = ""; break;
206 }
207 } else {
208 dataerrln("Collator couldn't be instantiated!");
209 name = "";
210 }
211 }
212
213 #endif /* #if !UCONFIG_NO_COLLATION */