]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/cg7coll.c
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cg7coll.c
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2001, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6/********************************************************************************
7*
8* File CG7COLL.C
9*
10* Modification History:
11* Name Description
12* Madhu Katragadda Ported for C API
13*********************************************************************************/
14/**
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.
19 *
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).
24 *
25 * Sample Rules: & Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'
26 * Effect : Expansion and contraction can actually be combined.
27 *
28 * Sample Rules: & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-'
29 * Effect : sorted sequence as the following,
30 * aardvark
31 * a-rdvark
32 * abbot
33 * coop
34 * co-p
35 * cop
36 */
37#include <stdlib.h>
38#include <string.h>
39#include <stdio.h>
40
41#include "unicode/utypes.h"
42
43#if !UCONFIG_NO_COLLATION
44
45#include "unicode/ucol.h"
46#include "unicode/uloc.h"
47#include "cintltst.h"
48#include "cg7coll.h"
49#include "ccolltst.h"
50#include "callcoll.h"
51#include "unicode/ustring.h"
52
53
54const char* locales[8] = {
55 "en_US",
56 "en_GB",
57 "en_CA",
58 "fr_FR",
59 "fr_CA",
60 "de_DE",
61 "it_IT",
62 "ja_JP"
63};
64
65
66
67const static UChar testCases[][MAX_TOKEN_LEN] = {
68 { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/,
69 0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0073 /*'s'*/, 0x0000}, /* 9 */
70 { 0x0050 /*'P'*/, 0x0061 /*'a'*/, 0x0074/*'t'*/, 0x0000}, /* 1 */
71 { 0x0070 /*'p'*/, 0x00E9, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x00E9, 0x0000}, /* 2 */
72 { 0x0070 /*'p'*/, 0x00EA, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0065 /*'e'*/, 0x0000}, /* 3 */
73 { 0x0070 /*'p'*/, 0x00E9, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0065 /*'e'*/, 0x0072 /*'r'*/, 0x0000}, /* 4 */
74 { 0x0070 /*'p'*/, 0x00EA, 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0065 /*'e'*/, 0x0072 /*'r'*/, 0x0000}, /* 5 */
75 { 0x0054 /*'T'*/, 0x006f /*'o'*/, 0x0064 /*'d'*/, 0x0000}, /* 6 */
76 { 0x0054 /*'T'*/, 0x00F6, 0x006e /*'n'*/, 0x0065 /*'e'*/, 0x0000}, /* 7 */
77 { 0x0054 /*'T'*/, 0x006f /*'o'*/, 0x0066 /*'f'*/, 0x0075 /*'u'*/, 0x0000}, /* 8 */
78 { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/,
79 0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0000}, /* 12 */
80 { 0x0054 /*'T'*/, 0x006f /*'o'*/, 0x006e /*'n'*/, 0x0000}, /* 10 */
81 { 0x0050 /*'P'*/, 0x0041 /*'A'*/, 0x0054 /*'T'*/, 0x0000}, /* 11 */
82 { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/,
83 0x002d /*'-'*/, 0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0000}, /* 13 */
84 { 0x0062 /*'b'*/, 0x006c /*'l'*/, 0x0061 /*'a'*/, 0x0062 /*'c'*/, 0x006b /*'k'*/,
85 0x002d /*'-'*/, 0x0062 /*'b'*/, 0x0069 /*'i'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0073/*'s'*/, 0x0000}, /* 0 */
86 {0x0070 /*'p'*/, 0x0061 /*'a'*/, 0x0074 /*'t'*/, 0x0000}, /* 14 */
87 /* Additional tests */
88 { 0x0063 /*'c'*/, 0x007a /*'z'*/, 0x0061 /*'a'*/, 0x0072 /*'r'*/, 0x0000 }, /* 15 */
89 { 0x0063 /*'c'*/, 0x0068 /*'h'*/, 0x0075 /*'u'*/, 0x0072 /*'r'*/, 0x006f /*'o'*/, 0x0000 }, /* 16 */
90 { 0x0063 /*'c'*/, 0x0061 /*'a'*/, 0x0074 /*'t'*/, 0x000 }, /* 17 */
91 { 0x0064 /*'d'*/, 0x0061 /*'a'*/, 0x0072 /*'r'*/, 0x006e /*'n'*/, 0x0000 }, /* 18 */
92 { 0x003f /*'?'*/, 0x0000 }, /* 19 */
93 { 0x0071 /*'q'*/, 0x0075 /*'u'*/, 0x0069 /*'i'*/, 0x0063 /*'c'*/, 0x006b /*'k'*/, 0x0000 }, /* 20 */
94 { 0x0023 /*'#'*/, 0x0000 }, /* 21 */
95 { 0x0026 /*'&'*/, 0x0000 }, /* 22 */
96 { 0x0061 /*'a'*/, 0x002d /*'-'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0076 /*'v'*/, 0x0061 /*'a'*/,
97 0x0072/*'r'*/, 0x006b/*'k'*/, 0x0000}, /* 24 */
98 { 0x0061 /*'a'*/, 0x0061 /*'a'*/, 0x0072 /*'r'*/, 0x0064 /*'d'*/, 0x0076 /*'v'*/, 0x0061 /*'a'*/,
99 0x0072/*'r'*/, 0x006b/*'k'*/, 0x0000}, /* 23 */
100 { 0x0061 /*'a'*/, 0x0062 /*'b'*/, 0x0062 /*'b'*/, 0x006f /*'o'*/, 0x0074 /*'t'*/, 0x0000}, /* 25 */
101 { 0x0063 /*'c'*/, 0x006f /*'o'*/, 0x002d /*'-'*/, 0x0070 /*'p'*/, 0x0000}, /* 27 */
102 { 0x0063 /*'c'*/, 0x006f /*'o'*/, 0x0070 /*'p'*/, 0x0000}, /* 28 */
103 { 0x0063 /*'c'*/, 0x006f /*'o'*/, 0x006f /*'o'*/, 0x0070 /*'p'*/, 0x0000}, /* 26 */
104 { 0x007a /*'z'*/, 0x0065 /*'e'*/, 0x0062 /*'b'*/, 0x0072 /*'r'*/, 0x0061 /*'a'*/, 0x0000} /* 29 */
105};
106
107const static int32_t results[TESTLOCALES][TOTALTESTSET] = {
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_US */
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_GB */
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 }, /* en_CA */
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_FR */
112 { 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 */
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 }, /* de_DE */
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 }, /* it_IT */
115 { 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 */
116 /* new table collation with rules "& Z < p, P" loop to FIXEDTESTSET */
117 { 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 },
118 /* new table collation with rules "& C < ch , cH, Ch, CH " loop to TOTALTESTSET */
119 { 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 },
120 /* new table collation with rules "& Question-mark ; ? & Hash-mark ; # & Ampersand ; '&' " loop to TOTALTESTSET */
121 { 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 },
122 /* analogous to Japanese rules " & aa ; a- & ee ; e- & ii ; i- & oo ; o- & uu ; u- " */ /* loop to TOTALTESTSET */
123 { 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 }
124};
125
126void addRuleBasedCollTest(TestNode** root)
127{
128 addTest(root, &TestG7Locales, "tscoll/cg7coll/TestG7Locales");
129 addTest(root, &TestDemo1, "tscoll/cg7coll/TestDemo1");
130 addTest(root, &TestDemo2, "tscoll/cg7coll/TestDemo2");
131 addTest(root, &TestDemo3, "tscoll/cg7coll/TestDemo3");
132 addTest(root, &TestDemo4, "tscoll/cg7coll/TestDemo4");
133
134
135}
136
137static void TestG7Locales()
138{
139 UCollator *myCollation, *tblColl1;
140 UErrorCode status = U_ZERO_ERROR;
141 const UChar *defRules;
142 int32_t i, rlen, j, n;
143 log_verbose("Testing ucol_openRules for all the locales\n");
144 for (i = 0; i < 8; i++)
145 {
146 status = U_ZERO_ERROR;
147 myCollation = ucol_open(locales[i], &status);
148 ucol_setAttribute(myCollation, UCOL_STRENGTH, UCOL_QUATERNARY, &status);
149 ucol_setAttribute(myCollation, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
150
151 if (U_FAILURE(status))
152 {
153 log_err("Error in creating collator in %s: %s\n", locales[i], myErrorName(status));
154 continue;
155 }
156
157 defRules = ucol_getRules(myCollation, &rlen);
158 status = U_ZERO_ERROR;
159 tblColl1 = ucol_openRules(defRules, rlen, UCOL_OFF,
160 UCOL_DEFAULT_STRENGTH,NULL, &status);
161 if (U_FAILURE(status))
162 {
163
164 log_err("Error in creating collator in %s: %s\n", locales[i], myErrorName(status));
165 continue;
166 }
167
168
169 log_verbose("Locale %s\n", locales[i]);
170 log_verbose(" tests start...\n");
171
172 j = 0;
173 n = 0;
174 for (j = 0; j < FIXEDTESTSET; j++)
175 {
176 for (n = j+1; n < FIXEDTESTSET; n++)
177 {
178 doTest(tblColl1, testCases[results[i][j]], testCases[results[i][n]], UCOL_LESS);
179 }
180 }
181
182 ucol_close(myCollation);
183 ucol_close(tblColl1);
184 }
185}
186
187static void TestDemo1()
188{
189 UCollator *myCollation;
190 int32_t j, n;
191 const char *rules = "& Z < p, P";
192 int32_t len=strlen(rules);
193 UChar *temp = (UChar*)malloc(sizeof(UChar) * (len+1));
194 UErrorCode status = U_ZERO_ERROR;
195 u_uastrcpy(temp, rules);
196
197 log_verbose("Demo Test 1 : Create a new table collation with rules \" & Z < p, P \" \n");
198
199 myCollation = ucol_openRules(temp, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH,NULL, &status);
200
201 if (U_FAILURE(status))
202 {
203 log_err( "Demo Test 1 Rule collation object creation failed. : %s\n", myErrorName(status));
204 return;
205 }
206
207 for (j = 0; j < FIXEDTESTSET; j++)
208 {
209 for (n = j+1; n < FIXEDTESTSET; n++)
210 {
211 doTest(myCollation, testCases[results[8][j]], testCases[results[8][n]], UCOL_LESS);
212 }
213 }
214
215 ucol_close(myCollation);
216 free(temp);
217}
218
219static void TestDemo2()
220{
221 UCollator *myCollation;
222 int32_t j, n;
223 const char *rules = "& C < ch , cH, Ch, CH";
224 int32_t len=strlen(rules);
225 UChar *temp = (UChar*)malloc(sizeof(UChar) * (len+1));
226 UErrorCode status = U_ZERO_ERROR;
227 u_uastrcpy(temp, rules);
228
229 log_verbose("Demo Test 2 : Create a new table collation with rules \"& C < ch , cH, Ch, CH\"");
230
231 myCollation = ucol_openRules(temp, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH, NULL, &status);
232
233 if (U_FAILURE(status))
234 {
235 log_err( "Demo Test 2 Rule collation object creation failed.: %s\n", myErrorName(status));
236 return;
237 }
238 for (j = 0; j < TOTALTESTSET; j++)
239 {
240 for (n = j+1; n < TOTALTESTSET; n++)
241 {
242 doTest(myCollation, testCases[results[9][j]], testCases[results[9][n]], UCOL_LESS);
243 }
244 }
245 ucol_close(myCollation);
246 free(temp);
247}
248
249static void TestDemo3()
250{
251 UCollator *myCollation;
252 int32_t j, n;
253 const char *rules = "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'";
254 int32_t len=strlen(rules);
255 UChar *temp = (UChar*)malloc(sizeof(UChar) * (len+1));
256 UErrorCode status = U_ZERO_ERROR;
257 u_uastrcpy(temp, rules);
258
259 log_verbose("Demo Test 3 : Create a new table collation with rules \"& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'\" \n");
260
261 myCollation = ucol_openRules(temp, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH, NULL, &status);
262
263 if (U_FAILURE(status))
264 {
265 log_err( "Demo Test 3 Rule collation object creation failed.: %s\n", myErrorName(status));
266 return;
267 }
268
269 for (j = 0; j < TOTALTESTSET; j++)
270 {
271 for (n = j+1; n < TOTALTESTSET; n++)
272 {
273 doTest(myCollation, testCases[results[10][j]], testCases[results[10][n]], UCOL_LESS);
274 }
275 }
276 ucol_close(myCollation);
277 free(temp);
278}
279
280static void TestDemo4()
281{
282 UCollator *myCollation;
283 int32_t j, n;
284 const char *rules = " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' ";
285 int32_t len=strlen(rules);
286 UChar *temp = (UChar*)malloc(sizeof(UChar) * (len+1));
287 UErrorCode status = U_ZERO_ERROR;
288 u_uastrcpy(temp, rules);
289
290 log_verbose("Demo Test 4 : Create a new table collation with rules \" & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' \"\n");
291
292 myCollation = ucol_openRules(temp, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH, NULL, &status);
293
294 if (U_FAILURE(status))
295 {
296 log_err( "Demo Test 4 Rule collation object creation failed.: %s\n", myErrorName(status));
297 return;
298 }
299 for (j = 0; j < TOTALTESTSET; j++)
300 {
301 for (n = j+1; n < TOTALTESTSET; n++)
302 {
303 doTest(myCollation, testCases[results[11][j]], testCases[results[11][n]], UCOL_LESS);
304 }
305 }
306 ucol_close(myCollation);
307 free(temp);
308}
309
310#endif /* #if !UCONFIG_NO_COLLATION */