]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/apicoll.cpp
ICU-6.2.6.tar.gz
[apple/icu.git] / icuSources / test / intltest / apicoll.cpp
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
374ca955 3 * Copyright (c) 1997-2004, International Business Machines Corporation and
b75a7d8f
A
4 * others. All Rights Reserved.
5 ********************************************************************/
6//===============================================================================
7//
8// File apitest.cpp
9//
10//
11//
12// Created by: Helena Shih
13//
14// Modification History:
15//
16// Date Name Description
17// 2/5/97 aliu Added streamIn and streamOut methods. Added
18// constructor which reads RuleBasedCollator object from
19// a binary file. Added writeToFile method which streams
20// RuleBasedCollator out to a binary file. The streamIn
21// and streamOut methods use istream and ostream objects
22// in binary mode.
23// 6/30/97 helena Added tests for CollationElementIterator::setText, getOffset
24// setOffset and DecompositionIterator::getOffset, setOffset.
25// DecompositionIterator is made public so add class scope
26// testing.
27// 02/10/98 damiba Added test for compare(UnicodeString&, UnicodeString&, int32_t)
28//===============================================================================
29
30#include "unicode/utypes.h"
31
32#if !UCONFIG_NO_COLLATION
33
34#include "unicode/coll.h"
35#include "unicode/tblcoll.h"
36#include "unicode/coleitr.h"
37#include "unicode/sortkey.h"
38#include "apicoll.h"
39#include "unicode/chariter.h"
40#include "unicode/schriter.h"
41#include "unicode/ustring.h"
42
43#include "sfwdchit.h"
44#include "cmemory.h"
45
46void
47CollationAPITest::doAssert(UBool condition, const char *message)
48{
49 if (!condition) {
50 errln(UnicodeString("ERROR : ") + message);
51 }
52}
53
374ca955 54#ifdef U_USE_COLLATION_OBSOLETE_2_6
b75a7d8f
A
55/*
56 * Test Collator::createInstance(... version...) for some locale. Called by TestProperty().
57 */
58static void
59TestOpenVersion(IntlTest &test, const Locale &locale) {
60 UVersionInfo version1, version2;
61 Collator *collator1, *collator2;
62 UErrorCode errorCode;
63
64 errorCode=U_ZERO_ERROR;
65 collator1=Collator::createInstance(locale, errorCode);
66 if(U_SUCCESS(errorCode)) {
67 /* get the current version */
68 collator1->getVersion(version1);
69 delete collator1;
70
71 /* try to get that same version again */
72 collator2=Collator::createInstance(locale, version1, errorCode);
73 if(U_SUCCESS(errorCode)) {
74 collator2->getVersion(version2);
75 if(0!=uprv_memcmp(version1, version2, sizeof(UVersionInfo))) {
76 test.errln("error: Collator::createInstance(\"%s\", (%s collator)->getVersion()) returns a different collator\n", locale.getName(), locale.getName());
77 }
78 delete collator2;
79 } else {
80 test.errln("error: Collator::createInstance(\"%s\", (%s collator)->getVersion()) fails: %s\n", locale.getName(), locale.getName(), u_errorName(errorCode));
81 }
82 }
83}
374ca955 84#endif
b75a7d8f
A
85
86// Collator Class Properties
87// ctor, dtor, createInstance, compare, getStrength/setStrength
88// getDecomposition/setDecomposition, getDisplayName
89void
90CollationAPITest::TestProperty(/* char* par */)
91{
92 UErrorCode success = U_ZERO_ERROR;
93 Collator *col = 0;
94 /*
95 All the collations have the same version in an ICU
96 version.
97 ICU 2.0 currVersionArray = {0x18, 0xC0, 0x02, 0x02};
98 ICU 2.1 currVersionArray = {0x19, 0x00, 0x03, 0x03};
99 ICU 2.2 currVersionArray = {0x21, 0x40, 0x04, 0x04};
100 ICU 2.4 currVersionArray = {0x21, 0x40, 0x04, 0x04};
374ca955 101 ICU 2.6 currVersionArray = {0x21, 0x40, 0x03, 0x03};
b75a7d8f 102 */
374ca955 103 UVersionInfo currVersionArray = {0x29, 0x80, 0x00, 0x04};
b75a7d8f
A
104 UVersionInfo versionArray;
105 int i = 0;
106
107 logln("The property tests begin : ");
108 logln("Test ctors : ");
109 col = Collator::createInstance(Locale::getEnglish(), success);
110
111 if (U_FAILURE(success))
112 {
113 errln("Default Collator creation failed.");
114 return;
115 }
116
117 col->getVersion(versionArray);
118 for (i=0; i<4; ++i) {
119 if (versionArray[i] != currVersionArray[i]) {
120 errln("Testing ucol_getVersion() - unexpected result: %d.%d.%d.%d",
121 versionArray[0], versionArray[1], versionArray[2], versionArray[3]);
122 break;
123 }
124 }
125
126 doAssert((col->compare("ab", "abc") == Collator::LESS), "ab < abc comparison failed");
127 doAssert((col->compare("ab", "AB") == Collator::LESS), "ab < AB comparison failed");
128 doAssert((col->compare("blackbird", "black-bird") == Collator::GREATER), "black-bird > blackbird comparison failed");
129 doAssert((col->compare("black bird", "black-bird") == Collator::LESS), "black bird > black-bird comparison failed");
130 doAssert((col->compare("Hello", "hello") == Collator::GREATER), "Hello > hello comparison failed");
131
132
133 /*start of update [Bertrand A. D. 02/10/98]*/
134 doAssert((col->compare("ab", "abc", 2) == Collator::EQUAL), "ab = abc with length 2 comparison failed");
135 doAssert((col->compare("ab", "AB", 2) == Collator::LESS), "ab < AB with length 2 comparison failed");
136 doAssert((col->compare("ab", "Aa", 1) == Collator::LESS), "ab < Aa with length 1 comparison failed");
137 doAssert((col->compare("ab", "Aa", 2) == Collator::GREATER), "ab > Aa with length 2 comparison failed");
138 doAssert((col->compare("black-bird", "blackbird", 5) == Collator::EQUAL), "black-bird = blackbird with length of 5 comparison failed");
139 doAssert((col->compare("black bird", "black-bird", 10) == Collator::LESS), "black bird < black-bird with length 10 comparison failed");
140 doAssert((col->compare("Hello", "hello", 5) == Collator::GREATER), "Hello > hello with length 5 comparison failed");
141 /*end of update [Bertrand A. D. 02/10/98]*/
142
143
144 logln("Test ctors ends.");
145 logln("testing Collator::getStrength() method ...");
146 doAssert((col->getStrength() == Collator::TERTIARY), "collation object has the wrong strength");
147 doAssert((col->getStrength() != Collator::PRIMARY), "collation object's strength is primary difference");
148
149
150 logln("testing Collator::setStrength() method ...");
151 col->setStrength(Collator::SECONDARY);
152 doAssert((col->getStrength() != Collator::TERTIARY), "collation object's strength is secondary difference");
153 doAssert((col->getStrength() != Collator::PRIMARY), "collation object's strength is primary difference");
154 doAssert((col->getStrength() == Collator::SECONDARY), "collation object has the wrong strength");
155
156 UnicodeString name;
157
158 logln("Get display name for the US English collation in German : ");
159 logln(Collator::getDisplayName(Locale::getUS(), Locale::getGerman(), name));
160 doAssert((name == UnicodeString("Englisch (Vereinigte Staaten)")), "getDisplayName failed");
161
162 logln("Get display name for the US English collation in English : ");
163 logln(Collator::getDisplayName(Locale::getUS(), Locale::getEnglish(), name));
164 doAssert((name == UnicodeString("English (United States)")), "getDisplayName failed");
165#if 0
166 // weiv : this test is bogus if we're running on any machine that has different default locale than English.
167 // Therefore, it is banned!
168 logln("Get display name for the US English in default locale language : ");
169 logln(Collator::getDisplayName(Locale::US, name));
170 doAssert((name == UnicodeString("English (United States)")), "getDisplayName failed if this is an English machine");
171#endif
172 delete col; col = 0;
173 RuleBasedCollator *rcol = (RuleBasedCollator *)Collator::createInstance("da_DK",
174 success);
175 doAssert(rcol->getRules().length() != 0, "da_DK rules does not have length 0");
176 delete rcol;
177
178 col = Collator::createInstance(Locale::getFrench(), success);
179 if (U_FAILURE(success))
180 {
181 errln("Creating French collation failed.");
182 return;
183 }
184
185 col->setStrength(Collator::PRIMARY);
186 logln("testing Collator::getStrength() method again ...");
187 doAssert((col->getStrength() != Collator::TERTIARY), "collation object has the wrong strength");
188 doAssert((col->getStrength() == Collator::PRIMARY), "collation object's strength is not primary difference");
189
190 logln("testing French Collator::setStrength() method ...");
191 col->setStrength(Collator::TERTIARY);
192 doAssert((col->getStrength() == Collator::TERTIARY), "collation object's strength is not tertiary difference");
193 doAssert((col->getStrength() != Collator::PRIMARY), "collation object's strength is primary difference");
194 doAssert((col->getStrength() != Collator::SECONDARY), "collation object's strength is secondary difference");
195
196 logln("Create junk collation: ");
197 Locale abcd("ab", "CD", "");
198 success = U_ZERO_ERROR;
199 Collator *junk = 0;
200 junk = Collator::createInstance(abcd, success);
201
202 if (U_FAILURE(success))
203 {
204 errln("Junk collation creation failed, should at least return default.");
205 delete col;
206 return;
207 }
208
209 delete col;
210 col = Collator::createInstance(success);
211 if (U_FAILURE(success))
212 {
213 errln("Creating default collator failed.");
214 delete junk;
215 return;
216 }
217
218 doAssert(((RuleBasedCollator *)col)->getRules() == ((RuleBasedCollator *)junk)->getRules(),
219 "The default collation should be returned.");
220 Collator *frCol = Collator::createInstance(Locale::getFrance(), success);
221 if (U_FAILURE(success))
222 {
223 errln("Creating French collator failed.");
224 delete col; delete junk;
225 return;
226 }
227
228 doAssert((*frCol != *junk), "The junk is the same as the French collator.");
229 Collator *aFrCol = frCol->clone();
230 doAssert((*frCol == *aFrCol), "The cloning of a French collator failed.");
231 logln("Collator property test ended.");
232
233 delete col;
234 delete frCol;
235 delete aFrCol;
236 delete junk;
237
374ca955 238#ifdef U_USE_COLLATION_OBSOLETE_2_6
b75a7d8f
A
239 /* test Collator::createInstance(...version...) */
240 TestOpenVersion(*this, "");
241 TestOpenVersion(*this, "da");
242 TestOpenVersion(*this, "fr");
243 TestOpenVersion(*this, "ja");
244
245 /* try some bogus version */
246 versionArray[0]=0;
247 versionArray[1]=0x99;
248 versionArray[2]=0xc7;
249 versionArray[3]=0xfe;
250 col=Collator::createInstance(Locale(), versionArray, success);
251 if(U_SUCCESS(success)) {
252 errln("error: ucol_openVersion(bogus version) succeeded");
253 delete col;
254 }
374ca955 255#endif
b75a7d8f
A
256}
257
258void
259CollationAPITest::TestRuleBasedColl()
260{
261 RuleBasedCollator *col1, *col2, *col3, *col4;
262 UErrorCode status = U_ZERO_ERROR;
263
264 UnicodeString ruleset1("&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
265 UnicodeString ruleset2("&9 < a, A < b, B < c, C < d, D, e, E");
266
267 col1 = new RuleBasedCollator(ruleset1, status);
268 if (U_FAILURE(status)) {
269 errln("RuleBased Collator creation failed.\n");
270 return;
271 }
272 else {
273 logln("PASS: RuleBased Collator creation passed\n");
274 }
275
276 status = U_ZERO_ERROR;
277 col2 = new RuleBasedCollator(ruleset2, status);
278 if (U_FAILURE(status)) {
279 errln("RuleBased Collator creation failed.\n");
280 return;
281 }
282 else {
283 logln("PASS: RuleBased Collator creation passed\n");
284 }
285
286 status = U_ZERO_ERROR;
287 Locale locale("aa", "AA");
288 col3 = (RuleBasedCollator *)Collator::createInstance(locale, status);
289 if (U_FAILURE(status)) {
290 errln("Fallback Collator creation failed.: %s\n");
291 return;
292 }
293 else {
294 logln("PASS: Fallback Collator creation passed\n");
295 }
296 delete col3;
297
298 status = U_ZERO_ERROR;
299 col3 = (RuleBasedCollator *)Collator::createInstance(status);
300 if (U_FAILURE(status)) {
301 errln("Default Collator creation failed.: %s\n");
302 return;
303 }
304 else {
305 logln("PASS: Default Collator creation passed\n");
306 }
307
308 UnicodeString rule1 = col1->getRules();
309 UnicodeString rule2 = col2->getRules();
310 UnicodeString rule3 = col3->getRules();
311
312 doAssert(rule1 != rule2, "Default collator getRules failed");
313 doAssert(rule2 != rule3, "Default collator getRules failed");
314 doAssert(rule1 != rule3, "Default collator getRules failed");
315
316 col4 = new RuleBasedCollator(rule2, status);
317 if (U_FAILURE(status)) {
318 errln("RuleBased Collator creation failed.\n");
319 return;
320 }
321
322 UnicodeString rule4 = col4->getRules();
323 doAssert(rule2 == rule4, "Default collator getRules failed");
324 int32_t length4 = 0;
325 uint8_t *clonedrule4 = col4->cloneRuleData(length4, status);
326 if (U_FAILURE(status)) {
327 errln("Cloned rule data failed.\n");
328 return;
329 }
330
331 // free(clonedrule4); BAD API!!!!
332 uprv_free(clonedrule4);
333
334
335 delete col1;
336 delete col2;
337 delete col3;
338 delete col4;
339}
340
341void
342CollationAPITest::TestRules()
343{
344 RuleBasedCollator *coll;
345 UErrorCode status = U_ZERO_ERROR;
346 UnicodeString rules;
347
348 coll = (RuleBasedCollator *)Collator::createInstance(Locale::getEnglish(), status);
349 if (U_FAILURE(status)) {
350 errln("English Collator creation failed.\n");
351 return;
352 }
353 else {
354 logln("PASS: RuleBased Collator creation passed\n");
355 }
356
357 coll->getRules(UCOL_TAILORING_ONLY, rules);
374ca955
A
358 if (rules.length() != 0x0a) {
359 errln("English tailored rules failed - length is 0x%x expected 0x%x", rules.length(), 0x0e);
b75a7d8f
A
360 }
361
362 coll->getRules(UCOL_FULL_RULES, rules);
363 if (rules.length() < 0) {
364 errln("English full rules failed");
365 }
366 delete coll;
367}
368
369void
370CollationAPITest::TestDecomposition() {
371 UErrorCode status = U_ZERO_ERROR;
372 Collator *en_US = Collator::createInstance("en_US", status),
373 *el_GR = Collator::createInstance("el_GR", status),
374 *vi_VN = Collator::createInstance("vi_VN", status);
375
376 if (U_FAILURE(status)) {
377 errln("ERROR: collation creation failed.\n");
378 return;
379 }
380
381 /* there is no reason to have canonical decomposition in en_US OR default locale */
382 if (vi_VN->getAttribute(UCOL_NORMALIZATION_MODE, status) != UCOL_ON)
383 {
374ca955 384 errln("ERROR: vi_VN collation did not have canonical decomposition for normalization!\n");
b75a7d8f
A
385 }
386
387 if (el_GR->getAttribute(UCOL_NORMALIZATION_MODE, status) != UCOL_ON)
388 {
374ca955 389 errln("ERROR: el_GR collation did not have canonical decomposition for normalization!\n");
b75a7d8f
A
390 }
391
392 if (en_US->getAttribute(UCOL_NORMALIZATION_MODE, status) != UCOL_OFF)
393 {
374ca955 394 errln("ERROR: en_US collation had canonical decomposition for normalization!\n");
b75a7d8f
A
395 }
396
397 delete en_US;
398 delete el_GR;
399 delete vi_VN;
400}
401
402void
403CollationAPITest::TestSafeClone() {
404 static const int CLONETEST_COLLATOR_COUNT = 3;
405 Collator *someCollators [CLONETEST_COLLATOR_COUNT];
406 Collator *col;
407 UErrorCode err = U_ZERO_ERROR;
408 int index;
409
410 UnicodeString test1("abCda");
411 UnicodeString test2("abcda");
412
413 /* one default collator & two complex ones */
414 someCollators[0] = Collator::createInstance("en_US", err);
415 someCollators[1] = Collator::createInstance("ko", err);
416 someCollators[2] = Collator::createInstance("ja_JP", err);
417 if(U_FAILURE(err)) {
418 errln("Couldn't instantiate collators. Error: %s", u_errorName(err));
419 delete someCollators[0];
420 delete someCollators[1];
421 delete someCollators[2];
422 return;
423 }
424
425 /* change orig & clone & make sure they are independent */
426
427 for (index = 0; index < CLONETEST_COLLATOR_COUNT; index++)
428 {
429 col = someCollators[index]->safeClone();
430 if (col == 0) {
431 errln("SafeClone of collator should not return null\n");
432 break;
433 }
434 col->setStrength(Collator::TERTIARY);
435 someCollators[index]->setStrength(Collator::PRIMARY);
436 col->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, err);
437 someCollators[index]->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, err);
438
439 doAssert(col->greater(test1, test2), "Result should be \"abCda\" >>> \"abcda\" ");
440 doAssert(someCollators[index]->equals(test1, test2), "Result should be \"abcda\" == \"abCda\"");
441 delete col;
442 delete someCollators[index];
443 }
444}
445
446void
447CollationAPITest::TestHashCode(/* char* par */)
448{
449 logln("hashCode tests begin.");
450 UErrorCode success = U_ZERO_ERROR;
451 Collator *col1 = 0;
452 col1 = Collator::createInstance(Locale::getEnglish(), success);
453 if (U_FAILURE(success))
454 {
455 errln("Default collation creation failed.");
456 return;
457 }
458
459 Collator *col2 = 0;
460 Locale dk("da", "DK", "");
461 col2 = Collator::createInstance(dk, success);
462 if (U_FAILURE(success))
463 {
464 errln("Danish collation creation failed.");
465 return;
466 }
467
468 Collator *col3 = 0;
469 col3 = Collator::createInstance(Locale::getEnglish(), success);
470 if (U_FAILURE(success))
471 {
472 errln("2nd default collation creation failed.");
473 return;
474 }
475
476 logln("Collator::hashCode() testing ...");
477
478 doAssert(col1->hashCode() != col2->hashCode(), "Hash test1 result incorrect" );
479 doAssert(!(col1->hashCode() == col2->hashCode()), "Hash test2 result incorrect" );
480 doAssert(col1->hashCode() == col3->hashCode(), "Hash result not equal" );
481
482 logln("hashCode tests end.");
483 delete col1;
484 delete col2;
485
486 UnicodeString test1("Abcda");
487 UnicodeString test2("abcda");
488
489 CollationKey sortk1, sortk2, sortk3;
490 UErrorCode status = U_ZERO_ERROR;
491
492 col3->getCollationKey(test1, sortk1, status);
493 col3->getCollationKey(test2, sortk2, status);
494 col3->getCollationKey(test2, sortk3, status);
495
496 doAssert(sortk1.hashCode() != sortk2.hashCode(), "Hash test1 result incorrect");
497 doAssert(sortk2.hashCode() == sortk3.hashCode(), "Hash result not equal" );
498
499 delete col3;
500}
501
502//----------------------------------------------------------------------------
503// CollationKey -- Tests the CollationKey methods
504//
505void
506CollationAPITest::TestCollationKey(/* char* par */)
507{
508 logln("testing CollationKey begins...");
509 Collator *col = 0;
510 UErrorCode success=U_ZERO_ERROR;
511 col = Collator::createInstance(Locale::getEnglish(), success);
512 if (U_FAILURE(success))
513 {
514 errln("Default collation creation failed.");
515 return;
516 }
517 col->setStrength(Collator::TERTIARY);
518
519 CollationKey sortk1, sortk2;
520 UnicodeString test1("Abcda"), test2("abcda");
521 UErrorCode key1Status = U_ZERO_ERROR, key2Status = U_ZERO_ERROR;
522
523 logln("Testing weird arguments");
524 col->getCollationKey(NULL, 0, sortk1, key1Status);
525 // key gets reset here
526 int32_t length;
527 sortk1.getByteArray(length);
528 doAssert(sortk1.isBogus() == FALSE && length == 0,
529 "Empty string should return an empty collation key");
530 // bogus key returned here
531 key1Status = U_ILLEGAL_ARGUMENT_ERROR;
532 col->getCollationKey(NULL, 0, sortk1, key1Status);
533 doAssert(sortk1.getByteArray(length) == NULL && length == 0,
534 "Error code should return bogus collation key");
535
536 key1Status = U_ZERO_ERROR;
537 logln("Use tertiary comparison level testing ....");
538
539 col->getCollationKey(test1, sortk1, key1Status);
540 doAssert((sortk1.compareTo(col->getCollationKey(test2, sortk2, key2Status)))
541 == Collator::GREATER,
542 "Result should be \"Abcda\" >>> \"abcda\"");
543
544 CollationKey sortk3(sortk2), sortkNew, sortkEmpty;
545
546
547 sortkNew = sortk1;
548 doAssert((sortk1 != sortk2), "The sort keys should be different");
549 doAssert((sortk1.hashCode() != sortk2.hashCode()), "sort key hashCode() failed");
550 doAssert((sortk2 == sortk3), "The sort keys should be the same");
551 doAssert((sortk1 == sortkNew), "The sort keys assignment failed");
552 doAssert((sortk1.hashCode() == sortkNew.hashCode()), "sort key hashCode() failed");
553 doAssert((sortkNew != sortk3), "The sort keys should be different");
554 doAssert(sortk1.compareTo(sortk3) == Collator::GREATER, "Result should be \"Abcda\" >>> \"abcda\"");
555 doAssert(sortk2.compareTo(sortk3) == Collator::EQUAL, "Result should be \"abcda\" == \"abcda\"");
556 doAssert(sortkEmpty.compareTo(sortk1) == Collator::LESS, "Result should be (empty key) <<< \"Abcda\"");
557 doAssert(sortk1.compareTo(sortkEmpty) == Collator::GREATER, "Result should be \"Abcda\" >>> (empty key)");
558 doAssert(sortkEmpty.compareTo(sortkEmpty) == Collator::EQUAL, "Result should be (empty key) == (empty key)");
559 doAssert(sortk1.compareTo(sortk3, success) == UCOL_GREATER, "Result should be \"Abcda\" >>> \"abcda\"");
560 doAssert(sortk2.compareTo(sortk3, success) == UCOL_EQUAL, "Result should be \"abcda\" == \"abcda\"");
561 doAssert(sortkEmpty.compareTo(sortk1, success) == UCOL_LESS, "Result should be (empty key) <<< \"Abcda\"");
562 doAssert(sortk1.compareTo(sortkEmpty, success) == UCOL_GREATER, "Result should be \"Abcda\" >>> (empty key)");
563 doAssert(sortkEmpty.compareTo(sortkEmpty, success) == UCOL_EQUAL, "Result should be (empty key) == (empty key)");
564
565 int32_t cnt1, cnt2, cnt3, cnt4;
566
567 const uint8_t* byteArray1 = sortk1.getByteArray(cnt1);
568 const uint8_t* byteArray2 = sortk2.getByteArray(cnt2);
569
570 /*
571 this is a bad test since it is dependent on the version of uca data,
572 which changes
573 will remove it.
574 const char sortk2_compat[] = {
575 // this is a 1.8 sortkey
576 0x17, 0x19, 0x1B, 0x1D, 0x17, 0x01, 0x08, 0x01, 0x08, 0x00
577 };
578 */
579
580 const uint8_t* byteArray3 = 0;
581 byteArray3 = sortk1.getByteArray(cnt3);
582
583 const uint8_t* byteArray4 = 0;
584 byteArray4 = sortk2.getByteArray(cnt4);
585
586 CollationKey sortk4(byteArray1, cnt1), sortk5(byteArray2, cnt2);
587 CollationKey sortk6(byteArray3, cnt3), sortk7(byteArray4, cnt4);
588
589 /*
590 doAssert(memcmp(byteArray2, sortk2_compat, strlen(sortk2_compat)) == 0,
591 "Binary format for 'abcda' sortkey different!");
592 */
593 doAssert(sortk1.compareTo(sortk4) == Collator::EQUAL, "CollationKey::toByteArray(sortk1) Failed.");
594 doAssert(sortk2.compareTo(sortk5) == Collator::EQUAL, "CollationKey::toByteArray(sortk2) Failed.");
595 doAssert(sortk4.compareTo(sortk5) == Collator::GREATER, "sortk4 >>> sortk5 Failed");
596 doAssert(sortk1.compareTo(sortk6) == Collator::EQUAL, "CollationKey::getByteArray(sortk1) Failed.");
597 doAssert(sortk2.compareTo(sortk7) == Collator::EQUAL, "CollationKey::getByteArray(sortk2) Failed.");
598 doAssert(sortk6.compareTo(sortk7) == Collator::GREATER, "sortk6 >>> sortk7 Failed");
599
600 logln("Equality tests : ");
601 doAssert(sortk1 == sortk4, "sortk1 == sortk4 Failed.");
602 doAssert(sortk2 == sortk5, "sortk2 == sortk5 Failed.");
603 doAssert(sortk1 != sortk5, "sortk1 != sortk5 Failed.");
604 doAssert(sortk1 == sortk6, "sortk1 == sortk6 Failed.");
605 doAssert(sortk2 == sortk7, "sortk2 == sortk7 Failed.");
606 doAssert(sortk1 != sortk7, "sortk1 != sortk7 Failed.");
607
608 byteArray1 = 0;
609 byteArray2 = 0;
610
611 sortk3 = sortk1;
612 doAssert(sortk1 == sortk3, "sortk1 = sortk3 assignment Failed.");
613 doAssert(sortk2 != sortk3, "sortk2 != sortk3 Failed.");
614 logln("testing sortkey ends...");
615
616 col->setStrength(Collator::SECONDARY);
617 doAssert(col->getCollationKey(test1, sortk1, key1Status).compareTo(
618 col->getCollationKey(test2, sortk2, key2Status))
619 == Collator::EQUAL,
620 "Result should be \"Abcda\" == \"abcda\"");
621 delete col;
622}
623
624//----------------------------------------------------------------------------
625// Tests the CollatorElementIterator class.
626// ctor, RuleBasedCollator::createCollationElementIterator(), operator==, operator!=
627//
628void
629CollationAPITest::TestElemIter(/* char* par */)
630{
631 logln("testing sortkey begins...");
632 Collator *col = 0;
633 UErrorCode success = U_ZERO_ERROR;
634 col = Collator::createInstance(Locale::getEnglish(), success);
635 if (U_FAILURE(success))
636 {
637 errln("Default collation creation failed.");
638 return;
639 }
640
641 UnicodeString testString1("XFILE What subset of all possible test cases has the highest probability of detecting the most errors?");
642 UnicodeString testString2("Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?");
643 logln("Constructors and comparison testing....");
644 CollationElementIterator *iterator1 = ((RuleBasedCollator*)col)->createCollationElementIterator(testString1);
645
646 CharacterIterator *chariter=new StringCharacterIterator(testString1);
647 CollationElementIterator *coliter=((RuleBasedCollator*)col)->createCollationElementIterator(*chariter);
648
649 // copy ctor
650 CollationElementIterator *iterator2 = ((RuleBasedCollator*)col)->createCollationElementIterator(testString1);
651 CollationElementIterator *iterator3 = ((RuleBasedCollator*)col)->createCollationElementIterator(testString2);
652
653 int32_t offset = iterator1->getOffset();
654 if (offset != 0) {
655 errln("Error in getOffset for collation element iterator\n");
656 return;
657 }
658 iterator1->setOffset(6, success);
659 if (U_FAILURE(success)) {
660 errln("Error in setOffset for collation element iterator\n");
661 return;
662 }
663 iterator1->setOffset(0, success);
664 int32_t order1, order2, order3;
665 doAssert((*iterator1 == *iterator2), "The two iterators should be the same");
666 doAssert((*iterator1 != *iterator3), "The two iterators should be different");
667
668 doAssert((*coliter == *iterator1), "The two iterators should be the same");
669 doAssert((*coliter == *iterator2), "The two iterators should be the same");
670 doAssert((*coliter != *iterator3), "The two iterators should be different");
671
672 order1 = iterator1->next(success);
673 if (U_FAILURE(success))
674 {
675 errln("Somehow ran out of memory stepping through the iterator.");
676 return;
677 }
678
679 doAssert((*iterator1 != *iterator2), "The first iterator advance failed");
680 order2 = iterator2->getOffset();
681 doAssert((order1 != order2), "The order result should not be the same");
682 order2 = iterator2->next(success);
683 if (U_FAILURE(success))
684 {
685 errln("Somehow ran out of memory stepping through the iterator.");
686 return;
687 }
688
689 doAssert((*iterator1 == *iterator2), "The second iterator advance failed");
690 doAssert((order1 == order2), "The order result should be the same");
691 order3 = iterator3->next(success);
692 if (U_FAILURE(success))
693 {
694 errln("Somehow ran out of memory stepping through the iterator.");
695 return;
696 }
697
698 doAssert((CollationElementIterator::primaryOrder(order1) ==
699 CollationElementIterator::primaryOrder(order3)), "The primary orders should be the same");
700 doAssert((CollationElementIterator::secondaryOrder(order1) ==
701 CollationElementIterator::secondaryOrder(order3)), "The secondary orders should be the same");
702 doAssert((CollationElementIterator::tertiaryOrder(order1) ==
703 CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be the same");
704
705 order1 = iterator1->next(success); order3 = iterator3->next(success);
706 if (U_FAILURE(success))
707 {
708 errln("Somehow ran out of memory stepping through the iterator.");
709 return;
710 }
711
712 doAssert((CollationElementIterator::primaryOrder(order1) ==
713 CollationElementIterator::primaryOrder(order3)), "The primary orders should be identical");
714 doAssert((CollationElementIterator::tertiaryOrder(order1) !=
715 CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be different");
716
717 order1 = iterator1->next(success);
718 order3 = iterator3->next(success);
719 /* NO! Secondary orders of two CEs are not related, especially in the case of '_' vs 'I' */
720 /*
721 doAssert((CollationElementIterator::secondaryOrder(order1) !=
722 CollationElementIterator::secondaryOrder(order3)), "The secondary orders should not be the same");
723 */
724 doAssert((order1 != CollationElementIterator::NULLORDER), "Unexpected end of iterator reached");
725
726 iterator1->reset(); iterator2->reset(); iterator3->reset();
727 order1 = iterator1->next(success);
728 if (U_FAILURE(success))
729 {
730 errln("Somehow ran out of memory stepping through the iterator.");
731 return;
732 }
733
734 doAssert((*iterator1 != *iterator2), "The first iterator advance failed");
735
736 order2 = iterator2->next(success);
737 if (U_FAILURE(success))
738 {
739 errln("Somehow ran out of memory stepping through the iterator.");
740 return;
741 }
742
743 doAssert((*iterator1 == *iterator2), "The second iterator advance failed");
744 doAssert((order1 == order2), "The order result should be the same");
745
746 order3 = iterator3->next(success);
747 if (U_FAILURE(success))
748 {
749 errln("Somehow ran out of memory stepping through the iterator.");
750 return;
751 }
752
753 doAssert((CollationElementIterator::primaryOrder(order1) ==
754 CollationElementIterator::primaryOrder(order3)), "The primary orders should be the same");
755 doAssert((CollationElementIterator::secondaryOrder(order1) ==
756 CollationElementIterator::secondaryOrder(order3)), "The secondary orders should be the same");
757 doAssert((CollationElementIterator::tertiaryOrder(order1) ==
758 CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be the same");
759
760 order1 = iterator1->next(success); order2 = iterator2->next(success); order3 = iterator3->next(success);
761 if (U_FAILURE(success))
762 {
763 errln("Somehow ran out of memory stepping through the iterator.");
764 return;
765 }
766
767 doAssert((CollationElementIterator::primaryOrder(order1) ==
768 CollationElementIterator::primaryOrder(order3)), "The primary orders should be identical");
769 doAssert((CollationElementIterator::tertiaryOrder(order1) !=
770 CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be different");
771
772 order1 = iterator1->next(success); order3 = iterator3->next(success);
773 if (U_FAILURE(success))
774 {
775 errln("Somehow ran out of memory stepping through the iterator.");
776 return;
777 }
778
779 /* NO! Secondary orders of two CEs are not related, especially in the case of '_' vs 'I' */
780 /*
781 doAssert((CollationElementIterator::secondaryOrder(order1) !=
782 CollationElementIterator::secondaryOrder(order3)), "The secondary orders should not be the same");
783 */
784 doAssert((order1 != CollationElementIterator::NULLORDER), "Unexpected end of iterator reached");
785 doAssert((*iterator2 != *iterator3), "The iterators should be different");
786
787
788 //test error values
789 success=U_UNSUPPORTED_ERROR;
790 Collator *colerror=NULL;
791 colerror=Collator::createInstance(Locale::getEnglish(), success);
792 if (colerror != 0 || success == U_ZERO_ERROR){
793 errln("Error: createInstance(UErrorCode != U_ZERO_ERROR) should just return and not create an instance\n");
794 }
795 int32_t position=coliter->previous(success);
796 if(position != CollationElementIterator::NULLORDER){
797 errln((UnicodeString)"Expected NULLORDER got" + position);
798 }
799 coliter->reset();
800 coliter->setText(*chariter, success);
801 if(!U_FAILURE(success)){
802 errln("Expeceted error");
803 }
804 iterator1->setText((UnicodeString)"hello there", success);
805 if(!U_FAILURE(success)){
806 errln("Expeceted error");
807 }
808
809 delete chariter;
810 delete coliter;
811 delete iterator1;
812 delete iterator2;
813 delete iterator3;
814 delete col;
815
816
817
818 logln("testing CollationElementIterator ends...");
819}
820
821// Test RuleBasedCollator ctor, dtor, operator==, operator!=, clone, copy, and getRules
822void
823CollationAPITest::TestOperators(/* char* par */)
824{
825 UErrorCode success = U_ZERO_ERROR;
826 UnicodeString ruleset1("< a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
827 UnicodeString ruleset2("< a, A < b, B < c, C < d, D, e, E");
828 RuleBasedCollator *col1 = new RuleBasedCollator(ruleset1, success);
829 if (U_FAILURE(success)) {
830 errln("RuleBasedCollator creation failed.");
831 return;
832 }
833 success = U_ZERO_ERROR;
834 RuleBasedCollator *col2 = new RuleBasedCollator(ruleset2, success);
835 if (U_FAILURE(success)) {
836 errln("The RuleBasedCollator constructor failed when building with the 2nd rule set.");
837 return;
838 }
839 logln("The operator tests begin : ");
840 logln("testing operator==, operator!=, clone methods ...");
841 doAssert((*col1 != *col2), "The two different table collations compared equal");
842 *col1 = *col2;
843 doAssert((*col1 == *col2), "Collator objects not equal after assignment (operator=)");
844
845 success = U_ZERO_ERROR;
846 Collator *col3 = Collator::createInstance(Locale::getEnglish(), success);
847 if (U_FAILURE(success)) {
848 errln("Default collation creation failed.");
849 return;
850 }
851 doAssert((*col1 != *col3), "The two different table collations compared equal");
852 Collator* col4 = col1->clone();
853 Collator* col5 = col3->clone();
854 doAssert((*col1 == *col4), "Cloned collation objects not equal");
855 doAssert((*col3 != *col4), "Two different table collations compared equal");
856 doAssert((*col3 == *col5), "Cloned collation objects not equal");
857 doAssert((*col4 != *col5), "Two cloned collations compared equal");
858
859 const UnicodeString& defRules = ((RuleBasedCollator*)col3)->getRules();
860 RuleBasedCollator* col6 = new RuleBasedCollator(defRules, success);
861 if (U_FAILURE(success)) {
862 errln("Creating default collation with rules failed.");
863 return;
864 }
865 doAssert((((RuleBasedCollator*)col3)->getRules() == col6->getRules()), "Default collator getRules failed");
866
867 success = U_ZERO_ERROR;
868 RuleBasedCollator *col7 = new RuleBasedCollator(ruleset2, Collator::TERTIARY, success);
869 if (U_FAILURE(success)) {
870 errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with tertiary strength.");
871 return;
872 }
873 success = U_ZERO_ERROR;
874 RuleBasedCollator *col8 = new RuleBasedCollator(ruleset2, UCOL_OFF, success);
875 if (U_FAILURE(success)) {
876 errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with Normalizer::NO_OP.");
877 return;
878 }
879 success = U_ZERO_ERROR;
880 RuleBasedCollator *col9 = new RuleBasedCollator(ruleset2, Collator::PRIMARY, UCOL_ON, success);
881 if (U_FAILURE(success)) {
882 errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with tertiary strength and Normalizer::NO_OP.");
883 return;
884 }
885 // doAssert((*col7 == *col8), "The two equal table collations compared different");
886 doAssert((*col7 != *col9), "The two different table collations compared equal");
887 doAssert((*col8 != *col9), "The two different table collations compared equal");
888
889 logln("operator tests ended.");
890 delete col1;
891 delete col2;
892 delete col3;
893 delete col4;
894 delete col5;
895 delete col6;
896 delete col7;
897 delete col8;
898 delete col9;
899}
900
901// test clone and copy
902void
903CollationAPITest::TestDuplicate(/* char* par */)
904{
905 UErrorCode status = U_ZERO_ERROR;
906 Collator *col1 = Collator::createInstance(Locale::getEnglish(), status);
907 if (U_FAILURE(status)) {
908 logln("Default collator creation failed.");
909 return;
910 }
911 Collator *col2 = col1->clone();
912 doAssert((*col1 == *col2), "Cloned object is not equal to the orginal");
913 UnicodeString ruleset("< a, A < b, B < c, C < d, D, e, E");
914 RuleBasedCollator *col3 = new RuleBasedCollator(ruleset, status);
915 doAssert((*col1 != *col3), "Cloned object is equal to some dummy");
916 *col3 = *((RuleBasedCollator*)col1);
917 doAssert((*col1 == *col3), "Copied object is not equal to the orginal");
918 delete col1;
919 delete col2;
920 delete col3;
921}
922
923void
924CollationAPITest::TestCompare(/* char* par */)
925{
926 logln("The compare tests begin : ");
927 Collator *col = 0;
928 UErrorCode success = U_ZERO_ERROR;
929 col = Collator::createInstance(Locale::getEnglish(), success);
930 if (U_FAILURE(success)) {
931 errln("Default collation creation failed.");
932 return;
933 }
934 UnicodeString test1("Abcda"), test2("abcda");
935 logln("Use tertiary comparison level testing ....");
936
937 doAssert((!col->equals(test1, test2) ), "Result should be \"Abcda\" != \"abcda\"");
938 doAssert((col->greater(test1, test2) ), "Result should be \"Abcda\" >>> \"abcda\"");
939 doAssert((col->greaterOrEqual(test1, test2) ), "Result should be \"Abcda\" >>> \"abcda\"");
940
941 col->setStrength(Collator::SECONDARY);
942 logln("Use secondary comparison level testing ....");
943
944 doAssert((col->equals(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
945 doAssert((!col->greater(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
946 doAssert((col->greaterOrEqual(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
947
948 col->setStrength(Collator::PRIMARY);
949 logln("Use primary comparison level testing ....");
950
951 doAssert((col->equals(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
952 doAssert((!col->greater(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
953 doAssert((col->greaterOrEqual(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
954
955 // Test different APIs
956 const UChar* t1 = test1.getBuffer();
957 int32_t t1Len = test1.length();
958 const UChar* t2 = test2.getBuffer();
959 int32_t t2Len = test2.length();
960
961 doAssert((col->compare(test1, test2) == Collator::EQUAL), "Problem");
962 doAssert((col->compare(test1, test2, success) == UCOL_EQUAL), "Problem");
963 doAssert((col->compare(t1, t1Len, t2, t2Len) == Collator::EQUAL), "Problem");
964 doAssert((col->compare(t1, t1Len, t2, t2Len, success) == UCOL_EQUAL), "Problem");
965 doAssert((col->compare(test1, test2, t1Len) == Collator::EQUAL), "Problem");
966 doAssert((col->compare(test1, test2, t1Len, success) == UCOL_EQUAL), "Problem");
967
968 col->setAttribute(UCOL_STRENGTH, UCOL_TERTIARY, success);
969 doAssert((col->compare(test1, test2) == Collator::GREATER), "Problem");
970 doAssert((col->compare(test1, test2, success) == UCOL_GREATER), "Problem");
971 doAssert((col->compare(t1, t1Len, t2, t2Len) == Collator::GREATER), "Problem");
972 doAssert((col->compare(t1, t1Len, t2, t2Len, success) == UCOL_GREATER), "Problem");
973 doAssert((col->compare(test1, test2, t1Len) == Collator::GREATER), "Problem");
974 doAssert((col->compare(test1, test2, t1Len, success) == UCOL_GREATER), "Problem");
975
976
977
978 logln("The compare tests end.");
979 delete col;
980}
981
982void
983CollationAPITest::TestGetAll(/* char* par */)
984{
985 int32_t count;
986 const Locale* list = Collator::getAvailableLocales(count);
987 for (int32_t i = 0; i < count; ++i) {
988 UnicodeString locName, dispName;
989 log("Locale name: ");
990 log(list[i].getName());
991 log(" , the display name is : ");
992 logln(list[i].getDisplayName(dispName));
993 }
994}
995
996void CollationAPITest::TestSortKey()
997{
998 UErrorCode status = U_ZERO_ERROR;
999 /*
1000 this is supposed to open default date format, but later on it treats
1001 it like it is "en_US"
1002 - very bad if you try to run the tests on machine where default
1003 locale is NOT "en_US"
1004 */
1005 Collator *col = Collator::createInstance(Locale::getEnglish(), status);
1006 if (U_FAILURE(status)) {
1007 errln("ERROR: Default collation creation failed.: %s\n", u_errorName(status));
1008 return;
1009 }
1010
1011 if (col->getStrength() != Collator::TERTIARY)
1012 {
1013 errln("ERROR: default collation did not have UCOL_DEFAULT_STRENGTH !\n");
1014 }
1015
1016 /* Need to use identical strength */
1017 col->setAttribute(UCOL_STRENGTH, UCOL_IDENTICAL, status);
1018
1019 uint8_t key2compat[] = {
374ca955
A
1020 /* 2.6.1 key */
1021 0x26, 0x28, 0x2A, 0x2C, 0x26, 0x01,
1022 0x09, 0x01, 0x09, 0x01, 0x25, 0x01,
1023 0x92, 0x93, 0x94, 0x95, 0x92, 0x00
1024
b75a7d8f 1025 /* 2.2 key */
374ca955 1026 /*
b75a7d8f
A
1027 0x1D, 0x1F, 0x21, 0x23, 0x1D, 0x01,
1028 0x09, 0x01, 0x09, 0x01, 0x1C, 0x01,
1029 0x92, 0x93, 0x94, 0x95, 0x92, 0x00
374ca955 1030 */
b75a7d8f
A
1031 /* 2.0 key */
1032 /*
1033 0x19, 0x1B, 0x1D, 0x1F, 0x19,
1034 0x01, 0x09, 0x01, 0x09, 0x01,
1035 0x18, 0x01,
1036 0x92, 0x93, 0x94, 0x95, 0x92,
1037 0x00
1038 */
1039 /* 1.8.1 key.*/
1040 /*
1041 0x19, 0x1B, 0x1D, 0x1F, 0x19,
1042 0x01, 0x0A, 0x01, 0x0A, 0x01,
1043 0x92, 0x93, 0x94, 0x95, 0x92,
1044 0x00
1045 */
1046 };
1047
1048 UChar test1[6] = {0x41, 0x62, 0x63, 0x64, 0x61, 0},
1049 test2[6] = {0x61, 0x62, 0x63, 0x64, 0x61, 0},
1050 test3[6] = {0x61, 0x62, 0x63, 0x64, 0x61, 0};
1051
1052 uint8_t sortkey1[64];
1053 uint8_t sortkey2[64];
1054 uint8_t sortkey3[64];
1055
1056 logln("Use tertiary comparison level testing ....\n");
1057
1058 CollationKey key1;
1059 col->getCollationKey(test1, u_strlen(test1), key1, status);
1060
1061 CollationKey key2;
1062 col->getCollationKey(test2, u_strlen(test2), key2, status);
1063
1064 CollationKey key3;
1065 col->getCollationKey(test3, u_strlen(test3), key3, status);
1066
1067 doAssert(key1.compareTo(key2) == Collator::GREATER,
1068 "Result should be \"Abcda\" > \"abcda\"");
1069 doAssert(key2.compareTo(key1) == Collator::LESS,
1070 "Result should be \"abcda\" < \"Abcda\"");
1071 doAssert(key2.compareTo(key3) == Collator::EQUAL,
1072 "Result should be \"abcda\" == \"abcda\"");
1073
1074 int32_t keylength = 0;
1075 doAssert(strcmp((const char *)(key2.getByteArray(keylength)),
1076 (const char *)key2compat) == 0,
1077 "Binary format for 'abcda' sortkey different!");
1078
1079 col->getSortKey(test1, sortkey1, 64);
1080 col->getSortKey(test2, sortkey2, 64);
1081 col->getSortKey(test3, sortkey3, 64);
1082
1083 const uint8_t *tempkey = key1.getByteArray(keylength);
1084 doAssert(memcmp(tempkey, sortkey1, keylength) == 0,
1085 "Test1 string should have the same collation key and sort key");
1086 tempkey = key2.getByteArray(keylength);
1087 doAssert(memcmp(tempkey, sortkey2, keylength) == 0,
1088 "Test2 string should have the same collation key and sort key");
1089 tempkey = key3.getByteArray(keylength);
1090 doAssert(memcmp(tempkey, sortkey3, keylength) == 0,
1091 "Test3 string should have the same collation key and sort key");
1092
1093 col->getSortKey(test1, 5, sortkey1, 64);
1094 col->getSortKey(test2, 5, sortkey2, 64);
1095 col->getSortKey(test3, 5, sortkey3, 64);
1096
1097 tempkey = key1.getByteArray(keylength);
1098 doAssert(memcmp(tempkey, sortkey1, keylength) == 0,
1099 "Test1 string should have the same collation key and sort key");
1100 tempkey = key2.getByteArray(keylength);
1101 doAssert(memcmp(tempkey, sortkey2, keylength) == 0,
1102 "Test2 string should have the same collation key and sort key");
1103 tempkey = key3.getByteArray(keylength);
1104 doAssert(memcmp(tempkey, sortkey3, keylength) == 0,
1105 "Test3 string should have the same collation key and sort key");
1106
1107 UnicodeString strtest1(test1);
1108 col->getSortKey(strtest1, sortkey1, 64);
1109 UnicodeString strtest2(test2);
1110 col->getSortKey(strtest2, sortkey2, 64);
1111 UnicodeString strtest3(test3);
1112 col->getSortKey(strtest3, sortkey3, 64);
1113
1114 tempkey = key1.getByteArray(keylength);
1115 doAssert(memcmp(tempkey, sortkey1, keylength) == 0,
1116 "Test1 string should have the same collation key and sort key");
1117 tempkey = key2.getByteArray(keylength);
1118 doAssert(memcmp(tempkey, sortkey2, keylength) == 0,
1119 "Test2 string should have the same collation key and sort key");
1120 tempkey = key3.getByteArray(keylength);
1121 doAssert(memcmp(tempkey, sortkey3, keylength) == 0,
1122 "Test3 string should have the same collation key and sort key");
1123
1124 logln("Use secondary comparision level testing ...\n");
1125 col->setStrength(Collator::SECONDARY);
1126
1127 col->getCollationKey(test1, u_strlen(test1), key1, status);
1128 col->getCollationKey(test2, u_strlen(test2), key2, status);
1129 col->getCollationKey(test3, u_strlen(test3), key3, status);
1130
1131 doAssert(key1.compareTo(key2) == Collator::EQUAL,
1132 "Result should be \"Abcda\" == \"abcda\"");
1133 doAssert(key2.compareTo(key3) == Collator::EQUAL,
1134 "Result should be \"abcda\" == \"abcda\"");
1135
1136 tempkey = key2.getByteArray(keylength);
1137 doAssert(memcmp(tempkey, key2compat, keylength - 1) == 0,
1138 "Binary format for 'abcda' sortkey different!");
1139
1140 col->getSortKey(test1, sortkey1, 64);
1141 col->getSortKey(test2, sortkey2, 64);
1142 col->getSortKey(test3, sortkey3, 64);
1143
1144 tempkey = key1.getByteArray(keylength);
1145 doAssert(memcmp(tempkey, sortkey1, keylength) == 0,
1146 "Test1 string should have the same collation key and sort key");
1147 tempkey = key2.getByteArray(keylength);
1148 doAssert(memcmp(tempkey, sortkey2, keylength) == 0,
1149 "Test2 string should have the same collation key and sort key");
1150 tempkey = key3.getByteArray(keylength);
1151 doAssert(memcmp(tempkey, sortkey3, keylength) == 0,
1152 "Test3 string should have the same collation key and sort key");
1153
1154 col->getSortKey(test1, 5, sortkey1, 64);
1155 col->getSortKey(test2, 5, sortkey2, 64);
1156 col->getSortKey(test3, 5, sortkey3, 64);
1157
1158 tempkey = key1.getByteArray(keylength);
1159 doAssert(memcmp(tempkey, sortkey1, keylength) == 0,
1160 "Test1 string should have the same collation key and sort key");
1161 tempkey = key2.getByteArray(keylength);
1162 doAssert(memcmp(tempkey, sortkey2, keylength) == 0,
1163 "Test2 string should have the same collation key and sort key");
1164 tempkey = key3.getByteArray(keylength);
1165 doAssert(memcmp(tempkey, sortkey3, keylength) == 0,
1166 "Test3 string should have the same collation key and sort key");
1167
1168 col->getSortKey(strtest1, sortkey1, 64);
1169 col->getSortKey(strtest2, sortkey2, 64);
1170 col->getSortKey(strtest3, sortkey3, 64);
1171
1172 tempkey = key1.getByteArray(keylength);
1173 doAssert(memcmp(tempkey, sortkey1, keylength) == 0,
1174 "Test1 string should have the same collation key and sort key");
1175 tempkey = key2.getByteArray(keylength);
1176 doAssert(memcmp(tempkey, sortkey2, keylength) == 0,
1177 "Test2 string should have the same collation key and sort key");
1178 tempkey = key3.getByteArray(keylength);
1179 doAssert(memcmp(tempkey, sortkey3, keylength) == 0,
1180 "Test3 string should have the same collation key and sort key");
1181
1182 logln("testing sortkey ends...");
1183 delete col;
1184}
1185
1186void CollationAPITest::TestMaxExpansion()
1187{
1188 UErrorCode status = U_ZERO_ERROR;
1189 UChar ch = 0;
374ca955 1190 UChar32 unassigned = 0xEFFFD;
b75a7d8f
A
1191 uint32_t sorder = 0;
1192 uint32_t temporder = 0;
1193
1194 UnicodeString rule("&a < ab < c/aba < d < z < ch");
1195 RuleBasedCollator coll(rule, status);
1196 if(U_FAILURE(status)) {
1197 errln("Collator creation failed with error %s", u_errorName(status));
1198 return;
1199 }
1200 UnicodeString str(ch);
1201 CollationElementIterator *iter =
1202 coll.createCollationElementIterator(str);
1203
1204 while (ch < 0xFFFF && U_SUCCESS(status)) {
1205 int count = 1;
1206 uint32_t order;
1207 int32_t size = 0;
1208
1209 ch ++;
1210
1211 str.setCharAt(0, ch);
1212 iter->setText(str, status);
1213 order = iter->previous(status);
1214
1215 /* thai management */
1216 if (order == 0)
1217 order = iter->previous(status);
1218
374ca955 1219 while (U_SUCCESS(status) && iter->previous(status) != UCOL_NULLORDER) {
b75a7d8f
A
1220 count ++;
1221 }
1222
1223 size = coll.getMaxExpansion(order);
1224 if (U_FAILURE(status) || size < count) {
1225 errln("Failure at codepoint %d, maximum expansion count < %d\n",
1226 ch, count);
1227 }
1228 }
1229
1230 /* testing for exact max expansion */
1231 ch = 0;
1232 while (ch < 0x61) {
1233 uint32_t order;
1234 int32_t size;
1235 str.setCharAt(0, ch);
1236 iter->setText(str, status);
1237 order = iter->previous(status);
1238 size = coll.getMaxExpansion(order);
1239 if (U_FAILURE(status) || size != 1) {
1240 errln("Failure at codepoint %d, maximum expansion count < %d\n",
1241 ch, 1);
1242 }
1243 ch ++;
1244 }
1245
1246 ch = 0x63;
1247 str.setTo(ch);
1248 iter->setText(str, status);
1249 temporder = iter->previous(status);
1250
1251 if (U_FAILURE(status) || coll.getMaxExpansion(temporder) != 3) {
1252 errln("Failure at codepoint %d, maximum expansion count != %d\n",
1253 ch, 3);
1254 }
1255
1256 ch = 0x64;
1257 str.setTo(ch);
1258 iter->setText(str, status);
1259 temporder = iter->previous(status);
1260
1261 if (U_FAILURE(status) || coll.getMaxExpansion(temporder) != 1) {
1262 errln("Failure at codepoint %d, maximum expansion count != %d\n",
1263 ch, 3);
1264 }
1265
374ca955 1266 str.setTo(unassigned);
b75a7d8f
A
1267 iter->setText(str, status);
1268 sorder = iter->previous(status);
1269
1270 if (U_FAILURE(status) || coll.getMaxExpansion(sorder) != 2) {
1271 errln("Failure at supplementary codepoints, maximum expansion count < %d\n",
1272 2);
1273 }
1274
1275 /* testing jamo */
1276 ch = 0x1165;
1277 str.setTo(ch);
1278 iter->setText(str, status);
1279 temporder = iter->previous(status);
1280 if (U_FAILURE(status) || coll.getMaxExpansion(temporder) > 3) {
1281 errln("Failure at codepoint %d, maximum expansion count > %d\n",
1282 ch, 3);
1283 }
1284
1285 delete iter;
1286
1287 /* testing special jamo &a<\u1160 */
1288 rule = CharsToUnicodeString("\\u0026\\u0071\\u003c\\u1165\\u002f\\u0071\\u0071\\u0071\\u0071");
1289
1290 RuleBasedCollator jamocoll(rule, status);
1291 iter = jamocoll.createCollationElementIterator(str);
1292 temporder = iter->previous(status);
1293 if (U_FAILURE(status) || iter->getMaxExpansion(temporder) != 6) {
1294 errln("Failure at codepoint %d, maximum expansion count > %d\n",
1295 ch, 5);
1296 }
1297
1298 delete iter;
1299}
1300
1301void CollationAPITest::TestDisplayName()
1302{
1303 UErrorCode error = U_ZERO_ERROR;
1304 Collator *coll = Collator::createInstance("en_US", error);
1305 if (U_FAILURE(error)) {
1306 errln("Failure creating english collator");
1307 return;
1308 }
1309 UnicodeString name;
1310 UnicodeString result;
1311 coll->getDisplayName(Locale::getCanadaFrench(), result);
1312 Locale::getCanadaFrench().getDisplayName(name);
1313 if (result.compare(name)) {
1314 errln("Failure getting the correct name for locale en_US");
1315 }
1316
1317 coll->getDisplayName(Locale::getSimplifiedChinese(), result);
1318 Locale::getSimplifiedChinese().getDisplayName(name);
1319 if (result.compare(name)) {
1320 errln("Failure getting the correct name for locale zh_SG");
1321 }
1322 delete coll;
1323}
1324
1325void CollationAPITest::TestAttribute()
1326{
1327 UErrorCode error = U_ZERO_ERROR;
1328 Collator *coll = Collator::createInstance(error);
1329
1330 if (U_FAILURE(error)) {
1331 errln("Creation of default collator failed");
1332 return;
1333 }
1334
1335 coll->setAttribute(UCOL_FRENCH_COLLATION, UCOL_OFF, error);
1336 if (coll->getAttribute(UCOL_FRENCH_COLLATION, error) != UCOL_OFF ||
1337 U_FAILURE(error)) {
1338 errln("Setting and retrieving of the french collation failed");
1339 }
1340
1341 coll->setAttribute(UCOL_FRENCH_COLLATION, UCOL_ON, error);
1342 if (coll->getAttribute(UCOL_FRENCH_COLLATION, error) != UCOL_ON ||
1343 U_FAILURE(error)) {
1344 errln("Setting and retrieving of the french collation failed");
1345 }
1346
1347 coll->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, error);
1348 if (coll->getAttribute(UCOL_ALTERNATE_HANDLING, error) != UCOL_SHIFTED ||
1349 U_FAILURE(error)) {
1350 errln("Setting and retrieving of the alternate handling failed");
1351 }
1352
1353 coll->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, error);
1354 if (coll->getAttribute(UCOL_ALTERNATE_HANDLING, error) != UCOL_NON_IGNORABLE ||
1355 U_FAILURE(error)) {
1356 errln("Setting and retrieving of the alternate handling failed");
1357 }
1358
1359 coll->setAttribute(UCOL_CASE_FIRST, UCOL_LOWER_FIRST, error);
1360 if (coll->getAttribute(UCOL_CASE_FIRST, error) != UCOL_LOWER_FIRST ||
1361 U_FAILURE(error)) {
1362 errln("Setting and retrieving of the case first attribute failed");
1363 }
1364
1365 coll->setAttribute(UCOL_CASE_FIRST, UCOL_UPPER_FIRST, error);
1366 if (coll->getAttribute(UCOL_CASE_FIRST, error) != UCOL_UPPER_FIRST ||
1367 U_FAILURE(error)) {
1368 errln("Setting and retrieving of the case first attribute failed");
1369 }
1370
1371 coll->setAttribute(UCOL_CASE_LEVEL, UCOL_ON, error);
1372 if (coll->getAttribute(UCOL_CASE_LEVEL, error) != UCOL_ON ||
1373 U_FAILURE(error)) {
1374 errln("Setting and retrieving of the case level attribute failed");
1375 }
1376
1377 coll->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, error);
1378 if (coll->getAttribute(UCOL_CASE_LEVEL, error) != UCOL_OFF ||
1379 U_FAILURE(error)) {
1380 errln("Setting and retrieving of the case level attribute failed");
1381 }
1382
1383 coll->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, error);
1384 if (coll->getAttribute(UCOL_NORMALIZATION_MODE, error) != UCOL_ON ||
1385 U_FAILURE(error)) {
1386 errln("Setting and retrieving of the normalization on/off attribute failed");
1387 }
1388
1389 coll->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_OFF, error);
1390 if (coll->getAttribute(UCOL_NORMALIZATION_MODE, error) != UCOL_OFF ||
1391 U_FAILURE(error)) {
1392 errln("Setting and retrieving of the normalization on/off attribute failed");
1393 }
1394
1395 coll->setAttribute(UCOL_STRENGTH, UCOL_PRIMARY, error);
1396 if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_PRIMARY ||
1397 U_FAILURE(error)) {
1398 errln("Setting and retrieving of the collation strength failed");
1399 }
1400
1401 coll->setAttribute(UCOL_STRENGTH, UCOL_SECONDARY, error);
1402 if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_SECONDARY ||
1403 U_FAILURE(error)) {
1404 errln("Setting and retrieving of the collation strength failed");
1405 }
1406
1407 coll->setAttribute(UCOL_STRENGTH, UCOL_TERTIARY, error);
1408 if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_TERTIARY ||
1409 U_FAILURE(error)) {
1410 errln("Setting and retrieving of the collation strength failed");
1411 }
1412
1413 coll->setAttribute(UCOL_STRENGTH, UCOL_QUATERNARY, error);
1414 if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_QUATERNARY ||
1415 U_FAILURE(error)) {
1416 errln("Setting and retrieving of the collation strength failed");
1417 }
1418
1419 coll->setAttribute(UCOL_STRENGTH, UCOL_IDENTICAL, error);
1420 if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_IDENTICAL ||
1421 U_FAILURE(error)) {
1422 errln("Setting and retrieving of the collation strength failed");
1423 }
1424
1425 delete coll;
1426}
1427
1428void CollationAPITest::TestVariableTopSetting() {
1429 UErrorCode status = U_ZERO_ERROR;
1430
1431 UChar vt[256] = { 0 };
1432
1433 Collator *coll = Collator::createInstance(status);
1434 if(U_FAILURE(status)) {
1435 delete coll;
1436 errln("Collator creation failed with error %s", u_errorName(status));
1437 return;
1438 }
1439
1440 uint32_t oldVarTop = coll->getVariableTop(status);
1441
1442 vt[0] = 0x0041;
1443
1444 uint32_t newVarTop = coll->setVariableTop(vt, 1, status);
1445
1446 if((newVarTop & 0xFFFF0000) != (coll->getVariableTop(status) & 0xFFFF0000)) {
1447 errln("Didn't set vartop properly\n");
1448 }
1449
1450 coll->setVariableTop(oldVarTop, status);
1451
1452 uint32_t newerVarTop = coll->setVariableTop(UnicodeString(vt, 1), status);
1453
1454 if((newVarTop & 0xFFFF0000) != (newerVarTop & 0xFFFF0000)) {
1455 errln("Didn't set vartop properly from UnicodeString!\n");
1456 }
1457
1458 delete coll;
1459
1460}
1461
1462void CollationAPITest::TestGetLocale() {
1463 UErrorCode status = U_ZERO_ERROR;
1464 const char *rules = "&a<x<y<z";
1465 UChar rlz[256] = {0};
1466
1467 Collator *coll = NULL;
1468 Locale locale;
1469
1470 int32_t i = 0;
1471
1472 static const struct {
1473 const char* requestedLocale;
1474 const char* validLocale;
1475 const char* actualLocale;
1476 } testStruct[] = {
1477 { "sr_YU", "sr_YU", "root" },
1478 { "sh_YU", "sh_YU", "sh" },
1479 { "en_US_CALIFORNIA", "en_US", "root" },
1480 { "fr_FR_NONEXISTANT", "fr_FR", "fr" }
1481 };
1482
1483 u_unescape(rules, rlz, 256);
1484
1485 /* test opening collators for different locales */
1486 for(i = 0; i<(int32_t)(sizeof(testStruct)/sizeof(testStruct[0])); i++) {
1487 status = U_ZERO_ERROR;
1488 coll = Collator::createInstance(testStruct[i].requestedLocale, status);
1489 if(U_FAILURE(status)) {
1490 log("Failed to open collator for %s with %s\n", testStruct[i].requestedLocale, u_errorName(status));
1491 delete coll;
1492 continue;
1493 }
1494 locale = coll->getLocale(ULOC_REQUESTED_LOCALE, status);
1495 if(locale != testStruct[i].requestedLocale) {
1496 log("[Coll %s]: Error in requested locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].requestedLocale, locale.getName());
1497 }
1498 locale = coll->getLocale(ULOC_VALID_LOCALE, status);
1499 if(locale != testStruct[i].validLocale) {
1500 log("[Coll %s]: Error in valid locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].validLocale, locale.getName());
1501 }
1502 locale = coll->getLocale(ULOC_ACTUAL_LOCALE, status);
1503 if(locale != testStruct[i].actualLocale) {
1504 log("[Coll %s]: Error in actual locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].actualLocale, locale.getName());
1505 }
1506 delete coll;
1507 }
1508
1509 /* completely non-existant locale for collator should get a default collator */
1510 {
374ca955 1511 Collator *defaultColl = Collator::createInstance((const Locale)NULL, status);
b75a7d8f
A
1512 coll = Collator::createInstance("blahaha", status);
1513 if(U_FAILURE(status)) {
1514 log("Failed to open collator with %s\n", u_errorName(status));
1515 delete coll;
1516 delete defaultColl;
1517 return;
1518 }
1519 if(coll->getLocale(ULOC_REQUESTED_LOCALE, status) != "blahaha") {
1520 log("Nonexisting locale didn't preserve the requested locale\n");
1521 }
1522 if(coll->getLocale(ULOC_VALID_LOCALE, status) !=
1523 defaultColl->getLocale(ULOC_VALID_LOCALE, status)) {
1524 log("Valid locale for nonexisting locale locale collator differs "
1525 "from valid locale for default collator\n");
1526 }
1527 if(coll->getLocale(ULOC_ACTUAL_LOCALE, status) !=
1528 defaultColl->getLocale(ULOC_ACTUAL_LOCALE, status)) {
1529 log("Actual locale for nonexisting locale locale collator differs "
1530 "from actual locale for default collator\n");
1531 }
1532 delete coll;
1533 delete defaultColl;
1534 }
1535
1536
1537
1538 /* collator instantiated from rules should have all three locales NULL */
1539 coll = new RuleBasedCollator(rlz, status);
1540 locale = coll->getLocale(ULOC_REQUESTED_LOCALE, status);
1541 if(!locale.isBogus()) {
1542 log("For collator instantiated from rules, requested locale %s is not bogus\n", locale.getName());
1543 }
1544 locale = coll->getLocale(ULOC_VALID_LOCALE, status);
1545 if(!locale.isBogus()) {
1546 log("For collator instantiated from rules, valid locale %s is not bogus\n", locale.getName());
1547 }
1548 locale = coll->getLocale(ULOC_ACTUAL_LOCALE, status);
1549 if(!locale.isBogus()) {
1550 log("For collator instantiated from rules, actual locale %s is not bogus\n", locale.getName());
1551 }
1552 delete coll;
1553}
1554
1555struct teststruct {
1556 const char *original;
1557 uint8_t key[256];
1558};
1559
1560
1561
1562U_CDECL_BEGIN
1563static int U_CALLCONV
1564compare_teststruct(const void *string1, const void *string2) {
1565 return(strcmp((const char *)((struct teststruct *)string1)->key, (const char *)((struct teststruct *)string2)->key));
1566}
1567U_CDECL_END
1568
1569
1570void CollationAPITest::TestBounds(void) {
1571 UErrorCode status = U_ZERO_ERROR;
1572
1573 Collator *coll = Collator::createInstance(Locale("sh"), status);
1574 if(U_FAILURE(status)) {
1575 delete coll;
1576 errln("Collator creation failed with %s", u_errorName(status));
1577 return;
1578 }
1579
1580 uint8_t sortkey[512], lower[512], upper[512];
1581 UChar buffer[512];
1582
1583 const char *test[] = {
1584 "John Smith",
1585 "JOHN SMITH",
1586 "john SMITH",
1587 "j\\u00F6hn sm\\u00EFth",
1588 "J\\u00F6hn Sm\\u00EFth",
1589 "J\\u00D6HN SM\\u00CFTH",
1590 "john smithsonian",
1591 "John Smithsonian"
1592 };
1593
1594 static struct teststruct tests[] = {
1595 {"\\u010CAKI MIHALJ", {0}},
1596 {"\\u010CAKI MIHALJ", {0}},
1597 {"\\u010CAKI PIRO\\u0160KA", {0}},
1598 {"\\u010CABAI ANDRIJA", {0}},
1599 {"\\u010CABAI LAJO\\u0160", {0}},
1600 {"\\u010CABAI MARIJA", {0}},
1601 {"\\u010CABAI STEVAN", {0}},
1602 {"\\u010CABAI STEVAN", {0}},
1603 {"\\u010CABARKAPA BRANKO", {0}},
1604 {"\\u010CABARKAPA MILENKO", {0}},
1605 {"\\u010CABARKAPA MIROSLAV", {0}},
1606 {"\\u010CABARKAPA SIMO", {0}},
1607 {"\\u010CABARKAPA STANKO", {0}},
1608 {"\\u010CABARKAPA TAMARA", {0}},
1609 {"\\u010CABARKAPA TOMA\\u0160", {0}},
1610 {"\\u010CABDARI\\u0106 NIKOLA", {0}},
1611 {"\\u010CABDARI\\u0106 ZORICA", {0}},
1612 {"\\u010CABI NANDOR", {0}},
1613 {"\\u010CABOVI\\u0106 MILAN", {0}},
1614 {"\\u010CABRADI AGNEZIJA", {0}},
1615 {"\\u010CABRADI IVAN", {0}},
1616 {"\\u010CABRADI JELENA", {0}},
1617 {"\\u010CABRADI LJUBICA", {0}},
1618 {"\\u010CABRADI STEVAN", {0}},
1619 {"\\u010CABRDA MARTIN", {0}},
1620 {"\\u010CABRILO BOGDAN", {0}},
1621 {"\\u010CABRILO BRANISLAV", {0}},
1622 {"\\u010CABRILO LAZAR", {0}},
1623 {"\\u010CABRILO LJUBICA", {0}},
1624 {"\\u010CABRILO SPASOJA", {0}},
1625 {"\\u010CADE\\u0160 ZDENKA", {0}},
1626 {"\\u010CADESKI BLAGOJE", {0}},
1627 {"\\u010CADOVSKI VLADIMIR", {0}},
1628 {"\\u010CAGLJEVI\\u0106 TOMA", {0}},
1629 {"\\u010CAGOROVI\\u0106 VLADIMIR", {0}},
1630 {"\\u010CAJA VANKA", {0}},
1631 {"\\u010CAJI\\u0106 BOGOLJUB", {0}},
1632 {"\\u010CAJI\\u0106 BORISLAV", {0}},
1633 {"\\u010CAJI\\u0106 RADOSLAV", {0}},
1634 {"\\u010CAK\\u0160IRAN MILADIN", {0}},
1635 {"\\u010CAKAN EUGEN", {0}},
1636 {"\\u010CAKAN EVGENIJE", {0}},
1637 {"\\u010CAKAN IVAN", {0}},
1638 {"\\u010CAKAN JULIJAN", {0}},
1639 {"\\u010CAKAN MIHAJLO", {0}},
1640 {"\\u010CAKAN STEVAN", {0}},
1641 {"\\u010CAKAN VLADIMIR", {0}},
1642 {"\\u010CAKAN VLADIMIR", {0}},
1643 {"\\u010CAKAN VLADIMIR", {0}},
1644 {"\\u010CAKARA ANA", {0}},
1645 {"\\u010CAKAREVI\\u0106 MOMIR", {0}},
1646 {"\\u010CAKAREVI\\u0106 NEDELJKO", {0}},
1647 {"\\u010CAKI \\u0160ANDOR", {0}},
1648 {"\\u010CAKI AMALIJA", {0}},
1649 {"\\u010CAKI ANDRA\\u0160", {0}},
1650 {"\\u010CAKI LADISLAV", {0}},
1651 {"\\u010CAKI LAJO\\u0160", {0}},
1652 {"\\u010CAKI LASLO", {0}}
1653 };
1654
1655
1656
1657 int32_t i = 0, j = 0, k = 0, buffSize = 0, skSize = 0, lowerSize = 0, upperSize = 0;
1658 int32_t arraySize = sizeof(tests)/sizeof(tests[0]);
1659
1660 for(i = 0; i<arraySize; i++) {
1661 buffSize = u_unescape(tests[i].original, buffer, 512);
1662 skSize = coll->getSortKey(buffer, buffSize, tests[i].key, 512);
1663 }
1664
1665 qsort(tests, arraySize, sizeof(struct teststruct), compare_teststruct);
1666
1667 for(i = 0; i < arraySize-1; i++) {
1668 for(j = i+1; j < arraySize; j++) {
1669 lowerSize = coll->getBound(tests[i].key, -1, UCOL_BOUND_LOWER, 1, lower, 512, status);
1670 upperSize = coll->getBound(tests[j].key, -1, UCOL_BOUND_UPPER, 1, upper, 512, status);
1671 for(k = i; k <= j; k++) {
1672 if(strcmp((const char *)lower, (const char *)tests[k].key) > 0) {
1673 errln("Problem with lower! j = %i (%s vs %s)", k, tests[k].original, tests[i].original);
1674 }
1675 if(strcmp((const char *)upper, (const char *)tests[k].key) <= 0) {
1676 errln("Problem with upper! j = %i (%s vs %s)", k, tests[k].original, tests[j].original);
1677 }
1678 }
1679 }
1680 }
1681
1682
1683 for(i = 0; i<(int32_t)(sizeof(test)/sizeof(test[0])); i++) {
1684 buffSize = u_unescape(test[i], buffer, 512);
1685 skSize = coll->getSortKey(buffer, buffSize, sortkey, 512);
1686 lowerSize = ucol_getBound(sortkey, skSize, UCOL_BOUND_LOWER, 1, lower, 512, &status);
1687 upperSize = ucol_getBound(sortkey, skSize, UCOL_BOUND_UPPER_LONG, 1, upper, 512, &status);
1688 for(j = i+1; j<(int32_t)(sizeof(test)/sizeof(test[0])); j++) {
1689 buffSize = u_unescape(test[j], buffer, 512);
1690 skSize = coll->getSortKey(buffer, buffSize, sortkey, 512);
1691 if(strcmp((const char *)lower, (const char *)sortkey) > 0) {
1692 errln("Problem with lower! i = %i, j = %i (%s vs %s)", i, j, test[i], test[j]);
1693 }
1694 if(strcmp((const char *)upper, (const char *)sortkey) <= 0) {
1695 errln("Problem with upper! i = %i, j = %i (%s vs %s)", i, j, test[i], test[j]);
1696 }
1697 }
1698 }
1699 delete coll;
1700}
1701
1702
1703void CollationAPITest::TestGetTailoredSet()
1704{
1705 struct {
1706 const char *rules;
1707 const char *tests[20];
1708 int32_t testsize;
1709 } setTest[] = {
1710 { "&a < \\u212b", { "\\u212b", "A\\u030a", "\\u00c5" }, 3},
1711 { "& S < \\u0161 <<< \\u0160", { "\\u0161", "s\\u030C", "\\u0160", "S\\u030C" }, 4}
1712 };
1713
1714 uint32_t i = 0, j = 0;
1715 UErrorCode status = U_ZERO_ERROR;
1716
1717 RuleBasedCollator *coll = NULL;
1718 UnicodeString buff;
1719 UnicodeSet *set = NULL;
1720
1721 for(i = 0; i < sizeof(setTest)/sizeof(setTest[0]); i++) {
1722 buff = UnicodeString(setTest[i].rules, "").unescape();
1723 coll = new RuleBasedCollator(buff, status);
1724 if(U_SUCCESS(status)) {
1725 set = coll->getTailoredSet(status);
1726 if(set->size() != setTest[i].testsize) {
1727 errln("Tailored set size different (%d) than expected (%d)", set->size(), setTest[i].testsize);
1728 }
1729 for(j = 0; j < (uint32_t)setTest[i].testsize; j++) {
1730 buff = UnicodeString(setTest[i].tests[j], "").unescape();
1731 if(!set->contains(buff)) {
1732 errln("Tailored set doesn't contain %s... It should", setTest[i].tests[j]);
1733 }
1734 }
1735 delete set;
1736 } else {
1737 errln("Couldn't open collator with rules %s\n", setTest[i].rules);
1738 }
1739 delete coll;
1740 }
1741}
1742
1743void CollationAPITest::TestUClassID()
1744{
1745 char id = *((char *)RuleBasedCollator::getStaticClassID());
1746 if (id != 0) {
1747 errln("Static class id for RuleBasedCollator should be 0");
1748 }
1749 UErrorCode status = U_ZERO_ERROR;
1750 RuleBasedCollator *coll
1751 = (RuleBasedCollator *)Collator::createInstance(status);
1752 if(U_FAILURE(status)) {
1753 delete coll;
1754 errln("Collator creation failed with %s", u_errorName(status));
1755 return;
1756 }
1757 id = *((char *)coll->getDynamicClassID());
1758 if (id != 0) {
1759 errln("Dynamic class id for RuleBasedCollator should be 0");
1760 }
1761 id = *((char *)CollationKey::getStaticClassID());
1762 if (id != 0) {
1763 errln("Static class id for CollationKey should be 0");
1764 }
1765 CollationKey *key = new CollationKey();
1766 id = *((char *)key->getDynamicClassID());
1767 if (id != 0) {
1768 errln("Dynamic class id for CollationKey should be 0");
1769 }
1770 id = *((char *)CollationElementIterator::getStaticClassID());
1771 if (id != 0) {
1772 errln("Static class id for CollationElementIterator should be 0");
1773 }
1774 UnicodeString str("testing");
1775 CollationElementIterator *iter = coll->createCollationElementIterator(str);
1776 id = *((char *)iter->getDynamicClassID());
1777 if (id != 0) {
1778 errln("Dynamic class id for CollationElementIterator should be 0");
1779 }
1780 delete key;
1781 delete iter;
1782 delete coll;
1783}
1784
1785class TestCollator : public Collator
1786{
1787public:
1788 virtual Collator* clone(void) const;
1789
1790 // dang, markus says we can't use 'using' in ICU. I hate doing this for
1791 // deprecated methods...
1792
1793 // using Collator::compare;
1794
1795 virtual EComparisonResult compare(const UnicodeString& source,
1796 const UnicodeString& target) const
1797 {
1798 return Collator::compare(source, target);
1799 }
1800
1801 virtual EComparisonResult compare(const UnicodeString& source,
1802 const UnicodeString& target,
1803 int32_t length) const
1804 {
1805 return Collator::compare(source, target, length);
1806 }
1807
1808 virtual EComparisonResult compare(const UChar* source,
1809 int32_t sourceLength,
1810 const UChar* target,
1811 int32_t targetLength) const
1812 {
1813 return Collator::compare(source, sourceLength, target, targetLength);
1814 }
1815
1816
1817 virtual UCollationResult compare(const UnicodeString& source,
1818 const UnicodeString& target,
1819 UErrorCode& status) const;
1820 virtual UCollationResult compare(const UnicodeString& source,
1821 const UnicodeString& target,
1822 int32_t length,
1823 UErrorCode& status) const;
1824 virtual UCollationResult compare(const UChar* source,
1825 int32_t sourceLength,
1826 const UChar* target,
1827 int32_t targetLength,
1828 UErrorCode& status) const;
1829 virtual CollationKey& getCollationKey(const UnicodeString& source,
1830 CollationKey& key,
1831 UErrorCode& status) const;
1832 virtual CollationKey& getCollationKey(const UChar*source,
1833 int32_t sourceLength,
1834 CollationKey& key,
1835 UErrorCode& status) const;
1836 virtual int32_t hashCode(void) const;
1837 virtual const Locale getLocale(ULocDataLocaleType type,
1838 UErrorCode& status) const;
1839 virtual ECollationStrength getStrength(void) const;
1840 virtual void setStrength(ECollationStrength newStrength);
1841 virtual UClassID getDynamicClassID(void) const;
1842 virtual void getVersion(UVersionInfo info) const;
1843 virtual void setAttribute(UColAttribute attr, UColAttributeValue value,
1844 UErrorCode &status);
1845 virtual UColAttributeValue getAttribute(UColAttribute attr,
1846 UErrorCode &status);
1847 virtual uint32_t setVariableTop(const UChar *varTop, int32_t len,
1848 UErrorCode &status);
1849 virtual uint32_t setVariableTop(const UnicodeString varTop,
1850 UErrorCode &status);
1851 virtual void setVariableTop(const uint32_t varTop, UErrorCode &status);
1852 virtual uint32_t getVariableTop(UErrorCode &status) const;
1853 virtual Collator* safeClone(void);
1854 virtual int32_t getSortKey(const UnicodeString& source,
1855 uint8_t* result,
1856 int32_t resultLength) const;
1857 virtual int32_t getSortKey(const UChar*source, int32_t sourceLength,
1858 uint8_t*result, int32_t resultLength) const;
1859 virtual UnicodeSet *getTailoredSet(UErrorCode &status) const;
1860 virtual UBool operator!=(const Collator& other) const;
1861 virtual void setLocales(const Locale& requestedLocale, const Locale& validLocale);
1862 TestCollator() : Collator() {};
1863 TestCollator(UCollationStrength collationStrength,
1864 UNormalizationMode decompositionMode) : Collator(collationStrength, decompositionMode) {};
1865};
1866
1867inline UBool TestCollator::operator!=(const Collator& other) const {
1868 return Collator::operator!=(other);
1869}
1870
1871#define returnEComparisonResult(data) \
1872 if (data < 0) return Collator::LESS;\
1873 if (data > 0) return Collator::GREATER;\
1874 return Collator::EQUAL;
1875
1876Collator* TestCollator::clone() const
1877{
1878 return new TestCollator();
1879}
1880
1881UCollationResult TestCollator::compare(const UnicodeString& source,
1882 const UnicodeString& target,
1883 UErrorCode& status) const
1884{
1885 if(U_SUCCESS(status)) {
1886 return UCollationResult(source.compare(target));
1887 } else {
1888 return UCOL_EQUAL;
1889 }
1890}
1891
1892UCollationResult TestCollator::compare(const UnicodeString& source,
1893 const UnicodeString& target,
1894 int32_t length,
1895 UErrorCode& status) const
1896{
1897 if(U_SUCCESS(status)) {
1898 return UCollationResult(source.compare(0, length, target));
1899 } else {
1900 return UCOL_EQUAL;
1901 }
1902}
1903
1904UCollationResult TestCollator::compare(const UChar* source,
1905 int32_t sourceLength,
1906 const UChar* target,
1907 int32_t targetLength,
1908 UErrorCode& status) const
1909{
1910 UnicodeString s(source, sourceLength);
1911 UnicodeString t(target, targetLength);
1912 return compare(s, t, status);
1913}
1914
1915CollationKey& TestCollator::getCollationKey(const UnicodeString& source,
1916 CollationKey& key,
1917 UErrorCode& status) const
1918{
1919 char temp[100];
1920 int length = 100;
1921 length = source.extract(temp, length, NULL, status);
1922 temp[length] = 0;
1923 CollationKey tempkey((uint8_t*)temp, length);
1924 key = tempkey;
1925 return key;
1926}
1927
1928CollationKey& TestCollator::getCollationKey(const UChar*source,
1929 int32_t sourceLength,
1930 CollationKey& key,
1931 UErrorCode& status) const
1932{
1933 //s tack allocation used since collationkey does not keep the unicodestring
1934 UnicodeString str(source, sourceLength);
1935 return getCollationKey(str, key, status);
1936}
1937
1938int32_t TestCollator::getSortKey(const UnicodeString& source, uint8_t* result,
1939 int32_t resultLength) const
1940{
1941 UErrorCode status = U_ZERO_ERROR;
1942 int32_t length = source.extract((char *)result, resultLength, NULL,
1943 status);
1944 result[length] = 0;
1945 return length;
1946}
1947
1948int32_t TestCollator::getSortKey(const UChar*source, int32_t sourceLength,
1949 uint8_t*result, int32_t resultLength) const
1950{
1951 UnicodeString str(source, sourceLength);
1952 return getSortKey(str, result, resultLength);
1953}
1954
1955int32_t TestCollator::hashCode() const
1956{
1957 return 0;
1958}
1959
1960const Locale TestCollator::getLocale(ULocDataLocaleType type,
1961 UErrorCode& status) const
1962{
1963 // api not used, this is to make the compiler happy
1964 if (U_FAILURE(status)) {
1965 type = ULOC_DATA_LOCALE_TYPE_LIMIT;
1966 }
1967 return NULL;
1968}
1969
1970Collator::ECollationStrength TestCollator::getStrength() const
1971{
1972 return TERTIARY;
1973}
1974
1975void TestCollator::setStrength(Collator::ECollationStrength newStrength)
1976{
1977 // api not used, this is to make the compiler happy
1978 newStrength = TERTIARY;
1979}
1980
1981UClassID TestCollator::getDynamicClassID(void) const
1982{
1983 return 0;
1984}
1985
1986void TestCollator::getVersion(UVersionInfo info) const
1987{
1988 // api not used, this is to make the compiler happy
1989 memset(info, 0, U_MAX_VERSION_LENGTH);
1990}
1991
1992void TestCollator::setAttribute(UColAttribute attr, UColAttributeValue value,
1993 UErrorCode &status)
1994{
1995 // api not used, this is to make the compiler happy
1996 if (U_FAILURE(status)) {
1997 attr = UCOL_ATTRIBUTE_COUNT;
1998 value = UCOL_OFF;
1999 }
2000}
2001
2002UColAttributeValue TestCollator::getAttribute(UColAttribute attr,
2003 UErrorCode &status)
2004{
2005 // api not used, this is to make the compiler happy
2006 if (U_FAILURE(status) || attr == UCOL_ATTRIBUTE_COUNT) {
2007 return UCOL_OFF;
2008 }
2009 return UCOL_DEFAULT;
2010}
2011
2012uint32_t TestCollator::setVariableTop(const UChar *varTop, int32_t len,
2013 UErrorCode &status)
2014{
2015 // api not used, this is to make the compiler happy
2016 if (U_SUCCESS(status) && (varTop == 0 || len < -1)) {
2017 status = U_ILLEGAL_ARGUMENT_ERROR;
2018 }
2019 return 0;
2020}
2021
2022uint32_t TestCollator::setVariableTop(const UnicodeString varTop,
2023 UErrorCode &status)
2024{
2025 // api not used, this is to make the compiler happy
2026 if (U_SUCCESS(status) && varTop.length() == 0) {
2027 status = U_ILLEGAL_ARGUMENT_ERROR;
2028 }
2029 return 0;
2030}
2031
2032void TestCollator::setVariableTop(const uint32_t varTop, UErrorCode &status)
2033{
2034 // api not used, this is to make the compiler happy
2035 if (U_SUCCESS(status) && varTop == 0) {
2036 status = U_ILLEGAL_ARGUMENT_ERROR;
2037 }
2038}
2039
2040uint32_t TestCollator::getVariableTop(UErrorCode &status) const
2041{
2042
2043 // api not used, this is to make the compiler happy
2044 if (U_SUCCESS(status)) {
2045 return 0;
2046 }
2047 return (uint32_t)(0xFFFFFFFFu);
2048}
2049
2050Collator* TestCollator::safeClone(void)
2051{
2052 return new TestCollator();
2053}
2054
2055UnicodeSet * TestCollator::getTailoredSet(UErrorCode &status) const
2056{
2057 return Collator::getTailoredSet(status);
2058}
2059
2060void TestCollator::setLocales(const Locale& requestedLocale, const Locale& validLocale)
2061{
2062 Collator::setLocales(requestedLocale, validLocale);
2063}
2064
2065
2066void CollationAPITest::TestSubclass()
2067{
2068 TestCollator col1;
2069 TestCollator col2;
2070 doAssert(col1 != col2, "2 instance of TestCollator should be different");
2071 if (col1.hashCode() != col2.hashCode()) {
2072 errln("Every TestCollator has the same hashcode");
2073 }
2074 UnicodeString abc("abc", 3);
2075 UnicodeString bcd("bcd", 3);
2076 if (col1.compare(abc, bcd) != abc.compare(bcd)) {
2077 errln("TestCollator compare should be the same as the default "
2078 "string comparison");
2079 }
2080 CollationKey key;
2081 UErrorCode status = U_ZERO_ERROR;
2082 col1.getCollationKey(abc, key, status);
2083 int32_t length = 0;
2084 const char* bytes = (const char *)key.getByteArray(length);
2085 UnicodeString keyarray(bytes, length, NULL, status);
2086 if (abc != keyarray) {
2087 errln("TestCollator collationkey API is returning wrong values");
2088 }
2089
2090 UnicodeSet expectedset(0, 0x10FFFF);
2091 UnicodeSet *defaultset = col1.getTailoredSet(status);
2092 if (!defaultset->containsAll(expectedset)
2093 || !expectedset.containsAll(*defaultset)) {
2094 errln("Error: expected default tailoring to be 0 to 0x10ffff");
2095 }
2096 delete defaultset;
2097
2098 // use base class implementation
2099 Locale loc1 = Locale::getGermany();
2100 Locale loc2 = Locale::getFrance();
2101 col1.setLocales(loc1, loc2); // default implementation has no effect
2102
2103 UnicodeString displayName;
2104 col1.getDisplayName(loc1, loc2, displayName); // de_DE collator in fr_FR locale
2105
2106 TestCollator col3(UCOL_TERTIARY, UNORM_NONE);
2107 UnicodeString a("a");
2108 UnicodeString b("b");
2109 Collator::EComparisonResult result = Collator::EComparisonResult(a.compare(b));
2110 if(col1.compare(a, b) != result) {
2111 errln("Collator doesn't give default result");
2112 }
2113 if(col1.compare(a, b, 1) != result) {
2114 errln("Collator doesn't give default result");
2115 }
2116 if(col1.compare(a.getBuffer(), a.length(), b.getBuffer(), b.length()) != result) {
2117 errln("Collator doesn't give default result");
2118 }
2119}
2120
374ca955
A
2121void CollationAPITest::TestNULLCharTailoring()
2122{
2123 UErrorCode status = U_ZERO_ERROR;
2124 UChar buf[256] = {0};
2125 int32_t len = u_unescape("&a < '\\u0000'", buf, 256);
2126 UnicodeString first((UChar)0x0061);
2127 UnicodeString second((UChar)0);
2128 RuleBasedCollator *coll = new RuleBasedCollator(UnicodeString(buf, len), status);
2129 if(U_FAILURE(status)) {
2130 errln("Failed to open collator");
2131 }
2132 UCollationResult res = coll->compare(first, second, status);
2133 if(res != UCOL_LESS) {
2134 errln("a should be less then NULL after tailoring");
2135 }
2136 delete coll;
2137}
2138
b75a7d8f
A
2139void CollationAPITest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par */)
2140{
2141 if (exec) logln("TestSuite CollationAPITest: ");
2142 switch (index) {
2143 case 0: name = "TestProperty"; if (exec) TestProperty(/* par */); break;
2144 case 1: name = "TestOperators"; if (exec) TestOperators(/* par */); break;
2145 case 2: name = "TestDuplicate"; if (exec) TestDuplicate(/* par */); break;
2146 case 3: name = "TestCompare"; if (exec) TestCompare(/* par */); break;
2147 case 4: name = "TestHashCode"; if (exec) TestHashCode(/* par */); break;
2148 case 5: name = "TestCollationKey"; if (exec) TestCollationKey(/* par */); break;
2149 case 6: name = "TestElemIter"; if (exec) TestElemIter(/* par */); break;
2150 case 7: name = "TestGetAll"; if (exec) TestGetAll(/* par */); break;
2151 case 8: name = "TestRuleBasedColl"; if (exec) TestRuleBasedColl(/* par */); break;
2152 case 9: name = "TestDecomposition"; if (exec) TestDecomposition(/* par */); break;
2153 case 10: name = "TestSafeClone"; if (exec) TestSafeClone(/* par */); break;
2154 case 11: name = "TestSortKey"; if (exec) TestSortKey(); break;
2155 case 12: name = "TestMaxExpansion"; if (exec) TestMaxExpansion(); break;
2156 case 13: name = "TestDisplayName"; if (exec) TestDisplayName(); break;
2157 case 14: name = "TestAttribute"; if (exec) TestAttribute(); break;
2158 case 15: name = "TestVariableTopSetting"; if (exec) TestVariableTopSetting(); break;
2159 case 16: name = "TestRules"; if (exec) TestRules(); break;
2160 case 17: name = "TestGetLocale"; if (exec) TestGetLocale(); break;
2161 case 18: name = "TestBounds"; if (exec) TestBounds(); break;
2162 case 19: name = "TestGetTailoredSet"; if (exec) TestGetTailoredSet(); break;
2163 case 20: name = "TestUClassID"; if (exec) TestUClassID(); break;
2164 case 21: name = "TestSubclass"; if (exec) TestSubclass(); break;
374ca955 2165 case 22: name = "TestNULLCharTailoring"; if (exec) TestNULLCharTailoring(); break;
b75a7d8f
A
2166 default: name = ""; break;
2167 }
2168}
2169
2170#endif /* #if !UCONFIG_NO_COLLATION */