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

« back to all changes in this revision

Viewing changes to source/test/intltest/g7coll.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
 * COPYRIGHT: 
 
3
 * Copyright (c) 1997-2001, International Business Machines Corporation and
 
4
 * others. All Rights Reserved.
 
5
 ********************************************************************/
 
6
 
 
7
#include "unicode/coll.h"
 
8
#include "unicode/tblcoll.h"
 
9
#include "unicode/unistr.h"
 
10
#include "unicode/sortkey.h"
 
11
#include "g7coll.h"
 
12
#include "sfwdchit.h"
 
13
 
 
14
 
 
15
const UChar G7CollationTest::testCases[][G7CollationTest::MAX_TOKEN_LEN] = {
 
16
    {  0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/, 
 
17
        0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0073 /*'s'*/, 0x0000},                    /* 9 */
 
18
    { 0x0050 /*'P'*/, 0x0061 /*'a'*/, 0x0074/*'t'*/, 0x0000},                                                    /* 1 */
 
19
    { 0x0070 /*'p'*/, 0x00E9, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x00E9, 0x0000},                                    /* 2 */
 
20
    { 0x0070 /*'p'*/, 0x00EA, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0065 /*'e'*/, 0x0000},                           /* 3 */
 
21
    { 0x0070 /*'p'*/, 0x00E9, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0065 /*'e'*/, 0x0072 /*'r'*/, 0x0000},            /* 4 */
 
22
    { 0x0070 /*'p'*/, 0x00EA, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0065 /*'e'*/, 0x0072 /*'r'*/, 0x0000},            /* 5 */
 
23
    { 0x0054 /*'T'*/, 0x006f /*'o'*/, 0x0064 /*'d'*/, 0x0000},                                                    /* 6 */
 
24
    { 0x0054 /*'T'*/, 0x00F6, 0x006e /*'n'*/, 0x0065 /*'e'*/, 0x0000},                                            /* 7 */
 
25
    { 0x0054 /*'T'*/, 0x006f /*'o'*/, 0x0066 /*'f'*/, 0x0075 /*'u'*/, 0x0000},                                   /* 8 */
 
26
    { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/, 
 
27
      0x0062  /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0000},                                    /* 12 */
 
28
    { 0x0054 /*'T'*/, 0x006f /*'o'*/, 0x006e /*'n'*/, 0x0000},                                                    /* 10 */
 
29
    { 0x0050  /*'P'*/, 0x0041 /*'A'*/, 0x0054 /*'T'*/, 0x0000},                                                    /* 11 */
 
30
    { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/, 
 
31
        0x002d /*'-'*/,  0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0000},                /* 13 */
 
32
    { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/, 
 
33
        0x002d /*'-'*/,  0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0073/*'s'*/, 0x0000},  /* 0 */
 
34
    {0x0070 /*'p'*/, 0x0061 /*'a'*/, 0x0074 /*'t'*/, 0x0000},                                                    /* 14 */
 
35
    /* Additional tests */
 
36
    { 0x0063 /*'c'*/, 0x007a /*'z'*/, 0x0061 /*'a'*/, 0x0072 /*'r'*/, 0x0000 },                                 /* 15 */
 
37
    { 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0075 /*'u'*/, 0x0072 /*'r'*/, 0x006f /*'o'*/, 0x0000 },                  /* 16 */
 
38
    { 0x0063 /*'c'*/, 0x0061 /*'a'*/, 0x0074 /*'t'*/, 0x000 },                                                    /* 17 */ 
 
39
    { 0x0064 /*'d'*/, 0x0061 /*'a'*/, 0x0072 /*'r'*/, 0x006e /*'n'*/, 0x0000 },                                 /* 18 */
 
40
    { 0x003f /*'?'*/, 0x0000 },                                                                                /* 19 */
 
41
    { 0x0071 /*'q'*/, 0x0075 /*'u'*/, 0x0069 /*'i'*/, 0x0063 /*'c'*/, 0x006b /*'k'*/, 0x0000 },                  /* 20 */
 
42
    { 0x0023 /*'#'*/, 0x0000 },                                                                                /* 21 */
 
43
    { 0x0026 /*'&'*/, 0x0000 },                                                                                /* 22 */
 
44
    {  0x0061 /*'a'*/, 0x002d /*'-'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0076 /*'v'*/, 0x0061 /*'a'*/, 
 
45
                0x0072/*'r'*/, 0x006b/*'k'*/, 0x0000},                                                        /* 24 */
 
46
    { 0x0061 /*'a'*/, 0x0061 /*'a'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0076 /*'v'*/, 0x0061 /*'a'*/, 
 
47
                0x0072/*'r'*/, 0x006b/*'k'*/, 0x0000},                                                        /* 23 */
 
48
    { 0x0061 /*'a'*/, 0x0062 /*'b'*/, 0x0062 /*'b'*/, 0x006f /*'o'*/, 0x0074 /*'t'*/, 0x0000},                   /* 25 */
 
49
    { 0x0063 /*'c'*/, 0x006f /*'o'*/, 0x002d /*'-'*/, 0x0070 /*'p'*/, 0x0000},                                 /* 27 */
 
50
    { 0x0063 /*'c'*/, 0x006f  /*'o'*/, 0x0070 /*'p'*/, 0x0000},                                                /* 28 */
 
51
    { 0x0063 /*'c'*/, 0x006f /*'o'*/, 0x006f /*'o'*/, 0x0070 /*'p'*/, 0x0000},                                 /* 26 */
 
52
    { 0x007a /*'z'*/, 0x0065  /*'e'*/, 0x0062 /*'b'*/, 0x0072 /*'r'*/, 0x0061 /*'a'*/, 0x0000}                    /* 29 */
 
53
};
 
