~ubuntu-branches/ubuntu/quantal/icu/quantal

« back to all changes in this revision

Viewing changes to source/test/intltest/thcoll.cpp

  • Committer: Package Import Robot
  • Author(s): Yves Arrouye
  • Date: 2002-03-03 15:31:13 UTC
  • Revision ID: package-import@ubuntu.com-20020303153113-3ssceqlq45xbmbnc
Tags: upstream-2.0-2.1pre20020303
ImportĀ upstreamĀ versionĀ 2.0-2.1pre20020303

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
**********************************************************************
 
9
*/
 
10
 
 
11
#include "thcoll.h"
 
12
#include "unicode/utypes.h"
 
13
#include "unicode/coll.h"
 
14
#include "unicode/sortkey.h"
 
15
#include "cstring.h"
 
16
#include "filestrm.h"
 
17
 
 
18
/**
 
19
 * The TestDictionary test expects a file of this name, with this
 
20
 * encoding, to be present in the directory $ICU/source/test/testdata.
 
21
 */
 
22
#define TEST_FILE           "th18057.txt"
 
23
#define TEST_FILE_ENCODING  "UTF8"
 
24
 
 
25
/**
 
26
 * This is the most failures we show in TestDictionary.  If this number
 
27
 * is < 0, we show all failures.
 
28
 */
 
29
#define MAX_FAILURES_TO_SHOW 8
 
30
 
 
31
#define CASE(id,test)                 \
 
32
    case id:                          \
 
33
        name = #test;                 \
 
34
        if (exec) {                   \
 
35
            logln(#test "---");       \
 
36
            logln((UnicodeString)""); \
 
37
            test();                   \
 
38
        }                             \
 
39
        break;
 
40
 
 
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);
 
46
    } else {
 
47
        delete coll;
 
48
        coll = 0;
 
49
    }
 
50
}
 
51
 
 
52
CollationThaiTest::~CollationThaiTest() {
 
53
    delete coll;
 
54
}
 
55
 
 
56
void CollationThaiTest::runIndexedTest(int32_t index, UBool exec, const char* &name,
 
57
                                       char* /*par*/) {
 
58
 
 
59
    if((!coll) && exec) {
 
60
      errln(__FILE__ " cannot test - failed to create collator.");
 
61
      name = "";
 
62
      return;
 
63
    }
 
64
 
 
65
    switch (index) {
 
66
        CASE(0,TestDictionary)
 
67
        CASE(1,TestCornerCases)
 
68
        default: name = ""; break;
 
69
    }
 
70
}
 
71
 
 
72
/**
 
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.
 
77
 */
 
78
static UBool readLine(FileStream *in, UnicodeString& line) {
 
79
    if (T_FileStream_eof(in)) {
 
80
        return FALSE;
 
81
    }
 
82
    char buffer[128];
 
83
    char* p = buffer;
 
84
    char* limit = p + sizeof(buffer) - 1; // Leave space for 0
 
85
    while (p<limit) {
 
86
        int c = T_FileStream_getc(in);
 
87
        if (c < 0 || c == 0xD || c == 0xA) {
 
88
            break;
 
89
        }
 
90
        *p++ = c;
 
91
    }
 
92
    *p = 0;
 
93
    line = UnicodeString(buffer, TEST_FILE_ENCODING);
 
94
    return TRUE;
 
95
}
 
96
 
 
97
/**
 
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.
 
101
 */
 
