1 /********************************************************************
3 * Copyright (c) 1997-2008, 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"
51 //#include "rndmcoll.h"
53 // Set to 1 to test offsets in backAndForth()
54 #define TEST_OFFSETS 0
56 #define TESTCLASS(n,classname) \
60 logln(#classname "---"); \
67 void IntlTestCollator::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par
)
70 logln("TestSuite Collator: ");
74 TESTCLASS(0, CollationEnglishTest
);
75 TESTCLASS(1, CollationFrenchTest
);
76 TESTCLASS(2, CollationGermanTest
);
77 TESTCLASS(3, CollationSpanishTest
);
78 TESTCLASS(4, CollationKanaTest
);
79 TESTCLASS(5, CollationTurkishTest
);
80 TESTCLASS(6, CollationDummyTest
);
81 TESTCLASS(7, G7CollationTest
);
82 TESTCLASS(8, CollationMonkeyTest
);
83 TESTCLASS(9, CollationAPITest
);
84 TESTCLASS(10, CollationRegressionTest
);
85 TESTCLASS(11, CollationCurrencyTest
);
86 TESTCLASS(12, CollationIteratorTest
);
87 TESTCLASS(13, CollationThaiTest
);
88 TESTCLASS(14, LotusCollationKoreanTest
);
89 TESTCLASS(15, StringSearchTest
);
90 TESTCLASS(16, ContractionTableTest
);
91 TESTCLASS(17, DataDrivenCollatorTest
);
92 TESTCLASS(18, UCAConformanceTest
);
93 TESTCLASS(19, CollationServiceTest
);
94 TESTCLASS(20, CollationFinnishTest
); // removed by weiv - we have changed Finnish collation
95 //TESTCLASS(21, RandomCollatorTest); // See ticket 5747 about reenabling this test.
96 TESTCLASS(21, SSearchTest
);
98 default: name
= ""; break;
103 IntlTestCollator::compareUsingPartials(UCollator
*coll
, const UChar source
[], int32_t sLen
, const UChar target
[], int32_t tLen
, int32_t pieceSize
, UErrorCode
&status
) {
104 int32_t partialSKResult
= 0;
105 uint8_t sBuf
[512], tBuf
[512];
106 UCharIterator sIter
, tIter
;
107 uint32_t sState
[2], tState
[2];
108 int32_t sSize
= pieceSize
, tSize
= pieceSize
;
110 status
= U_ZERO_ERROR
;
111 sState
[0] = 0; sState
[1] = 0;
112 tState
[0] = 0; tState
[1] = 0;
113 while(sSize
== pieceSize
&& tSize
== pieceSize
&& partialSKResult
== 0) {
114 uiter_setString(&sIter
, source
, sLen
);
115 uiter_setString(&tIter
, target
, tLen
);
116 sSize
= ucol_nextSortKeyPart(coll
, &sIter
, sState
, sBuf
, pieceSize
, &status
);
117 tSize
= ucol_nextSortKeyPart(coll
, &tIter
, tState
, tBuf
, pieceSize
, &status
);
119 if(sState
[0] != 0 || tState
[0] != 0) {
120 log("State != 0 : %08X %08X\n", sState
[0], tState
[0]);
124 partialSKResult
= memcmp(sBuf
, tBuf
, pieceSize
);
127 if(partialSKResult
< 0) {
129 } else if(partialSKResult
> 0) {
137 IntlTestCollator::doTestVariant(Collator
* col
, const UnicodeString
&source
, const UnicodeString
&target
, Collator::EComparisonResult result
)
139 UErrorCode status
= U_ZERO_ERROR
;
141 UCollator
*myCollation
= (UCollator
*)((RuleBasedCollator
*)col
)->getUCollator();
143 Collator::EComparisonResult compareResult
= col
->compare(source
, target
);
145 CollationKey srckey
, tgtkey
;
146 col
->getCollationKey(source
, srckey
, status
);
147 col
->getCollationKey(target
, tgtkey
, status
);
148 if (U_FAILURE(status
)){
149 errln("Creation of collation keys failed\n");
151 Collator::EComparisonResult keyResult
= srckey
.compareTo(tgtkey
);
153 reportCResult(source
, target
, srckey
, tgtkey
, compareResult
, keyResult
, result
, result
);
155 UColAttributeValue norm
= ucol_getAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, &status
);
157 int32_t sLen
= source
.length(), tLen
= target
.length();
158 const UChar
* src
= source
.getBuffer();
159 const UChar
* trg
= target
.getBuffer();
160 UCollationResult compareResultIter
= (UCollationResult
)result
;
163 UCharIterator sIter
, tIter
;
164 uiter_setString(&sIter
, src
, sLen
);
165 uiter_setString(&tIter
, trg
, tLen
);
166 compareResultIter
= ucol_strcollIter(myCollation
, &sIter
, &tIter
, &status
);
167 if(compareResultIter
!= (UCollationResult
)result
) {
168 errln("Different result for iterative comparison "+source
+" "+target
);
171 /* convert the strings to UTF-8 and do try comparing with char iterator */
172 if(!quick
) { /*!QUICK*/
173 char utf8Source
[256], utf8Target
[256];
174 int32_t utf8SourceLen
= 0, utf8TargetLen
= 0;
175 u_strToUTF8(utf8Source
, 256, &utf8SourceLen
, src
, sLen
, &status
);
176 if(U_FAILURE(status
)) { /* probably buffer is not big enough */
177 log("Src UTF-8 buffer too small! Will not compare!\n");
179 u_strToUTF8(utf8Target
, 256, &utf8TargetLen
, trg
, tLen
, &status
);
180 if(U_SUCCESS(status
)) { /* probably buffer is not big enough */
181 UCollationResult compareResultUTF8
= (UCollationResult
)result
, compareResultUTF8Norm
= (UCollationResult
)result
;
182 UCharIterator sIter
, tIter
;
183 /*log_verbose("Strings converted to UTF-8:%s, %s\n", aescstrdup(source,-1), aescstrdup(target,-1));*/
184 uiter_setUTF8(&sIter
, utf8Source
, utf8SourceLen
);
185 uiter_setUTF8(&tIter
, utf8Target
, utf8TargetLen
);
186 /*uiter_setString(&sIter, source, sLen);
187 uiter_setString(&tIter, target, tLen);*/
188 compareResultUTF8
= ucol_strcollIter(myCollation
, &sIter
, &tIter
, &status
);
189 ucol_setAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &status
);
190 sIter
.move(&sIter
, 0, UITER_START
);
191 tIter
.move(&tIter
, 0, UITER_START
);
192 compareResultUTF8Norm
= ucol_strcollIter(myCollation
, &sIter
, &tIter
, &status
);
193 ucol_setAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, norm
, &status
);
194 if(compareResultUTF8
!= compareResultIter
) {
195 errln("different results in iterative comparison for UTF-16 and UTF-8 encoded strings. "+source
+", "+target
);
197 if(compareResultUTF8
!= compareResultUTF8Norm
) {
198 errln("different results in iterative when normalization is turned on with UTF-8 strings. "+source
+", "+target
);
201 log("Target UTF-8 buffer too small! Did not compare!\n");
203 if(U_FAILURE(status
)) {
204 log("UTF-8 strcoll failed! Ignoring result\n");
209 /* testing the partial sortkeys */
211 int32_t partialSizes
[] = { 3, 1, 2, 4, 8, 20, 80 }; /* just size 3 in the quick mode */
212 int32_t partialSizesSize
= 1;
214 partialSizesSize
= 7;
217 log("partial sortkey test piecesize=");
218 for(i
= 0; i
< partialSizesSize
; i
++) {
219 UCollationResult partialSKResult
= (UCollationResult
)result
, partialNormalizedSKResult
= (UCollationResult
)result
;
220 log("%i ", partialSizes
[i
]);
222 partialSKResult
= compareUsingPartials(myCollation
, src
, sLen
, trg
, tLen
, partialSizes
[i
], status
);
223 if(partialSKResult
!= (UCollationResult
)result
) {
224 errln("Partial sortkey comparison returned wrong result: "+source
+", "+target
+" (size "+partialSizes
[i
]+")");
227 if(norm
!= UCOL_ON
&& !quick
) {
229 ucol_setAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &status
);
230 partialNormalizedSKResult
= compareUsingPartials(myCollation
, src
, sLen
, trg
, tLen
, partialSizes
[i
], status
);
231 ucol_setAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, norm
, &status
);
232 if(partialSKResult
!= partialNormalizedSKResult
) {
233 errln("Partial sortkey comparison gets different result when normalization is on: "+source
+", "+target
+" (size "+partialSizes
[i
]+")");
240 if (compareResult != result) {
241 errln("String comparison failed in variant test\n");
243 if (keyResult != result) {
244 errln("Collation key comparison failed in variant test\n");
250 IntlTestCollator::doTest(Collator
* col
, const UChar
*source
, const UChar
*target
, Collator::EComparisonResult result
) {
251 doTest(col
, UnicodeString(source
), UnicodeString(target
), result
);
255 IntlTestCollator::doTest(Collator
* col
, const UnicodeString
&source
, const UnicodeString
&target
, Collator::EComparisonResult result
)
258 doTestVariant(col
, source
, target
, result
);
259 if(result
== Collator::LESS
) {
260 doTestVariant(col
, target
, source
, Collator::GREATER
);
261 } else if (result
== Collator::GREATER
) {
262 doTestVariant(col
, target
, source
, Collator::LESS
);
265 UErrorCode status
= U_ZERO_ERROR
;
266 CollationElementIterator
* c
= ((RuleBasedCollator
*)col
)->createCollationElementIterator( source
);
267 logln("Testing iterating source: "+source
);
269 c
->setText(target
, status
);
270 logln("Testing iterating target: "+target
);
277 // used for collation result reporting, defined here for convenience
278 // (maybe moved later)
280 IntlTestCollator::reportCResult( const UnicodeString
&source
, const UnicodeString
&target
,
281 CollationKey
&sourceKey
, CollationKey
&targetKey
,
282 Collator::EComparisonResult compareResult
,
283 Collator::EComparisonResult keyResult
,
284 Collator::EComparisonResult incResult
,
285 Collator::EComparisonResult expectedResult
)
287 if (expectedResult
< -1 || expectedResult
> 1)
289 errln("***** invalid call to reportCResult ****");
293 UBool ok1
= (compareResult
== expectedResult
);
294 UBool ok2
= (keyResult
== expectedResult
);
295 UBool ok3
= (incResult
== expectedResult
);
298 if (ok1
&& ok2
&& ok3
&& !verbose
) {
299 // Keep non-verbose, passing tests fast
302 UnicodeString
msg1(ok1
? "Ok: compare(" : "FAIL: compare(");
303 UnicodeString
msg2(", "), msg3(") returned "), msg4("; expected ");
304 UnicodeString prettySource
, prettyTarget
, sExpect
, sResult
;
306 IntlTest::prettify(source
, prettySource
);
307 IntlTest::prettify(target
, prettyTarget
);
308 appendCompareResult(compareResult
, sResult
);
309 appendCompareResult(expectedResult
, sExpect
);
312 logln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
);
314 errln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
+ msg4
+ sExpect
);
317 msg1
= UnicodeString(ok2
? "Ok: key(" : "FAIL: key(");
318 msg2
= ").compareTo(key(";
319 msg3
= ")) returned ";
321 appendCompareResult(keyResult
, sResult
);
324 logln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
);
326 errln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
+ msg4
+ sExpect
);
331 prettify(sourceKey
, prettySource
);
332 prettify(targetKey
, prettyTarget
);
334 errln(msg1
+ prettySource
+ msg2
+ prettyTarget
);
336 msg1
= UnicodeString (ok3
? "Ok: incCompare(" : "FAIL: incCompare(");
338 msg3
= ") returned ";
340 appendCompareResult(incResult
, sResult
);
343 logln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
);
345 errln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
+ msg4
+ sExpect
);
351 IntlTestCollator::appendCompareResult(Collator::EComparisonResult result
,
352 UnicodeString
& target
)
354 if (result
== Collator::LESS
)
358 else if (result
== Collator::EQUAL
)
362 else if (result
== Collator::GREATER
)
368 UnicodeString huh
= "?";
370 target
+= (huh
+ (int32_t)result
);
376 // Produce a printable representation of a CollationKey
377 UnicodeString
&IntlTestCollator::prettify(const CollationKey
&source
, UnicodeString
&target
)
379 int32_t i
, byteCount
;
380 const uint8_t *bytes
= source
.getByteArray(byteCount
);
385 for (i
= 0; i
< byteCount
; i
+= 1)
387 appendHex(bytes
[i
], 2, target
);
396 void IntlTestCollator::backAndForth(CollationElementIterator
&iter
)
398 // Run through the iterator forwards and stick it into an array
399 int32_t orderLength
= 0;
400 Order
*orders
= getOrders(iter
, orderLength
);
401 UErrorCode status
= U_ZERO_ERROR
;
403 // Now go through it backwards and make sure we get the same values
404 int32_t index
= orderLength
;
407 // reset the iterator
410 while ((o
= iter
.previous(status
)) != CollationElementIterator::NULLORDER
)
412 int32_t offset
= iter
.getOffset();
417 } else { // this is an error, orders exhausted but there are non-ignorable CEs from
419 errln("Backward iteration returned a non ignorable after orders are exhausted");
425 if (o
!= orders
[index
].order
) {
429 while (index
> 0 && orders
[--index
].order
== 0) {
433 if (o
!= orders
[index
].order
) {
434 errln("Mismatched order at index %d: 0x%0:8X vs. 0x%0:8X", index
,
435 orders
[index
].order
, o
);
443 if (offset
!= orders
[index
].offset
) {
444 errln("Mismatched offset at index %d: %d vs. %d", index
,
445 orders
[index
].offset
, offset
);
453 while (index
!= 0 && orders
[index
- 1].order
== 0)
460 UnicodeString
msg("Didn't get back to beginning - index is ");
465 while ((o
= iter
.next(status
)) != CollationElementIterator::NULLORDER
)
467 UnicodeString
hexString("0x");
469 appendHex(o
, 8, hexString
);
476 while ((o
= iter
.previous(status
)) != CollationElementIterator::NULLORDER
)
478 UnicodeString
hexString("0x");
480 appendHex(o
, 8, hexString
);
493 * Return an integer array containing all of the collation orders
494 * returned by calls to next on the specified iterator
496 IntlTestCollator::Order
*IntlTestCollator::getOrders(CollationElementIterator
&iter
, int32_t &orderLength
)
498 int32_t maxSize
= 100;
500 Order
*orders
= new Order
[maxSize
];
501 UErrorCode status
= U_ZERO_ERROR
;
502 int32_t offset
= iter
.getOffset();
505 while ((order
= iter
.next(status
)) != CollationElementIterator::NULLORDER
)
510 Order
*temp
= new Order
[maxSize
];
512 uprv_memcpy(temp
, orders
, size
* sizeof(Order
));
517 orders
[size
].order
= order
;
518 orders
[size
].offset
= offset
;
520 offset
= iter
.getOffset();
526 Order
*temp
= new Order
[size
];
528 uprv_memcpy(temp
, orders
, size
* sizeof(Order
));
537 #endif /* #if !UCONFIG_NO_COLLATION */