1
/********************************************************************
3
* Copyright (c) 1997-2001, International Business Machines Corporation and
4
* others. All Rights Reserved.
5
********************************************************************/
7
#include "unicode/coll.h"
8
#include "unicode/tblcoll.h"
9
#include "unicode/unistr.h"
10
#include "unicode/sortkey.h"
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 */
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 }
75
void G7CollationTest::doTest( Collator* myCollation, UnicodeString source, UnicodeString target, Collator::EComparisonResult result)
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");
86
Collator::EComparisonResult keyResult = sortKey1.compareTo(sortKey2);
87
reportCResult( source, target, sortKey1, sortKey2, compareResult, keyResult, compareResult, result );
90
void G7CollationTest::TestG7Locales(/* char* par */)
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", "")
105
for (i = 0; i < 8; i++)
107
Collator *myCollation= 0;
108
UnicodeString dispName;
109
UErrorCode status = U_ZERO_ERROR;
110
RuleBasedCollator* tblColl1 = 0;
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))
120
msg += locales[i].getDisplayName(dispName);
121
msg += "creation failed.";
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))
132
UnicodeString msg, name;
135
msg += locales[i].getDisplayName(name);
136
msg += "collation failed.";
145
msg += locales[i].getDisplayName(dispName);
146
msg += "tests start :";
150
for (j = 0; j < FIXEDTESTSET; j++)
152
for (n = j+1; n < FIXEDTESTSET; n++)
154
doTest(tblColl1, testCases[results[i][j]], testCases[results[i][n]], Collator::LESS);
163
void G7CollationTest::TestDemo1(/* char* par */)
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);
173
if (U_FAILURE(status))
175
errln( "Demo Test 1 Table Collation object creation failed.");
180
for (j = 0; j < FIXEDTESTSET; j++)
182
for (n = j+1; n < FIXEDTESTSET; n++)
184
doTest(myCollation, testCases[results[8][j]], testCases[results[8][n]], Collator::LESS);
192
void G7CollationTest::TestDemo2(/* char* par */)
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);
202
if (U_FAILURE(status))
204
errln("Demo Test 2 Table Collation object creation failed.");
209
for (j = 0; j < TOTALTESTSET; j++)
211
for (n = j+1; n < TOTALTESTSET; n++)
213
doTest(myCollation, testCases[results[9][j]], testCases[results[9][n]], Collator::LESS);
221
void G7CollationTest::TestDemo3(/* char* par */)
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);
231
if (U_FAILURE(status))
233
errln("Demo Test 3 Table Collation object creation failed.");
238
for (j = 0; j < TOTALTESTSET; j++)
240
for (n = j+1; n < TOTALTESTSET; n++)
242
doTest(myCollation, testCases[results[10][j]], testCases[results[10][n]], Collator::LESS);
250
void G7CollationTest::TestDemo4(/* char* par */)
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);
260
if (U_FAILURE(status))
262
errln( "Demo Test 4 Table Collation object creation failed." );
267
for (j = 0; j < TOTALTESTSET; j++)
269
for (n = j+1; n < TOTALTESTSET; n++)
271
doTest(myCollation, testCases[results[11][j]], testCases[results[11][n]], Collator::LESS);
279
void G7CollationTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
281
if (exec) logln("TestSuite G7CollationTest: ");
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;