102
void CollationThaiTest::TestDictionary(void) {
 
103
    if (coll == 0) {
 
104
        errln("Error: could not construct Thai collator");
 
105
        return;
 
106
    }
 
107
 
 
108
    // Read in a dictionary of Thai words
 
109
    UErrorCode status = U_ZERO_ERROR;
 
110
    char buffer[1024];
 
111
    uprv_strcpy(buffer,IntlTest::loadTestData(status) );
 
112
    char* index = 0;
 
113
   
 
114
    index=strrchr(buffer,(char)U_FILE_SEP_CHAR);
 
115
 
 
116
    if((unsigned int)(index-buffer) != (strlen(buffer)-1)){
 
117
            *(index+1)=0;
 
118
    }
 
119
    uprv_strcat(buffer,".."U_FILE_SEP_STRING);
 
120
    uprv_strcat(buffer, TEST_FILE);
 
121
 
 
122
    FileStream *in = T_FileStream_open(buffer, "rb");
 
123
    if (in == 0) {
 
124
        errln((UnicodeString)"Error: could not open test file " + buffer);
 
125
        return;        
 
126
    }
 
127
 
 
128
    //
 
129
    // Loop through each word in the dictionary and compare it to the previous
 
130
    // word.  They should be in sorted order.
 
131
    //
 
132
    UnicodeString lastWord, word;
 
133
    int32_t line = 0;
 
134
    int32_t failed = 0;
 
135
    int32_t wordCount = 0;
 
136
    while (readLine(in, word)) {
 
137
        line++;
 
138
 
 
139
        // Skip comments and blank lines
 
140
        if (word.charAt(0) == 0x23 || word.length() == 0) {
 
141
            continue;
 
142
        }
 
143
 
 
144
        // Show the first 8 words being compared, so we can see what's happening
 
145
        ++wordCount;
 
146
        if (wordCount <= 8) {
 
147
            UnicodeString str;
 
148
            logln((UnicodeString)"Word " + wordCount + ": " + IntlTest::prettify(word, str));
 
149
        }
 
150
 
 
151
        if (lastWord.length() > 0) {
 
152
            int32_t result = coll->compare(lastWord, word);
 
153
 
 
154
            if (result >= 0) {
 
155
                failed++;
 
156
                if (MAX_FAILURES_TO_SHOW < 0 || failed <= MAX_FAILURES_TO_SHOW) {
 
157
                    UnicodeString str;
 
158
                    UnicodeString msg =
 
159
                        UnicodeString("--------------------------------------------\n")
 
160
                        + line
 
161
                        + " compare(" + IntlTest::prettify(lastWord, str);
 
162
                    msg += UnicodeString(", ")
 
163
                        + IntlTest::prettify(word, str) + ") returned " + result
 
164
                        + ", expected -1\n";
 
165
                    UErrorCode status = U_ZERO_ERROR;
 
166
                    CollationKey k1, k2;
 
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));
 
171
                        return;
 
172
                    }
 
173
                    msg.append("key1: ").append(prettify(k1, str)).append("\n");
 
174
                    msg.append("key2: ").append(prettify(k2, str));
 
175
                    errln(msg);
 
176
                }
 
177
            }
 
178
        }
 
179
        lastWord = word;
 
180
    }
 
181
 
 
182
    if (failed != 0) {
 
183
        if (failed > MAX_FAILURES_TO_SHOW) {
 
184
            errln((UnicodeString)"Too many failures; only the first " +
 
185
                  MAX_FAILURES_TO_SHOW + " failures were shown");
 
186
        }
 
187
        errln((UnicodeString)"Summary: " + failed + " of " + (line - 1) +
 
188
              " comparisons failed");
 
189
    }
 
190
 
 
191
    logln((UnicodeString)"Words checked: " + wordCount);
 
192
    T_FileStream_close(in);
 
193
}
 
194
 
 
195
/**
 
196
 * Odd corner conditions taken from "How to Sort Thai Without Rewriting Sort",
 
197
 * by Doug Cooper, http://seasrc.th.net/paper/thaisort.zip
 
198
 */
 
