1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2010, 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, 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 */
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_status(status, "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            ucol_close(myCollation);
164            log_err_status(status, "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    static const char rules[] = "& Z < p, P";
192    int32_t len=(int32_t)strlen(rules);
193    UChar temp[sizeof(rules)];
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_status(status, "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}
217
218static void TestDemo2()
219{
220    UCollator *myCollation;
221    int32_t j, n;
222    static const char rules[] = "& C < ch , cH, Ch, CH";
223    int32_t len=(int32_t)strlen(rules);
224    UChar temp[sizeof(rules)];
225    UErrorCode status = U_ZERO_ERROR;
226    u_uastrcpy(temp, rules);
227
228    log_verbose("Demo Test 2 : Create a new table collation with rules \"& C < ch , cH, Ch, CH\"");
229
230    myCollation = ucol_openRules(temp, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH, NULL, &status);
231
232    if (U_FAILURE(status))
233    {
234        log_err_status(status, "Demo Test 2 Rule collation object creation failed.: %s\n", myErrorName(status));
235        return;
236    }
237    for (j = 0; j < TOTALTESTSET; j++)
238    {
239        for (n = j+1; n < TOTALTESTSET; n++)
240        {
241            doTest(myCollation, testCases[results[9][j]], testCases[results[9][n]], UCOL_LESS);
242        }
243    }
244    ucol_close(myCollation);
245}
246
247static void TestDemo3()
248{
249    UCollator *myCollation;
250    int32_t j, n;
251    static const char rules[] = "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'";
252    int32_t len=(int32_t)strlen(rules);
253    UChar temp[sizeof(rules)];
254    UErrorCode status = U_ZERO_ERROR;
255    u_uastrcpy(temp, rules);
256
257    log_verbose("Demo Test 3 : Create a new table collation with rules \"& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'\" \n");
258
259    myCollation = ucol_openRules(temp, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH, NULL, &status);
260
261    if (U_FAILURE(status))
262    {
263        log_err_status(status, "Demo Test 3 Rule collation object creation failed.: %s\n", myErrorName(status));
264        return;
265    }
266
267    for (j = 0; j < TOTALTESTSET; j++)
268    {
269        for (n = j+1; n < TOTALTESTSET; n++)
270        {
271            doTest(myCollation, testCases[results[10][j]], testCases[results[10][n]], UCOL_LESS);
272        }
273    }
274    ucol_close(myCollation);
275}
276
277static void TestDemo4()
278{
279    UCollator *myCollation;
280    int32_t j, n;
281    static const char rules[] = " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' ";
282    int32_t len=(int32_t)strlen(rules);
283    UChar temp[sizeof(rules)];
284    UErrorCode status = U_ZERO_ERROR;
285    u_uastrcpy(temp, rules);
286
287    log_verbose("Demo Test 4 : Create a new table collation with rules \" & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' \"\n");
288
289    myCollation = ucol_openRules(temp, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH, NULL, &status);
290
291    if (U_FAILURE(status))
292    {
293        log_err_status(status, "Demo Test 4 Rule collation object creation failed.: %s\n", myErrorName(status));
294        return;
295    }
296    for (j = 0; j < TOTALTESTSET; j++)
297    {
298        for (n = j+1; n < TOTALTESTSET; n++)
299        {
300            doTest(myCollation, testCases[results[11][j]], testCases[results[11][n]], UCOL_LESS);
301        }
302    }
303    ucol_close(myCollation);
304}
305
306#endif /* #if !UCONFIG_NO_COLLATION */
307