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