]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/cg7coll.c
1 /********************************************************************
3 * Copyright (c) 1997-2014, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /********************************************************************************
10 * Modification History:
12 * Madhu Katragadda Ported for C API
13 *********************************************************************************/
15 * G7CollationTest is a third level test class. This test performs the examples
16 * mentioned on the IBM Java international demos web site.
17 * Sample Rules: & Z < p , P
18 * Effect : Making P sort after Z.
20 * Sample Rules: & c < ch , cH, Ch, CH
21 * Effect : As well as adding sequences of characters that act as a single character (this is
22 * known as contraction), you can also add characters that act like a sequence of
23 * characters (this is known as expansion).
25 * Sample Rules: & Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'
26 * Effect : Expansion and contraction can actually be combined.
28 * Sample Rules: & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-'
29 * Effect : sorted sequence as the following,
41 #include "unicode/utypes.h"
43 #if !UCONFIG_NO_COLLATION
45 #include "unicode/ucol.h"
46 #include "unicode/uloc.h"
51 #include "unicode/ustring.h"
53 const char* locales
[8] = {
66 const static UChar testCases
[][MAX_TOKEN_LEN
] = {
67 { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/,
68 0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0073 /*'s'*/, 0x0000}, /* 9 */
69 { 0x0050 /*'P'*/, 0x0061 /*'a'*/, 0x0074/*'t'*/, 0x0000}, /* 1 */
70 { 0x0070 /*'p'*/, 0x00E9, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x00E9, 0x0000}, /* 2 */
71 { 0x0070 /*'p'*/, 0x00EA, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0065 /*'e'*/, 0x0000}, /* 3 */
72 { 0x0070 /*'p'*/, 0x00E9, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0065 /*'e'*/, 0x0072 /*'r'*/, 0x0000}, /* 4 */
73 { 0x0070 /*'p'*/, 0x00EA, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0065 /*'e'*/, 0x0072 /*'r'*/, 0x0000}, /* 5 */
74 { 0x0054 /*'T'*/, 0x006f /*'o'*/, 0x0064 /*'d'*/, 0x0000}, /* 6 */
75 { 0x0054 /*'T'*/, 0x00F6, 0x006e /*'n'*/, 0x0065 /*'e'*/, 0x0000}, /* 7 */
76 { 0x0054 /*'T'*/, 0x006f /*'o'*/, 0x0066 /*'f'*/, 0x0075 /*'u'*/, 0x0000}, /* 8 */
77 { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/,
78 0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0000}, /* 12 */
79 { 0x0054 /*'T'*/, 0x006f /*'o'*/, 0x006e /*'n'*/, 0x0000}, /* 10 */
80 { 0x0050 /*'P'*/, 0x0041 /*'A'*/, 0x0054 /*'T'*/, 0x0000}, /* 11 */
81 { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/,
82 0x002d /*'-'*/, 0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0000}, /* 13 */
83 { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/,
84 0x002d /*'-'*/, 0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0073/*'s'*/, 0x0000}, /* 0 */
85 {0x0070 /*'p'*/, 0x0061 /*'a'*/, 0x0074 /*'t'*/, 0x0000}, /* 14 */
86 /* Additional tests */
87 { 0x0063 /*'c'*/, 0x007a /*'z'*/, 0x0061 /*'a'*/, 0x0072 /*'r'*/, 0x0000 }, /* 15 */
88 { 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0075 /*'u'*/, 0x0072 /*'r'*/, 0x006f /*'o'*/, 0x0000 }, /* 16 */
89 { 0x0063 /*'c'*/, 0x0061 /*'a'*/, 0x0074 /*'t'*/, 0x000 }, /* 17 */
90 { 0x0064 /*'d'*/, 0x0061 /*'a'*/, 0x0072 /*'r'*/, 0x006e /*'n'*/, 0x0000 }, /* 18 */
91 { 0x003f /*'?'*/, 0x0000 }, /* 19 */
92 { 0x0071 /*'q'*/, 0x0075 /*'u'*/, 0x0069 /*'i'*/, 0x0063 /*'c'*/, 0x006b /*'k'*/, 0x0000 }, /* 20 */
93 { 0x0023 /*'#'*/, 0x0000 }, /* 21 */
94 { 0x0026 /*'&'*/, 0x0000 }, /* 22 */
95 { 0x0061 /*'a'*/, 0x002d /*'-'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0076 /*'v'*/, 0x0061 /*'a'*/,
96 0x0072/*'r'*/, 0x006b/*'k'*/, 0x0000}, /* 24 */
97 { 0x0061 /*'a'*/, 0x0061 /*'a'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0076 /*'v'*/, 0x0061 /*'a'*/,
98 0x0072/*'r'*/, 0x006b/*'k'*/, 0x0000}, /* 23 */
99 { 0x0061 /*'a'*/, 0x0062 /*'b'*/, 0x0062 /*'b'*/, 0x006f /*'o'*/, 0x0074 /*'t'*/, 0x0000}, /* 25 */
100 { 0x0063 /*'c'*/, 0x006f /*'o'*/, 0x002d /*'-'*/, 0x0070 /*'p'*/, 0x0000}, /* 27 */
101 { 0x0063 /*'c'*/, 0x006f /*'o'*/, 0x0070 /*'p'*/, 0x0000}, /* 28 */
102 { 0x0063 /*'c'*/, 0x006f /*'o'*/, 0x006f /*'o'*/, 0x0070 /*'p'*/, 0x0000}, /* 26 */
103 { 0x007a /*'z'*/, 0x0065 /*'e'*/, 0x0062 /*'b'*/, 0x0072 /*'r'*/, 0x0061 /*'a'*/, 0x0000} /* 29 */
106 const static int32_t results
[TESTLOCALES
][TOTALTESTSET
] = {
107 { 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 */
108 { 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 */
109 { 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 */
110 { 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 */
111 { 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 */
112 { 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 */
113 { 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 */
114 { 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 */
115 /* new table collation with rules "& Z < p, P" loop to FIXEDTESTSET */
116 { 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 },
117 /* new table collation with rules "& C < ch , cH, Ch, CH " loop to TOTALTESTSET */
118 { 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 },
119 /* new table collation with rules "& Question-mark ; ? & Hash-mark ; # & Ampersand ; '&' " loop to TOTALTESTSET */
120 { 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 },
121 /* analogous to Japanese rules " & aa ; a- & ee ; e- & ii ; i- & oo ; o- & uu ; u- " */ /* loop to TOTALTESTSET */
122 { 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 }
125 void addRuleBasedCollTest(TestNode
** root
)
127 addTest(root
, &TestG7Locales
, "tscoll/cg7coll/TestG7Locales");
128 addTest(root
, &TestDemo1
, "tscoll/cg7coll/TestDemo1");
129 addTest(root
, &TestDemo2
, "tscoll/cg7coll/TestDemo2");
130 addTest(root
, &TestDemo3
, "tscoll/cg7coll/TestDemo3");
131 addTest(root
, &TestDemo4
, "tscoll/cg7coll/TestDemo4");
136 static void TestG7Locales()
138 UCollator
*myCollation
;
139 UErrorCode status
= U_ZERO_ERROR
;
140 const UChar
*defRules
;
141 int32_t i
, rlen
, j
, n
;
142 log_verbose("Testing ucol_openRules for all the locales\n");
143 for (i
= 0; i
< UPRV_LENGTHOF(locales
); i
++)
145 const char *locale
= locales
[i
];
146 status
= U_ZERO_ERROR
;
147 myCollation
= ucol_open(locale
, &status
);
148 ucol_setAttribute(myCollation
, UCOL_STRENGTH
, UCOL_QUATERNARY
, &status
);
149 ucol_setAttribute(myCollation
, UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, &status
);
151 if (U_FAILURE(status
))
153 log_err_status(status
, "Error in creating collator in %s: %s\n", locale
, myErrorName(status
));
154 ucol_close(myCollation
);
158 defRules
= ucol_getRules(myCollation
, &rlen
);
159 if (rlen
== 0 && (strcmp(locale
, "fr_CA") == 0 || strcmp(locale
, "ja_JP") == 0)) {
160 log_data_err("%s UCollator missing rule string\n", locale
);
161 if (log_knownIssue("10671", "TestG7Locales does not test ignore-punctuation")) {
162 ucol_close(myCollation
);
167 status
= U_ZERO_ERROR
;
168 tblColl1
= ucol_openRules(defRules
, rlen
, UCOL_OFF
,
169 UCOL_DEFAULT_STRENGTH
,NULL
, &status
);
170 ucol_close(myCollation
);
171 if (U_FAILURE(status
))
173 log_err_status(status
, "Error in creating collator in %s: %s\n", locale
, myErrorName(status
));
176 myCollation
= tblColl1
;
179 log_verbose("Locale %s\n", locales
[i
]);
180 log_verbose(" tests start...\n");
184 for (j
= 0; j
< FIXEDTESTSET
; j
++)
186 for (n
= j
+1; n
< FIXEDTESTSET
; n
++)
188 doTest(myCollation
, testCases
[results
[i
][j
]], testCases
[results
[i
][n
]], UCOL_LESS
);
192 ucol_close(myCollation
);
196 static void TestDemo1()
198 UCollator
*myCollation
;
200 static const char rules
[] = "& Z < p, P";
201 int32_t len
=(int32_t)strlen(rules
);
202 UChar temp
[sizeof(rules
)];
203 UErrorCode status
= U_ZERO_ERROR
;
204 u_uastrcpy(temp
, rules
);
206 log_verbose("Demo Test 1 : Create a new table collation with rules \" & Z < p, P \" \n");
208 myCollation
= ucol_openRules(temp
, len
, UCOL_OFF
, UCOL_DEFAULT_STRENGTH
,NULL
, &status
);
210 if (U_FAILURE(status
))
212 log_err_status(status
, "Demo Test 1 Rule collation object creation failed. : %s\n", myErrorName(status
));
216 for (j
= 0; j
< FIXEDTESTSET
; j
++)
218 for (n
= j
+1; n
< FIXEDTESTSET
; n
++)
220 doTest(myCollation
, testCases
[results
[8][j
]], testCases
[results
[8][n
]], UCOL_LESS
);
224 ucol_close(myCollation
);
227 static void TestDemo2()
229 UCollator
*myCollation
;
231 static const char rules
[] = "& C < ch , cH, Ch, CH";
232 int32_t len
=(int32_t)strlen(rules
);
233 UChar temp
[sizeof(rules
)];
234 UErrorCode status
= U_ZERO_ERROR
;
235 u_uastrcpy(temp
, rules
);
237 log_verbose("Demo Test 2 : Create a new table collation with rules \"& C < ch , cH, Ch, CH\"");
239 myCollation
= ucol_openRules(temp
, len
, UCOL_OFF
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
241 if (U_FAILURE(status
))
243 log_err_status(status
, "Demo Test 2 Rule collation object creation failed.: %s\n", myErrorName(status
));
246 for (j
= 0; j
< TOTALTESTSET
; j
++)
248 for (n
= j
+1; n
< TOTALTESTSET
; n
++)
250 doTest(myCollation
, testCases
[results
[9][j
]], testCases
[results
[9][n
]], UCOL_LESS
);
253 ucol_close(myCollation
);
256 static void TestDemo3()
258 UCollator
*myCollation
;
260 static const char rules
[] = "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'";
261 int32_t len
=(int32_t)strlen(rules
);
262 UChar temp
[sizeof(rules
)];
263 UErrorCode status
= U_ZERO_ERROR
;
264 u_uastrcpy(temp
, rules
);
266 log_verbose("Demo Test 3 : Create a new table collation with rules \"& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'\" \n");
268 myCollation
= ucol_openRules(temp
, len
, UCOL_OFF
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
270 if (U_FAILURE(status
))
272 log_err_status(status
, "Demo Test 3 Rule collation object creation failed.: %s\n", myErrorName(status
));
276 for (j
= 0; j
< TOTALTESTSET
; j
++)
278 for (n
= j
+1; n
< TOTALTESTSET
; n
++)
280 doTest(myCollation
, testCases
[results
[10][j
]], testCases
[results
[10][n
]], UCOL_LESS
);
283 ucol_close(myCollation
);
286 static void TestDemo4()
288 UCollator
*myCollation
;
290 static const char rules
[] = " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' ";
291 int32_t len
=(int32_t)strlen(rules
);
292 UChar temp
[sizeof(rules
)];
293 UErrorCode status
= U_ZERO_ERROR
;
294 u_uastrcpy(temp
, rules
);
296 log_verbose("Demo Test 4 : Create a new table collation with rules \" & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' \"\n");
298 myCollation
= ucol_openRules(temp
, len
, UCOL_OFF
, UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
300 if (U_FAILURE(status
))
302 log_err_status(status
, "Demo Test 4 Rule collation object creation failed.: %s\n", myErrorName(status
));
305 for (j
= 0; j
< TOTALTESTSET
; j
++)
307 for (n
= j
+1; n
< TOTALTESTSET
; n
++)
309 doTest(myCollation
, testCases
[results
[11][j
]], testCases
[results
[11][n
]], UCOL_LESS
);
312 ucol_close(myCollation
);
315 #endif /* #if !UCONFIG_NO_COLLATION */