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