1 /********************************************************************
3 * Copyright (c) 1997-2014, 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/sortkey.h"
24 #include "unicode/uchar.h"
25 #include "unicode/ustring.h"
50 #include "alphaindextst.h"
52 // Set to 1 to test offsets in backAndForth()
53 #define TEST_OFFSETS 0
55 extern IntlTest
*createCollationTest();
57 void IntlTestCollator::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par
)
60 logln("TestSuite Collator: ");
64 TESTCASE_AUTO_CLASS(CollationEnglishTest
);
65 TESTCASE_AUTO_CLASS(CollationFrenchTest
);
66 TESTCASE_AUTO_CLASS(CollationGermanTest
);
67 TESTCASE_AUTO_CLASS(CollationSpanishTest
);
68 TESTCASE_AUTO_CLASS(CollationKanaTest
);
69 TESTCASE_AUTO_CLASS(CollationTurkishTest
);
70 TESTCASE_AUTO_CLASS(CollationDummyTest
);
71 TESTCASE_AUTO_CLASS(G7CollationTest
);
72 TESTCASE_AUTO_CLASS(CollationMonkeyTest
);
73 TESTCASE_AUTO_CLASS(CollationAPITest
);
74 TESTCASE_AUTO_CLASS(CollationRegressionTest
);
75 TESTCASE_AUTO_CLASS(CollationCurrencyTest
);
76 TESTCASE_AUTO_CLASS(CollationIteratorTest
);
77 TESTCASE_AUTO_CLASS(CollationThaiTest
);
78 TESTCASE_AUTO_CLASS(LotusCollationKoreanTest
);
79 TESTCASE_AUTO_CLASS(StringSearchTest
);
80 TESTCASE_AUTO_CLASS(UCAConformanceTest
);
81 TESTCASE_AUTO_CLASS(CollationServiceTest
);
82 TESTCASE_AUTO_CLASS(CollationFinnishTest
); // removed by weiv - we have changed Finnish collation
83 TESTCASE_AUTO_CLASS(SSearchTest
);
84 #if !UCONFIG_NO_NORMALIZATION
85 TESTCASE_AUTO_CLASS(AlphabeticIndexTest
);
87 TESTCASE_AUTO_CREATE_CLASS(CollationTest
);
92 IntlTestCollator::compareUsingPartials(UCollator
*coll
, const UChar source
[], int32_t sLen
, const UChar target
[], int32_t tLen
, int32_t pieceSize
, UErrorCode
&status
) {
93 int32_t partialSKResult
= 0;
94 uint8_t sBuf
[512], tBuf
[512];
95 UCharIterator sIter
, tIter
;
96 uint32_t sState
[2], tState
[2];
97 int32_t sSize
= pieceSize
, tSize
= pieceSize
;
99 status
= U_ZERO_ERROR
;
100 sState
[0] = 0; sState
[1] = 0;
101 tState
[0] = 0; tState
[1] = 0;
102 while(sSize
== pieceSize
&& tSize
== pieceSize
&& partialSKResult
== 0) {
103 uiter_setString(&sIter
, source
, sLen
);
104 uiter_setString(&tIter
, target
, tLen
);
105 sSize
= ucol_nextSortKeyPart(coll
, &sIter
, sState
, sBuf
, pieceSize
, &status
);
106 tSize
= ucol_nextSortKeyPart(coll
, &tIter
, tState
, tBuf
, pieceSize
, &status
);
108 if(sState
[0] != 0 || tState
[0] != 0) {
109 log("State != 0 : %08X %08X\n", sState
[0], tState
[0]);
113 partialSKResult
= memcmp(sBuf
, tBuf
, pieceSize
);
116 if(partialSKResult
< 0) {
118 } else if(partialSKResult
> 0) {
126 IntlTestCollator::doTestVariant(Collator
* col
, const UnicodeString
&source
, const UnicodeString
&target
, Collator::EComparisonResult result
)
128 UErrorCode status
= U_ZERO_ERROR
;
130 UCollator
*myCollation
= col
->toUCollator();
132 Collator::EComparisonResult compareResult
= col
->compare(source
, target
);
134 CollationKey srckey
, tgtkey
;
135 col
->getCollationKey(source
, srckey
, status
);
136 col
->getCollationKey(target
, tgtkey
, status
);
137 if (U_FAILURE(status
)){
138 errln("Creation of collation keys failed\n");
140 Collator::EComparisonResult keyResult
= srckey
.compareTo(tgtkey
);
142 reportCResult(source
, target
, srckey
, tgtkey
, compareResult
, keyResult
, result
, result
);
144 UColAttributeValue norm
= ucol_getAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, &status
);
146 int32_t sLen
= source
.length(), tLen
= target
.length();
147 const UChar
* src
= source
.getBuffer();
148 const UChar
* trg
= target
.getBuffer();
149 UCollationResult compareResultIter
= (UCollationResult
)result
;
152 UCharIterator sIter
, tIter
;
153 uiter_setString(&sIter
, src
, sLen
);
154 uiter_setString(&tIter
, trg
, tLen
);
155 compareResultIter
= ucol_strcollIter(myCollation
, &sIter
, &tIter
, &status
);
156 if(compareResultIter
!= (UCollationResult
)result
) {
157 errln("Different result for iterative comparison "+source
+" "+target
);
160 /* convert the strings to UTF-8 and do try comparing with char iterator */
161 if(!quick
) { /*!QUICK*/
162 char utf8Source
[256], utf8Target
[256];
163 int32_t utf8SourceLen
= 0, utf8TargetLen
= 0;
164 u_strToUTF8(utf8Source
, 256, &utf8SourceLen
, src
, sLen
, &status
);
165 if(U_FAILURE(status
)) { /* probably buffer is not big enough */
166 log("Src UTF-8 buffer too small! Will not compare!\n");
168 u_strToUTF8(utf8Target
, 256, &utf8TargetLen
, trg
, tLen
, &status
);
169 if(U_SUCCESS(status
)) { /* probably buffer is not big enough */
170 UCollationResult compareResultUTF8
= (UCollationResult
)result
, compareResultUTF8Norm
= (UCollationResult
)result
;
171 UCharIterator sIter
, tIter
;
172 /*log_verbose("Strings converted to UTF-8:%s, %s\n", aescstrdup(source,-1), aescstrdup(target,-1));*/
173 uiter_setUTF8(&sIter
, utf8Source
, utf8SourceLen
);
174 uiter_setUTF8(&tIter
, utf8Target
, utf8TargetLen
);
175 /*uiter_setString(&sIter, source, sLen);
176 uiter_setString(&tIter, target, tLen);*/
177 compareResultUTF8
= ucol_strcollIter(myCollation
, &sIter
, &tIter
, &status
);
178 ucol_setAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &status
);
179 sIter
.move(&sIter
, 0, UITER_START
);
180 tIter
.move(&tIter
, 0, UITER_START
);
181 compareResultUTF8Norm
= ucol_strcollIter(myCollation
, &sIter
, &tIter
, &status
);
182 ucol_setAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, norm
, &status
);
183 if(compareResultUTF8
!= compareResultIter
) {
184 errln("different results in iterative comparison for UTF-16 and UTF-8 encoded strings. "+source
+", "+target
);
186 if(compareResultUTF8
!= compareResultUTF8Norm
) {
187 errln("different results in iterative when normalization is turned on with UTF-8 strings. "+source
+", "+target
);
190 log("Target UTF-8 buffer too small! Did not compare!\n");
192 if(U_FAILURE(status
)) {
193 log("UTF-8 strcoll failed! Ignoring result\n");
198 /* testing the partial sortkeys */
200 int32_t partialSizes
[] = { 3, 1, 2, 4, 8, 20, 80 }; /* just size 3 in the quick mode */
201 int32_t partialSizesSize
= 1;
203 partialSizesSize
= 7;
206 log("partial sortkey test piecesize=");
207 for(i
= 0; i
< partialSizesSize
; i
++) {
208 UCollationResult partialSKResult
= (UCollationResult
)result
, partialNormalizedSKResult
= (UCollationResult
)result
;
209 log("%i ", partialSizes
[i
]);
211 partialSKResult
= compareUsingPartials(myCollation
, src
, sLen
, trg
, tLen
, partialSizes
[i
], status
);
212 if(partialSKResult
!= (UCollationResult
)result
) {
213 errln("Partial sortkey comparison returned wrong result: "+source
+", "+target
+" (size "+partialSizes
[i
]+")");
216 if(norm
!= UCOL_ON
&& !quick
) {
218 ucol_setAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &status
);
219 partialNormalizedSKResult
= compareUsingPartials(myCollation
, src
, sLen
, trg
, tLen
, partialSizes
[i
], status
);
220 ucol_setAttribute(myCollation
, UCOL_NORMALIZATION_MODE
, norm
, &status
);
221 if(partialSKResult
!= partialNormalizedSKResult
) {
222 errln("Partial sortkey comparison gets different result when normalization is on: "+source
+", "+target
+" (size "+partialSizes
[i
]+")");
229 if (compareResult != result) {
230 errln("String comparison failed in variant test\n");
232 if (keyResult != result) {
233 errln("Collation key comparison failed in variant test\n");
239 IntlTestCollator::doTest(Collator
* col
, const UChar
*source
, const UChar
*target
, Collator::EComparisonResult result
) {
240 doTest(col
, UnicodeString(source
), UnicodeString(target
), result
);
244 IntlTestCollator::doTest(Collator
* col
, const UnicodeString
&source
, const UnicodeString
&target
, Collator::EComparisonResult result
)
247 doTestVariant(col
, source
, target
, result
);
248 if(result
== Collator::LESS
) {
249 doTestVariant(col
, target
, source
, Collator::GREATER
);
250 } else if (result
== Collator::GREATER
) {
251 doTestVariant(col
, target
, source
, Collator::LESS
);
254 UErrorCode status
= U_ZERO_ERROR
;
255 LocalPointer
<CollationElementIterator
> c(((RuleBasedCollator
*)col
)->createCollationElementIterator(source
));
256 logln("Testing iterating source: "+source
);
258 c
->setText(target
, status
);
259 logln("Testing iterating target: "+target
);
265 // used for collation result reporting, defined here for convenience
266 // (maybe moved later)
268 IntlTestCollator::reportCResult( const UnicodeString
&source
, const UnicodeString
&target
,
269 CollationKey
&sourceKey
, CollationKey
&targetKey
,
270 Collator::EComparisonResult compareResult
,
271 Collator::EComparisonResult keyResult
,
272 Collator::EComparisonResult incResult
,
273 Collator::EComparisonResult expectedResult
)
275 if (expectedResult
< -1 || expectedResult
> 1)
277 errln("***** invalid call to reportCResult ****");
281 UBool ok1
= (compareResult
== expectedResult
);
282 UBool ok2
= (keyResult
== expectedResult
);
283 UBool ok3
= (incResult
== expectedResult
);
286 if (ok1
&& ok2
&& ok3
&& !verbose
) {
287 // Keep non-verbose, passing tests fast
290 UnicodeString
msg1(ok1
? "Ok: compare(" : "FAIL: compare(");
291 UnicodeString
msg2(", "), msg3(") returned "), msg4("; expected ");
292 UnicodeString prettySource
, prettyTarget
, sExpect
, sResult
;
294 IntlTest::prettify(source
, prettySource
);
295 IntlTest::prettify(target
, prettyTarget
);
296 appendCompareResult(compareResult
, sResult
);
297 appendCompareResult(expectedResult
, sExpect
);
300 logln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
);
302 errln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
+ msg4
+ sExpect
);
305 msg1
= UnicodeString(ok2
? "Ok: key(" : "FAIL: key(");
306 msg2
= ").compareTo(key(";
307 msg3
= ")) returned ";
309 appendCompareResult(keyResult
, sResult
);
312 logln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
);
314 errln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
+ msg4
+ sExpect
);
319 prettify(sourceKey
, prettySource
);
320 prettify(targetKey
, prettyTarget
);
322 errln(msg1
+ prettySource
+ msg2
+ prettyTarget
);
324 msg1
= UnicodeString (ok3
? "Ok: incCompare(" : "FAIL: incCompare(");
326 msg3
= ") returned ";
328 appendCompareResult(incResult
, sResult
);
331 logln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
);
333 errln(msg1
+ prettySource
+ msg2
+ prettyTarget
+ msg3
+ sResult
+ msg4
+ sExpect
);
339 IntlTestCollator::appendCompareResult(Collator::EComparisonResult result
,
340 UnicodeString
& target
)
342 if (result
== Collator::LESS
)
346 else if (result
== Collator::EQUAL
)
350 else if (result
== Collator::GREATER
)
356 UnicodeString huh
= "?";
358 target
+= (huh
+ (int32_t)result
);
364 // Produce a printable representation of a CollationKey
365 UnicodeString
&IntlTestCollator::prettify(const CollationKey
&source
, UnicodeString
&target
)
367 int32_t i
, byteCount
;
368 const uint8_t *bytes
= source
.getByteArray(byteCount
);
373 for (i
= 0; i
< byteCount
; i
+= 1)
378 appendHex(bytes
[i
], 2, target
);
386 void IntlTestCollator::backAndForth(CollationElementIterator
&iter
)
388 // Run through the iterator forwards and stick it into an array
389 int32_t orderLength
= 0;
390 LocalArray
<Order
> orders(getOrders(iter
, orderLength
));
391 UErrorCode status
= U_ZERO_ERROR
;
393 // Now go through it backwards and make sure we get the same values
394 int32_t index
= orderLength
;
397 // reset the iterator
400 while ((o
= iter
.previous(status
)) != CollationElementIterator::NULLORDER
)
402 /*int32_t offset = */iter
.getOffset();
407 } else { // this is an error, orders exhausted but there are non-ignorable CEs from
409 errln("Backward iteration returned a non ignorable after orders are exhausted");
415 if (o
!= orders
[index
].order
) {
419 while (index
> 0 && orders
[--index
].order
== 0) {
423 if (o
!= orders
[index
].order
) {
424 errln("Mismatched order at index %d: 0x%0:8X vs. 0x%0:8X", index
,
425 orders
[index
].order
, o
);
433 if (offset
!= orders
[index
].offset
) {
434 errln("Mismatched offset at index %d: %d vs. %d", index
,
435 orders
[index
].offset
, offset
);
443 while (index
!= 0 && orders
[index
- 1].order
== 0)
450 UnicodeString
msg("Didn't get back to beginning - index is ");
455 while ((o
= iter
.next(status
)) != CollationElementIterator::NULLORDER
)
457 UnicodeString
hexString("0x");
459 appendHex(o
, 8, hexString
);
466 while ((o
= iter
.previous(status
)) != CollationElementIterator::NULLORDER
)
468 UnicodeString
hexString("0x");
470 appendHex(o
, 8, hexString
);
480 * Return an integer array containing all of the collation orders
481 * returned by calls to next on the specified iterator
483 IntlTestCollator::Order
*IntlTestCollator::getOrders(CollationElementIterator
&iter
, int32_t &orderLength
)
485 int32_t maxSize
= 100;
487 LocalArray
<Order
> orders(new Order
[maxSize
]);
488 UErrorCode status
= U_ZERO_ERROR
;
489 int32_t offset
= iter
.getOffset();
492 while ((order
= iter
.next(status
)) != CollationElementIterator::NULLORDER
)
497 Order
*temp
= new Order
[maxSize
];
499 uprv_memcpy(temp
, orders
.getAlias(), size
* sizeof(Order
));
500 orders
.adoptInstead(temp
);
503 orders
[size
].order
= order
;
504 orders
[size
].offset
= offset
;
506 offset
= iter
.getOffset();
509 if (U_FAILURE(status
)) {
510 errln("CollationElementIterator.next() failed - %s",
511 u_errorName(status
));
516 Order
*temp
= new Order
[size
];
518 uprv_memcpy(temp
, orders
.getAlias(), size
* sizeof(Order
));
519 orders
.adoptInstead(temp
);
523 return orders
.orphan();
526 #endif /* #if !UCONFIG_NO_COLLATION */