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) 2011-2014, International Business Machines Corporation and
      7  * others. All Rights Reserved.
      8  *******************************************************************************
      9  */
     10 
     11 /**
     12  * Port From:   ICU4C v2.1 : collate/CollationMonkeyTest
     13  * Source File: $ICU4CRoot/source/test/intltest/mnkytst.cpp
     14  **/
     15 
     16 package android.icu.dev.test.collator;
     17 
     18 import java.util.Locale;
     19 import java.util.Random;
     20 
     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.testsharding.MainTestShard;
     30 
     31 /**
     32  * CollationFrozenMonkeyTest is a third level test class.  This tests the random
     33  * substrings of the default test strings to verify if the compare and
     34  * sort key algorithm works correctly on frozen collators.  For example, any string is always
     35  * less than the string itself appended with any character.
     36  */
     37 
     38 @MainTestShard
     39 @RunWith(JUnit4.class)
     40 public class CollationFrozenMonkeyTest extends TestFmwk {
     41 
     42     private String source = "-abcdefghijklmnopqrstuvwxyz#&^$@";
     43 
     44     @Test
     45     public void TestCollationKey() {
     46         if(source.length() == 0) {
     47             errln("CollationMonkeyTest.TestCollationKey(): source is empty - ICU_DATA not set or data missing?");
     48             return;
     49         }
     50         Collator myPrimaryCollator;
     51         Collator mySecondaryCollator;
     52         Collator myTertiaryCollator;
     53         try {
     54             Collator myCollator = Collator.getInstance(new Locale("en", "CA"));
     55             myCollator.freeze();
     56 
     57             myPrimaryCollator = myCollator.cloneAsThawed();
     58             myPrimaryCollator.setStrength(Collator.PRIMARY);
     59             myPrimaryCollator.freeze();
     60 
     61             mySecondaryCollator = myPrimaryCollator.cloneAsThawed();
     62             mySecondaryCollator.setStrength(Collator.SECONDARY);
     63             mySecondaryCollator.freeze();
     64 
     65             myTertiaryCollator = mySecondaryCollator.cloneAsThawed();
     66             myTertiaryCollator.setStrength(Collator.TERTIARY);
     67             myTertiaryCollator.freeze();
     68         } catch (Exception e) {
     69             warnln("ERROR: in creation of collator of ENGLISH locale");
     70             return;
     71         }
     72 
     73         Random rand = createRandom(); // use test framework's random seed
     74         int s = rand.nextInt(0x7fff) % source.length();
     75         int t = rand.nextInt(0x7fff) % source.length();
     76         int slen = Math.abs(rand.nextInt(0x7fff) % source.length() - source.length()) % source.length();
     77         int tlen = Math.abs(rand.nextInt(0x7fff) % source.length() - source.length()) % source.length();
     78         String subs = source.substring(Math.min(s, slen), Math.min(s + slen, source.length()));
     79         String subt = source.substring(Math.min(t, tlen), Math.min(t + tlen, source.length()));
     80 
     81         CollationKey collationKey1, collationKey2;
     82 
     83         collationKey1 = myTertiaryCollator.getCollationKey(subs);
     84         collationKey2 = myTertiaryCollator.getCollationKey(subt);
     85         int result = collationKey1.compareTo(collationKey2);  // Tertiary
     86         int revResult = collationKey2.compareTo(collationKey1);  // Tertiary
     87         report( subs, subt, result, revResult);
     88 
     89         collationKey1 = mySecondaryCollator.getCollationKey(subs);
     90         collationKey2 = mySecondaryCollator.getCollationKey(subt);
     91         result = collationKey1.compareTo(collationKey2);  // Secondary
     92         revResult = collationKey2.compareTo(collationKey1);   // Secondary
     93         report( subs, subt, result, revResult);
     94 
     95         collationKey1 = myPrimaryCollator.getCollationKey(subs);
     96         collationKey2 = myPrimaryCollator.getCollationKey(subt);
     97         result = collationKey1.compareTo(collationKey2);  // Primary
     98         revResult = collationKey2.compareTo(collationKey1);   // Primary
     99         report(subs, subt, result, revResult);
    100 
    101         String msg = "";
    102         String addOne = subs + String.valueOf(0xE000);
    103 
    104         collationKey1 = myPrimaryCollator.getCollationKey(subs);
    105         collationKey2 = myPrimaryCollator.getCollationKey(addOne);
    106         result = collationKey1.compareTo(collationKey2);
    107         if (result != -1) {
    108             msg += "CollationKey(";
    109             msg += subs;
    110             msg += ") .LT. CollationKey(";
    111             msg += addOne;
    112             msg += ") Failed.";
    113             errln(msg);
    114         }
    115 
    116         msg = "";
    117         result = collationKey2.compareTo(collationKey1);
    118         if (result != 1) {
    119             msg += "CollationKey(";
    120             msg += addOne;
    121             msg += ") .GT. CollationKey(";
    122             msg += subs;
    123             msg += ") Failed.";
    124             errln(msg);
    125         }
    126     }
    127 
    128     // perform monkey tests using Collator.compare
    129     @Test
    130     public void TestCompare() {
    131         if(source.length() == 0) {
    132             errln("CollationMonkeyTest.TestCompare(): source is empty - ICU_DATA not set or data missing?");
    133             return;
    134         }
    135 
    136         Collator myPrimaryCollator;
    137         Collator mySecondaryCollator;
    138         Collator myTertiaryCollator;
    139         try {
    140             Collator myCollator = Collator.getInstance(new Locale("en", "CA"));
    141             myCollator.freeze();
    142 
    143             myPrimaryCollator = myCollator.cloneAsThawed();
    144             myPrimaryCollator.setStrength(Collator.PRIMARY);
    145             myPrimaryCollator.freeze();
    146 
    147             mySecondaryCollator = myPrimaryCollator.cloneAsThawed();
    148             mySecondaryCollator.setStrength(Collator.SECONDARY);
    149             mySecondaryCollator.freeze();
    150 
    151             myTertiaryCollator = mySecondaryCollator.cloneAsThawed();
    152             myTertiaryCollator.setStrength(Collator.TERTIARY);
    153             myTertiaryCollator.freeze();
    154         } catch (Exception e) {
    155             warnln("ERROR: in creation of collator of ENGLISH locale");
    156             return;
    157         }
    158 
    159 
    160         /* Seed the random-number generator with current time so that
    161          * the numbers will be different every time we run.
    162          */
    163 
    164         Random rand = createRandom(); // use test framework's random seed
    165         int s = rand.nextInt(0x7fff) % source.length();
    166         int t = rand.nextInt(0x7fff) % source.length();
    167         int slen = Math.abs(rand.nextInt(0x7fff) % source.length() - source.length()) % source.length();
    168         int tlen = Math.abs(rand.nextInt(0x7fff) % source.length() - source.length()) % source.length();
    169         String subs = source.substring(Math.min(s, slen), Math.min(s + slen, source.length()));
    170         String subt = source.substring(Math.min(t, tlen), Math.min(t + tlen, source.length()));
    171 
    172         int result = myTertiaryCollator.compare(subs, subt);  // Tertiary
    173         int revResult = myTertiaryCollator.compare(subt, subs);  // Tertiary
    174         report(subs, subt, result, revResult);
    175 
    176         result = mySecondaryCollator.compare(subs, subt);  // Secondary
    177         revResult = mySecondaryCollator.compare(subt, subs);  // Secondary
    178         report(subs, subt, result, revResult);
    179 
    180         result = myPrimaryCollator.compare(subs, subt);  // Primary
    181         revResult = myPrimaryCollator.compare(subt, subs);  // Primary
    182         report(subs, subt, result, revResult);
    183 
    184         String msg = "";
    185         String addOne = subs + String.valueOf(0xE000);
    186 
    187         result = myPrimaryCollator.compare(subs, addOne);
    188         if (result != -1) {
    189             msg += "Test : ";
    190             msg += subs;
    191             msg += " .LT. ";
    192             msg += addOne;
    193             msg += " Failed.";
    194             errln(msg);
    195         }
    196 
    197         msg = "";
    198         result = myPrimaryCollator.compare(addOne, subs);
    199         if (result != 1) {
    200             msg += "Test : ";
    201             msg += addOne;
    202             msg += " .GT. ";
    203             msg += subs;
    204             msg += " Failed.";
    205             errln(msg);
    206         }
    207     }
    208 
    209     void report(String s, String t, int result, int revResult) {
    210         if (revResult != -result) {
    211             String msg = "";
    212             msg += s;
    213             msg += " and ";
    214             msg += t;
    215             msg += " round trip comparison failed";
    216             msg += " (result " + result + ", reverse Result " + revResult + ")";
    217             errln(msg);
    218         }
    219     }
    220 
    221     @Test
    222     public void TestRules() {
    223         String testSourceCases[] = {
    224             "\u0061\u0062\u007a",
    225             "\u0061\u0062\u007a",
    226         };
    227 
    228         String testTargetCases[] = {
    229             "\u0061\u0062\u00e4",
    230             "\u0061\u0062\u0061\u0308",
    231         };
    232 
    233         int i=0;
    234         logln("Demo Test 1 : Create a new table collation with rules \"& z < 0x00e4\"");
    235         Collator col = Collator.getInstance(new Locale("en", "US"));
    236         String baseRules = ((RuleBasedCollator)col).getRules();
    237         String newRules = " & z < ";
    238         newRules = baseRules + newRules + String.valueOf(0x00e4);
    239         RuleBasedCollator myCollation = null;
    240         try {
    241             myCollation = new RuleBasedCollator(newRules);
    242         } catch (Exception e) {
    243             warnln( "Demo Test 1 Table Collation object creation failed.");
    244             return;
    245         }
    246 
    247         for(i=0; i<2; i++){
    248             doTest(myCollation, testSourceCases[i], testTargetCases[i], -1);
    249         }
    250         logln("Demo Test 2 : Create a new table collation with rules \"& z < a 0x0308\"");
    251         newRules = "";
    252         newRules = baseRules + " & z < a" + String.valueOf(0x0308);
    253         try {
    254             myCollation = new RuleBasedCollator(newRules);
    255         } catch (Exception e) {
    256             errln( "Demo Test 1 Table Collation object creation failed.");
    257             return;
    258         }
    259         for(i=0; i<2; i++){
    260             doTest(myCollation, testSourceCases[i], testTargetCases[i], -1);
    261         }
    262     }
    263 
    264     void doTest(RuleBasedCollator myCollation, String mysource, String target, int result) {
    265         int compareResult = myCollation.compare(source, target);
    266         CollationKey sortKey1, sortKey2;
    267 
    268         try {
    269             sortKey1 = myCollation.getCollationKey(source);
    270             sortKey2 = myCollation.getCollationKey(target);
    271         } catch (Exception e) {
    272             errln("SortKey generation Failed.\n");
    273             return;
    274         }
    275         int keyResult = sortKey1.compareTo(sortKey2);
    276         reportCResult( mysource, target, sortKey1, sortKey2, compareResult, keyResult, compareResult, result );
    277     }
    278 
    279     public void reportCResult(String src, String target, CollationKey sourceKey, CollationKey targetKey,
    280                               int compareResult, int keyResult, int incResult, int expectedResult ) {
    281         if (expectedResult < -1 || expectedResult > 1) {
    282             errln("***** invalid call to reportCResult ****");
    283             return;
    284         }
    285         boolean ok1 = (compareResult == expectedResult);
    286         boolean ok2 = (keyResult == expectedResult);
    287         boolean ok3 = (incResult == expectedResult);
    288         if (ok1 && ok2 && ok3 && !isVerbose()) {
    289             return;
    290         } else {
    291             String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
    292             String msg2 = "\", \"";
    293             String msg3 = "\") returned ";
    294             String msg4 = "; expected ";
    295             String sExpect = new String("");
    296             String sResult = new String("");
    297             sResult = CollationTest.appendCompareResult(compareResult, sResult);
    298             sExpect = CollationTest.appendCompareResult(expectedResult, sExpect);
    299             if (ok1) {
    300                 logln(msg1 + src + msg2 + target + msg3 + sResult);
    301             } else {
    302                 errln(msg1 + src + msg2 + target + msg3 + sResult + msg4 + sExpect);
    303             }
    304             msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
    305             msg2 = "\").compareTo(key(\"";
    306             msg3 = "\")) returned ";
    307             sResult = CollationTest.appendCompareResult(keyResult, sResult);
    308             if (ok2) {
    309                 logln(msg1 + src + msg2 + target + msg3 + sResult);
    310             } else {
    311                 errln(msg1 + src + msg2 + target + msg3 + sResult + msg4 + sExpect);
    312                 msg1 = "  ";
    313                 msg2 = " vs. ";
    314                 errln(msg1 + CollationTest.prettify(sourceKey) + msg2 + CollationTest.prettify(targetKey));
    315             }
    316             msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
    317             msg2 = "\", \"";
    318             msg3 = "\") returned ";
    319             sResult = CollationTest.appendCompareResult(incResult, sResult);
    320             if (ok3) {
    321                 logln(msg1 + src + msg2 + target + msg3 + sResult);
    322             } else {
    323                 errln(msg1 + src + msg2 + target + msg3 + sResult + msg4 + sExpect);
    324             }
    325         }
    326     }
    327 }
    328