1 /********************************************************************
3 * Copyright (c) 1997-2005, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
8 * IntlTestCollator is the medium level test class for everything in the directory "collate".
11 /***********************************************************************
12 * Modification history
13 * Date Name Description
14 * 02/14/2001 synwee Compare with cintltst and commented away tests
16 ***********************************************************************/
18 #include "unicode/utypes.h"
20 #if !UCONFIG_NO_COLLATION
22 #include "unicode/uchar.h"
23 #include "unicode/ustring.h"
52 #define TESTCLASS(n,classname) \
56 logln(#classname "---"); \
63 void IntlTestCollator::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par
)
66 logln("TestSuite Collator: ");
70 TESTCLASS(0, CollationEnglishTest
);
71 TESTCLASS(1, CollationFrenchTest
);
72 TESTCLASS(2, CollationGermanTest
);
73 TESTCLASS(3, CollationSpanishTest
);
74 TESTCLASS(4, CollationKanaTest
);
75 TESTCLASS(5, CollationTurkishTest
);
76 TESTCLASS(6, CollationDummyTest
);
77 TESTCLASS(7, G7CollationTest
);
78 TESTCLASS(8, CollationMonkeyTest
);
79 TESTCLASS(9, CollationAPITest
);
80 TESTCLASS(10, CollationRegressionTest
);
81 TESTCLASS(11, CollationCurrencyTest
);
82 TESTCLASS(12, CollationIteratorTest
);
83 TESTCLASS(13, CollationThaiTest
);
84 TESTCLASS(14, LotusCollationKoreanTest
);
85 TESTCLASS(15, StringSearchTest
);
86 TESTCLASS(16, ContractionTableTest
);
87 TESTCLASS(17, DataDrivenCollatorTest
);
88 TESTCLASS(18, UCAConformanceTest
);
89 TESTCLASS(19, CollationServiceTest
);
90 TESTCLASS(20, CollationFinnishTest
); // removed by weiv - we have changed Finnish collation
91 TESTCLASS(21, RandomCollatorTest
);
93 default: name
= ""; break;
98 IntlTestCollator::compareUsingPartials(UCollator
*coll
, const UChar source
[], int32_t sLen
, const UChar target
[], int32_t tLen
, int32_t pieceSize
, UErrorCode
&status
) {
99 int32_t partialSKResult
= 0;
100 uint8_t sBuf
[512], tBuf
[512];
101 UCharIterator sIter
, tIter
;
102 uint32_t sState
[2], tState
[2];
103 int32_t sSize
= pieceSize
, tSize
= pieceSize
;
105 status
= U_ZERO_ERROR
;
106 sState
[0] = 0; sState
[1] = 0;
107 tState
[0] = 0; tState
[1] = 0;
108 while(sSize
== pieceSize
&& tSize
== pieceSize
&& partialSKResult
== 0) {
109 uiter_setString(&sIter
, source
, sLen
);
110 uiter_setString(&tIter
, target
, tLen
);
111 sSize
= ucol_nextSortKeyPart(coll
, &sIter
, sState
, sBuf
, pieceSize
, &status
);
112 tSize
= ucol_nextSortKeyPart(coll
, &tIter
, tState
, tBuf
, pieceSize
, &status
);
114 if(sState
[0] != 0 || tState
[0] != 0) {
115 log("State != 0 : %08X %08X\n", sState
[0], tState
[0]);
119 partialSKResult
= memcmp(sBuf
, tBuf
, pieceSize
);
122 if(partialSKResult
< 0) {
124 } else if(partialSKResult
> 0) {
132 IntlTestCollator::doTestVariant(Collator
* col
, const UnicodeString
&source
, const UnicodeString
&target
, Collator::EComparisonResult result
)
134 UErrorCode status
= U_ZERO_ERROR
;
136 UCollator
*myCollation
= (UCollator
*)((RuleBasedCollator
*)col
)->getUCollator();
138 Collator::EComparisonResult compareResult
= col
->compare(source
, target
);
140 CollationKey srckey
, tgtkey
;
141 col
->getCollationKey(source
, srckey
, status
);
142 col
->getCollationKey(target
, tgtkey
, status
);
143 if (U_FAILURE(status
)){
144 errln("Creation of collation keys failed\n");
146 Collator::EComparisonResult keyResult
= srckey
.compareTo(tgtkey
);
148 reportCResult(source
, target
, srckey
, tgtkey
, compareResult
, keyResult
, result
, result
);
150 UColAttributeValue norm
= ucol_getAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, &status
);
152 int32_t sLen
= source
.length(), tLen
= target
.length();
153 const UChar
* src
= source
.getBuffer();
154 const UChar
* trg
= target
.getBuffer();
155 UCollationResult compareResultIter
= (UCollationResult
)result
;
158 UCharIterator sIter
, tIter
;
159 uiter_setString(&sIter
, src
, sLen
);
160 uiter_setString(&tIter
, trg
, tLen
);
161 compareResultIter
= ucol_strcollIter(myCollation
, &sIter
, &tIter
, &status
);
162 if(compareResultIter
!= (UCollationResult
)result
) {
163 errln("Different result for iterative comparison "+source
+" "+target
);
166 /* convert the strings to UTF-8 and do try comparing with char iterator */
167 if(!quick
) { /*!QUICK*/
168 char utf8Source
[256], utf8Target
[256];
169 int32_t utf8SourceLen
= 0, utf8TargetLen
= 0;
170 u_strToUTF8(utf8Source
, 256, &utf8SourceLen
, src
, sLen
, &status
);
171 if(U_FAILURE(status
)) { /* probably buffer is not big enough */
172 log("Src UTF-8 buffer too small! Will not compare!\n");
174 u_strToUTF8(utf8Target
, 256, &utf8TargetLen
, trg
, tLen
, &status
);
175 if(U_SUCCESS(status
)) { /* probably buffer is not big enough */
176 UCollationResult compareResultUTF8
= (UCollationResult
)result
, compareResultUTF8Norm
= (UCollationResult
)result
;
177 UCharIterator sIter
, tIter
;
178 /*log_verbose("Strings converted to UTF-8:%s, %s\n", aescstrdup(source,-1), aescstrdup(target,-1));*/
179 uiter_setUTF8(&sIter
, utf8Source
, utf8SourceLen
);
180 uiter_setUTF8(&tIter
, utf8Target
, utf8TargetLen
);
181 /*uiter_setString(&sIter, source, sLen);
182 uiter_setString(&tIter, target, tLen);*/
183 compareResultUTF8
= ucol_strcollIter(myCollation
, &sIter
, &tIter
, &status
);
184 ucol_setAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &status
);
185 sIter
.move(&sIter
, 0, UITER_START
);
186 tIter
.move(&tIter
, 0, UITER_START
);
187 compareResultUTF8Norm
= ucol_strcollIter(myCollation
, &sIter
, &tIter
, &status
);
188 ucol_setAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, norm
, &status
);
189 if(compareResultUTF8
!= compareResultIter
) {
190 errln("different results in iterative comparison for UTF-16 and UTF-8 encoded strings. "+source
+", "+target
);
192 if(compareResultUTF8
!= compareResultUTF8Norm
) {
193 errln("different results in iterative when normalization is turned on with UTF-8 strings. "+source
+", "+target
);
196 log("Target UTF-8 buffer too small! Did not compare!\n");
198 if(U_FAILURE(status
)) {
199 log("UTF-8 strcoll failed! Ignoring result\n");
204 /* testing the partial sortkeys */
206 int32_t partialSizes
[] = { 3, 1, 2, 4, 8, 20, 80 }; /* just size 3 in the quick mode */
207 int32_t partialSizesSize
= 1;
209 partialSizesSize
= 7;
212 log("partial sortkey test piecesize=");
213 for(i
= 0; i
< partialSizesSize
; i
++) {
214 UCollationResult partialSKResult
= (UCollationResult
)result
, partialNormalizedSKResult
= (UCollationResult
)result
;
215 log("%i ", partialSizes
[i
]);
217 partialSKResult
= compareUsingPartials(myCollation
, src
, sLen
, trg
, tLen
, partialSizes
[i
], status
);
218 if(partialSKResult
!= (UCollationResult
)result
) {
219 errln("Partial sortkey comparison returned wrong result: "+source
+", "+target
+" (size "+partialSizes
[i
]+")");
222 if(norm
!= UCOL_ON
&& !quick
) {
224 ucol_setAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &status
);
225 partialNormalizedSKResult
= compareUsingPartials(myCollation
, src
, sLen
, trg
, tLen
, partialSizes
[i
], status
);
226 ucol_setAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, norm
, &status
);
227 if(partialSKResult
!= partialNormalizedSKResult
) {
228 errln("Partial sortkey comparison gets different result when normalization is on: "+source
+", "+target
+" (size "+partialSizes
[i
]+")");
235 if (compareResult != result) {
236 errln("String comparison failed in variant test\n");
238 if (keyResult != result) {
239 errln("Collation key comparison failed in variant test\n");
245 IntlTestCollator::doTest(Collator
* col
, const UChar
*source
, const UChar
*target
, Collator::EComparisonResult result
) {
246 doTest(col
, UnicodeString(source
), UnicodeString(target
), result
);
250 IntlTestCollator::doTest(Collator
* col
, const UnicodeString
&source
, const UnicodeString
&target
, Collator::EComparisonResult result
)
253 doTestVariant(col
, source
, target
, result
);
254 if(result
== Collator::LESS
) {
255 doTestVariant(col
, target
, source
, Collator::GREATER
);
256 } else if (result
== Collator::GREATER
) {
257 doTestVariant(col
, target
, source
, Collator::LESS
);
260 UErrorCode status
= U_ZERO_ERROR
;
261 CollationElementIterator
* c
= ((RuleBasedCollator
*)col
)->createCollationElementIterator( source
);
262 logln("Testing iterating source: "+source
);
264 c
->setText(target
, status
);
265 logln("Testing iterating target: "+target
);
272 // used for collation result reporting, defined here for convenience
273 // (maybe moved later)
275 IntlTestCollator::reportCResult( const UnicodeString
&source
, const UnicodeString
&target
,
276 CollationKey
&sourceKey
, CollationKey
&targetKey
,
277 Collator::EComparisonResult compareResult
,
278 Collator::EComparisonResult keyResult
,
279 Collator::EComparisonResult incResult
,
280 Collator::EComparisonResult expectedResult
)
282 if (expectedResult
< -1 || expectedResult
> 1)
284 errln("***** invalid call to reportCResult ****");
288 UBool ok1
= (compareResult
== expectedResult
);
289 UBool ok2
= (keyResult
== expectedResult
);
290 UBool ok3
= (incResult
== expectedResult
);
293 if (ok1
&& ok2
&& ok3
&& !verbose
) {
294 // Keep non-verbose, passing tests fast
297 UnicodeString
msg1(ok1
? "Ok: compare(" : "FAIL: compare(");
298 UnicodeString
msg2(", "), msg3(") returned "), msg4("; expected ");
299 UnicodeString prettySource
, prettyTarget
, sExpect
, sResult
;
301 IntlTest::prettify(source
, prettySource
);
302 IntlTest::prettify(target
, prettyTarget
);
303 appendCompareResult(compareResult
, sResult
);
304 appendCompareResult(expectedResult
, sExpect
);
307 logln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
);
309 errln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
+ msg4
+ sExpect
);
312 msg1
= UnicodeString(ok2
? "Ok: key(" : "FAIL: key(");
313 msg2
= ").compareTo(key(";
314 msg3
= ")) returned ";
316 appendCompareResult(keyResult
, sResult
);
319 logln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
);
321 errln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
+ msg4
+ sExpect
);
326 prettify(sourceKey
, prettySource
);
327 prettify(targetKey
, prettyTarget
);
329 errln(msg1
+ prettySource
+ msg2
+ prettyTarget
);
331 msg1
= UnicodeString (ok3
? "Ok: incCompare(" : "FAIL: incCompare(");
333 msg3
= ") returned ";
335 appendCompareResult(incResult
, sResult
);
338 logln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
);
340 errln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
+ msg4
+ sExpect
);
346 IntlTestCollator::appendCompareResult(Collator::EComparisonResult result
,
347 UnicodeString
& target
)
349 if (result
== Collator::LESS
)
353 else if (result
== Collator::EQUAL
)
357 else if (result
== Collator::GREATER
)
363 UnicodeString huh
= "?";
365 target
+= (huh
+ (int32_t)result
);
371 // Produce a printable representation of a CollationKey
372 UnicodeString
&IntlTestCollator::prettify(const CollationKey
&source
, UnicodeString
&target
)
374 int32_t i
, byteCount
;
375 const uint8_t *bytes
= source
.getByteArray(byteCount
);
380 for (i
= 0; i
< byteCount
; i
+= 1)
382 appendHex(bytes
[i
], 2, target
);
391 void IntlTestCollator::backAndForth(CollationElementIterator
&iter
)
393 // Run through the iterator forwards and stick it into an array
394 int32_t orderLength
= 0;
395 int32_t *orders
= getOrders(iter
, orderLength
);
396 UErrorCode status
= U_ZERO_ERROR
;
398 // Now go through it backwards and make sure we get the same values
399 int32_t index
= orderLength
;
402 // reset the iterator
405 while ((o
= iter
.previous(status
)) != CollationElementIterator::NULLORDER
)
410 } else { // this is an error, orders exhausted but there are non-ignorable CEs from
412 errln("Backward iteration returned a non ignorable after orders are exhausted");
416 if (o
!= orders
[--index
])
422 while (index
> 0 && orders
[--index
] == 0)
425 if (o
!= orders
[index
])
427 errln("Mismatch at index %d: 0x%X vs 0x%X", index
,
435 while (index
!= 0 && orders
[index
- 1] == 0)
442 UnicodeString
msg("Didn't get back to beginning - index is ");
447 while ((o
= iter
.next(status
)) != CollationElementIterator::NULLORDER
)
449 UnicodeString
hexString("0x");
451 appendHex(o
, 8, hexString
);
458 while ((o
= iter
.previous(status
)) != CollationElementIterator::NULLORDER
)
460 UnicodeString
hexString("0x");
462 appendHex(o
, 8, hexString
);
474 * Return an integer array containing all of the collation orders
475 * returned by calls to next on the specified iterator
477 int32_t *IntlTestCollator::getOrders(CollationElementIterator
&iter
, int32_t &orderLength
)
479 int32_t maxSize
= 100;
481 int32_t *orders
= new int32_t[maxSize
];
482 UErrorCode status
= U_ZERO_ERROR
;
485 while ((order
= iter
.next(status
)) != CollationElementIterator::NULLORDER
)
490 int32_t *temp
= new int32_t[maxSize
];
492 uprv_memcpy(temp
, orders
, size
* sizeof(int32_t));
497 orders
[size
++] = order
;
502 int32_t *temp
= new int32_t[size
];
504 uprv_memcpy(temp
, orders
, size
* sizeof(int32_t));
513 #endif /* #if !UCONFIG_NO_COLLATION */