2
**********************************************************************
3
* Copyright (C) 2001, International Business Machines
4
* Corporation and others. All Rights Reserved.
5
**********************************************************************
6
* Date Name Description
7
* 12/09/99 aliu Ported from Java.
8
**********************************************************************
12
#include "unicode/utypes.h"
13
#include "unicode/coll.h"
14
#include "unicode/sortkey.h"
19
* The TestDictionary test expects a file of this name, with this
20
* encoding, to be present in the directory $ICU/source/test/testdata.
22
#define TEST_FILE "th18057.txt"
23
#define TEST_FILE_ENCODING "UTF8"
26
* This is the most failures we show in TestDictionary. If this number
27
* is < 0, we show all failures.
29
#define MAX_FAILURES_TO_SHOW 8
31
#define CASE(id,test) \
36
logln((UnicodeString)""); \
41
CollationThaiTest::CollationThaiTest() {
42
UErrorCode status = U_ZERO_ERROR;
43
coll = Collator::createInstance(Locale("th", "TH", ""), status);
44
if (coll && U_SUCCESS(status)) {
45
coll->setStrength(Collator::TERTIARY);
52
CollationThaiTest::~CollationThaiTest() {
56
void CollationThaiTest::runIndexedTest(int32_t index, UBool exec, const char* &name,
60
errln(__FILE__ " cannot test - failed to create collator.");
66
CASE(0,TestDictionary)
67
CASE(1,TestCornerCases)
68
default: name = ""; break;
73
* Read a line terminated by a single ^J or ^M, and convert it from
74
* the TEST_FILE_ENCODING to Unicode. ASSUMES FILE LINES ARE 127
75
* characters long or less. This is true for th18057.txt, which
76
* has 80-char or shorter lines. DOES NOT HANDLE ^M^J sequence.
78
static UBool readLine(FileStream *in, UnicodeString& line) {
79
if (T_FileStream_eof(in)) {
84
char* limit = p + sizeof(buffer) - 1; // Leave space for 0
86
int c = T_FileStream_getc(in);
87
if (c < 0 || c == 0xD || c == 0xA) {
93
line = UnicodeString(buffer, TEST_FILE_ENCODING);
98
* Read the external dictionary file, which is already in proper
99
* sorted order, and confirm that the collator compares each line as
100
* preceding the following line.
102
void CollationThaiTest::TestDictionary(void) {
104
errln("Error: could not construct Thai collator");
108
// Read in a dictionary of Thai words
109
UErrorCode status = U_ZERO_ERROR;
111
uprv_strcpy(buffer,IntlTest::loadTestData(status) );
114
index=strrchr(buffer,(char)U_FILE_SEP_CHAR);
116
if((unsigned int)(index-buffer) != (strlen(buffer)-1)){
119
uprv_strcat(buffer,".."U_FILE_SEP_STRING);
120
uprv_strcat(buffer, TEST_FILE);
122
FileStream *in = T_FileStream_open(buffer, "rb");
124
errln((UnicodeString)"Error: could not open test file " + buffer);
129
// Loop through each word in the dictionary and compare it to the previous
130
// word. They should be in sorted order.
132
UnicodeString lastWord, word;
135
int32_t wordCount = 0;
136
while (readLine(in, word)) {
139
// Skip comments and blank lines
140
if (word.charAt(0) == 0x23 || word.length() == 0) {
144
// Show the first 8 words being compared, so we can see what's happening
146
if (wordCount <= 8) {
148
logln((UnicodeString)"Word " + wordCount + ": " + IntlTest::prettify(word, str));
151
if (lastWord.length() > 0) {
152
int32_t result = coll->compare(lastWord, word);
156
if (MAX_FAILURES_TO_SHOW < 0 || failed <= MAX_FAILURES_TO_SHOW) {
159
UnicodeString("--------------------------------------------\n")
161
+ " compare(" + IntlTest::prettify(lastWord, str);
162
msg += UnicodeString(", ")
163
+ IntlTest::prettify(word, str) + ") returned " + result
165
UErrorCode status = U_ZERO_ERROR;
167
coll->getCollationKey(lastWord, k1, status);
168
coll->getCollationKey(word, k2, status);
169
if (U_FAILURE(status)) {
170
errln((UnicodeString)"Fail: getCollationKey returned " + u_errorName(status));
173
msg.append("key1: ").append(prettify(k1, str)).append("\n");
174
msg.append("key2: ").append(prettify(k2, str));
183
if (failed > MAX_FAILURES_TO_SHOW) {
184
errln((UnicodeString)"Too many failures; only the first " +
185
MAX_FAILURES_TO_SHOW + " failures were shown");
187
errln((UnicodeString)"Summary: " + failed + " of " + (line - 1) +
188
" comparisons failed");
191
logln((UnicodeString)"Words checked: " + wordCount);
192
T_FileStream_close(in);
196
* Odd corner conditions taken from "How to Sort Thai Without Rewriting Sort",
197
* by Doug Cooper, http://seasrc.th.net/paper/thaisort.zip
199
void CollationThaiTest::TestCornerCases(void) {
200
const char* TESTS[] = {
201
// Shorter words precede longer
202
"\\u0e01", "<", "\\u0e01\\u0e01",
204
// Tone marks are considered after letters (i.e. are primary ignorable)
205
"\\u0e01\\u0e32", "<", "\\u0e01\\u0e49\\u0e32",
207
// ditto for other over-marks
208
"\\u0e01\\u0e32", "<", "\\u0e01\\u0e32\\u0e4c",
210
// commonly used mark-in-context order.
211
// In effect, marks are sorted after each syllable.
212
"\\u0e01\\u0e32\\u0e01\\u0e49\\u0e32", "<", "\\u0e01\\u0e48\\u0e32\\u0e01\\u0e49\\u0e32",
214
// Hyphens and other punctuation follow whitespace but come before letters
215
"\\u0e01\\u0e32", "<", "\\u0e01\\u0e32-",
216
"\\u0e01\\u0e32-", "<", "\\u0e01\\u0e32\\u0e01\\u0e32",
218
// Doubler follows an indentical word without the doubler
219
"\\u0e01\\u0e32", "<", "\\u0e01\\u0e32\\u0e46",
220
"\\u0e01\\u0e32\\u0e46", "<", "\\u0e01\\u0e32\\u0e01\\u0e32",
223
// \\u0e45 after either \\u0e24 or \\u0e26 is treated as a single
224
// combining character, similar to "c < ch" in traditional spanish.
225
// TODO: beef up this case
226
"\\u0e24\\u0e29\\u0e35", "<", "\\u0e24\\u0e45\\u0e29\\u0e35",
227
"\\u0e26\\u0e29\\u0e35", "<", "\\u0e26\\u0e45\\u0e29\\u0e35",
229
// Vowels reorder, should compare \\u0e2d and \\u0e34
230
"\\u0e40\\u0e01\\u0e2d", "<", "\\u0e40\\u0e01\\u0e34",
232
// Tones are compared after the rest of the word (e.g. primary ignorable)
233
"\\u0e01\\u0e32\\u0e01\\u0e48\\u0e32", "<", "\\u0e01\\u0e49\\u0e32\\u0e01\\u0e32",
235
// Periods are ignored entirely
236
"\\u0e01.\\u0e01.", "<", "\\u0e01\\u0e32",
238
const int32_t TESTS_length = (int32_t)(sizeof(TESTS)/sizeof(TESTS[0]));
241
errln("Error: could not construct Thai collator");
244
compareArray(*coll, TESTS, TESTS_length);
247
//------------------------------------------------------------------------
248
// Internal utilities
249
//------------------------------------------------------------------------
251
void CollationThaiTest::compareArray(const Collator& c, const char* tests[],
252
int32_t testsLength) {
253
UErrorCode status = U_ZERO_ERROR;
254
for (int32_t i = 0; i < testsLength; i += 3) {
257
if (tests[i+1][0] == '<') {
259
} else if (tests[i+1][0] == '>') {
261
} else if (tests[i+1][0] == '=') {
264
// expect = Integer.decode(tests[i+1]).intValue();
265
errln((UnicodeString)"Error: unknown operator " + tests[i+1]);
269
UnicodeString s1, s2;
270
parseChars(s1, tests[i]);
271
parseChars(s2, tests[i+2]);
273
int32_t result = c.compare(s1, s2);
274
if (sign(result) != sign(expect))
276
UnicodeString t1, t2;
277
errln(UnicodeString("") +
278
i/3 + ": compare(" + IntlTest::prettify(s1, t1)
279
+ " , " + IntlTest::prettify(s2, t2)
280
+ ") got " + result + "; expected " + expect);
283
c.getCollationKey(s1, k1, status);
284
c.getCollationKey(s2, k2, status);
285
if (U_FAILURE(status)) {
286
errln((UnicodeString)"Fail: getCollationKey returned " + u_errorName(status));
289
errln((UnicodeString)" key1: " + prettify(k1, t1) );
290
errln((UnicodeString)" key2: " + prettify(k2, t2) );
294
// Collator.compare worked OK; now try the collation keys
296
c.getCollationKey(s1, k1, status);
297
c.getCollationKey(s2, k2, status);
298
if (U_FAILURE(status)) {
299
errln((UnicodeString)"Fail: getCollationKey returned " + u_errorName(status));
303
result = k1.compareTo(k2);
304
if (sign(result) != sign(expect)) {
305
UnicodeString t1, t2;
306
errln(UnicodeString("") +
307
i/3 + ": key(" + IntlTest::prettify(s1, t1)
308
+ ").compareTo(key(" + IntlTest::prettify(s2, t2)
309
+ ")) got " + result + "; expected " + expect);
311
errln((UnicodeString)" " + prettify(k1, t1) + " vs. " + prettify(k2, t2));
317
int8_t CollationThaiTest::sign(int32_t i) {
318
if (i < 0) return -1;
324
* Set a UnicodeString corresponding to the given string. Use
325
* UnicodeString and the default converter, unless we see the sequence
326
* "\\u", in which case we interpret the subsequent escape.
328
UnicodeString& CollationThaiTest::parseChars(UnicodeString& result,
330
return result = CharsToUnicodeString(chars);