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/CollationGermanTest
     12  * Source File: $ICU4CRoot/source/test/intltest/decoll.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 
     26  public class CollationGermanTest extends TestFmwk{
     27     private static char[][] testSourceCases = {
     28         {0x47, 0x72, 0x00F6, 0x00DF, 0x65},
     29         {0x61, 0x62, 0x63},
     30         {0x54, 0x00F6, 0x6e, 0x65},
     31         {0x54, 0x00F6, 0x6e, 0x65},
     32         {0x54, 0x00F6, 0x6e, 0x65},
     33         {0x61, 0x0308, 0x62, 0x63},
     34         {0x00E4, 0x62, 0x63},
     35         {0x00E4, 0x62, 0x63},
     36         {0x53, 0x74, 0x72, 0x61, 0x00DF, 0x65},
     37         {0x65, 0x66, 0x67},
     38         {0x00E4, 0x62, 0x63},
     39         {0x53, 0x74, 0x72, 0x61, 0x00DF, 0x65}
     40     };
     41 
     42     private static char[][] testTargetCases = {
     43         {0x47, 0x72, 0x6f, 0x73, 0x73, 0x69, 0x73, 0x74},
     44         {0x61, 0x0308, 0x62, 0x63},
     45         {0x54, 0x6f, 0x6e},
     46         {0x54, 0x6f, 0x64},
     47         {0x54, 0x6f, 0x66, 0x75},
     48         {0x41, 0x0308, 0x62, 0x63},
     49         {0x61, 0x0308, 0x62, 0x63},
     50         {0x61, 0x65, 0x62, 0x63},
     51         {0x53, 0x74, 0x72, 0x61, 0x73, 0x73, 0x65},
     52         {0x65, 0x66, 0x67},
     53         {0x61, 0x65, 0x62, 0x63},
     54         {0x53, 0x74, 0x72, 0x61, 0x73, 0x73, 0x65}
     55     };
     56 
     57     private static int results[][] =
     58     {
     59         //  Primary  Tertiary
     60         { -1,        -1 },
     61         { 0,         -1 },
     62         { 1,          1 },
     63         { 1,          1 },
     64         { 1,          1 },
     65         { 0,         -1 },
     66         { 0,          0 },
     67         { -1,        -1 },
     68         { 0,          1 },
     69         { 0,          0 },
     70         { -1,        -1 },
     71         { 0,          1 }
     72     };
     73 
     74     private Collator myCollation = null;
     75 
     76     public CollationGermanTest() {
     77     }
     78 
     79     @Before
     80     public void init() throws Exception {
     81         myCollation = Collator.getInstance(Locale.GERMAN);
     82         if(myCollation == null) {
     83             errln("ERROR: in creation of collator of GERMAN locale");
     84         }
     85     }
     86 
     87     // perform test with strength TERTIARY
     88     @Test
     89     public void TestTertiary(){
     90         if(myCollation == null ) {
     91             errln("decoll: cannot start test, collator is null\n");
     92             return;
     93         }
     94 
     95         int i = 0;
     96         myCollation.setStrength(Collator.TERTIARY);
     97         myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
     98         for (i = 0; i < 12 ; i++)
     99         {
    100             doTest(testSourceCases[i], testTargetCases[i], results[i][1]);
    101         }
    102     }
    103 
    104     // perform test with strength SECONDARY
    105     //This method in icu4c has no implementation.
    106     @Test
    107     public void TestSecondary(){
    108     }
    109 
    110     // perform test with strength PRIMARY
    111     @Test
    112     public void TestPrimary(){
    113         if(myCollation == null ) {
    114             errln("decoll: cannot start test, collator is null\n");
    115             return;
    116         }
    117         int i;
    118         myCollation.setStrength(Collator.PRIMARY);
    119         myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    120         for (i = 0; i < 12 ; i++)
    121         {
    122             doTest(testSourceCases[i], testTargetCases[i], results[i][0]);
    123         }
    124     }
    125 
    126 
    127     //main test routine, tests rules specific to germa locale
    128     private void doTest(char[] source, char[] target, int result){
    129         String s = new String(source);
    130         String t = new String(target);
    131         int compareResult = myCollation.compare(s, t);
    132         CollationKey sortKey1, sortKey2;
    133         sortKey1 = myCollation.getCollationKey(s);
    134         sortKey2 = myCollation.getCollationKey(t);
    135         int keyResult = sortKey1.compareTo(sortKey2);
    136         reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
    137 
    138     }
    139 
    140     private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,
    141                                 int compareResult, int keyResult, int incResult, int expectedResult ){
    142         if (expectedResult < -1 || expectedResult > 1)
    143         {
    144             errln("***** invalid call to reportCResult ****");
    145             return;
    146         }
    147 
    148         boolean ok1 = (compareResult == expectedResult);
    149         boolean ok2 = (keyResult == expectedResult);
    150         boolean ok3 = (incResult == expectedResult);
    151 
    152         if (ok1 && ok2 && ok3 && !isVerbose()){
    153             return;
    154         }else{
    155             String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
    156             String msg2 = "\", \"";
    157             String msg3 = "\") returned ";
    158             String msg4 = "; expected ";
    159 
    160             String sExpect = new String("");
    161             String sResult = new String("");
    162             sResult = CollationTest.appendCompareResult(compareResult, sResult);
    163             sExpect = CollationTest.appendCompareResult(expectedResult, sExpect);
    164             if (ok1) {
    165                 logln(msg1 + source + msg2 + target + msg3 + sResult);
    166             } else {
    167                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
    168             }
    169 
    170             msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
    171             msg2 = "\").compareTo(key(\"";
    172             msg3 = "\")) returned ";
    173             sResult = CollationTest.appendCompareResult(keyResult, sResult);
    174             if (ok2) {
    175                 logln(msg1 + source + msg2 + target + msg3 + sResult);
    176             } else {
    177                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
    178                 msg1 = "  ";
    179                 msg2 = " vs. ";
    180                 errln(msg1 + CollationTest.prettify(sourceKey) + msg2 + CollationTest.prettify(targetKey));
    181             }
    182 
    183             msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
    184             msg2 = "\", \"";
    185             msg3 = "\") returned ";
    186 
    187             sResult = CollationTest.appendCompareResult(incResult, sResult);
    188 
    189             if (ok3) {
    190                 logln(msg1 + source + msg2 + target + msg3 + sResult);
    191             } else {
    192                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
    193             }
    194         }
    195     }
    196 }
    197