1 /********************************************************************
3 * Copyright (c) 1997-2009, 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/localpointer.h"
23 #include "unicode/uchar.h"
24 #include "unicode/ustring.h"
52 //#include "rndmcoll.h"
54 // Set to 1 to test offsets in backAndForth()
55 #define TEST_OFFSETS 0
57 #define TESTCLASS(n,classname) \
61 logln(#classname "---"); \
68 void IntlTestCollator::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par
)
71 logln("TestSuite Collator: ");
75 TESTCLASS(0, CollationEnglishTest
);
76 TESTCLASS(1, CollationFrenchTest
);
77 TESTCLASS(2, CollationGermanTest
);
78 TESTCLASS(3, CollationSpanishTest
);
79 TESTCLASS(4, CollationKanaTest
);
80 TESTCLASS(5, CollationTurkishTest
);
81 TESTCLASS(6, CollationDummyTest
);
82 TESTCLASS(7, G7CollationTest
);
83 TESTCLASS(8, CollationMonkeyTest
);
84 TESTCLASS(9, CollationAPITest
);
85 TESTCLASS(10, CollationRegressionTest
);
86 TESTCLASS(11, CollationCurrencyTest
);
87 TESTCLASS(12, CollationIteratorTest
);
88 TESTCLASS(13, CollationThaiTest
);
89 TESTCLASS(14, LotusCollationKoreanTest
);
90 TESTCLASS(15, StringSearchTest
);
91 TESTCLASS(16, ContractionTableTest
);
92 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
93 TESTCLASS(17, DataDrivenCollatorTest
);
95 TESTCLASS(18, UCAConformanceTest
);
96 TESTCLASS(19, CollationServiceTest
);
97 TESTCLASS(20, CollationFinnishTest
); // removed by weiv - we have changed Finnish collation
98 //TESTCLASS(21, RandomCollatorTest); // See ticket 5747 about reenabling this test.
99 TESTCLASS(21, SSearchTest
);
101 default: name
= ""; break;
106 IntlTestCollator::compareUsingPartials(UCollator
*coll
, const UChar source
[], int32_t sLen
, const UChar target
[], int32_t tLen
, int32_t pieceSize
, UErrorCode
&status
) {
107 int32_t partialSKResult
= 0;
108 uint8_t sBuf
[512], tBuf
[512];
109 UCharIterator sIter
, tIter
;
110 uint32_t sState
[2], tState
[2];
111 int32_t sSize
= pieceSize
, tSize
= pieceSize
;
113 status
= U_ZERO_ERROR
;
114 sState
[0] = 0; sState
[1] = 0;
115 tState
[0] = 0; tState
[1] = 0;
116 while(sSize
== pieceSize
&& tSize
== pieceSize
&& partialSKResult
== 0) {
117 uiter_setString(&sIter
, source
, sLen
);
118 uiter_setString(&tIter
, target
, tLen
);
119 sSize
= ucol_nextSortKeyPart(coll
, &sIter
, sState
, sBuf
, pieceSize
, &status
);
120 tSize
= ucol_nextSortKeyPart(coll
, &tIter
, tState
, tBuf
, pieceSize
, &status
);
122 if(sState
[0] != 0 || tState
[0] != 0) {
123 log("State != 0 : %08X %08X\n", sState
[0], tState
[0]);
127 partialSKResult
= memcmp(sBuf
, tBuf
, pieceSize
);
130 if(partialSKResult
< 0) {
132 } else if(partialSKResult
> 0) {
140 IntlTestCollator::doTestVariant(Collator
* col
, const UnicodeString
&source
, const UnicodeString
&target
, Collator::EComparisonResult result
)
142 UErrorCode status
= U_ZERO_ERROR
;
144 UCollator
*myCollation
= (UCollator
*)((RuleBasedCollator
*)col
)->getUCollator();
146 Collator::EComparisonResult compareResult
= col
->compare(source
, target
);
148 CollationKey srckey
, tgtkey
;
149 col
->getCollationKey(source
, srckey
, status
);
150 col
->getCollationKey(target
, tgtkey
, status
);
151 if (U_FAILURE(status
)){
152 errln("Creation of collation keys failed\n");
154 Collator::EComparisonResult keyResult
= srckey
.compareTo(tgtkey
);
156 reportCResult(source
, target
, srckey
, tgtkey
, compareResult
, keyResult
, result
, result
);
158 UColAttributeValue norm
= ucol_getAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, &status
);
160 int32_t sLen
= source
.length(), tLen
= target
.length();
161 const UChar
* src
= source
.getBuffer();
162 const UChar
* trg
= target
.getBuffer();
163 UCollationResult compareResultIter
= (UCollationResult
)result
;
166 UCharIterator sIter
, tIter
;
167 uiter_setString(&sIter
, src
, sLen
);
168 uiter_setString(&tIter
, trg
, tLen
);
169 compareResultIter
= ucol_strcollIter(myCollation
, &sIter
, &tIter
, &status
);
170 if(compareResultIter
!= (UCollationResult
)result
) {
171 errln("Different result for iterative comparison "+source
+" "+target
);
174 /* convert the strings to UTF-8 and do try comparing with char iterator */
175 if(!quick
) { /*!QUICK*/
176 char utf8Source
[256], utf8Target
[256];
177 int32_t utf8SourceLen
= 0, utf8TargetLen
= 0;
178 u_strToUTF8(utf8Source
, 256, &utf8SourceLen
, src
, sLen
, &status
);
179 if(U_FAILURE(status
)) { /* probably buffer is not big enough */
180 log("Src UTF-8 buffer too small! Will not compare!\n");
182 u_strToUTF8(utf8Target
, 256, &utf8TargetLen
, trg
, tLen
, &status
);
183 if(U_SUCCESS(status
)) { /* probably buffer is not big enough */
184 UCollationResult compareResultUTF8
= (UCollationResult
)result
, compareResultUTF8Norm
= (UCollationResult
)result
;
185 UCharIterator sIter
, tIter
;
186 /*log_verbose("Strings converted to UTF-8:%s, %s\n", aescstrdup(source,-1), aescstrdup(target,-1));*/
187 uiter_setUTF8(&sIter
, utf8Source
, utf8SourceLen
);
188 uiter_setUTF8(&tIter
, utf8Target
, utf8TargetLen
);
189 /*uiter_setString(&sIter, source, sLen);
190 uiter_setString(&tIter, target, tLen);*/
191 compareResultUTF8
= ucol_strcollIter(myCollation
, &sIter
, &tIter
, &status
);
192 ucol_setAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &status
);
193 sIter
.move(&sIter
, 0, UITER_START
);
194 tIter
.move(&tIter
, 0, UITER_START
);
195 compareResultUTF8Norm
= ucol_strcollIter(myCollation
, &sIter
, &tIter
, &status
);
196 ucol_setAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, norm
, &status
);
197 if(compareResultUTF8
!= compareResultIter
) {
198 errln("different results in iterative comparison for UTF-16 and UTF-8 encoded strings. "+source
+", "+target
);
200 if(compareResultUTF8
!= compareResultUTF8Norm
) {
201 errln("different results in iterative when normalization is turned on with UTF-8 strings. "+source
+", "+target
);
204 log("Target UTF-8 buffer too small! Did not compare!\n");
206 if(U_FAILURE(status
)) {
207 log("UTF-8 strcoll failed! Ignoring result\n");
212 /* testing the partial sortkeys */
214 int32_t partialSizes
[] = { 3, 1, 2, 4, 8, 20, 80 }; /* just size 3 in the quick mode */
215 int32_t partialSizesSize
= 1;
217 partialSizesSize
= 7;
220 log("partial sortkey test piecesize=");
221 for(i
= 0; i
< partialSizesSize
; i
++) {
222 UCollationResult partialSKResult
= (UCollationResult
)result
, partialNormalizedSKResult
= (UCollationResult
)result
;
223 log("%i ", partialSizes
[i
]);
225 partialSKResult
= compareUsingPartials(myCollation
, src
, sLen
, trg
, tLen
, partialSizes
[i
], status
);
226 if(partialSKResult
!= (UCollationResult
)result
) {
227 errln("Partial sortkey comparison returned wrong result: "+source
+", "+target
+" (size "+partialSizes
[i
]+")");
230 if(norm
!= UCOL_ON
&& !quick
) {
232 ucol_setAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &status
);
233 partialNormalizedSKResult
= compareUsingPartials(myCollation
, src
, sLen
, trg
, tLen
, partialSizes
[i
], status
);
234 ucol_setAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, norm
, &status
);
235 if(partialSKResult
!= partialNormalizedSKResult
) {
236 errln("Partial sortkey comparison gets different result when normalization is on: "+source
+", "+target
+" (size "+partialSizes
[i
]+")");
243 if (compareResult != result) {
244 errln("String comparison failed in variant test\n");
246 if (keyResult != result) {
247 errln("Collation key comparison failed in variant test\n");
253 IntlTestCollator::doTest(Collator
* col
, const UChar
*source
, const UChar
*target
, Collator::EComparisonResult result
) {
254 doTest(col
, UnicodeString(source
), UnicodeString(target
), result
);
258 IntlTestCollator::doTest(Collator
* col
, const UnicodeString
&source
, const UnicodeString
&target
, Collator::EComparisonResult result
)
261 doTestVariant(col
, source
, target
, result
);
262 if(result
== Collator::LESS
) {
263 doTestVariant(col
, target
, source
, Collator::GREATER
);
264 } else if (result
== Collator::GREATER
) {
265 doTestVariant(col
, target
, source
, Collator::LESS
);
268 UErrorCode status
= U_ZERO_ERROR
;
269 LocalPointer
<CollationElementIterator
> c(((RuleBasedCollator
*)col
)->createCollationElementIterator(source
));
270 logln("Testing iterating source: "+source
);
272 c
->setText(target
, status
);
273 logln("Testing iterating target: "+target
);
279 // used for collation result reporting, defined here for convenience
280 // (maybe moved later)
282 IntlTestCollator::reportCResult( const UnicodeString
&source
, const UnicodeString
&target
,
283 CollationKey
&sourceKey
, CollationKey
&targetKey
,
284 Collator::EComparisonResult compareResult
,
285 Collator::EComparisonResult keyResult
,
286 Collator::EComparisonResult incResult
,
287 Collator::EComparisonResult expectedResult
)
289 if (expectedResult
< -1 || expectedResult
> 1)
291 errln("***** invalid call to reportCResult ****");
295 UBool ok1
= (compareResult
== expectedResult
);
296 UBool ok2
= (keyResult
== expectedResult
);
297 UBool ok3
= (incResult
== expectedResult
);
300 if (ok1
&& ok2
&& ok3
&& !verbose
) {
301 // Keep non-verbose, passing tests fast
304 UnicodeString
msg1(ok1
? "Ok: compare(" : "FAIL: compare(");
305 UnicodeString
msg2(", "), msg3(") returned "), msg4("; expected ");
306 UnicodeString prettySource
, prettyTarget
, sExpect
, sResult
;
308 IntlTest::prettify(source
, prettySource
);
309 IntlTest::prettify(target
, prettyTarget
);
310 appendCompareResult(compareResult
, sResult
);
311 appendCompareResult(expectedResult
, sExpect
);
314 logln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
);
316 errln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
+ msg4
+ sExpect
);
319 msg1
= UnicodeString(ok2
? "Ok: key(" : "FAIL: key(");
320 msg2
= ").compareTo(key(";
321 msg3
= ")) returned ";
323 appendCompareResult(keyResult
, sResult
);
326 logln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
);
328 errln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
+ msg4
+ sExpect
);
333 prettify(sourceKey
, prettySource
);
334 prettify(targetKey
, prettyTarget
);
336 errln(msg1
+ prettySource
+ msg2
+ prettyTarget
);
338 msg1
= UnicodeString (ok3
? "Ok: incCompare(" : "FAIL: incCompare(");
340 msg3
= ") returned ";
342 appendCompareResult(incResult
, sResult
);
345 logln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
);
347 errln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
+ msg4
+ sExpect
);
353 IntlTestCollator::appendCompareResult(Collator::EComparisonResult result
,
354 UnicodeString
& target
)
356 if (result
== Collator::LESS
)
360 else if (result
== Collator::EQUAL
)
364 else if (result
== Collator::GREATER
)
370 UnicodeString huh
= "?";
372 target
+= (huh
+ (int32_t)result
);
378 // Produce a printable representation of a CollationKey
379 UnicodeString
&IntlTestCollator::prettify(const CollationKey
&source
, UnicodeString
&target
)
381 int32_t i
, byteCount
;
382 const uint8_t *bytes
= source
.getByteArray(byteCount
);
387 for (i
= 0; i
< byteCount
; i
+= 1)
392 appendHex(bytes
[i
], 2, target
);
400 void IntlTestCollator::backAndForth(CollationElementIterator
&iter
)
402 // Run through the iterator forwards and stick it into an array
403 int32_t orderLength
= 0;
404 LocalArray
<Order
> orders(getOrders(iter
, orderLength
));
405 UErrorCode status
= U_ZERO_ERROR
;
407 // Now go through it backwards and make sure we get the same values
408 int32_t index
= orderLength
;
411 // reset the iterator
414 while ((o
= iter
.previous(status
)) != CollationElementIterator::NULLORDER
)
416 /*int32_t offset = */iter
.getOffset();
421 } else { // this is an error, orders exhausted but there are non-ignorable CEs from
423 errln("Backward iteration returned a non ignorable after orders are exhausted");
429 if (o
!= orders
[index
].order
) {
433 while (index
> 0 && orders
[--index
].order
== 0) {
437 if (o
!= orders
[index
].order
) {
438 errln("Mismatched order at index %d: 0x%0:8X vs. 0x%0:8X", index
,
439 orders
[index
].order
, o
);
447 if (offset
!= orders
[index
].offset
) {
448 errln("Mismatched offset at index %d: %d vs. %d", index
,
449 orders
[index
].offset
, offset
);
457 while (index
!= 0 && orders
[index
- 1].order
== 0)
464 UnicodeString
msg("Didn't get back to beginning - index is ");
469 while ((o
= iter
.next(status
)) != CollationElementIterator::NULLORDER
)
471 UnicodeString
hexString("0x");
473 appendHex(o
, 8, hexString
);
480 while ((o
= iter
.previous(status
)) != CollationElementIterator::NULLORDER
)
482 UnicodeString
hexString("0x");
484 appendHex(o
, 8, hexString
);
494 * Return an integer array containing all of the collation orders
495 * returned by calls to next on the specified iterator
497 IntlTestCollator::Order
*IntlTestCollator::getOrders(CollationElementIterator
&iter
, int32_t &orderLength
)
499 int32_t maxSize
= 100;
501 LocalArray
<Order
> orders(new Order
[maxSize
]);
502 UErrorCode status
= U_ZERO_ERROR
;
503 int32_t offset
= iter
.getOffset();
506 while ((order
= iter
.next(status
)) != CollationElementIterator::NULLORDER
)
511 Order
*temp
= new Order
[maxSize
];
513 uprv_memcpy(temp
, orders
.getAlias(), size
* sizeof(Order
));
514 orders
.adoptInstead(temp
);
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
.getAlias(), size
* sizeof(Order
));
529 orders
.adoptInstead(temp
);
533 return orders
.orphan();
536 #endif /* #if !UCONFIG_NO_COLLATION */