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/CollationTurkishTest
     13  * Source File: $ICU4CRoot/source/test/intltest/trcoll.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.testsharding.MainTestShard;
     29 
     30 @MainTestShard
     31 @RunWith(JUnit4.class)
     32 public class CollationTurkishTest extends TestFmwk{
     33     private static char[][] testSourceCases = {
     34         {0x73, 0x0327},
     35         {0x76, 0x00E4, 0x74},
     36         {0x6f, 0x6c, 0x64},
     37         {0x00FC, 0x6f, 0x69, 0x64},
     38         {0x68, 0x011E, 0x61, 0x6c, 0x74},
     39         {0x73, 0x74, 0x72, 0x65, 0x73, 0x015E},
     40         {0x76, 0x6f, 0x0131, 0x64},
     41         {0x69, 0x64, 0x65, 0x61},
     42         {0x00FC, 0x6f, 0x69, 0x64},
     43         {0x76, 0x6f, 0x0131, 0x64},
     44         {0x69, 0x64, 0x65, 0x61}
     45     };
     46 
     47     private static char[][] testTargetCases = {
     48         {0x75, 0x0308},
     49         {0x76, 0x62, 0x74},
     50         {0x00D6, 0x61, 0x79},
     51         {0x76, 0x6f, 0x69, 0x64},
     52         {0x68, 0x61, 0x6c, 0x74},
     53         {0x015E, 0x74, 0x72, 0x65, 0x015E, 0x73},
     54         {0x76, 0x6f, 0x69, 0x64},
     55         {0x49, 0x64, 0x65, 0x61},
     56         {0x76, 0x6f, 0x69, 0x64},
     57         {0x76, 0x6f, 0x69, 0x64},
     58         {0x49, 0x64, 0x65, 0x61}
     59     };
     60 
     61     private static int[] results = {
     62         -1,
     63         -1,
     64         -1,
     65         -1,
     66         1,
     67         -1,
     68         -1,
     69         1,
     70     // test priamry > 8
     71         -1,
     72         -1,
     73         1
     74     };
     75 
     76     private Collator myCollation = null;
     77 
     78     public CollationTurkishTest() {
     79 
     80     }
     81 
     82     @Before
     83     public void init()throws Exception{
     84         myCollation = Collator.getInstance(new Locale("tr", ""));
     85     }
     86 
     87     @Test
     88     public void TestTertiary() {
     89         int i = 0;
     90         myCollation.setStrength(Collator.TERTIARY);
     91         for (i = 0; i < 8 ; i++) {
     92             doTest(testSourceCases[i], testTargetCases[i], results[i]);
     93         }
     94     }
     95 
     96     @Test
     97     public void TestPrimary() {
     98         int i;
     99         myCollation.setStrength(Collator.PRIMARY);
    100         for (i = 8; i < 11; i++) {
    101             doTest(testSourceCases[i], testTargetCases[i], results[i]);
    102         }
    103     }
    104 
    105 
    106     // main test routine, tests rules specific to turkish locale
    107     private void doTest(char[] source, char[] target, int result) {
    108         String s = new String(source);
    109         String t = new String(target);
    110         int compareResult = myCollation.compare(s, t);
    111         CollationKey sortKey1, sortKey2;
    112         sortKey1 = myCollation.getCollationKey(s);
    113         sortKey2 = myCollation.getCollationKey(t);
    114         int keyResult = sortKey1.compareTo(sortKey2);
    115         reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
    116 
    117     }
    118 
    119     private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,
    120                                 int compareResult, int keyResult, int incResult, int expectedResult ) {
    121         if (expectedResult < -1 || expectedResult > 1) {
    122             errln("***** invalid call to reportCResult ****");
    123             return;
    124         }
    125 
    126         boolean ok1 = (compareResult == expectedResult);
    127         boolean ok2 = (keyResult == expectedResult);
    128         boolean ok3 = (incResult == expectedResult);
    129 
    130         if (ok1 && ok2 && ok3 && !isVerbose()) {
    131             return;
    132         } else {
    133             String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
    134             String msg2 = "\", \"";
    135             String msg3 = "\") returned ";
    136             String msg4 = "; expected ";
    137 
    138             String sExpect = new String("");
    139             String sResult = new String("");
    140             sResult = CollationTest.appendCompareResult(compareResult, sResult);
    141             sExpect = CollationTest.appendCompareResult(expectedResult, sExpect);
    142             if (ok1) {
    143                 logln(msg1 + source + msg2 + target + msg3 + sResult);
    144             } else {
    145                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
    146             }
    147 
    148             msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
    149             msg2 = "\").compareTo(key(\"";
    150             msg3 = "\")) returned ";
    151             sResult = CollationTest.appendCompareResult(keyResult, sResult);
    152             if (ok2) {
    153                 logln(msg1 + source + msg2 + target + msg3 + sResult);
    154             } else {
    155                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
    156                 msg1 = "  ";
    157                 msg2 = " vs. ";
    158                 errln(msg1 + CollationTest.prettify(sourceKey) + msg2 + CollationTest.prettify(targetKey));
    159             }
    160 
    161             msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
    162             msg2 = "\", \"";
    163             msg3 = "\") returned ";
    164 
    165             sResult = CollationTest.appendCompareResult(incResult, sResult);
    166 
    167             if (ok3) {
    168                 logln(msg1 + source + msg2 + target + msg3 + sResult);
    169             } else {
    170                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
    171             }
    172         }
    173     }
    174 }
    175