]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/g7coll.cpp
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / test / intltest / g7coll.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2003, 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 "g7coll.h"
16 #include "sfwdchit.h"
17
18
19 const UChar G7CollationTest::testCases[][G7CollationTest::MAX_TOKEN_LEN] = {
20 { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/,
21 0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0073 /*'s'*/, 0x0000}, /* 9 */
22 { 0x0050 /*'P'*/, 0x0061 /*'a'*/, 0x0074/*'t'*/, 0x0000}, /* 1 */
23 { 0x0070 /*'p'*/, 0x00E9, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x00E9, 0x0000}, /* 2 */
24 { 0x0070 /*'p'*/, 0x00EA, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0065 /*'e'*/, 0x0000}, /* 3 */
25 { 0x0070 /*'p'*/, 0x00E9, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0065 /*'e'*/, 0x0072 /*'r'*/, 0x0000}, /* 4 */
26 { 0x0070 /*'p'*/, 0x00EA, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0065 /*'e'*/, 0x0072 /*'r'*/, 0x0000}, /* 5 */
27 { 0x0054 /*'T'*/, 0x006f /*'o'*/, 0x0064 /*'d'*/, 0x0000}, /* 6 */
28 { 0x0054 /*'T'*/, 0x00F6, 0x006e /*'n'*/, 0x0065 /*'e'*/, 0x0000}, /* 7 */
29 { 0x0054 /*'T'*/, 0x006f /*'o'*/, 0x0066 /*'f'*/, 0x0075 /*'u'*/, 0x0000}, /* 8 */
30 { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/,
31 0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0000}, /* 12 */
32 { 0x0054 /*'T'*/, 0x006f /*'o'*/, 0x006e /*'n'*/, 0x0000}, /* 10 */
33 { 0x0050 /*'P'*/, 0x0041 /*'A'*/, 0x0054 /*'T'*/, 0x0000}, /* 11 */
34 { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/,
35 0x002d /*'-'*/, 0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0000}, /* 13 */
36 { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/,
37 0x002d /*'-'*/, 0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0073/*'s'*/, 0x0000}, /* 0 */
38 {0x0070 /*'p'*/, 0x0061 /*'a'*/, 0x0074 /*'t'*/, 0x0000}, /* 14 */
39 /* Additional tests */
40 { 0x0063 /*'c'*/, 0x007a /*'z'*/, 0x0061 /*'a'*/, 0x0072 /*'r'*/, 0x0000 }, /* 15 */
41 { 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0075 /*'u'*/, 0x0072 /*'r'*/, 0x006f /*'o'*/, 0x0000 }, /* 16 */
42 { 0x0063 /*'c'*/, 0x0061 /*'a'*/, 0x0074 /*'t'*/, 0x000 }, /* 17 */
43 { 0x0064 /*'d'*/, 0x0061 /*'a'*/, 0x0072 /*'r'*/, 0x006e /*'n'*/, 0x0000 }, /* 18 */
44 { 0x003f /*'?'*/, 0x0000 }, /* 19 */
45 { 0x0071 /*'q'*/, 0x0075 /*'u'*/, 0x0069 /*'i'*/, 0x0063 /*'c'*/, 0x006b /*'k'*/, 0x0000 }, /* 20 */
46 { 0x0023 /*'#'*/, 0x0000 }, /* 21 */
47 { 0x0026 /*'&'*/, 0x0000 }, /* 22 */
48 { 0x0061 /*'a'*/, 0x002d /*'-'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0076 /*'v'*/, 0x0061 /*'a'*/,
49 0x0072/*'r'*/, 0x006b/*'k'*/, 0x0000}, /* 24 */
50 { 0x0061 /*'a'*/, 0x0061 /*'a'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0076 /*'v'*/, 0x0061 /*'a'*/,
51 0x0072/*'r'*/, 0x006b/*'k'*/, 0x0000}, /* 23 */
52 { 0x0061 /*'a'*/, 0x0062 /*'b'*/, 0x0062 /*'b'*/, 0x006f /*'o'*/, 0x0074 /*'t'*/, 0x0000}, /* 25 */
53 { 0x0063 /*'c'*/, 0x006f /*'o'*/, 0x002d /*'-'*/, 0x0070 /*'p'*/, 0x0000}, /* 27 */
54 { 0x0063 /*'c'*/, 0x006f /*'o'*/, 0x0070 /*'p'*/, 0x0000}, /* 28 */
55 { 0x0063 /*'c'*/, 0x006f /*'o'*/, 0x006f /*'o'*/, 0x0070 /*'p'*/, 0x0000}, /* 26 */
56 { 0x007a /*'z'*/, 0x0065 /*'e'*/, 0x0062 /*'b'*/, 0x0072 /*'r'*/, 0x0061 /*'a'*/, 0x0000} /* 29 */
57 };
58
59 const int32_t G7CollationTest::results[G7CollationTest::TESTLOCALES][G7CollationTest::TOTALTESTSET] = {
60 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* en_US */
61 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* en_GB */
62 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* en_CA */
63 { 12, 13, 9, 0, 14, 1, 11, 3, 2, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* fr_FR */
64 { 12, 13, 9, 0, 14, 1, 11, 3, 2, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* fr_CA */
65 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* de_DE */
66 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* it_IT */
67 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* ja_JP */
68 /* new table collation with rules "& Z < p, P" loop to FIXEDTESTSET */
69 { 12, 13, 9, 0, 6, 8, 10, 7, 14, 1, 11, 2, 3, 4, 5, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 },
70 /* new table collation with rules "& C < ch , cH, Ch, CH " loop to TOTALTESTSET */
71 { 19, 22, 21, 23, 24, 25, 12, 13, 9, 0, 17, 26, 28, 27, 15, 16, 18, 14, 1, 11, 2, 3, 4, 5, 20, 6, 8, 10, 7, 29 },
72 /* new table collation with rules "& Question-mark ; ? & Hash-mark ; # & Ampersand ; '&' " loop to TOTALTESTSET */
73 { 23, 24, 25, 22, 12, 13, 9, 0, 17, 16, 26, 28, 27, 15, 18, 21, 14, 1, 11, 2, 3, 4, 5, 19, 20, 6, 8, 10, 7, 29 },
74 /* analogous to Japanese rules " & aa ; a- & ee ; e- & ii ; i- & oo ; o- & uu ; u- " */ /* loop to TOTALTESTSET */
75 { 19, 22, 21, 24, 23, 25, 12, 13, 9, 0, 17, 16, 28, 26, 27, 15, 18, 14, 1, 11, 2, 3, 4, 5, 20, 6, 8, 10, 7, 29 }
76 };
77
78
79 void G7CollationTest::TestG7Locales(/* char* par */)
80 {
81 int32_t i;
82 const Locale locales[8] = {
83 Locale("en", "US", ""),
84 Locale("en", "GB", ""),
85 Locale("en", "CA", ""),
86 Locale("fr", "FR", ""),
87 Locale("fr", "CA", ""),
88 Locale("de", "DE", ""),
89 Locale("it", "IT", ""),
90 Locale("ja", "JP", "")
91 };
92
93
94 for (i = 0; i < 8; i++)
95 {
96 Collator *myCollation= 0;
97 UnicodeString dispName;
98 UErrorCode status = U_ZERO_ERROR;
99 RuleBasedCollator* tblColl1 = 0;
100
101 myCollation = Collator::createInstance(locales[i], status);
102 if(U_FAILURE(status)) {
103 delete myCollation;
104 errln("Couldn't instantiate collator. Error: %s", u_errorName(status));
105 return;
106 }
107 myCollation->setStrength(Collator::QUATERNARY);
108 myCollation->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, status);
109 if (U_FAILURE(status))
110 {
111 UnicodeString msg;
112
113 msg += "Locale ";
114 msg += locales[i].getDisplayName(dispName);
115 msg += "creation failed.";
116
117 errln(msg);
118 continue;
119 }
120
121 // const UnicodeString& defRules = ((RuleBasedCollator*)myCollation)->getRules();
122 status = U_ZERO_ERROR;
123 tblColl1 = new RuleBasedCollator(((RuleBasedCollator*)myCollation)->getRules(), status);
124 if (U_FAILURE(status))
125 {
126 UnicodeString msg, name;
127
128 msg += "Recreate ";
129 msg += locales[i].getDisplayName(name);
130 msg += "collation failed.";
131
132 errln(msg);
133 continue;
134 }
135
136 UnicodeString msg;
137
138 msg += "Locale ";
139 msg += locales[i].getDisplayName(dispName);
140 msg += "tests start :";
141 logln(msg);
142
143 int32_t j, n;
144 for (j = 0; j < FIXEDTESTSET; j++)
145 {
146 for (n = j+1; n < FIXEDTESTSET; n++)
147 {
148 doTest(tblColl1, testCases[results[i][j]], testCases[results[i][n]], Collator::LESS);
149 }
150 }
151
152 delete myCollation;
153 delete tblColl1;
154 }
155 }
156
157 void G7CollationTest::TestDemo1(/* char* par */)
158 {
159 logln("Demo Test 1 : Create a new table collation with rules \"& Z < p, P\"");
160 UErrorCode status = U_ZERO_ERROR;
161 Collator *col = Collator::createInstance("en_US", status);
162 if(U_FAILURE(status)) {
163 delete col;
164 errln("Couldn't instantiate collator. Error: %s", u_errorName(status));
165 return;
166 }
167 const UnicodeString baseRules = ((RuleBasedCollator*)col)->getRules();
168 UnicodeString newRules(" & Z < p, P");
169 newRules.insert(0, baseRules);
170 RuleBasedCollator *myCollation = new RuleBasedCollator(newRules, status);
171
172 if (U_FAILURE(status))
173 {
174 errln( "Demo Test 1 Table Collation object creation failed.");
175 return;
176 }
177
178 int32_t j, n;
179 for (j = 0; j < FIXEDTESTSET; j++)
180 {
181 for (n = j+1; n < FIXEDTESTSET; n++)
182 {
183 doTest(myCollation, testCases[results[8][j]], testCases[results[8][n]], Collator::LESS);
184 }
185 }
186
187 delete myCollation;
188 delete col;
189 }
190
191 void G7CollationTest::TestDemo2(/* char* par */)
192 {
193 logln("Demo Test 2 : Create a new table collation with rules \"& C < ch , cH, Ch, CH\"");
194 UErrorCode status = U_ZERO_ERROR;
195 Collator *col = Collator::createInstance("en_US", status);
196 if(U_FAILURE(status)) {
197 delete col;
198 errln("Couldn't instantiate collator. Error: %s", u_errorName(status));
199 return;
200 }
201 const UnicodeString baseRules = ((RuleBasedCollator*)col)->getRules();
202 UnicodeString newRules("& C < ch , cH, Ch, CH");
203 newRules.insert(0, baseRules);
204 RuleBasedCollator *myCollation = new RuleBasedCollator(newRules, status);
205
206 if (U_FAILURE(status))
207 {
208 errln("Demo Test 2 Table Collation object creation failed.");
209 return;
210 }
211
212 int32_t j, n;
213 for (j = 0; j < TOTALTESTSET; j++)
214 {
215 for (n = j+1; n < TOTALTESTSET; n++)
216 {
217 doTest(myCollation, testCases[results[9][j]], testCases[results[9][n]], Collator::LESS);
218 }
219 }
220
221 delete myCollation;
222 delete col;
223 }
224
225 void G7CollationTest::TestDemo3(/* char* par */)
226 {
227 logln("Demo Test 3 : Create a new table collation with rules \"& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'\"");
228 UErrorCode status = U_ZERO_ERROR;
229 Collator *col = Collator::createInstance("en_US", status);
230 if(U_FAILURE(status)) {
231 errln("Couldn't instantiate collator. Error: %s", u_errorName(status));
232 delete col;
233 return;
234 }
235 const UnicodeString baseRules = ((RuleBasedCollator*)col)->getRules();
236 UnicodeString newRules = "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'";
237 newRules.insert(0, baseRules);
238 RuleBasedCollator *myCollation = new RuleBasedCollator(newRules, status);
239
240 if (U_FAILURE(status))
241 {
242 errln("Demo Test 3 Table Collation object creation failed.");
243 return;
244 }
245
246 int32_t j, n;
247 for (j = 0; j < TOTALTESTSET; j++)
248 {
249 for (n = j+1; n < TOTALTESTSET; n++)
250 {
251 doTest(myCollation, testCases[results[10][j]], testCases[results[10][n]], Collator::LESS);
252 }
253 }
254
255 delete myCollation;
256 delete col;
257 }
258
259 void G7CollationTest::TestDemo4(/* char* par */)
260 {
261 logln("Demo Test 4 : Create a new table collation with rules \" & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' \"");
262 UErrorCode status = U_ZERO_ERROR;
263 Collator *col = Collator::createInstance("en_US", status);
264 if(U_FAILURE(status)) {
265 delete col;
266 errln("Couldn't instantiate collator. Error: %s", u_errorName(status));
267 return;
268 }
269
270 const UnicodeString baseRules = ((RuleBasedCollator*)col)->getRules();
271 UnicodeString newRules = " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' ";
272 newRules.insert(0, baseRules);
273 RuleBasedCollator *myCollation = new RuleBasedCollator(newRules, status);
274
275 int32_t j, n;
276 for (j = 0; j < TOTALTESTSET; j++)
277 {
278 for (n = j+1; n < TOTALTESTSET; n++)
279 {
280 doTest(myCollation, testCases[results[11][j]], testCases[results[11][n]], Collator::LESS);
281 }
282 }
283
284 delete myCollation;
285 delete col;
286 }
287
288 void G7CollationTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
289 {
290 if (exec) logln("TestSuite G7CollationTest: ");
291 switch (index) {
292 case 0: name = "TestG7Locales"; if (exec) TestG7Locales(/* par */); break;
293 case 1: name = "TestDemo1"; if (exec) TestDemo1(/* par */); break;
294 case 2: name = "TestDemo2"; if (exec) TestDemo2(/* par */); break;
295 case 3: name = "TestDemo3"; if (exec) TestDemo3(/* par */); break;
296 case 4: name = "TestDemo4"; if (exec) TestDemo4(/* par */); break;
297 default: name = ""; break;
298 }
299 }
300
301 #endif /* #if !UCONFIG_NO_COLLATION */