Home | History | Annotate | Download | only in collator
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /*
      4  *******************************************************************************
      5  * Copyright (C) 2002-2014, International Business Machines Corporation and
      6  * others. All Rights Reserved.
      7  *******************************************************************************
      8  */
      9 
     10 /**
     11  * Port From:   ICU4C v2.1 : Collate/CollationFrenchTest
     12  * Source File: $ICU4CRoot/source/test/intltest/frcoll.cpp
     13  **/
     14 
     15 package com.ibm.icu.dev.test.collator;
     16 
     17 import java.util.Locale;
     18 
     19 import org.junit.Before;
     20 import org.junit.Test;
     21 
     22 import com.ibm.icu.dev.test.TestFmwk;
     23 import com.ibm.icu.text.CollationKey;
     24 import com.ibm.icu.text.Collator;
     25 import com.ibm.icu.text.RuleBasedCollator;
     26 
     27 public class CollationFrenchTest extends TestFmwk{
     28     private static char[][] testSourceCases = {
     29         {0x0061/*'a'*/, 0x0062/*'b'*/, 0x0063/*'c'*/},
     30         {0x0043/*'C'*/, 0x004f/*'O'*/, 0x0054/*'T'*/, 0x0045/*'E'*/},
     31         {0x0063/*'c'*/, 0x006f/*'o'*/, 0x002d/*'-'*/, 0x006f/*'o'*/, 0x0070/*'p'*/},
     32         {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/},
     33         {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/},
     34         {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/},
     35         {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/},
     36         {0x0048/*'H'*/, 0x0065/*'e'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x006f/*'o'*/},
     37         {0x01f1},
     38         {0xfb00},
     39         {0x01fa},
     40         {0x0101}
     41     };
     42 
     43     private static char[][] testTargetCases = {
     44         {0x0041/*'A'*/, 0x0042/*'B'*/, 0x0043/*'C'*/},
     45         {0x0063/*'c'*/, 0x00f4, 0x0074/*'t'*/, 0x0065/*'e'*/},
     46         {0x0043/*'C'*/, 0x004f/*'O'*/, 0x004f/*'O'*/, 0x0050/*'P'*/},
     47         {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x00E9},
     48         {0x0070/*'p'*/,  0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x00E9},
     49         {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/},
     50         {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/},
     51         {0x0068/*'h'*/, 0x0065/*'e'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x004f/*'O'*/},
     52         {0x01ee},
     53         {0x25ca},
     54         {0x00e0},
     55         {0x01df}
     56     };
     57 
     58     private static int[] results = {
     59         -1,
     60         -1,
     61         -1, /*Collator::GREATER,*/
     62         -1,
     63         1,
     64         1,
     65         -1,
     66         1,
     67        -1, /*Collator::GREATER,*/
     68         1,
     69         -1,
     70         -1
     71     };
     72 
     73     // 0x0300 is grave, 0x0301 is acute
     74     // the order of elements in this array must be different than the order in CollationEnglishTest
     75     private static char[][] testAcute = {
     76     /*00*/    {0x0065/*'e'*/, 0x0065/*'e'*/},
     77     /*01*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/},
     78     /*02*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/},
     79     /*03*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/},
     80     /*04*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/},
     81     /*05*/    {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0301},
     82     /*06*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0301},
     83     /*07*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0301},
     84     /*08*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0301},
     85     /*09*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0301},
     86     /*0a*/    {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0300, 0x0301},
     87     /*0b*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0301},
     88     /*0c*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0301},
     89     /*0d*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0301},
     90     /*0e*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0301},
     91     /*0f*/    {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0300},
     92     /*10*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0300},
     93     /*11*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0300},
     94     /*12*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0300},
     95     /*13*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0300},
     96     /*14*/    {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0301, 0x0300},
     97     /*15*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0300},
     98     /*16*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0300},
     99     /*17*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0300},
    100     /*18*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0300}
    101     };
    102 
    103     private static char[][] testBugs = {
    104         {0x0061/*'a'*/},
    105         {0x0041/*'A'*/},
    106         {0x0065/*'e'*/},
    107         {0x0045/*'E'*/},
    108         {0x00e9},
    109         {0x00e8},
    110         {0x00ea},
    111         {0x00eb},
    112         {0x0065/*'e'*/, 0x0061/*'a'*/},
    113         {0x0078/*'x'*/}
    114     };
    115 
    116 
    117     private Collator myCollation = null;
    118 
    119     public CollationFrenchTest() {
    120     }
    121 
    122     @Before
    123     public void init()throws Exception {
    124         myCollation = Collator.getInstance(Locale.CANADA_FRENCH);
    125     }
    126 
    127     // perform tests with strength TERTIARY
    128     @Test
    129     public void TestTertiary() {
    130         int i = 0;
    131         myCollation.setStrength(Collator.TERTIARY);
    132 
    133         for (i = 0; i < 12 ; i++) {
    134             doTest(testSourceCases[i], testTargetCases[i], results[i]);
    135         }
    136     }
    137 
    138     // perform tests with strength SECONDARY
    139     @Test
    140     public void TestSecondary() {
    141         //test acute and grave ordering
    142         int i = 0;
    143         int j;
    144         int expected;
    145 
    146         myCollation.setStrength(Collator.SECONDARY);
    147 
    148         for (i = 0; i < testAcute.length; i++) {
    149             for (j = 0; j < testAcute.length; j++) {
    150                 if (i <  j) {
    151                     expected = -1;
    152                 } else if (i == j) {
    153                     expected = 0;
    154                 } else {
    155                     expected = 1;
    156                 }
    157                 doTest(testAcute[i], testAcute[j], expected );
    158             }
    159         }
    160     }
    161 
    162     // perform extra tests
    163     @Test
    164     public void TestExtra() {
    165         int i, j;
    166         myCollation.setStrength(Collator.TERTIARY);
    167         for (i = 0; i < 9 ; i++) {
    168             for (j = i + 1; j < 10; j += 1) {
    169                 doTest(testBugs[i], testBugs[j], -1);
    170             }
    171         }
    172     }
    173 
    174     @Test
    175     public void TestContinuationReordering()
    176     {
    177         String rule = "&0x2f00 << 0x2f01";
    178         try {
    179             RuleBasedCollator collator = new RuleBasedCollator(rule);
    180             collator.setFrenchCollation(true);
    181             CollationKey key1
    182                         = collator.getCollationKey("a\u0325\u2f00\u2f01b\u0325");
    183             CollationKey key2
    184                         = collator.getCollationKey("a\u0325\u2f01\u2f01b\u0325");
    185             if (key1.compareTo(key2) >= 0) {
    186                 errln("Error comparing continuation strings");
    187             }
    188         } catch (Exception e) {
    189             errln(e.toString());
    190         }
    191     }
    192 
    193     // main test routine, test rules specific to the french locale
    194     private void doTest(char[] source, char[] target, int result) {
    195         String s = new String(source);
    196         String t = new String(target);
    197         int compareResult = myCollation.compare(s, t);
    198         CollationKey sortKey1, sortKey2;
    199         sortKey1 = myCollation.getCollationKey(s);
    200         sortKey2 = myCollation.getCollationKey(t);
    201         int keyResult = sortKey1.compareTo(sortKey2);
    202         reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
    203     }
    204 
    205     private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,
    206                                 int compareResult, int keyResult, int incResult, int expectedResult ) {
    207         if (expectedResult < -1 || expectedResult > 1) {
    208             errln("***** invalid call to reportCResult ****");
    209             return;
    210         }
    211 
    212         boolean ok1 = (compareResult == expectedResult);
    213         boolean ok2 = (keyResult == expectedResult);
    214         boolean ok3 = (incResult == expectedResult);
    215 
    216         if (ok1 && ok2 && ok3 && !isVerbose()) {
    217             return;
    218         } else {
    219             String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
    220             String msg2 = "\", \"";
    221             String msg3 = "\") returned ";
    222             String msg4 = "; expected ";
    223 
    224             String sExpect = new String("");
    225             String sResult = new String("");
    226             sResult = CollationTest.appendCompareResult(compareResult, sResult);
    227             sExpect = CollationTest.appendCompareResult(expectedResult, sExpect);
    228             if (ok1) {
    229                 logln(msg1 + source + msg2 + target + msg3 + sResult);
    230             } else {
    231                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
    232             }
    233 
    234             msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
    235             msg2 = "\").compareTo(key(\"";
    236             msg3 = "\")) returned ";
    237             sResult = CollationTest.appendCompareResult(keyResult, sResult);
    238             if (ok2) {
    239                 logln(msg1 + source + msg2 + target + msg3 + sResult);
    240             } else {
    241                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
    242                 msg1 = "  ";
    243                 msg2 = " vs. ";
    244                 errln(msg1 + CollationTest.prettify(sourceKey) + msg2 + CollationTest.prettify(targetKey));
    245             }
    246 
    247             msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
    248             msg2 = "\", \"";
    249             msg3 = "\") returned ";
    250 
    251             sResult = CollationTest.appendCompareResult(incResult, sResult);
    252 
    253             if (ok3) {
    254                 logln(msg1 + source + msg2 + target + msg3 + sResult);
    255             } else {
    256                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
    257             }
    258         }
    259     }
    260 }
    261