• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /********************************************************************
2  * COPYRIGHT:
3  * Copyright (c) 1997-2014, 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 const char* locales[8] = {
54         "en_US",
55         "en_GB",
56         "en_CA",
57         "fr_FR",
58         "fr_CA",
59         "de_DE",
60         "it_IT",
61         "ja_JP"
62 };
63 
64 
65 
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 */
104 };
105 
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 }
123 };
124 
addRuleBasedCollTest(TestNode ** root)125 void addRuleBasedCollTest(TestNode** root)
126 {
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");
132 
133 
134 }
135 
TestG7Locales()136 static void TestG7Locales()
137 {
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++)
144     {
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);
150 
151         if (U_FAILURE(status))
152         {
153             log_err_status(status, "Error in creating collator in %s:  %s\n", locale, myErrorName(status));
154             ucol_close(myCollation);
155             continue;
156         }
157 
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);
163                 continue;
164             }
165         } else {
166             UCollator *tblColl1;
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))
172             {
173                 log_err_status(status, "Error in creating collator in %s:  %s\n", locale, myErrorName(status));
174                 continue;
175             }
176             myCollation = tblColl1;
177         }
178 
179         log_verbose("Locale  %s\n", locales[i]);
180         log_verbose("  tests start...\n");
181 
182         j = 0;
183         n = 0;
184         for (j = 0; j < FIXEDTESTSET; j++)
185         {
186             for (n = j+1; n < FIXEDTESTSET; n++)
187             {
188                 doTest(myCollation, testCases[results[i][j]], testCases[results[i][n]], UCOL_LESS);
189             }
190         }
191 
192         ucol_close(myCollation);
193     }
194 }
195 
TestDemo1()196 static void TestDemo1()
197 {
198     UCollator *myCollation;
199     int32_t j, n;
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);
205 
206     log_verbose("Demo Test 1 : Create a new table collation with rules \" & Z < p, P \" \n");
207 
208     myCollation = ucol_openRules(temp, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH,NULL, &status);
209 
210     if (U_FAILURE(status))
211     {
212         log_err_status(status, "Demo Test 1 Rule collation object creation failed. : %s\n", myErrorName(status));
213         return;
214     }
215 
216     for (j = 0; j < FIXEDTESTSET; j++)
217     {
218         for (n = j+1; n < FIXEDTESTSET; n++)
219         {
220             doTest(myCollation, testCases[results[8][j]], testCases[results[8][n]], UCOL_LESS);
221         }
222     }
223 
224     ucol_close(myCollation);
225 }
226 
TestDemo2()227 static void TestDemo2()
228 {
229     UCollator *myCollation;
230     int32_t j, n;
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);
236 
237     log_verbose("Demo Test 2 : Create a new table collation with rules \"& C < ch , cH, Ch, CH\"");
238 
239     myCollation = ucol_openRules(temp, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH, NULL, &status);
240 
241     if (U_FAILURE(status))
242     {
243         log_err_status(status, "Demo Test 2 Rule collation object creation failed.: %s\n", myErrorName(status));
244         return;
245     }
246     for (j = 0; j < TOTALTESTSET; j++)
247     {
248         for (n = j+1; n < TOTALTESTSET; n++)
249         {
250             doTest(myCollation, testCases[results[9][j]], testCases[results[9][n]], UCOL_LESS);
251         }
252     }
253     ucol_close(myCollation);
254 }
255 
TestDemo3()256 static void TestDemo3()
257 {
258     UCollator *myCollation;
259     int32_t j, n;
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);
265 
266     log_verbose("Demo Test 3 : Create a new table collation with rules \"& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'\" \n");
267 
268     myCollation = ucol_openRules(temp, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH, NULL, &status);
269 
270     if (U_FAILURE(status))
271     {
272         log_err_status(status, "Demo Test 3 Rule collation object creation failed.: %s\n", myErrorName(status));
273         return;
274     }
275 
276     for (j = 0; j < TOTALTESTSET; j++)
277     {
278         for (n = j+1; n < TOTALTESTSET; n++)
279         {
280             doTest(myCollation, testCases[results[10][j]], testCases[results[10][n]], UCOL_LESS);
281         }
282     }
283     ucol_close(myCollation);
284 }
285 
TestDemo4()286 static void TestDemo4()
287 {
288     UCollator *myCollation;
289     int32_t j, n;
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);
295 
296     log_verbose("Demo Test 4 : Create a new table collation with rules \" & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' \"\n");
297 
298     myCollation = ucol_openRules(temp, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH, NULL, &status);
299 
300     if (U_FAILURE(status))
301     {
302         log_err_status(status, "Demo Test 4 Rule collation object creation failed.: %s\n", myErrorName(status));
303         return;
304     }
305     for (j = 0; j < TOTALTESTSET; j++)
306     {
307         for (n = j+1; n < TOTALTESTSET; n++)
308         {
309             doTest(myCollation, testCases[results[11][j]], testCases[results[11][n]], UCOL_LESS);
310         }
311     }
312     ucol_close(myCollation);
313 }
314 
315 #endif /* #if !UCONFIG_NO_COLLATION */
316