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/G7CollationTest 12 * Source File: $ICU4CRoot/source/test/intltest/g7coll.cpp 13 **/ 14 15 package com.ibm.icu.dev.test.collator; 16 17 import java.util.Locale; 18 19 import org.junit.Test; 20 21 import com.ibm.icu.dev.test.TestFmwk; 22 import com.ibm.icu.text.CollationKey; 23 import com.ibm.icu.text.Collator; 24 import com.ibm.icu.text.RuleBasedCollator; 25 26 public class G7CollationTest extends TestFmwk{ 27 private static String[] testCases = { 28 "blackbirds", "Pat", "p\u00E9ch\u00E9", "p\u00EAche", "p\u00E9cher", 29 "p\u00EAcher", "Tod", "T\u00F6ne", "Tofu", "blackbird", "Ton", 30 "PAT", "black-bird", "black-birds", "pat", // 14 31 // Additional tests 32 "czar", "churo", "cat", "darn", "?", /* 19 */ 33 "quick", "#", "&", "a-rdvark", "aardvark", /* 23 */ 34 "abbot", "co-p", "cop", "coop", "zebra" 35 }; 36 37 private static int[][] results = { 38 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* en_US */ 39 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* en_GB */ 40 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* en_CA */ 41 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* fr_FR */ 42 { 12, 13, 9, 0, 14, 1, 11, 3, 2, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* fr_CA */ 43 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* de_DE */ 44 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* it_IT */ 45 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* ja_JP */ 46 /* new table collation with rules "& Z < p, P" loop to FIXEDTESTSET */ 47 { 12, 13, 9, 0, 6, 8, 10, 7, 14, 1, 11, 2, 3, 4, 5, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, 48 /* new table collation with rules "& C < ch , cH, Ch, CH " loop to TOTALTESTSET */ 49 { 19, 22, 21, 23, 24, 25, 12, 13, 9, 0, 17, 26, 28, 27, 15, 16, 18, 14, 1, 11, 2, 3, 4, 5, 20, 6, 8, 10, 7, 29 }, 50 /* new table collation with rules "& Question-mark ; ? & Hash-mark ; # & Ampersand ; '&' " loop to TOTALTESTSET */ 51 { 23, 24, 25, 22, 12, 13, 9, 0, 17, 16, 26, 28, 27, 15, 18, 21, 14, 1, 11, 2, 3, 4, 5, 19, 20, 6, 8, 10, 7, 29 }, 52 /* analogous to Japanese rules " & aa ; a- & ee ; e- & ii ; i- & oo ; o- & uu ; u- " */ /* loop to TOTALTESTSET */ 53 { 19, 22, 21, 24, 23, 25, 12, 13, 9, 0, 17, 16, 28, 26, 27, 15, 18, 14, 1, 11, 2, 3, 4, 5, 20, 6, 8, 10, 7, 29 } 54 }; 55 56 //private static final int MAX_TOKEN_LEN = 16; 57 //private static final int TESTLOCALES = 12; 58 private static final int FIXEDTESTSET = 15; 59 private static final int TOTALTESTSET = 30; 60 61 // perform test with added rules " & Z < p, P" 62 @Test TestDemo1()63 public void TestDemo1() { 64 logln("Demo Test 1 : Create a new table collation with rules \"& Z < p, P\""); 65 66 Collator col = Collator.getInstance(Locale.ENGLISH); 67 68 69 String baseRules = ((RuleBasedCollator)col).getRules(); 70 String newRules = " & Z < p, P"; 71 newRules = baseRules + newRules; 72 RuleBasedCollator myCollation = null; 73 try { 74 myCollation = new RuleBasedCollator(newRules); 75 } catch(Exception e) { 76 errln("Fail to create RuleBasedCollator with rules:" + newRules); 77 return; 78 } 79 80 int j, n; 81 for (j = 0; j < FIXEDTESTSET; j++) { 82 for (n = j+1; n < FIXEDTESTSET; n++) { 83 doTest(myCollation, testCases[results[8][j]], testCases[results[8][n]], -1); 84 } 85 } 86 } 87 88 89 // perorm test with added rules "& C < ch , cH, Ch, CH" 90 @Test TestDemo2()91 public void TestDemo2() { 92 logln("Demo Test 2 : Create a new table collation with rules \"& C < ch , cH, Ch, CH\""); 93 Collator col = Collator.getInstance(Locale.ENGLISH); 94 95 96 String baseRules = ((RuleBasedCollator)col).getRules(); 97 String newRules = "& C < ch , cH, Ch, CH"; 98 newRules = baseRules + newRules; 99 RuleBasedCollator myCollation = null; 100 try { 101 myCollation = new RuleBasedCollator(newRules); 102 }catch(Exception e){ 103 errln("Fail to create RuleBasedCollator with rules:" + newRules); 104 return; 105 } 106 107 int j, n; 108 for (j = 0; j < TOTALTESTSET; j++) { 109 for (n = j+1; n < TOTALTESTSET; n++) { 110 doTest(myCollation, testCases[results[9][j]], testCases[results[9][n]], -1); 111 } 112 } 113 } 114 115 116 // perform test with added rules 117 // "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'" 118 @Test TestDemo3()119 public void TestDemo3() { 120 // logln("Demo Test 3 : Create a new table collation with rules \"& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'\""); 121 Collator col = Collator.getInstance(Locale.ENGLISH); 122 123 124 String baseRules = ((RuleBasedCollator)col).getRules(); 125 String newRules = "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'"; 126 newRules = baseRules + newRules; 127 RuleBasedCollator myCollation = null; 128 try { 129 myCollation = new RuleBasedCollator(newRules); 130 }catch(Exception e){ 131 errln("Fail to create RuleBasedCollator with rules:" + newRules); 132 return; 133 } 134 135 int j, n; 136 for (j = 0; j < TOTALTESTSET; j++) { 137 for (n = j+1; n < TOTALTESTSET; n++) { 138 doTest(myCollation, testCases[results[10][j]], testCases[results[10][n]], -1); 139 } 140 } 141 } 142 143 144 // perform test with added rules 145 // " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' " 146 @Test TestDemo4()147 public void TestDemo4() { 148 logln("Demo Test 4 : Create a new table collation with rules \" & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' \""); 149 Collator col = Collator.getInstance(Locale.ENGLISH); 150 151 String baseRules = ((RuleBasedCollator)col).getRules(); 152 String newRules = " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' "; 153 newRules = baseRules + newRules; 154 RuleBasedCollator myCollation = null; 155 try { 156 myCollation = new RuleBasedCollator(newRules); 157 }catch(Exception e){ 158 errln("Fail to create RuleBasedCollator with rules:" + newRules); 159 return; 160 } 161 162 int j, n; 163 for (j = 0; j < TOTALTESTSET; j++) { 164 for (n = j+1; n < TOTALTESTSET; n++) { 165 doTest(myCollation, testCases[results[11][j]], testCases[results[11][n]], -1); 166 } 167 } 168 } 169 170 @Test TestG7Data()171 public void TestG7Data() { 172 Locale locales[] = { 173 Locale.US, 174 Locale.UK, 175 Locale.CANADA, 176 Locale.FRANCE, 177 Locale.CANADA_FRENCH, 178 Locale.GERMANY, 179 Locale.JAPAN, 180 Locale.ITALY 181 }; 182 int i = 0, j = 0; 183 for (i = 0; i < locales.length; i++) { 184 Collator myCollation= null; 185 RuleBasedCollator tblColl1 = null; 186 try { 187 myCollation = Collator.getInstance(locales[i]); 188 tblColl1 = new RuleBasedCollator(((RuleBasedCollator)myCollation).getRules()); 189 } catch (Exception foo) { 190 warnln("Exception: " + foo.getMessage() + 191 "; Locale : " + locales[i].getDisplayName() + " getRules failed"); 192 continue; 193 } 194 for (j = 0; j < FIXEDTESTSET; j++) { 195 for (int n = j+1; n < FIXEDTESTSET; n++) { 196 doTest(tblColl1, testCases[results[i][j]], testCases[results[i][n]], -1); 197 } 198 } 199 myCollation = null; 200 } 201 } 202 203 204 // main test routine, tests comparisons for a set of strings against sets of expected results doTest(Collator myCollation, String source, String target, int result)205 private void doTest(Collator myCollation, String source, String target, 206 int result){ 207 208 int compareResult = myCollation.compare(source, target); 209 CollationKey sortKey1, sortKey2; 210 sortKey1 = myCollation.getCollationKey(source); 211 sortKey2 = myCollation.getCollationKey(target); 212 int keyResult = sortKey1.compareTo(sortKey2); 213 reportCResult(source, target, sortKey1, sortKey2, compareResult, 214 keyResult, compareResult, result); 215 } 216 reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey, int compareResult, int keyResult, int incResult, int expectedResult )217 private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey, 218 int compareResult, int keyResult, int incResult, int expectedResult ){ 219 if (expectedResult < -1 || expectedResult > 1) { 220 errln("***** invalid call to reportCResult ****"); 221 return; 222 } 223 224 boolean ok1 = (compareResult == expectedResult); 225 boolean ok2 = (keyResult == expectedResult); 226 boolean ok3 = (incResult == expectedResult); 227 228 if (ok1 && ok2 && ok3 && !isVerbose()){ 229 return; 230 } else { 231 String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\""; 232 String msg2 = "\", \""; 233 String msg3 = "\") returned "; 234 String msg4 = "; expected "; 235 236 String sExpect = new String(""); 237 String sResult = new String(""); 238 sResult = CollationTest.appendCompareResult(compareResult, sResult); 239 sExpect = CollationTest.appendCompareResult(expectedResult, sExpect); 240 if (ok1) { 241 logln(msg1 + source + msg2 + target + msg3 + sResult); 242 } else { 243 // Android patch: Add --omitCollationRules to genrb. 244 logln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect); 245 // Android patch end. 246 } 247 248 msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\""; 249 msg2 = "\").compareTo(key(\""; 250 msg3 = "\")) returned "; 251 sResult = CollationTest.appendCompareResult(keyResult, sResult); 252 if (ok2) { 253 logln(msg1 + source + msg2 + target + msg3 + sResult); 254 } else { 255 // Android patch: Add --omitCollationRules to genrb. 256 logln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect); 257 // Android patch end. 258 msg1 = " "; 259 msg2 = " vs. "; 260 // Android patch: Add --omitCollationRules to genrb. 261 logln(msg1 + CollationTest.prettify(sourceKey) + msg2 + CollationTest.prettify(targetKey)); 262 // Android patch end. 263 } 264 265 msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\""; 266 msg2 = "\", \""; 267 msg3 = "\") returned "; 268 269 sResult = CollationTest.appendCompareResult(incResult, sResult); 270 271 if (ok3) { 272 logln(msg1 + source + msg2 + target + msg3 + sResult); 273 } else { 274 // Android patch: Add --omitCollationRules to genrb. 275 logln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect); 276 // Android patch end. 277 } 278 } 279 } 280 } 281