54
 
 
55
const int32_t G7CollationTest::results[G7CollationTest::TESTLOCALES][G7CollationTest::TOTALTESTSET] = {
 
56
    { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* en_US */
 
57
    { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* en_GB */
 
58
    { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* en_CA */
 
59
    { 12, 13, 9, 0, 14, 1, 11, 3, 2, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* fr_FR */
 
60
    { 12, 13, 9, 0, 14, 1, 11, 3, 2, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* fr_CA */
 
61
    { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* de_DE */
 
62
    { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* it_IT */
 
63
    { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* ja_JP */
 
64
    /* new table collation with rules "& Z < p, P"  loop to FIXEDTESTSET */
 
65
    { 12, 13, 9, 0, 6, 8, 10, 7, 14, 1, 11, 2, 3, 4, 5, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, 
 
66
    /* new table collation with rules "& C < ch , cH, Ch, CH " loop to TOTALTESTSET */
 
67
    { 19, 22, 21, 23, 24, 25, 12, 13, 9, 0, 17, 26, 28, 27, 15, 16, 18, 14, 1, 11, 2, 3, 4, 5, 20, 6, 8, 10, 7, 29 },
 
68
    /* new table collation with rules "& Question-mark ; ? & Hash-mark ; # & Ampersand ; '&'  " loop to TOTALTESTSET */
 
69
    { 23, 24, 25, 22, 12, 13, 9, 0, 17, 16, 26, 28, 27, 15, 18, 21, 14, 1, 11, 2, 3, 4, 5, 19, 20, 6, 8, 10, 7, 29 },
 
70
    /* analogous to Japanese rules " & aa ; a- & ee ; e- & ii ; i- & oo ; o- & uu ; u- " */  /* loop to TOTALTESTSET */
 
71
    { 19, 22, 21, 24, 23, 25, 12, 13, 9, 0, 17, 16, 28, 26, 27, 15, 18, 14, 1, 11, 2, 3, 4, 5, 20, 6, 8, 10, 7, 29 }
 
72
};
 
73
 
 
74
 
 
75
void G7CollationTest::doTest( Collator* myCollation, UnicodeString source, UnicodeString target, Collator::EComparisonResult result)
 
76
{
 
77
    Collator::EComparisonResult compareResult = myCollation->compare(source, target);
 
78
    CollationKey sortKey1, sortKey2;
 
79
    UErrorCode key1status = U_ZERO_ERROR, key2status = U_ZERO_ERROR; //nos
 
80
    myCollation->getCollationKey(source, /*nos*/ sortKey1, key1status );
 
81
    myCollation->getCollationKey(target, /*nos*/ sortKey2, key2status );
 
82
    if (U_FAILURE(key1status) || U_FAILURE(key2status)) {
 
83
        errln("SortKey generation Failed.\n");
 
84
        return;
 
85
    }
 
86
    Collator::EComparisonResult keyResult = sortKey1.compareTo(sortKey2);
 
87
    reportCResult( source, target, sortKey1, sortKey2, compareResult, keyResult, compareResult, result );
 
88
}
 
89
 
 
90
void G7CollationTest::TestG7Locales(/* char* par */)
 
91
{
 
92
    int32_t i;
 
93
    const Locale locales[8] = {
 
94
        Locale("en", "US", ""),
 
95
        Locale("en", "GB", ""),
 
96
        Locale("en", "CA", ""),
 
97
        Locale("fr", "FR", ""),
 
98
        Locale("fr", "CA", ""),
 
99
        Locale("de", "DE", ""),
 
100
        Locale("it", "IT", ""),
 
101
        Locale("ja", "JP", "")
 
102
    };
 
103
 
 
104
 
 
105
    for (i = 0; i < 8; i++)
 
106
    {
 
107
        Collator *myCollation= 0;
 
108
        UnicodeString dispName;
 
109
        UErrorCode status = U_ZERO_ERROR;
 
110
        RuleBasedCollator* tblColl1 = 0;
 
111
 
 
112
        myCollation = Collator::createInstance(locales[i], status);
 
113
        myCollation->setStrength(Collator::QUATERNARY);
 
114
        myCollation->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, status);
 
115
        if (U_FAILURE(status))
 
116
        {
 
117
            UnicodeString msg;
 
118
 
 
119
            msg += "Locale ";
 
120
            msg += locales[i].getDisplayName(dispName);
 
121
            msg += "creation failed.";
 
122
 
 
123
            errln(msg);
 
124
            continue;
 
125
        }
 
126
 
 
127
//        const UnicodeString& defRules = ((RuleBasedCollator*)myCollation)->getRules();
 
128
        status = U_ZERO_ERROR;
 
129
        tblColl1 = new RuleBasedCollator(((RuleBasedCollator*)myCollation)->getRules(), status);
 
130
        if (U_FAILURE(status))
 
131
        {
 
132
            UnicodeString msg, name;
 
133
 
 
134
            msg += "Recreate ";
 
135
            msg += locales[i].getDisplayName(name);
 
136
            msg += "collation failed.";
 
137
 
 
138
            errln(msg);
 
139
            continue;
 
140
        }
 
141
 
 
142
        UnicodeString msg;
 
143
 
 
144
        msg += "Locale ";
 
145
        msg += locales[i].getDisplayName(dispName);
 
146
        msg += "tests start :";
 
147
        logln(msg);
 
148
 
 
149
        int32_t j, n;
 
150
        for (j = 0; j < FIXEDTESTSET; j++)
 
151
        {
 
152
            for (n = j+1; n < FIXEDTESTSET; n++)
 
153
            {
 
154
                doTest(tblColl1, testCases[results[i][j]], testCases[results[i][n]], Collator::LESS);
 
155
            }
 
156
        }
 
157
 
 
158
        delete myCollation;
 
159
        delete tblColl1;
 
160
    }
 
161
}
 
162
 
 
163
void G7CollationTest::TestDemo1(/* char* par */)
 
164
{
 
165
    logln("Demo Test 1 : Create a new table collation with rules \"& Z < p, P\"");
 
166
    UErrorCode status = U_ZERO_ERROR;
 
167
    Collator *col = Collator::createInstance("en_US", status);
 
168
    const UnicodeString baseRules = ((RuleBasedCollator*)col)->getRules();
 
169
    UnicodeString newRules(" & Z < p, P");
 
170
    newRules.insert(0, baseRules);
 
171
    RuleBasedCollator *myCollation = new RuleBasedCollator(newRules, status);
 
172
 
 
173
    if (U_FAILURE(status))
 
174
    {
 
175
        errln( "Demo Test 1 Table Collation object creation failed.");
 
176
        return;
 
177
    }
 
178
 
 
179
    int32_t j, n;
 
180
    for (j = 0; j < FIXEDTESTSET; j++)
 
181
    {
 
182
        for (n = j+1; n < FIXEDTESTSET; n++)
 
183
        {
 
184
            doTest(myCollation, testCases[results[8][j]], testCases[results[8][n]], Collator::LESS);
 
185
        }
 
186
    }
 
187
 
 
188
    delete myCollation; 
 
189
    delete col;
 
190
}
 
191
 
 
192
void G7CollationTest::TestDemo2(/* char* par */)
 
193
{
 
194
    logln("Demo Test 2 : Create a new table collation with rules \"& C < ch , cH, Ch, CH\"");
 
195
    UErrorCode status = U_ZERO_ERROR;
 
196
    Collator *col = Collator::createInstance("en_US", status);
 
197
    const UnicodeString baseRules = ((RuleBasedCollator*)col)->getRules();
 
198
    UnicodeString newRules("& C < ch , cH, Ch, CH");
 
199
    newRules.insert(0, baseRules);
 
200
    RuleBasedCollator *myCollation = new RuleBasedCollator(newRules, status);
 
201
 
 
202
    if (U_FAILURE(status))
 
203
    {
 
204
        errln("Demo Test 2 Table Collation object creation failed.");
 
205
        return;
 
206
    }
 
207
 
 
208
    int32_t j, n;
 
209
    for (j = 0; j < TOTALTESTSET; j++)
 
210
    {
 
211
        for (n = j+1; n < TOTALTESTSET; n++)
 
212
        {
 
213
            doTest(myCollation, testCases[results[9][j]], testCases[results[9][n]], Collator::LESS);
 
214
        }
 
215
    }
 
216
    
 
217
    delete myCollation; 
 
218
    delete col;
 
219
}
 
220
 
 
221
void G7CollationTest::TestDemo3(/* char* par */)
 
222
{
 
223
    logln("Demo Test 3 : Create a new table collation with rules \"& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'\"");
 
224
    UErrorCode status = U_ZERO_ERROR;
 
225
    Collator *col = Collator::createInstance("en_US", status);
 
226
    const UnicodeString baseRules = ((RuleBasedCollator*)col)->getRules();
 
227
    UnicodeString newRules = "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'";
 
228
    newRules.insert(0, baseRules);
 
229
    RuleBasedCollator *myCollation = new RuleBasedCollator(newRules, status);
 
230
 
 
231
    if (U_FAILURE(status))
 
232
    {
 
233
        errln("Demo Test 3 Table Collation object creation failed.");
 
234
        return;
 
235
    }
 
236
 
 
237
    int32_t j, n;
 
238
    for (j = 0; j < TOTALTESTSET; j++)
 
239
    {
 
240
        for (n = j+1; n < TOTALTESTSET; n++)
 
241
        {
 
242
            doTest(myCollation, testCases[results[10][j]], testCases[results[10][n]], Collator::LESS);
 
243
        }
 
244
    }
 
245
    
 
246
    delete myCollation; 
 
247
    delete col;
 
248
}
 
249
 
 
250
void G7CollationTest::TestDemo4(/* char* par */)
 
251
{
 
252
    logln("Demo Test 4 : Create a new table collation with rules \" & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' \"");
 
253
    UErrorCode status = U_ZERO_ERROR;
 
254
    Collator *col = Collator::createInstance("en_US", status);
 
255
    const UnicodeString baseRules = ((RuleBasedCollator*)col)->getRules();
 
256
    UnicodeString newRules = " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' ";
 
257
    newRules.insert(0, baseRules);
 
258
    RuleBasedCollator *myCollation = new RuleBasedCollator(newRules, status);
 
259
 
 
260
    if (U_FAILURE(status))
 
261
    {
 
262
        errln( "Demo Test 4 Table Collation object creation failed." );
 
263
        return;
 
264
    }
 
265
 
 
266
    int32_t j, n;
 
267
    for (j = 0; j < TOTALTESTSET; j++)
 
268
    {
 
269
        for (n = j+1; n < TOTALTESTSET; n++)
 
270
        {
 
271
            doTest(myCollation, testCases[results[11][j]], testCases[results[11][n]], Collator::LESS);
 
272
        }
 
273
    }
 
274
 
 
275
    delete myCollation; 
 
276
    delete col;
 
277
}
 
278
 
 
279
void G7CollationTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
 
280
{
 
281
    if (exec) logln("TestSuite G7CollationTest: ");
 
282
    switch (index) {
 
283
        case 0: name = "TestG7Locales"; if (exec)   TestG7Locales(/* par */); break;
 
284
        case 1: name = "TestDemo1"; if (exec)   TestDemo1(/* par */); break;
 
285
        case 2: name = "TestDemo2"; if (exec)   TestDemo2(/* par */); break;
 
286
        case 3: name = "TestDemo3"; if (exec)   TestDemo3(/* par */); break;
 
287
        case 4: name = "TestDemo4"; if (exec)   TestDemo4(/* par */); break;
 
288
        default: name = ""; break;
 
289
    }
 
290
}
 
291
 
 
292