Home | History | Annotate | Download | only in cintltst
      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 
     54 const 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 
     67 const 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 
    107 const 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 
    126 void addRuleBasedCollTest(TestNode** root)
    127 {
    128     /* BEGIN android-removed
    129        To save space, Android does not include the collation tailoring rules.
    130        We skip the tailing tests for collations. */
    131     /* addTest(root, &TestG7Locales, "tscoll/cg7coll/TestG7Locales"); */
    132     /* END android-removed */
    133     addTest(root, &TestDemo1, "tscoll/cg7coll/TestDemo1");
    134     addTest(root, &TestDemo2, "tscoll/cg7coll/TestDemo2");
    135     addTest(root, &TestDemo3, "tscoll/cg7coll/TestDemo3");
    136     addTest(root, &TestDemo4, "tscoll/cg7coll/TestDemo4");
    137 
    138 
    139 }
    140 
    141 static void TestG7Locales()
    142 {
    143     UCollator *myCollation, *tblColl1;
    144     UErrorCode status = U_ZERO_ERROR;
    145     const UChar *defRules;
    146     int32_t i, rlen, j, n;
    147     log_verbose("Testing  ucol_openRules for all the locales\n");
    148     for (i = 0; i < 8; i++)
    149     {
    150         status = U_ZERO_ERROR;
    151         myCollation = ucol_open(locales[i], &status);
    152         ucol_setAttribute(myCollation, UCOL_STRENGTH, UCOL_QUATERNARY, &status);
    153         ucol_setAttribute(myCollation, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
    154 
    155         if (U_FAILURE(status))
    156         {
    157             log_err_status(status, "Error in creating collator in %s:  %s\n", locales[i], myErrorName(status));
    158             continue;
    159         }
    160 
    161         defRules = ucol_getRules(myCollation, &rlen);
    162         status = U_ZERO_ERROR;
    163         tblColl1 = ucol_openRules(defRules, rlen, UCOL_OFF,
    164                    UCOL_DEFAULT_STRENGTH,NULL, &status);
    165         if (U_FAILURE(status))
    166         {
    167             ucol_close(myCollation);
    168             log_err_status(status, "Error in creating collator in %s:  %s\n", locales[i], myErrorName(status));
    169             continue;
    170         }
    171 
    172 
    173         log_verbose("Locale  %s\n", locales[i]);
    174         log_verbose("  tests start...\n");
    175 
    176         j = 0;
    177         n = 0;
    178         for (j = 0; j < FIXEDTESTSET; j++)
    179         {
    180             for (n = j+1; n < FIXEDTESTSET; n++)
    181             {
    182                 doTest(tblColl1, testCases[results[i][j]], testCases[results[i][n]], UCOL_LESS);
    183             }
    184         }
    185 
    186         ucol_close(myCollation);
    187         ucol_close(tblColl1);
    188     }
    189 }
    190 
    191 static void TestDemo1()
    192 {
    193     UCollator *myCollation;
    194     int32_t j, n;
    195     static const char rules[] = "& Z < p, P";
    196     int32_t len=(int32_t)strlen(rules);
    197     UChar temp[sizeof(rules)];
    198     UErrorCode status = U_ZERO_ERROR;
    199     u_uastrcpy(temp, rules);
    200 
    201     log_verbose("Demo Test 1 : Create a new table collation with rules \" & Z < p, P \" \n");
    202 
    203     myCollation = ucol_openRules(temp, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH,NULL, &status);
    204 
    205     if (U_FAILURE(status))
    206     {
    207         log_err_status(status, "Demo Test 1 Rule collation object creation failed. : %s\n", myErrorName(status));
    208         return;
    209     }
    210 
    211     for (j = 0; j < FIXEDTESTSET; j++)
    212     {
    213         for (n = j+1; n < FIXEDTESTSET; n++)
    214         {
    215             doTest(myCollation, testCases[results[8][j]], testCases[results[8][n]], UCOL_LESS);
    216         }
    217     }
    218 
    219     ucol_close(myCollation);
    220 }
    221 
    222 static void TestDemo2()
    223 {
    224     UCollator *myCollation;
    225     int32_t j, n;
    226     static const char rules[] = "& C < ch , cH, Ch, CH";
    227     int32_t len=(int32_t)strlen(rules);
    228     UChar temp[sizeof(rules)];
    229     UErrorCode status = U_ZERO_ERROR;
    230     u_uastrcpy(temp, rules);
    231 
    232     log_verbose("Demo Test 2 : Create a new table collation with rules \"& C < ch , cH, Ch, CH\"");
    233 
    234     myCollation = ucol_openRules(temp, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH, NULL, &status);
    235 
    236     if (U_FAILURE(status))
    237     {
    238         log_err_status(status, "Demo Test 2 Rule collation object creation failed.: %s\n", myErrorName(status));
    239         return;
    240     }
    241     for (j = 0; j < TOTALTESTSET; j++)
    242     {
    243         for (n = j+1; n < TOTALTESTSET; n++)
    244         {
    245             doTest(myCollation, testCases[results[9][j]], testCases[results[9][n]], UCOL_LESS);
    246         }
    247     }
    248     ucol_close(myCollation);
    249 }
    250 
    251 static void TestDemo3()
    252 {
    253     UCollator *myCollation;
    254     int32_t j, n;
    255     static const char rules[] = "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'";
    256     int32_t len=(int32_t)strlen(rules);
    257     UChar temp[sizeof(rules)];
    258     UErrorCode status = U_ZERO_ERROR;
    259     u_uastrcpy(temp, rules);
    260 
    261     log_verbose("Demo Test 3 : Create a new table collation with rules \"& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'\" \n");
    262 
    263     myCollation = ucol_openRules(temp, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH, NULL, &status);
    264 
    265     if (U_FAILURE(status))
    266     {
    267         log_err_status(status, "Demo Test 3 Rule collation object creation failed.: %s\n", myErrorName(status));
    268         return;
    269     }
    270 
    271     for (j = 0; j < TOTALTESTSET; j++)
    272     {
    273         for (n = j+1; n < TOTALTESTSET; n++)
    274         {
    275             doTest(myCollation, testCases[results[10][j]], testCases[results[10][n]], UCOL_LESS);
    276         }
    277     }
    278     ucol_close(myCollation);
    279 }
    280 
    281 static void TestDemo4()
    282 {
    283     UCollator *myCollation;
    284     int32_t j, n;
    285     static const char rules[] = " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' ";
    286     int32_t len=(int32_t)strlen(rules);
    287     UChar temp[sizeof(rules)];
    288     UErrorCode status = U_ZERO_ERROR;
    289     u_uastrcpy(temp, rules);
    290 
    291     log_verbose("Demo Test 4 : Create a new table collation with rules \" & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' \"\n");
    292 
    293     myCollation = ucol_openRules(temp, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH, NULL, &status);
    294 
    295     if (U_FAILURE(status))
    296     {
    297         log_err_status(status, "Demo Test 4 Rule collation object creation failed.: %s\n", myErrorName(status));
    298         return;
    299     }
    300     for (j = 0; j < TOTALTESTSET; j++)
    301     {
    302         for (n = j+1; n < TOTALTESTSET; n++)
    303         {
    304             doTest(myCollation, testCases[results[11][j]], testCases[results[11][n]], UCOL_LESS);
    305         }
    306     }
    307     ucol_close(myCollation);
    308 }
    309 
    310 #endif /* #if !UCONFIG_NO_COLLATION */
    311