199
void CollationThaiTest::TestCornerCases(void) {
 
200
    const char* TESTS[] = {
 
201
        // Shorter words precede longer
 
202
        "\\u0e01",                               "<",    "\\u0e01\\u0e01",
 
203
 
 
204
        // Tone marks are considered after letters (i.e. are primary ignorable)
 
205
        "\\u0e01\\u0e32",                        "<",    "\\u0e01\\u0e49\\u0e32",
 
206
 
 
207
        // ditto for other over-marks
 
208
        "\\u0e01\\u0e32",                        "<",    "\\u0e01\\u0e32\\u0e4c",
 
209
 
 
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",
 
213
 
 
214
        // Hyphens and other punctuation follow whitespace but come before letters
 
215
        "\\u0e01\\u0e32",                        "<",    "\\u0e01\\u0e32-",
 
216
        "\\u0e01\\u0e32-",                       "<",    "\\u0e01\\u0e32\\u0e01\\u0e32",
 
217
 
 
218
        // Doubler follows an indentical word without the doubler
 
219
        "\\u0e01\\u0e32",                        "<",    "\\u0e01\\u0e32\\u0e46",
 
220
        "\\u0e01\\u0e32\\u0e46",                 "<",    "\\u0e01\\u0e32\\u0e01\\u0e32",
 
221
 
 
222
 
 
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",
 
228
 
 
229
        // Vowels reorder, should compare \\u0e2d and \\u0e34
 
230
        "\\u0e40\\u0e01\\u0e2d",                 "<",    "\\u0e40\\u0e01\\u0e34",
 
231
 
 
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",
 
234
 
 
235
        // Periods are ignored entirely
 
236
        "\\u0e01.\\u0e01.",                      "<",    "\\u0e01\\u0e32",
 
237
    };
 
238
    const int32_t TESTS_length = (int32_t)(sizeof(TESTS)/sizeof(TESTS[0]));
 
239
 
 
240
    if (coll == 0) {
 
241
        errln("Error: could not construct Thai collator");
 
242
        return;
 
243
    }
 
244
    compareArray(*coll, TESTS, TESTS_length);
 
245
}
 
246
 
 
247
//------------------------------------------------------------------------
 
248
// Internal utilities
 
249
//------------------------------------------------------------------------
 
250
 
 
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) {
 
255
 
 
256
        int32_t expect = 0;
 
257
        if (tests[i+1][0] == '<') {
 
258
            expect = -1;
 
259
        } else if (tests[i+1][0] == '>') {
 
260
            expect = 1;
 
261
        } else if (tests[i+1][0] == '=') {
 
262
            expect = 0;
 
263
        } else {
 
264
            // expect = Integer.decode(tests[i+1]).intValue();
 
265
            errln((UnicodeString)"Error: unknown operator " + tests[i+1]);
 
266
            return;
 
267
        }
 
268
 
 
269
        UnicodeString s1, s2;
 
270
        parseChars(s1, tests[i]);
 
271
        parseChars(s2, tests[i+2]);
 
272
 
 
273
        int32_t result = c.compare(s1, s2);
 
274
        if (sign(result) != sign(expect))
 
275
        {
 
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);
 
281
 
 
282
            CollationKey k1, k2;
 
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));
 
287
                return;
 
288
            }
 
289
            errln((UnicodeString)"  key1: " + prettify(k1, t1) );
 
290
            errln((UnicodeString)"  key2: " + prettify(k2, t2) );
 
291
        }
 
292
        else
 
293
        {
 
294
            // Collator.compare worked OK; now try the collation keys
 
295
            CollationKey k1, k2;
 
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));
 
300
                return;
 
301
            }
 
302
 
 
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);
 
310
                
 
311
                errln((UnicodeString)"  " + prettify(k1, t1) + " vs. " + prettify(k2, t2));
 
312
            }
 
313
        }
 
314
    }
 
315
}
 
316
 
 
317
int8_t CollationThaiTest::sign(int32_t i) {
 
318
    if (i < 0) return -1;
 
319
    if (i > 0) return 1;
 
320
    return 0;
 
321
}
 
322
 
 
323
/**
 
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.
 
327
 */
 
328
UnicodeString& CollationThaiTest::parseChars(UnicodeString& result,
 
329
                                             const char* chars) {
 
330
    return result = CharsToUnicodeString(chars);
 
331
}