2 **********************************************************************
3 * Copyright (C) 1999-2009, 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/sortkey.h"
19 #include "unicode/ustring.h"
25 * The TestDictionary test expects a file of this name, with this
26 * encoding, to be present in the directory $ICU/source/test/testdata.
28 //#define TEST_FILE "th18057.txt"
31 * This is the most failures we show in TestDictionary. If this number
32 * is < 0, we show all failures.
34 #define MAX_FAILURES_TO_SHOW -1
36 CollationThaiTest::CollationThaiTest() {
37 UErrorCode status
= U_ZERO_ERROR
;
38 coll
= Collator::createInstance(Locale("th", "TH", ""), status
);
39 if (coll
&& U_SUCCESS(status
)) {
40 //coll->setStrength(Collator::TERTIARY);
47 CollationThaiTest::~CollationThaiTest() {
51 void CollationThaiTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
,
55 dataerrln(__FILE__
" cannot test - failed to create collator.");
61 TESTCASE(0,TestDictionary
);
62 TESTCASE(1,TestCornerCases
);
63 TESTCASE(2,TestNamesList
);
64 TESTCASE(3,TestInvalidThai
);
65 TESTCASE(4,TestReordering
);
66 default: name
= ""; break;
71 * Read the external names list, and confirms that the collator
72 * gets the same results when comparing lines one to another
73 * using regular and iterative comparison.
75 void CollationThaiTest::TestNamesList(void) {
77 errln("Error: could not construct Thai collator");
81 UErrorCode ec
= U_ZERO_ERROR
;
82 TextFile
names("TestNames_Thai.txt", "UTF16LE", ec
);
84 logln("Can't open TestNames_Thai.txt: %s; skipping test",
90 // Loop through each word in the dictionary and compare it to the previous
91 // word. They should be in sorted order.
93 UnicodeString lastWord
, word
;
95 int32_t wordCount
= 0;
96 while (names
.readLineSkippingComments(word
, ec
, FALSE
) && U_SUCCESS(ec
)) {
98 // Show the first 8 words being compared, so we can see what's happening
100 if (wordCount
<= 8) {
102 logln((UnicodeString
)"Word " + wordCount
+ ": " + IntlTest::prettify(word
, str
));
105 if (lastWord
.length() > 0) {
106 Collator::EComparisonResult result
= coll
->compare(lastWord
, word
);
107 doTest(coll
, lastWord
, word
, result
);
112 assertSuccess("readLine", ec
);
114 logln((UnicodeString
)"Words checked: " + wordCount
);
118 * Read the external dictionary file, which is already in proper
119 * sorted order, and confirm that the collator compares each line as
120 * preceding the following line.
122 void CollationThaiTest::TestDictionary(void) {
124 errln("Error: could not construct Thai collator");
128 UErrorCode ec
= U_ZERO_ERROR
;
129 TextFile
riwords("riwords.txt", "UTF8", ec
);
131 logln("Can't open riwords.txt: %s; skipping test",
137 // Loop through each word in the dictionary and compare it to the previous
138 // word. They should be in sorted order.
140 UnicodeString lastWord
, word
;
142 int32_t wordCount
= 0;
143 while (riwords
.readLineSkippingComments(word
, ec
, FALSE
) && U_SUCCESS(ec
)) {
145 // Show the first 8 words being compared, so we can see what's happening
147 if (wordCount
<= 8) {
149 logln((UnicodeString
)"Word " + wordCount
+ ": " + IntlTest::prettify(word
, str
));
152 if (lastWord
.length() > 0) {
153 // line enabled for j2720
154 doTest(coll
, lastWord
, word
, Collator::LESS
);
155 int32_t result
= coll
->compare(lastWord
, word
);
159 if (MAX_FAILURES_TO_SHOW
< 0 || failed
<= MAX_FAILURES_TO_SHOW
) {
162 UnicodeString("--------------------------------------------\n")
163 + riwords
.getLineNumber()
164 + " compare(" + IntlTest::prettify(lastWord
, str
);
165 msg
+= UnicodeString(", ")
166 + IntlTest::prettify(word
, str
) + ") returned " + result
168 UErrorCode status
= U_ZERO_ERROR
;
170 coll
->getCollationKey(lastWord
, k1
, status
);
171 coll
->getCollationKey(word
, k2
, status
);
172 if (U_FAILURE(status
)) {
173 errln((UnicodeString
)"Fail: getCollationKey returned " + u_errorName(status
));
176 msg
.append("key1: ").append(prettify(k1
, str
)).append("\n");
177 msg
.append("key2: ").append(prettify(k2
, str
));
185 assertSuccess("readLine", ec
);
188 if (failed
> MAX_FAILURES_TO_SHOW
) {
189 errln((UnicodeString
)"Too many failures; only the first " +
190 MAX_FAILURES_TO_SHOW
+ " failures were shown");
192 errln((UnicodeString
)"Summary: " + failed
+ " of " + (riwords
.getLineNumber() - 1) +
193 " comparisons failed");
196 logln((UnicodeString
)"Words checked: " + wordCount
);
200 * Odd corner conditions taken from "How to Sort Thai Without Rewriting Sort",
201 * by Doug Cooper, http://seasrc.th.net/paper/thaisort.zip
203 void CollationThaiTest::TestCornerCases(void) {
204 const char* TESTS
[] = {
205 // Shorter words precede longer
206 "\\u0e01", "<", "\\u0e01\\u0e01",
208 // Tone marks are considered after letters (i.e. are primary ignorable)
209 "\\u0e01\\u0e32", "<", "\\u0e01\\u0e49\\u0e32",
211 // ditto for other over-marks
212 "\\u0e01\\u0e32", "<", "\\u0e01\\u0e32\\u0e4c",
214 // commonly used mark-in-context order.
215 // In effect, marks are sorted after each syllable.
216 "\\u0e01\\u0e32\\u0e01\\u0e49\\u0e32", "<", "\\u0e01\\u0e48\\u0e32\\u0e01\\u0e49\\u0e32",
218 // Hyphens and other punctuation follow whitespace but come before letters
219 "\\u0e01\\u0e32", "<", "\\u0e01\\u0e32-",
220 "\\u0e01\\u0e32-", "<", "\\u0e01\\u0e32\\u0e01\\u0e32",
222 // Doubler follows an indentical word without the doubler
223 "\\u0e01\\u0e32", "<", "\\u0e01\\u0e32\\u0e46",
224 "\\u0e01\\u0e32\\u0e46", "<", "\\u0e01\\u0e32\\u0e01\\u0e32",
227 // \\u0e45 after either \\u0e24 or \\u0e26 is treated as a single
228 // combining character, similar to "c < ch" in traditional spanish.
229 // TODO: beef up this case
230 "\\u0e24\\u0e29\\u0e35", "<", "\\u0e24\\u0e45\\u0e29\\u0e35",
231 "\\u0e26\\u0e29\\u0e35", "<", "\\u0e26\\u0e45\\u0e29\\u0e35",
233 // Vowels reorder, should compare \\u0e2d and \\u0e34
234 "\\u0e40\\u0e01\\u0e2d", "<", "\\u0e40\\u0e01\\u0e34",
236 // Tones are compared after the rest of the word (e.g. primary ignorable)
237 "\\u0e01\\u0e32\\u0e01\\u0e48\\u0e32", "<", "\\u0e01\\u0e49\\u0e32\\u0e01\\u0e32",
239 // Periods are ignored entirely
240 "\\u0e01.\\u0e01.", "<", "\\u0e01\\u0e32",
242 const int32_t TESTS_length
= (int32_t)(sizeof(TESTS
)/sizeof(TESTS
[0]));
245 errln("Error: could not construct Thai collator");
248 compareArray(*coll
, TESTS
, TESTS_length
);
251 //------------------------------------------------------------------------
252 // Internal utilities
253 //------------------------------------------------------------------------
255 void CollationThaiTest::compareArray(Collator
& c
, const char* tests
[],
256 int32_t testsLength
) {
257 for (int32_t i
= 0; i
< testsLength
; i
+= 3) {
259 Collator::EComparisonResult expect
;
260 if (tests
[i
+1][0] == '<') {
261 expect
= Collator::LESS
;
262 } else if (tests
[i
+1][0] == '>') {
263 expect
= Collator::GREATER
;
264 } else if (tests
[i
+1][0] == '=') {
265 expect
= Collator::EQUAL
;
267 // expect = Integer.decode(tests[i+1]).intValue();
268 errln((UnicodeString
)"Error: unknown operator " + tests
[i
+1]);
272 UnicodeString s1
, s2
;
273 parseChars(s1
, tests
[i
]);
274 parseChars(s2
, tests
[i
+2]);
276 doTest(&c
, s1
, s2
, expect
);
278 UErrorCode status
= U_ZERO_ERROR
;
279 int32_t result
= c
.compare(s1
, s2
);
280 if (sign(result
) != sign(expect
))
282 UnicodeString t1
, t2
;
283 errln(UnicodeString("") +
284 i
/3 + ": compare(" + IntlTest::prettify(s1
, t1
)
285 + " , " + IntlTest::prettify(s2
, t2
)
286 + ") got " + result
+ "; expected " + expect
);
289 c
.getCollationKey(s1
, k1
, status
);
290 c
.getCollationKey(s2
, k2
, status
);
291 if (U_FAILURE(status
)) {
292 errln((UnicodeString
)"Fail: getCollationKey returned " + u_errorName(status
));
295 errln((UnicodeString
)" key1: " + prettify(k1
, t1
) );
296 errln((UnicodeString
)" key2: " + prettify(k2
, t2
) );
300 // Collator.compare worked OK; now try the collation keys
302 c
.getCollationKey(s1
, k1
, status
);
303 c
.getCollationKey(s2
, k2
, status
);
304 if (U_FAILURE(status
)) {
305 errln((UnicodeString
)"Fail: getCollationKey returned " + u_errorName(status
));
309 result
= k1
.compareTo(k2
);
310 if (sign(result
) != sign(expect
)) {
311 UnicodeString t1
, t2
;
312 errln(UnicodeString("") +
313 i
/3 + ": key(" + IntlTest::prettify(s1
, t1
)
314 + ").compareTo(key(" + IntlTest::prettify(s2
, t2
)
315 + ")) got " + result
+ "; expected " + expect
);
317 errln((UnicodeString
)" " + prettify(k1
, t1
) + " vs. " + prettify(k2
, t2
));
324 int8_t CollationThaiTest::sign(int32_t i
) {
325 if (i
< 0) return -1;
331 * Set a UnicodeString corresponding to the given string. Use
332 * UnicodeString and the default converter, unless we see the sequence
333 * "\\u", in which case we interpret the subsequent escape.
335 UnicodeString
& CollationThaiTest::parseChars(UnicodeString
& result
,
337 return result
= CharsToUnicodeString(chars
);
340 UCollator
*thaiColl
= NULL
;
343 static int U_CALLCONV
344 StrCmp(const void *p1
, const void *p2
) {
345 return ucol_strcoll(thaiColl
, *(UChar
**) p1
, -1, *(UChar
**)p2
, -1);
352 void CollationThaiTest::TestInvalidThai(void) {
353 const char *tests
[LINES
] = {
354 "\\u0E44\\u0E01\\u0E44\\u0E01",
355 "\\u0E44\\u0E01\\u0E01\\u0E44",
356 "\\u0E01\\u0E44\\u0E01\\u0E44",
357 "\\u0E01\\u0E01\\u0E44\\u0E44",
358 "\\u0E44\\u0E44\\u0E01\\u0E01",
359 "\\u0E01\\u0E44\\u0E44\\u0E01",
362 UChar strings
[LINES
][20];
364 UChar
*toSort
[LINES
];
366 int32_t i
= 0, j
= 0, len
= 0;
368 UErrorCode coll_status
= U_ZERO_ERROR
;
369 UnicodeString iteratorText
;
371 thaiColl
= ucol_open ("th_TH", &coll_status
);
372 if (U_FAILURE(coll_status
)) {
373 errln("Error opening Thai collator: %s", u_errorName(coll_status
));
377 CollationElementIterator
* c
= ((RuleBasedCollator
*)coll
)->createCollationElementIterator( iteratorText
);
379 for(i
= 0; i
< (int32_t)(sizeof(tests
)/sizeof(tests
[0])); i
++) {
380 len
= u_unescape(tests
[i
], strings
[i
], 20);
382 toSort
[i
] = strings
[i
];
385 qsort (toSort
, LINES
, sizeof (UChar
*), StrCmp
);
387 for (i
=0; i
< LINES
; i
++)
390 for (j
=i
+1; j
< LINES
; j
++) {
391 if (ucol_strcoll (thaiColl
, toSort
[i
], -1, toSort
[j
], -1) == UCOL_GREATER
)
393 // inconsistency ordering found!
394 errln("Inconsistent ordering between strings %i and %i", i
, j
);
397 iteratorText
.setTo(toSort
[i
]);
398 c
->setText(iteratorText
, coll_status
);
403 ucol_close(thaiColl
);
407 void CollationThaiTest::TestReordering(void) {
408 const char *tests
[] = {
409 "\\u0E41c\\u0301", "=", "\\u0E41\\u0107", // composition
410 "\\u0E41\\uD835\\uDFCE", "<", "\\u0E41\\uD835\\uDFCF", // supplementaries
411 "\\u0E41\\uD834\\uDD5F", "=", "\\u0E41\\uD834\\uDD58\\uD834\\uDD65", // supplementary composition decomps to supplementary
412 "\\u0E41\\uD87E\\uDC02", "=", "\\u0E41\\u4E41", // supplementary composition decomps to BMP
413 "\\u0E41\\u0301", "=", "\\u0E41\\u0301", // unsafe (just checking backwards iteration)
414 "\\u0E41\\u0301\\u0316", "=", "\\u0E41\\u0316\\u0301",
415 // after UCA 4.1, the two lines below are not equal anymore do not have equal sign
416 "\\u0e24\\u0e41", "<", "\\u0e41\\u0e24", // exiting contraction bug
417 "\\u0e3f\\u0e3f\\u0e24\\u0e41", "<", "\\u0e3f\\u0e3f\\u0e41\\u0e24",
419 "abc\\u0E41c\\u0301", "=", "abc\\u0E41\\u0107", // composition
420 "abc\\u0E41\\uD834\\uDC00", "<", "abc\\u0E41\\uD834\\uDC01", // supplementaries
421 "abc\\u0E41\\uD834\\uDD5F", "=", "abc\\u0E41\\uD834\\uDD58\\uD834\\uDD65", // supplementary composition decomps to supplementary
422 "abc\\u0E41\\uD87E\\uDC02", "=", "abc\\u0E41\\u4E41", // supplementary composition decomps to BMP
423 "abc\\u0E41\\u0301", "=", "abc\\u0E41\\u0301", // unsafe (just checking backwards iteration)
424 "abc\\u0E41\\u0301\\u0316", "=", "abc\\u0E41\\u0316\\u0301",
426 "\\u0E41c\\u0301abc", "=", "\\u0E41\\u0107abc", // composition
427 "\\u0E41\\uD834\\uDC00abc", "<", "\\u0E41\\uD834\\uDC01abc", // supplementaries
428 "\\u0E41\\uD834\\uDD5Fabc", "=", "\\u0E41\\uD834\\uDD58\\uD834\\uDD65abc", // supplementary composition decomps to supplementary
429 "\\u0E41\\uD87E\\uDC02abc", "=", "\\u0E41\\u4E41abc", // supplementary composition decomps to BMP
430 "\\u0E41\\u0301abc", "=", "\\u0E41\\u0301abc", // unsafe (just checking backwards iteration)
431 "\\u0E41\\u0301\\u0316abc", "=", "\\u0E41\\u0316\\u0301abc",
433 "abc\\u0E41c\\u0301abc", "=", "abc\\u0E41\\u0107abc", // composition
434 "abc\\u0E41\\uD834\\uDC00abc", "<", "abc\\u0E41\\uD834\\uDC01abc", // supplementaries
435 "abc\\u0E41\\uD834\\uDD5Fabc", "=", "abc\\u0E41\\uD834\\uDD58\\uD834\\uDD65abc", // supplementary composition decomps to supplementary
436 "abc\\u0E41\\uD87E\\uDC02abc", "=", "abc\\u0E41\\u4E41abc", // supplementary composition decomps to BMP
437 "abc\\u0E41\\u0301abc", "=", "abc\\u0E41\\u0301abc", // unsafe (just checking backwards iteration)
438 "abc\\u0E41\\u0301\\u0316abc", "=", "abc\\u0E41\\u0316\\u0301abc",
441 compareArray(*coll
, tests
, sizeof(tests
)/sizeof(tests
[0]));
443 const char *rule
= "& c < ab";
444 const char *testcontraction
[] = { "\\u0E41ab", ">", "\\u0E41c"}; // After UCA 4.1 Thai are normal so won't break a contraction
446 UErrorCode status
= U_ZERO_ERROR
;
447 parseChars(rules
, rule
);
448 RuleBasedCollator
*rcoll
= new RuleBasedCollator(rules
, status
);
449 if(U_SUCCESS(status
)) {
450 compareArray(*rcoll
, testcontraction
, 3);
453 errln("Couldn't instantiate collator from rules");
459 #endif /* #if !UCONFIG_NO_COLLATION */