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/CollationFinnishTest
     12  * Source File: $ICU4CRoot/source/test/intltest/ficoll.cpp
     13  **/
     14 
     15 package com.ibm.icu.dev.test.collator;
     16 
     17 import org.junit.Before;
     18 import org.junit.Test;
     19 
     20 import com.ibm.icu.dev.test.TestFmwk;
     21 import com.ibm.icu.text.CollationKey;
     22 import com.ibm.icu.text.Collator;
     23 import com.ibm.icu.util.ULocale;
     24 
     25 public class CollationFinnishTest extends TestFmwk {
     26     private static char[][] testSourceCases = {
     27         {0x77, 0x61, 0x74},
     28         {0x76, 0x61, 0x74},
     29         {0x61, 0x00FC, 0x62, 0x65, 0x63, 0x6b},
     30         {0x4c, 0x00E5, 0x76, 0x69},
     31         {0x77, 0x61, 0x74}
     32     };
     33 
     34     private static char[][] testTargetCases = {
     35         {0x76, 0x61, 0x74},
     36         {0x77, 0x61, 0x79},
     37         {0x61, 0x78, 0x62, 0x65, 0x63, 0x6b},
     38         {0x4c, 0x00E4, 0x77, 0x65},
     39         {0x76, 0x61, 0x74}
     40     };
     41 
     42     private static int[] results = {
     43         1,
     44         -1,
     45         1,
     46         -1,
     47         // test primary > 4
     48         1,  // v < w per cldrbug 6615
     49     };
     50 
     51     private Collator myCollation = null;
     52 
     53     public CollationFinnishTest() {
     54     }
     55 
     56     @Before
     57     public void init()throws Exception{
     58         myCollation = Collator.getInstance(new ULocale("fi_FI@collation=standard"));
     59     }
     60 
     61 
     62     // perform tests with strength PRIMARY
     63     @Test
     64     public void TestPrimary() {
     65         int i = 0;
     66         myCollation.setStrength(Collator.PRIMARY);
     67         for(i = 4; i < 5; i++) {
     68             doTest(testSourceCases[i], testTargetCases[i], results[i]);
     69         }
     70     }
     71 
     72     // perform test with strength TERTIARY
     73     @Test
     74     public void TestTertiary() {
     75         int i = 0;
     76         myCollation.setStrength(Collator.TERTIARY);
     77         for(i = 0; i < 4; i++ ) {
     78             doTest(testSourceCases[i], testTargetCases[i], results[i]);
     79         }
     80     }
     81 
     82     // main test routine, tests rules specific to the finish locale
     83     private void doTest(char[] source, char[] target, int result) {
     84         String s = new String(source);
     85         String t = new String(target);
     86         int compareResult = myCollation.compare(s, t);
     87         CollationKey sortKey1, sortKey2;
     88         sortKey1 = myCollation.getCollationKey(s);
     89         sortKey2 = myCollation.getCollationKey(t);
     90         int keyResult = sortKey1.compareTo(sortKey2);
     91         reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
     92     }
     93 
     94     private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,
     95                                 int compareResult, int keyResult, int incResult, int expectedResult ) {
     96         if (expectedResult < -1 || expectedResult > 1) {
     97             errln("***** invalid call to reportCResult ****");
     98             return;
     99         }
    100 
    101         boolean ok1 = (compareResult == expectedResult);
    102         boolean ok2 = (keyResult == expectedResult);
    103         boolean ok3 = (incResult == expectedResult);
    104 
    105         if (ok1 && ok2 && ok3 && !isVerbose()) {
    106             return;
    107         } else {
    108             String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
    109             String msg2 = "\", \"";
    110             String msg3 = "\") returned ";
    111             String msg4 = "; expected ";
    112 
    113             String sExpect = new String("");
    114             String sResult = new String("");
    115             sResult = CollationTest.appendCompareResult(compareResult, sResult);
    116             sExpect = CollationTest.appendCompareResult(expectedResult, sExpect);
    117             if (ok1) {
    118                 logln(msg1 + source + msg2 + target + msg3 + sResult);
    119             } else {
    120                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
    121             }
    122 
    123             msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
    124             msg2 = "\").compareTo(key(\"";
    125             msg3 = "\")) returned ";
    126             sResult = CollationTest.appendCompareResult(keyResult, sResult);
    127             if (ok2) {
    128                 logln(msg1 + source + msg2 + target + msg3 + sResult);
    129             } else {
    130                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
    131                 msg1 = "  ";
    132                 msg2 = " vs. ";
    133                 errln(msg1 + CollationTest.prettify(sourceKey) + msg2 + CollationTest.prettify(targetKey));
    134             }
    135 
    136             msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
    137             msg2 = "\", \"";
    138             msg3 = "\") returned ";
    139 
    140             sResult = CollationTest.appendCompareResult(incResult, sResult);
    141 
    142             if (ok3) {
    143                 logln(msg1 + source + msg2 + target + msg3 + sResult);
    144             } else {
    145                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
    146             }
    147         }
    148     }
    149 }
    150