Home | History | Annotate | Download | only in collator
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 //  2016 and later: Unicode, Inc. and others.
      3 // License & terms of use: http://www.unicode.org/copyright.html#License
      4 /*
      5  *******************************************************************************
      6  * Copyright (C) 2002-2014, International Business Machines Corporation and
      7  * others. All Rights Reserved.
      8  *******************************************************************************
      9  */
     10 
     11 /**
     12  * Port From:   ICU4C v2.1 : Collate/CollationKanaTest
     13  * Source File: $ICU4CRoot/source/test/intltest/jacoll.cpp
     14  **/
     15 
     16 package android.icu.dev.test.collator;
     17 
     18 import java.util.Locale;
     19 
     20 import org.junit.Before;
     21 import org.junit.Test;
     22 import org.junit.runner.RunWith;
     23 import org.junit.runners.JUnit4;
     24 
     25 import android.icu.dev.test.TestFmwk;
     26 import android.icu.text.CollationKey;
     27 import android.icu.text.Collator;
     28 import android.icu.text.RuleBasedCollator;
     29 import android.icu.util.ULocale;
     30 import android.icu.testsharding.MainTestShard;
     31 
     32 @MainTestShard
     33 @RunWith(JUnit4.class)
     34 public class CollationKanaTest extends TestFmwk{
     35     private static char[][] testSourceCases = {
     36         {0xff9E},
     37         {0x3042},
     38         {0x30A2},
     39         {0x3042, 0x3042},
     40         {0x30A2, 0x30FC},
     41         {0x30A2, 0x30FC, 0x30C8}                               /*  6 */
     42     };
     43 
     44     private static char[][] testTargetCases = {
     45         {0xFF9F},
     46         {0x30A2},
     47         {0x3042, 0x3042},
     48         {0x30A2, 0x30FC},
     49         {0x30A2, 0x30FC, 0x30C8},
     50         {0x3042, 0x3042, 0x3068}                              /*  6 */
     51     };
     52 
     53     private static int[] results = {
     54         -1,
     55         0,   //Collator::LESS, /* Katakanas and Hiraganas are equal on tertiary level(ICU 2.0)*/
     56         -1,
     57         1, // Collator::LESS, /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*/
     58         -1,
     59         -1,    //Collator::GREATER /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*//*  6 */
     60     };
     61 
     62     private static char[][] testBaseCases = {
     63         {0x30AB},
     64         {0x30AB, 0x30AD},
     65         {0x30AD},
     66         {0x30AD, 0x30AD}
     67     };
     68 
     69     private static char[][] testPlainDakutenHandakutenCases = {
     70         {0x30CF, 0x30AB},
     71         {0x30D0, 0x30AB},
     72         {0x30CF, 0x30AD},
     73         {0x30D0, 0x30AD}
     74     };
     75 
     76     private static char[][] testSmallLargeCases = {
     77         {0x30C3, 0x30CF},
     78         {0x30C4, 0x30CF},
     79         {0x30C3, 0x30D0},
     80         {0x30C4, 0x30D0}
     81     };
     82 
     83     private static char[][] testKatakanaHiraganaCases = {
     84         {0x3042, 0x30C3},
     85         {0x30A2, 0x30C3},
     86         {0x3042, 0x30C4},
     87         {0x30A2, 0x30C4}
     88     };
     89 
     90     private static char[][] testChooonKigooCases = {
     91         /*0*/ {0x30AB, 0x30FC, 0x3042},
     92         /*1*/ {0x30AB, 0x30FC, 0x30A2},
     93         /*2*/ {0x30AB, 0x30A4, 0x3042},
     94         /*3*/ {0x30AB, 0x30A4, 0x30A2},
     95         /*6*/ {0x30AD, 0x30FC, 0x3042}, /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*/
     96         /*7*/ {0x30AD, 0x30FC, 0x30A2}, /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*/
     97         /*4*/ {0x30AD, 0x30A4, 0x3042},
     98         /*5*/ {0x30AD, 0x30A4, 0x30A2}
     99     };
    100 
    101     private Collator myCollation = null;
    102 
    103     public CollationKanaTest() {
    104     }
    105 
    106     @Before
    107     public void init()throws Exception {
    108         if(myCollation==null){
    109             myCollation = Collator.getInstance(Locale.JAPANESE);
    110         }
    111     }
    112 
    113     // performs test with strength TERIARY
    114     @Test
    115     public void TestTertiary() {
    116         int i = 0;
    117         myCollation.setStrength(Collator.TERTIARY);
    118 
    119         for (i = 0; i < 6; i++) {
    120             doTest(testSourceCases[i], testTargetCases[i], results[i]);
    121         }
    122     }
    123 
    124     /* Testing base letters */
    125     @Test
    126     public void TestBase() {
    127         int i;
    128         myCollation.setStrength(Collator.PRIMARY);
    129         for (i = 0; i < 3 ; i++) {
    130             doTest(testBaseCases[i], testBaseCases[i + 1], -1);
    131         }
    132     }
    133 
    134     /* Testing plain, Daku-ten, Handaku-ten letters */
    135     @Test
    136     public void TestPlainDakutenHandakuten() {
    137         int i;
    138         myCollation.setStrength(Collator.SECONDARY);
    139         for (i = 0; i < 3 ; i++) {
    140             doTest(testPlainDakutenHandakutenCases[i], testPlainDakutenHandakutenCases[i + 1], -1);
    141         }
    142     }
    143 
    144     /*
    145     * Test Small, Large letters
    146     */
    147     @Test
    148     public void TestSmallLarge() {
    149         int i;
    150         myCollation.setStrength(Collator.TERTIARY);
    151 
    152         for (i = 0; i < 3 ; i++) {
    153             doTest(testSmallLargeCases[i], testSmallLargeCases[i + 1], -1);
    154         }
    155     }
    156 
    157     /*
    158     * Test Katakana, Hiragana letters
    159     */
    160     @Test
    161     public void TestKatakanaHiragana() {
    162         int i;
    163         myCollation.setStrength(Collator.QUATERNARY);
    164         for (i = 0; i < 3 ; i++) {
    165             doTest(testKatakanaHiraganaCases[i], testKatakanaHiraganaCases[i + 1], -1);
    166         }
    167     }
    168 
    169     /*
    170     * Test Choo-on kigoo
    171     */
    172     @Test
    173     public void TestChooonKigoo() {
    174         int i;
    175         myCollation.setStrength(Collator.QUATERNARY);
    176         for (i = 0; i < 7 ; i++) {
    177             doTest(testChooonKigooCases[i], testChooonKigooCases[i + 1], -1);
    178         }
    179     }
    180 
    181     /*
    182      * Test common Hiragana and Katakana characters (e.g. 0x3099) (ticket:6140)
    183      */
    184     @Test
    185     public void TestCommonCharacters() {
    186         char[] tmp1 = { 0x3058, 0x30B8 };
    187         char[] tmp2 = { 0x3057, 0x3099, 0x30B7, 0x3099 };
    188         CollationKey key1, key2;
    189         int result;
    190         String string1 = new String(tmp1);
    191         String string2 = new String(tmp2);
    192         RuleBasedCollator rb = (RuleBasedCollator)Collator.getInstance(ULocale.JAPANESE);
    193         rb.setStrength(Collator.QUATERNARY);
    194         rb.setAlternateHandlingShifted(false);
    195 
    196         result = rb.compare(string1, string2);
    197 
    198         key1 = rb.getCollationKey(string1);
    199         key2 = rb.getCollationKey(string2);
    200 
    201         if ( result != 0 || !key1.equals(key2)) {
    202             errln("Failed Hiragana and Katakana common characters test. Expected results to be equal.");
    203         }
    204 
    205     }
    206     // main test routine, tests rules specific to "Kana" locale
    207     private void doTest(char[] source, char[] target, int result){
    208 
    209         String s = new String(source);
    210         String t = new String(target);
    211         int compareResult = myCollation.compare(s, t);
    212         CollationKey sortKey1, sortKey2;
    213         sortKey1 = myCollation.getCollationKey(s);
    214         sortKey2 = myCollation.getCollationKey(t);
    215         int keyResult = sortKey1.compareTo(sortKey2);
    216         reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
    217 
    218     }
    219 
    220     private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,
    221                                 int compareResult, int keyResult, int incResult, int expectedResult ){
    222         if (expectedResult < -1 || expectedResult > 1) {
    223             errln("***** invalid call to reportCResult ****");
    224             return;
    225         }
    226 
    227         boolean ok1 = (compareResult == expectedResult);
    228         boolean ok2 = (keyResult == expectedResult);
    229         boolean ok3 = (incResult == expectedResult);
    230 
    231         if (ok1 && ok2 && ok3 && !isVerbose()){
    232             return;
    233         } else {
    234             String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
    235             String msg2 = "\", \"";
    236             String msg3 = "\") returned ";
    237             String msg4 = "; expected ";
    238 
    239             String sExpect = new String("");
    240             String sResult = new String("");
    241             sResult = CollationTest.appendCompareResult(compareResult, sResult);
    242             sExpect = CollationTest.appendCompareResult(expectedResult, sExpect);
    243             if (ok1) {
    244                 logln(msg1 + source + msg2 + target + msg3 + sResult);
    245             } else {
    246                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
    247             }
    248 
    249             msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
    250             msg2 = "\").compareTo(key(\"";
    251             msg3 = "\")) returned ";
    252             sResult = CollationTest.appendCompareResult(keyResult, sResult);
    253             if (ok2) {
    254                 logln(msg1 + source + msg2 + target + msg3 + sResult);
    255             } else {
    256                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
    257                 msg1 = "  ";
    258                 msg2 = " vs. ";
    259                 errln(msg1 + CollationTest.prettify(sourceKey) + msg2 + CollationTest.prettify(targetKey));
    260             }
    261 
    262             msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
    263             msg2 = "\", \"";
    264             msg3 = "\") returned ";
    265 
    266             sResult = CollationTest.appendCompareResult(incResult, sResult);
    267 
    268             if (ok3) {
    269                 logln(msg1 + source + msg2 + target + msg3 + sResult);
    270             } else {
    271                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
    272             }
    273         }
    274     }
    275 }
    276