2 **********************************************************************
3 * Copyright (C) 1999-2016, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 12/09/99 aliu Ported from Java.
8 **********************************************************************
11 #include "unicode/utypes.h"
13 #if !UCONFIG_NO_COLLATION
16 #include "unicode/utypes.h"
17 #include "unicode/coll.h"
18 #include "unicode/localpointer.h"
19 #include "unicode/sortkey.h"
20 #include "unicode/tblcoll.h"
21 #include "unicode/ustring.h"
28 * The TestDictionary test expects a file of this name, with this
29 * encoding, to be present in the directory $ICU/source/test/testdata.
31 //#define TEST_FILE "th18057.txt"
34 * This is the most failures we show in TestDictionary. If this number
35 * is < 0, we show all failures.
37 #define MAX_FAILURES_TO_SHOW -1
39 CollationThaiTest::CollationThaiTest() {
40 UErrorCode status
= U_ZERO_ERROR
;
41 coll
= Collator::createInstance(Locale("th", "TH", ""), status
);
42 if (coll
&& U_SUCCESS(status
)) {
43 //coll->setStrength(Collator::TERTIARY);
50 CollationThaiTest::~CollationThaiTest() {
54 void CollationThaiTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
,
58 dataerrln(__FILE__
" cannot test - failed to create collator.");
64 TESTCASE(0,TestDictionary
);
65 TESTCASE(1,TestCornerCases
);
66 TESTCASE(2,TestNamesList
);
67 TESTCASE(3,TestInvalidThai
);
68 TESTCASE(4,TestReordering
);
69 default: name
= ""; break;
74 * Read the external names list, and confirms that the collator
75 * gets the same results when comparing lines one to another
76 * using regular and iterative comparison.
78 void CollationThaiTest::TestNamesList(void) {
80 errln("Error: could not construct Thai collator");
84 UErrorCode ec
= U_ZERO_ERROR
;
85 TextFile
names("TestNames_Thai.txt", "UTF16LE", ec
);
87 logln("Can't open TestNames_Thai.txt: %s; skipping test",
93 // Loop through each word in the dictionary and compare it to the previous
94 // word. They should be in sorted order.
96 UnicodeString lastWord
, word
;
98 int32_t wordCount
= 0;
99 while (names
.readLineSkippingComments(word
, ec
, FALSE
) && U_SUCCESS(ec
)) {
101 // Show the first 8 words being compared, so we can see what's happening
103 if (wordCount
<= 8) {
105 logln((UnicodeString
)"Word " + wordCount
+ ": " + IntlTest::prettify(word
, str
));
108 if (lastWord
.length() > 0) {
109 Collator::EComparisonResult result
= coll
->compare(lastWord
, word
);
110 doTest(coll
, lastWord
, word
, result
);
115 assertSuccess("readLine", ec
);
117 logln((UnicodeString
)"Words checked: " + wordCount
);
121 * Read the external dictionary file, which is already in proper
122 * sorted order, and confirm that the collator compares each line as
123 * preceding the following line.
125 void CollationThaiTest::TestDictionary(void) {
127 errln("Error: could not construct Thai collator");
131 UErrorCode ec
= U_ZERO_ERROR
;
132 TextFile
riwords("riwords.txt", "UTF8", ec
);
134 logln("Can't open riwords.txt: %s; skipping test",
140 // Loop through each word in the dictionary and compare it to the previous
141 // word. They should be in sorted order.
143 UnicodeString lastWord
, word
;
145 int32_t wordCount
= 0;
146 while (riwords
.readLineSkippingComments(word
, ec
, FALSE
) && U_SUCCESS(ec
)) {
148 // Show the first 8 words being compared, so we can see what's happening
150 if (wordCount
<= 8) {
152 logln((UnicodeString
)"Word " + wordCount
+ ": " + IntlTest::prettify(word
, str
));
155 if (lastWord
.length() > 0) {
156 int32_t result
= coll
->compare(lastWord
, word
);
160 if (MAX_FAILURES_TO_SHOW
< 0 || failed
<= MAX_FAILURES_TO_SHOW
) {
163 UnicodeString("--------------------------------------------\n")
164 + riwords
.getLineNumber()
165 + " compare(" + IntlTest::prettify(lastWord
, str
);
166 msg
+= UnicodeString(", ")
167 + IntlTest::prettify(word
, str
) + ") returned " + result
169 UErrorCode status
= U_ZERO_ERROR
;
171 coll
->getCollationKey(lastWord
, k1
, status
);
172 coll
->getCollationKey(word
, k2
, status
);
173 if (U_FAILURE(status
)) {
174 errln((UnicodeString
)"Fail: getCollationKey returned " + u_errorName(status
));
177 msg
.append("key1: ").append(prettify(k1
, str
)).append("\n");
178 msg
.append("key2: ").append(prettify(k2
, str
));
186 assertSuccess("readLine", ec
);
189 if (failed
> MAX_FAILURES_TO_SHOW
) {
190 errln((UnicodeString
)"Too many failures; only the first " +
191 MAX_FAILURES_TO_SHOW
+ " failures were shown");
193 errln((UnicodeString
)"Summary: " + failed
+ " of " + (riwords
.getLineNumber() - 1) +
194 " comparisons failed");
197 logln((UnicodeString
)"Words checked: " + wordCount
);
201 * Odd corner conditions taken from "How to Sort Thai Without Rewriting Sort",
202 * by Doug Cooper, http://seasrc.th.net/paper/thaisort.zip
204 void CollationThaiTest::TestCornerCases(void) {
205 const char* TESTS
[] = {
206 // Shorter words precede longer
207 "\\u0e01", "<", "\\u0e01\\u0e01",
209 // Tone marks are considered after letters (i.e. are primary ignorable)
210 "\\u0e01\\u0e32", "<", "\\u0e01\\u0e49\\u0e32",
212 // ditto for other over-marks
213 "\\u0e01\\u0e32", "<", "\\u0e01\\u0e32\\u0e4c",
215 // commonly used mark-in-context order.
216 // In effect, marks are sorted after each syllable.
217 "\\u0e01\\u0e32\\u0e01\\u0e49\\u0e32", "<", "\\u0e01\\u0e48\\u0e32\\u0e01\\u0e49\\u0e32",
219 // Hyphens and other punctuation follow whitespace but come before letters
220 "\\u0e01\\u0e32", "=", "\\u0e01\\u0e32-",
221 "\\u0e01\\u0e32-", "<", "\\u0e01\\u0e32\\u0e01\\u0e32",
223 // Doubler follows an indentical word without the doubler
224 "\\u0e01\\u0e32", "=", "\\u0e01\\u0e32\\u0e46",
225 "\\u0e01\\u0e32\\u0e46", "<", "\\u0e01\\u0e32\\u0e01\\u0e32",
228 // \\u0e45 after either \\u0e24 or \\u0e26 is treated as a single
229 // combining character, similar to "c < ch" in traditional spanish.
230 // TODO: beef up this case
231 "\\u0e24\\u0e29\\u0e35", "<", "\\u0e24\\u0e45\\u0e29\\u0e35",
232 "\\u0e26\\u0e29\\u0e35", "<", "\\u0e26\\u0e45\\u0e29\\u0e35",
234 // Vowels reorder, should compare \\u0e2d and \\u0e34
235 "\\u0e40\\u0e01\\u0e2d", "<", "\\u0e40\\u0e01\\u0e34",
237 // Tones are compared after the rest of the word (e.g. primary ignorable)
238 "\\u0e01\\u0e32\\u0e01\\u0e48\\u0e32", "<", "\\u0e01\\u0e49\\u0e32\\u0e01\\u0e32",
240 // Periods are ignored entirely
241 "\\u0e01.\\u0e01.", "<", "\\u0e01\\u0e32",
243 const int32_t TESTS_length
= UPRV_LENGTHOF(TESTS
);
246 errln("Error: could not construct Thai collator");
249 compareArray(*coll
, TESTS
, TESTS_length
);
252 //------------------------------------------------------------------------
253 // Internal utilities
254 //------------------------------------------------------------------------
256 void CollationThaiTest::compareArray(Collator
& c
, const char* tests
[],
257 int32_t testsLength
) {
258 for (int32_t i
= 0; i
< testsLength
; i
+= 3) {
260 Collator::EComparisonResult expect
;
261 if (tests
[i
+1][0] == '<') {
262 expect
= Collator::LESS
;
263 } else if (tests
[i
+1][0] == '>') {
264 expect
= Collator::GREATER
;
265 } else if (tests
[i
+1][0] == '=') {
266 expect
= Collator::EQUAL
;
268 // expect = Integer.decode(tests[i+1]).intValue();
269 errln((UnicodeString
)"Error: unknown operator " + tests
[i
+1]);
273 UnicodeString s1
, s2
;
274 parseChars(s1
, tests
[i
]);
275 parseChars(s2
, tests
[i
+2]);
277 doTest(&c
, s1
, s2
, expect
);
279 UErrorCode status
= U_ZERO_ERROR
;
280 int32_t result
= c
.compare(s1
, s2
);
281 if (sign(result
) != sign(expect
))
283 UnicodeString t1
, t2
;
284 errln(UnicodeString("") +
285 i
/3 + ": compare(" + IntlTest::prettify(s1
, t1
)
286 + " , " + IntlTest::prettify(s2
, t2
)
287 + ") got " + result
+ "; expected " + expect
);
290 c
.getCollationKey(s1
, k1
, status
);
291 c
.getCollationKey(s2
, k2
, status
);
292 if (U_FAILURE(status
)) {
293 errln((UnicodeString
)"Fail: getCollationKey returned " + u_errorName(status
));
296 errln((UnicodeString
)" key1: " + prettify(k1
, t1
) );
297 errln((UnicodeString
)" key2: " + prettify(k2
, t2
) );
301 // Collator.compare worked OK; now try the collation keys
303 c
.getCollationKey(s1
, k1
, status
);
304 c
.getCollationKey(s2
, k2
, status
);
305 if (U_FAILURE(status
)) {
306 errln((UnicodeString
)"Fail: getCollationKey returned " + u_errorName(status
));
310 result
= k1
.compareTo(k2
);
311 if (sign(result
) != sign(expect
)) {
312 UnicodeString t1
, t2
;
313 errln(UnicodeString("") +
314 i
/3 + ": key(" + IntlTest::prettify(s1
, t1
)
315 + ").compareTo(key(" + IntlTest::prettify(s2
, t2
)
316 + ")) got " + result
+ "; expected " + expect
);
318 errln((UnicodeString
)" " + prettify(k1
, t1
) + " vs. " + prettify(k2
, t2
));
325 int8_t CollationThaiTest::sign(int32_t i
) {
326 if (i
< 0) return -1;
332 * Set a UnicodeString corresponding to the given string. Use
333 * UnicodeString and the default converter, unless we see the sequence
334 * "\\u", in which case we interpret the subsequent escape.
336 UnicodeString
& CollationThaiTest::parseChars(UnicodeString
& result
,
338 return result
= CharsToUnicodeString(chars
);
341 UCollator
*thaiColl
= NULL
;
344 static int U_CALLCONV
345 StrCmp(const void *p1
, const void *p2
) {
346 return ucol_strcoll(thaiColl
, *(UChar
**) p1
, -1, *(UChar
**)p2
, -1);
353 void CollationThaiTest::TestInvalidThai(void) {
354 const char *tests
[LINES
] = {
355 "\\u0E44\\u0E01\\u0E44\\u0E01",
356 "\\u0E44\\u0E01\\u0E01\\u0E44",
357 "\\u0E01\\u0E44\\u0E01\\u0E44",
358 "\\u0E01\\u0E01\\u0E44\\u0E44",
359 "\\u0E44\\u0E44\\u0E01\\u0E01",
360 "\\u0E01\\u0E44\\u0E44\\u0E01",
363 UChar strings
[LINES
][20];
365 UChar
*toSort
[LINES
];
367 int32_t i
= 0, j
= 0, len
= 0;
369 UErrorCode coll_status
= U_ZERO_ERROR
;
370 UnicodeString iteratorText
;
372 thaiColl
= ucol_open ("th_TH", &coll_status
);
373 if (U_FAILURE(coll_status
)) {
374 errln("Error opening Thai collator: %s", u_errorName(coll_status
));
378 CollationElementIterator
* c
= ((RuleBasedCollator
*)coll
)->createCollationElementIterator( iteratorText
);
380 for(i
= 0; i
< UPRV_LENGTHOF(tests
); i
++) {
381 len
= u_unescape(tests
[i
], strings
[i
], 20);
383 toSort
[i
] = strings
[i
];
386 qsort (toSort
, LINES
, sizeof (UChar
*), StrCmp
);
388 for (i
=0; i
< LINES
; i
++)
391 for (j
=i
+1; j
< LINES
; j
++) {
392 if (ucol_strcoll (thaiColl
, toSort
[i
], -1, toSort
[j
], -1) == UCOL_GREATER
)
394 // inconsistency ordering found!
395 errln("Inconsistent ordering between strings %i and %i", i
, j
);
398 iteratorText
.setTo(toSort
[i
]);
399 c
->setText(iteratorText
, coll_status
);
404 ucol_close(thaiColl
);
408 void CollationThaiTest::TestReordering(void) {
409 // Until UCA 4.1, the collation code swapped Thai/Lao prevowels with the following consonants,
410 // resulting in consonant+prevowel == prevowel+consonant.
411 // From UCA 5.0 on, there are order-reversing contractions for prevowel+consonant.
412 // From UCA 5.0 until UCA 6.1, there was a tertiary difference between
413 // consonant+prevowel and prevowel+consonant.
414 // In UCA 6.2, they compare equal again.
415 // The test was modified to using a collator with strength=secondary,
416 // ignoring possible tertiary differences.
417 const char *tests
[] = {
418 "\\u0E41c\\u0301", "=", "\\u0E41\\u0107", // composition
419 "\\u0E41\\U0001D7CE", "<", "\\u0E41\\U0001D7CF", // supplementaries
420 "\\u0E41\\U0001D15F", "=", "\\u0E41\\U0001D158\\U0001D165", // supplementary composition decomps to supplementary
421 "\\u0E41\\U0002F802", "=", "\\u0E41\\u4E41", // supplementary composition decomps to BMP
422 "\\u0E41\\u0301", "=", "\\u0E41\\u0301", // unsafe (just checking backwards iteration)
423 "\\u0E41\\u0301\\u0316", "=", "\\u0E41\\u0316\\u0301",
425 "\\u0e24\\u0e41", "=", "\\u0e41\\u0e24", // exiting contraction bug
426 "\\u0e3f\\u0e3f\\u0e24\\u0e41", "=", "\\u0e3f\\u0e3f\\u0e41\\u0e24",
428 "abc\\u0E41c\\u0301", "=", "abc\\u0E41\\u0107", // composition
429 "abc\\u0E41\\U0001D000", "<", "abc\\u0E41\\U0001D001", // supplementaries
430 "abc\\u0E41\\U0001D15F", "=", "abc\\u0E41\\U0001D158\\U0001D165", // supplementary composition decomps to supplementary
431 "abc\\u0E41\\U0002F802", "=", "abc\\u0E41\\u4E41", // supplementary composition decomps to BMP
432 "abc\\u0E41\\u0301", "=", "abc\\u0E41\\u0301", // unsafe (just checking backwards iteration)
433 "abc\\u0E41\\u0301\\u0316", "=", "abc\\u0E41\\u0316\\u0301",
435 "\\u0E41c\\u0301abc", "=", "\\u0E41\\u0107abc", // composition
436 "\\u0E41\\U0001D000abc", "<", "\\u0E41\\U0001D001abc", // supplementaries
437 "\\u0E41\\U0001D15Fabc", "=", "\\u0E41\\U0001D158\\U0001D165abc", // supplementary composition decomps to supplementary
438 "\\u0E41\\U0002F802abc", "=", "\\u0E41\\u4E41abc", // supplementary composition decomps to BMP
439 "\\u0E41\\u0301abc", "=", "\\u0E41\\u0301abc", // unsafe (just checking backwards iteration)
440 "\\u0E41\\u0301\\u0316abc", "=", "\\u0E41\\u0316\\u0301abc",
442 "abc\\u0E41c\\u0301abc", "=", "abc\\u0E41\\u0107abc", // composition
443 "abc\\u0E41\\U0001D000abc", "<", "abc\\u0E41\\U0001D001abc", // supplementaries
444 "abc\\u0E41\\U0001D15Fabc", "=", "abc\\u0E41\\U0001D158\\U0001D165abc", // supplementary composition decomps to supplementary
445 "abc\\u0E41\\U0002F802abc", "=", "abc\\u0E41\\u4E41abc", // supplementary composition decomps to BMP
446 "abc\\u0E41\\u0301abc", "=", "abc\\u0E41\\u0301abc", // unsafe (just checking backwards iteration)
447 "abc\\u0E41\\u0301\\u0316abc", "=", "abc\\u0E41\\u0316\\u0301abc",
450 LocalPointer
<Collator
> coll2(coll
->clone());
451 UErrorCode status
= U_ZERO_ERROR
;
452 coll2
->setAttribute(UCOL_STRENGTH
, UCOL_SECONDARY
, status
);
453 if(U_FAILURE(status
)) {
454 errln("Unable to set the Thai collator clone to secondary strength");
457 compareArray(*coll2
, tests
, UPRV_LENGTHOF(tests
));
459 const char *rule
= "& c < ab";
460 const char *testcontraction
[] = { "\\u0E41ab", ">", "\\u0E41c"}; // After UCA 4.1 Thai are normal so won't break a contraction
462 parseChars(rules
, rule
);
463 LocalPointer
<RuleBasedCollator
> rcoll(new RuleBasedCollator(rules
, status
), status
);
464 if(U_SUCCESS(status
)) {
465 compareArray(*rcoll
, testcontraction
, 3);
467 errln("Couldn't instantiate collator from rules");
473 #endif /* #if !UCONFIG_NO_COLLATION */