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