]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/thcoll.cpp
ICU-57166.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / thcoll.cpp
CommitLineData
b75a7d8f
A
1/*
2**********************************************************************
2ca993e8 3* Copyright (C) 1999-2016, International Business Machines
b75a7d8f
A
4* Corporation and others. All Rights Reserved.
5**********************************************************************
6* Date Name Description
7* 12/09/99 aliu Ported from Java.
8**********************************************************************
9*/
10
11#include "unicode/utypes.h"
12
13#if !UCONFIG_NO_COLLATION
14
15#include "thcoll.h"
16#include "unicode/utypes.h"
17#include "unicode/coll.h"
51004dcb 18#include "unicode/localpointer.h"
b75a7d8f 19#include "unicode/sortkey.h"
57a6839d 20#include "unicode/tblcoll.h"
374ca955 21#include "unicode/ustring.h"
2ca993e8 22#include "cmemory.h"
b75a7d8f
A
23#include "cstring.h"
24#include "filestrm.h"
374ca955 25#include "textfile.h"
b75a7d8f
A
26
27/**
28 * The TestDictionary test expects a file of this name, with this
29 * encoding, to be present in the directory $ICU/source/test/testdata.
30 */
374ca955 31//#define TEST_FILE "th18057.txt"
b75a7d8f
A
32
33/**
34 * This is the most failures we show in TestDictionary. If this number
35 * is < 0, we show all failures.
36 */
374ca955 37#define MAX_FAILURES_TO_SHOW -1
b75a7d8f
A
38
39CollationThaiTest::CollationThaiTest() {
40 UErrorCode status = U_ZERO_ERROR;
41 coll = Collator::createInstance(Locale("th", "TH", ""), status);
42 if (coll && U_SUCCESS(status)) {
374ca955 43 //coll->setStrength(Collator::TERTIARY);
b75a7d8f
A
44 } else {
45 delete coll;
46 coll = 0;
47 }
48}
49
50CollationThaiTest::~CollationThaiTest() {
51 delete coll;
52}
53
54void CollationThaiTest::runIndexedTest(int32_t index, UBool exec, const char* &name,
55 char* /*par*/) {
56
57 if((!coll) && exec) {
729e4ab9 58 dataerrln(__FILE__ " cannot test - failed to create collator.");
73c04bcf 59 name = "some test";
b75a7d8f
A
60 return;
61 }
62
63 switch (index) {
374ca955
A
64 TESTCASE(0,TestDictionary);
65 TESTCASE(1,TestCornerCases);
66 TESTCASE(2,TestNamesList);
67 TESTCASE(3,TestInvalidThai);
68 TESTCASE(4,TestReordering);
b75a7d8f
A
69 default: name = ""; break;
70 }
71}
72
b75a7d8f
A
73/**
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.
77 */
78void CollationThaiTest::TestNamesList(void) {
79 if (coll == 0) {
80 errln("Error: could not construct Thai collator");
81 return;
82 }
83
374ca955
A
84 UErrorCode ec = U_ZERO_ERROR;
85 TextFile names("TestNames_Thai.txt", "UTF16LE", ec);
86 if (U_FAILURE(ec)) {
87 logln("Can't open TestNames_Thai.txt: %s; skipping test",
88 u_errorName(ec));
89 return;
b75a7d8f
A
90 }
91
92 //
93 // Loop through each word in the dictionary and compare it to the previous
94 // word. They should be in sorted order.
95 //
96 UnicodeString lastWord, word;
b75a7d8f
A
97 //int32_t failed = 0;
98 int32_t wordCount = 0;
374ca955 99 while (names.readLineSkippingComments(word, ec, FALSE) && U_SUCCESS(ec)) {
b75a7d8f
A
100
101 // Show the first 8 words being compared, so we can see what's happening
102 ++wordCount;
103 if (wordCount <= 8) {
104 UnicodeString str;
105 logln((UnicodeString)"Word " + wordCount + ": " + IntlTest::prettify(word, str));
106 }
107
108 if (lastWord.length() > 0) {
109 Collator::EComparisonResult result = coll->compare(lastWord, word);
110 doTest(coll, lastWord, word, result);
111 }
112 lastWord = word;
113 }
114
374ca955 115 assertSuccess("readLine", ec);
b75a7d8f
A
116
117 logln((UnicodeString)"Words checked: " + wordCount);
b75a7d8f
A
118}
119
120/**
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.
124 */
125void CollationThaiTest::TestDictionary(void) {
126 if (coll == 0) {
127 errln("Error: could not construct Thai collator");
128 return;
129 }
b75a7d8f 130
374ca955
A
131 UErrorCode ec = U_ZERO_ERROR;
132 TextFile riwords("riwords.txt", "UTF8", ec);
133 if (U_FAILURE(ec)) {
134 logln("Can't open riwords.txt: %s; skipping test",
135 u_errorName(ec));
136 return;
b75a7d8f
A
137 }
138
139 //
140 // Loop through each word in the dictionary and compare it to the previous
141 // word. They should be in sorted order.
142 //
143 UnicodeString lastWord, word;
b75a7d8f
A
144 int32_t failed = 0;
145 int32_t wordCount = 0;
374ca955 146 while (riwords.readLineSkippingComments(word, ec, FALSE) && U_SUCCESS(ec)) {
b75a7d8f
A
147
148 // Show the first 8 words being compared, so we can see what's happening
149 ++wordCount;
150 if (wordCount <= 8) {
151 UnicodeString str;
152 logln((UnicodeString)"Word " + wordCount + ": " + IntlTest::prettify(word, str));
153 }
154
155 if (lastWord.length() > 0) {
b75a7d8f
A
156 int32_t result = coll->compare(lastWord, word);
157
51004dcb 158 if (result > 0) {
b75a7d8f
A
159 failed++;
160 if (MAX_FAILURES_TO_SHOW < 0 || failed <= MAX_FAILURES_TO_SHOW) {
161 UnicodeString str;
162 UnicodeString msg =
163 UnicodeString("--------------------------------------------\n")
374ca955 164 + riwords.getLineNumber()
b75a7d8f
A
165 + " compare(" + IntlTest::prettify(lastWord, str);
166 msg += UnicodeString(", ")
167 + IntlTest::prettify(word, str) + ") returned " + result
168 + ", expected -1\n";
169 UErrorCode status = U_ZERO_ERROR;
170 CollationKey k1, k2;
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));
175 return;
176 }
177 msg.append("key1: ").append(prettify(k1, str)).append("\n");
178 msg.append("key2: ").append(prettify(k2, str));
179 errln(msg);
180 }
181 }
182 }
183 lastWord = word;
184 }
185
374ca955
A
186 assertSuccess("readLine", ec);
187
b75a7d8f
A
188 if (failed != 0) {
189 if (failed > MAX_FAILURES_TO_SHOW) {
190 errln((UnicodeString)"Too many failures; only the first " +
191 MAX_FAILURES_TO_SHOW + " failures were shown");
192 }
374ca955 193 errln((UnicodeString)"Summary: " + failed + " of " + (riwords.getLineNumber() - 1) +
b75a7d8f
A
194 " comparisons failed");
195 }
196
197 logln((UnicodeString)"Words checked: " + wordCount);
b75a7d8f
A
198}
199
200/**
201 * Odd corner conditions taken from "How to Sort Thai Without Rewriting Sort",
202 * by Doug Cooper, http://seasrc.th.net/paper/thaisort.zip
203 */
204void CollationThaiTest::TestCornerCases(void) {
205 const char* TESTS[] = {
206 // Shorter words precede longer
207 "\\u0e01", "<", "\\u0e01\\u0e01",
208
209 // Tone marks are considered after letters (i.e. are primary ignorable)
210 "\\u0e01\\u0e32", "<", "\\u0e01\\u0e49\\u0e32",
211
212 // ditto for other over-marks
213 "\\u0e01\\u0e32", "<", "\\u0e01\\u0e32\\u0e4c",
214
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",
218
219 // Hyphens and other punctuation follow whitespace but come before letters
51004dcb 220 "\\u0e01\\u0e32", "=", "\\u0e01\\u0e32-",
b75a7d8f
A
221 "\\u0e01\\u0e32-", "<", "\\u0e01\\u0e32\\u0e01\\u0e32",
222
223 // Doubler follows an indentical word without the doubler
51004dcb 224 "\\u0e01\\u0e32", "=", "\\u0e01\\u0e32\\u0e46",
b75a7d8f
A
225 "\\u0e01\\u0e32\\u0e46", "<", "\\u0e01\\u0e32\\u0e01\\u0e32",
226
227
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",
233
234 // Vowels reorder, should compare \\u0e2d and \\u0e34
235 "\\u0e40\\u0e01\\u0e2d", "<", "\\u0e40\\u0e01\\u0e34",
236
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",
239
240 // Periods are ignored entirely
241 "\\u0e01.\\u0e01.", "<", "\\u0e01\\u0e32",
242 };
2ca993e8 243 const int32_t TESTS_length = UPRV_LENGTHOF(TESTS);
b75a7d8f
A
244
245 if (coll == 0) {
246 errln("Error: could not construct Thai collator");
247 return;
248 }
249 compareArray(*coll, TESTS, TESTS_length);
250}
251
252//------------------------------------------------------------------------
253// Internal utilities
254//------------------------------------------------------------------------
255
374ca955 256void CollationThaiTest::compareArray(Collator& c, const char* tests[],
b75a7d8f 257 int32_t testsLength) {
b75a7d8f
A
258 for (int32_t i = 0; i < testsLength; i += 3) {
259
374ca955 260 Collator::EComparisonResult expect;
b75a7d8f 261 if (tests[i+1][0] == '<') {
374ca955 262 expect = Collator::LESS;
b75a7d8f 263 } else if (tests[i+1][0] == '>') {
374ca955 264 expect = Collator::GREATER;
b75a7d8f 265 } else if (tests[i+1][0] == '=') {
374ca955 266 expect = Collator::EQUAL;
b75a7d8f
A
267 } else {
268 // expect = Integer.decode(tests[i+1]).intValue();
269 errln((UnicodeString)"Error: unknown operator " + tests[i+1]);
270 return;
271 }
272
273 UnicodeString s1, s2;
274 parseChars(s1, tests[i]);
275 parseChars(s2, tests[i+2]);
276
374ca955
A
277 doTest(&c, s1, s2, expect);
278#if 0
279 UErrorCode status = U_ZERO_ERROR;
b75a7d8f
A
280 int32_t result = c.compare(s1, s2);
281 if (sign(result) != sign(expect))
282 {
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);
288
289 CollationKey k1, k2;
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));
294 return;
295 }
296 errln((UnicodeString)" key1: " + prettify(k1, t1) );
297 errln((UnicodeString)" key2: " + prettify(k2, t2) );
298 }
299 else
300 {
301 // Collator.compare worked OK; now try the collation keys
302 CollationKey k1, k2;
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));
307 return;
308 }
309
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);
317
318 errln((UnicodeString)" " + prettify(k1, t1) + " vs. " + prettify(k2, t2));
319 }
320 }
374ca955 321#endif
b75a7d8f
A
322 }
323}
324
325int8_t CollationThaiTest::sign(int32_t i) {
326 if (i < 0) return -1;
327 if (i > 0) return 1;
328 return 0;
329}
330
331/**
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.
335 */
336UnicodeString& CollationThaiTest::parseChars(UnicodeString& result,
337 const char* chars) {
338 return result = CharsToUnicodeString(chars);
339}
340
374ca955
A
341UCollator *thaiColl = NULL;
342
343U_CDECL_BEGIN
344static int U_CALLCONV
345StrCmp(const void *p1, const void *p2) {
346 return ucol_strcoll(thaiColl, *(UChar **) p1, -1, *(UChar **)p2, -1);
347}
348U_CDECL_END
349
350
351#define LINES 6
352
353void 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",
361 };
362
363 UChar strings[LINES][20];
364
365 UChar *toSort[LINES];
366
367 int32_t i = 0, j = 0, len = 0;
368
369 UErrorCode coll_status = U_ZERO_ERROR;
370 UnicodeString iteratorText;
371
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));
375 return;
376 }
377
378 CollationElementIterator* c = ((RuleBasedCollator *)coll)->createCollationElementIterator( iteratorText );
379
2ca993e8 380 for(i = 0; i < UPRV_LENGTHOF(tests); i++) {
374ca955
A
381 len = u_unescape(tests[i], strings[i], 20);
382 strings[i][len] = 0;
383 toSort[i] = strings[i];
384 }
385
386 qsort (toSort, LINES, sizeof (UChar *), StrCmp);
387
388 for (i=0; i < LINES; i++)
389 {
390 logln("%i", i);
391 for (j=i+1; j < LINES; j++) {
392 if (ucol_strcoll (thaiColl, toSort[i], -1, toSort[j], -1) == UCOL_GREATER)
393 {
394 // inconsistency ordering found!
395 errln("Inconsistent ordering between strings %i and %i", i, j);
396 }
397 }
398 iteratorText.setTo(toSort[i]);
399 c->setText(iteratorText, coll_status);
400 backAndForth(*c);
401 }
402
403
404 ucol_close(thaiColl);
405 delete c;
406}
407
408void CollationThaiTest::TestReordering(void) {
51004dcb
A
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",
424
425 "\\u0e24\\u0e41", "=", "\\u0e41\\u0e24", // exiting contraction bug
426 "\\u0e3f\\u0e3f\\u0e24\\u0e41", "=", "\\u0e3f\\u0e3f\\u0e41\\u0e24",
427
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",
434
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",
441
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",
448 };
449
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");
455 return;
456 }
2ca993e8 457 compareArray(*coll2, tests, UPRV_LENGTHOF(tests));
374ca955
A
458
459 const char *rule = "& c < ab";
73c04bcf 460 const char *testcontraction[] = { "\\u0E41ab", ">", "\\u0E41c"}; // After UCA 4.1 Thai are normal so won't break a contraction
374ca955 461 UnicodeString rules;
374ca955 462 parseChars(rules, rule);
b331163b 463 LocalPointer<RuleBasedCollator> rcoll(new RuleBasedCollator(rules, status), status);
374ca955
A
464 if(U_SUCCESS(status)) {
465 compareArray(*rcoll, testcontraction, 3);
374ca955
A
466 } else {
467 errln("Couldn't instantiate collator from rules");
468 }
469
470}
471
472
b75a7d8f 473#endif /* #if !UCONFIG_NO_COLLATION */