]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/g7coll.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1997-2014, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
9 #include "unicode/utypes.h"
11 #if !UCONFIG_NO_COLLATION
13 #include "unicode/coll.h"
14 #include "unicode/tblcoll.h"
15 #include "unicode/unistr.h"
16 #include "unicode/sortkey.h"
21 static const UChar testCases
[][G7CollationTest::MAX_TOKEN_LEN
] = {
22 { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/,
23 0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0073 /*'s'*/, 0x0000}, /* 9 */
24 { 0x0050 /*'P'*/, 0x0061 /*'a'*/, 0x0074/*'t'*/, 0x0000}, /* 1 */
25 { 0x0070 /*'p'*/, 0x00E9, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x00E9, 0x0000}, /* 2 */
26 { 0x0070 /*'p'*/, 0x00EA, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0065 /*'e'*/, 0x0000}, /* 3 */
27 { 0x0070 /*'p'*/, 0x00E9, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0065 /*'e'*/, 0x0072 /*'r'*/, 0x0000}, /* 4 */
28 { 0x0070 /*'p'*/, 0x00EA, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0065 /*'e'*/, 0x0072 /*'r'*/, 0x0000}, /* 5 */
29 { 0x0054 /*'T'*/, 0x006f /*'o'*/, 0x0064 /*'d'*/, 0x0000}, /* 6 */
30 { 0x0054 /*'T'*/, 0x00F6, 0x006e /*'n'*/, 0x0065 /*'e'*/, 0x0000}, /* 7 */
31 { 0x0054 /*'T'*/, 0x006f /*'o'*/, 0x0066 /*'f'*/, 0x0075 /*'u'*/, 0x0000}, /* 8 */
32 { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/,
33 0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0000}, /* 12 */
34 { 0x0054 /*'T'*/, 0x006f /*'o'*/, 0x006e /*'n'*/, 0x0000}, /* 10 */
35 { 0x0050 /*'P'*/, 0x0041 /*'A'*/, 0x0054 /*'T'*/, 0x0000}, /* 11 */
36 { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/,
37 0x002d /*'-'*/, 0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0000}, /* 13 */
38 { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/,
39 0x002d /*'-'*/, 0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0073/*'s'*/, 0x0000}, /* 0 */
40 {0x0070 /*'p'*/, 0x0061 /*'a'*/, 0x0074 /*'t'*/, 0x0000}, /* 14 */
41 /* Additional tests */
42 { 0x0063 /*'c'*/, 0x007a /*'z'*/, 0x0061 /*'a'*/, 0x0072 /*'r'*/, 0x0000 }, /* 15 */
43 { 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0075 /*'u'*/, 0x0072 /*'r'*/, 0x006f /*'o'*/, 0x0000 }, /* 16 */
44 { 0x0063 /*'c'*/, 0x0061 /*'a'*/, 0x0074 /*'t'*/, 0x000 }, /* 17 */
45 { 0x0064 /*'d'*/, 0x0061 /*'a'*/, 0x0072 /*'r'*/, 0x006e /*'n'*/, 0x0000 }, /* 18 */
46 { 0x003f /*'?'*/, 0x0000 }, /* 19 */
47 { 0x0071 /*'q'*/, 0x0075 /*'u'*/, 0x0069 /*'i'*/, 0x0063 /*'c'*/, 0x006b /*'k'*/, 0x0000 }, /* 20 */
48 { 0x0023 /*'#'*/, 0x0000 }, /* 21 */
49 { 0x0026 /*'&'*/, 0x0000 }, /* 22 */
50 { 0x0061 /*'a'*/, 0x002d /*'-'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0076 /*'v'*/, 0x0061 /*'a'*/,
51 0x0072/*'r'*/, 0x006b/*'k'*/, 0x0000}, /* 24 */
52 { 0x0061 /*'a'*/, 0x0061 /*'a'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0076 /*'v'*/, 0x0061 /*'a'*/,
53 0x0072/*'r'*/, 0x006b/*'k'*/, 0x0000}, /* 23 */
54 { 0x0061 /*'a'*/, 0x0062 /*'b'*/, 0x0062 /*'b'*/, 0x006f /*'o'*/, 0x0074 /*'t'*/, 0x0000}, /* 25 */
55 { 0x0063 /*'c'*/, 0x006f /*'o'*/, 0x002d /*'-'*/, 0x0070 /*'p'*/, 0x0000}, /* 27 */
56 { 0x0063 /*'c'*/, 0x006f /*'o'*/, 0x0070 /*'p'*/, 0x0000}, /* 28 */
57 { 0x0063 /*'c'*/, 0x006f /*'o'*/, 0x006f /*'o'*/, 0x0070 /*'p'*/, 0x0000}, /* 26 */
58 { 0x007a /*'z'*/, 0x0065 /*'e'*/, 0x0062 /*'b'*/, 0x0072 /*'r'*/, 0x0061 /*'a'*/, 0x0000} /* 29 */
61 static const int32_t results
[G7CollationTest::TESTLOCALES
][G7CollationTest::TOTALTESTSET
] = {
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 }, /* en_US */
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 }, /* en_GB */
64 { 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 */
65 { 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 }, /* fr_FR */
66 { 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 */
67 { 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 */
68 { 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 */
69 { 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 */
70 /* new table collation with rules "& Z < p, P" loop to FIXEDTESTSET */
71 { 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 },
72 /* new table collation with rules "& C < ch , cH, Ch, CH " loop to TOTALTESTSET */
73 { 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 },
74 /* new table collation with rules "& Question-mark ; ? & Hash-mark ; # & Ampersand ; '&' " loop to TOTALTESTSET */
75 { 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 },
76 /* analogous to Japanese rules " & aa ; a- & ee ; e- & ii ; i- & oo ; o- & uu ; u- " */ /* loop to TOTALTESTSET */
77 { 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 }
80 G7CollationTest::~G7CollationTest() {}
82 void G7CollationTest::TestG7Locales(/* char* par */)
85 const Locale locales
[8] = {
86 Locale("en", "US", ""),
87 Locale("en", "GB", ""),
88 Locale("en", "CA", ""),
89 Locale("fr", "FR", ""),
90 Locale("fr", "CA", ""),
91 Locale("de", "DE", ""),
92 Locale("it", "IT", ""),
93 Locale("ja", "JP", "")
96 for (i
= 0; i
< UPRV_LENGTHOF(locales
); i
++)
98 UnicodeString dispName
;
99 UErrorCode status
= U_ZERO_ERROR
;
101 const Locale
&locale
= locales
[i
];
102 LocalPointer
<Collator
> myCollation(Collator::createInstance(locale
, status
));
103 if(U_FAILURE(status
)) {
104 errcheckln(status
, "Couldn't instantiate collator. Error: %s", u_errorName(status
));
107 myCollation
->setStrength(Collator::QUATERNARY
);
108 myCollation
->setAttribute(UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, status
);
109 if (U_FAILURE(status
)) {
110 errln("Locale %s creation failed - %s", locale
.getName(), u_errorName(status
));
114 const UnicodeString
&rules
= ((RuleBasedCollator
*)myCollation
.getAlias())->getRules();
115 if (rules
.isEmpty() &&
116 (locale
== Locale::getCanadaFrench() || locale
== Locale::getJapanese())) {
117 dataerrln("%s Collator missing rule string", locale
.getName());
118 if (logKnownIssue("10671", "TestG7Locales does not test ignore-punctuation")) {
122 status
= U_ZERO_ERROR
;
123 RuleBasedCollator
*tblColl1
= new RuleBasedCollator(rules
, status
);
124 if (U_FAILURE(status
)) {
125 errln("Recreate %s collation failed - %s", locale
.getName(), u_errorName(status
));
128 myCollation
.adoptInstead(tblColl1
);
134 msg
+= locales
[i
].getDisplayName(dispName
);
135 msg
+= "tests start :";
139 for (j
= 0; j
< FIXEDTESTSET
; j
++)
141 for (n
= j
+1; n
< FIXEDTESTSET
; n
++)
143 doTest(myCollation
.getAlias(), testCases
[results
[i
][j
]], testCases
[results
[i
][n
]], Collator::LESS
);
149 void G7CollationTest::TestDemo1(/* char* par */)
151 logln("Demo Test 1 : Create a new table collation with rules \"& Z < p, P\"");
152 UErrorCode status
= U_ZERO_ERROR
;
153 Collator
*col
= Collator::createInstance("en_US", status
);
154 if(U_FAILURE(status
)) {
156 errcheckln(status
, "Couldn't instantiate collator. Error: %s", u_errorName(status
));
159 const UnicodeString baseRules
= ((RuleBasedCollator
*)col
)->getRules();
160 UnicodeString
newRules(" & Z < p, P");
161 newRules
.insert(0, baseRules
);
162 RuleBasedCollator
*myCollation
= new RuleBasedCollator(newRules
, status
);
164 if (U_FAILURE(status
))
166 errln( "Demo Test 1 Table Collation object creation failed.");
171 for (j
= 0; j
< FIXEDTESTSET
; j
++)
173 for (n
= j
+1; n
< FIXEDTESTSET
; n
++)
175 doTest(myCollation
, testCases
[results
[8][j
]], testCases
[results
[8][n
]], Collator::LESS
);
183 void G7CollationTest::TestDemo2(/* char* par */)
185 logln("Demo Test 2 : Create a new table collation with rules \"& C < ch , cH, Ch, CH\"");
186 UErrorCode status
= U_ZERO_ERROR
;
187 Collator
*col
= Collator::createInstance("en_US", status
);
188 if(U_FAILURE(status
)) {
190 errcheckln(status
, "Couldn't instantiate collator. Error: %s", u_errorName(status
));
193 const UnicodeString baseRules
= ((RuleBasedCollator
*)col
)->getRules();
194 UnicodeString
newRules("& C < ch , cH, Ch, CH");
195 newRules
.insert(0, baseRules
);
196 RuleBasedCollator
*myCollation
= new RuleBasedCollator(newRules
, status
);
198 if (U_FAILURE(status
))
200 errln("Demo Test 2 Table Collation object creation failed.");
205 for (j
= 0; j
< TOTALTESTSET
; j
++)
207 for (n
= j
+1; n
< TOTALTESTSET
; n
++)
209 doTest(myCollation
, testCases
[results
[9][j
]], testCases
[results
[9][n
]], Collator::LESS
);
217 void G7CollationTest::TestDemo3(/* char* par */)
219 logln("Demo Test 3 : Create a new table collation with rules \"& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'\"");
220 UErrorCode status
= U_ZERO_ERROR
;
221 Collator
*col
= Collator::createInstance("en_US", status
);
222 if(U_FAILURE(status
)) {
223 errcheckln(status
, "Couldn't instantiate collator. Error: %s", u_errorName(status
));
227 const UnicodeString baseRules
= ((RuleBasedCollator
*)col
)->getRules();
228 UnicodeString newRules
= "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'";
229 newRules
.insert(0, baseRules
);
230 RuleBasedCollator
*myCollation
= new RuleBasedCollator(newRules
, status
);
232 if (U_FAILURE(status
))
234 errln("Demo Test 3 Table Collation object creation failed.");
239 for (j
= 0; j
< TOTALTESTSET
; j
++)
241 for (n
= j
+1; n
< TOTALTESTSET
; n
++)
243 doTest(myCollation
, testCases
[results
[10][j
]], testCases
[results
[10][n
]], Collator::LESS
);
251 void G7CollationTest::TestDemo4(/* char* par */)
253 logln("Demo Test 4 : Create a new table collation with rules \" & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' \"");
254 UErrorCode status
= U_ZERO_ERROR
;
255 Collator
*col
= Collator::createInstance("en_US", status
);
256 if(U_FAILURE(status
)) {
258 errcheckln(status
, "Couldn't instantiate collator. Error: %s", u_errorName(status
));
262 const UnicodeString baseRules
= ((RuleBasedCollator
*)col
)->getRules();
263 UnicodeString newRules
= " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' ";
264 newRules
.insert(0, baseRules
);
265 RuleBasedCollator
*myCollation
= new RuleBasedCollator(newRules
, status
);
268 for (j
= 0; j
< TOTALTESTSET
; j
++)
270 for (n
= j
+1; n
< TOTALTESTSET
; n
++)
272 doTest(myCollation
, testCases
[results
[11][j
]], testCases
[results
[11][n
]], Collator::LESS
);
280 void G7CollationTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
282 if (exec
) logln("TestSuite G7CollationTest: ");
284 case 0: name
= "TestG7Locales"; if (exec
) TestG7Locales(/* par */); break;
285 case 1: name
= "TestDemo1"; if (exec
) TestDemo1(/* par */); break;
286 case 2: name
= "TestDemo2"; if (exec
) TestDemo2(/* par */); break;
287 case 3: name
= "TestDemo3"; if (exec
) TestDemo3(/* par */); break;
288 case 4: name
= "TestDemo4"; if (exec
) TestDemo4(/* par */); break;
289 default: name
= ""; break;
293 #endif /* #if !UCONFIG_NO_COLLATION */