Source Code Cross Referenced for CollationMiscTest.java in  » Internationalization-Localization » icu4j » com » ibm » icu » dev » test » collator » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Internationalization Localization » icu4j » com.ibm.icu.dev.test.collator 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         *******************************************************************************
0003:         * Copyright (C) 2002-2006, International Business Machines Corporation and    *
0004:         * others. All Rights Reserved.                                                *
0005:         *******************************************************************************
0006:         */
0007:
0008:        /** 
0009:         * Port From:   ICU4C v2.1 : cintltest
0010:         * Source File: $ICU4CRoot/source/test/cintltest/cmsccoll.c
0011:         */package com.ibm.icu.dev.test.collator;
0012:
0013:        import com.ibm.icu.dev.test.TestFmwk;
0014:        import com.ibm.icu.util.ULocale;
0015:        import com.ibm.icu.util.UResourceBundle;
0016:        import com.ibm.icu.impl.ICUResourceBundle;
0017:        import com.ibm.icu.impl.ImplicitCEGenerator;
0018:        import com.ibm.icu.impl.Utility;
0019:        import com.ibm.icu.text.CollationElementIterator;
0020:        import com.ibm.icu.text.CollationKey;
0021:        import com.ibm.icu.text.Collator;
0022:        import com.ibm.icu.text.Normalizer;
0023:        import com.ibm.icu.text.RuleBasedCollator;
0024:        import com.ibm.icu.text.UTF16;
0025:
0026:        import java.util.Set;
0027:        import java.util.Arrays;
0028:        import java.util.Locale;
0029:        import java.util.TreeSet;
0030:
0031:        public class CollationMiscTest extends TestFmwk {
0032:
0033:            public static void main(String[] args) throws Exception {
0034:                new CollationMiscTest().run(args);
0035:                // new CollationMiscTest().TestLocaleRuleBasedCollators(); 
0036:            }
0037:
0038:            private static final int NORM_BUFFER_TEST_LEN_ = 32;
0039:
0040:            private static final class Tester {
0041:                int u;
0042:                String NFC;
0043:                String NFD;
0044:            }
0045:
0046:            private static final boolean hasCollationElements(Locale locale) {
0047:                ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle
0048:                        .getBundleInstance(
0049:                                ICUResourceBundle.ICU_COLLATION_BASE_NAME,
0050:                                locale);
0051:                if (rb != null) {
0052:                    try {
0053:                        String collkey = rb
0054:                                .getStringWithFallback("collations/default");
0055:                        ICUResourceBundle elements = rb
0056:                                .getWithFallback("collations/" + collkey);
0057:                        if (elements != null) {
0058:                            return true;
0059:                        }
0060:                    } catch (Exception e) {
0061:                    }
0062:                }
0063:                return false;
0064:            }
0065:
0066:            public void TestComposeDecompose() {
0067:                Tester t[] = new Tester[0x30000];
0068:                t[0] = new Tester();
0069:                logln("Testing UCA extensively\n");
0070:                RuleBasedCollator coll;
0071:                try {
0072:                    coll = (RuleBasedCollator) Collator
0073:                            .getInstance(Locale.ENGLISH);
0074:                } catch (Exception e) {
0075:                    warnln("Error opening collator\n");
0076:                    return;
0077:                }
0078:
0079:                int noCases = 0;
0080:                for (int u = 0; u < 0x30000; u++) {
0081:                    String comp = UTF16.valueOf(u);
0082:                    int len = comp.length();
0083:                    t[noCases].NFC = Normalizer.normalize(u, Normalizer.NFC);
0084:                    t[noCases].NFD = Normalizer.normalize(u, Normalizer.NFD);
0085:
0086:                    if (t[noCases].NFC.length() != t[noCases].NFD.length()
0087:                            || (t[noCases].NFC.compareTo(t[noCases].NFD) != 0)
0088:                            || (len != t[noCases].NFD.length())
0089:                            || (comp.compareTo(t[noCases].NFD) != 0)) {
0090:                        t[noCases].u = u;
0091:                        if (len != t[noCases].NFD.length()
0092:                                || (comp.compareTo(t[noCases].NFD) != 0)) {
0093:                            t[noCases].NFC = comp;
0094:                        }
0095:                        noCases++;
0096:                        t[noCases] = new Tester();
0097:                    }
0098:                }
0099:
0100:                for (int u = 0; u < noCases; u++) {
0101:                    if (!coll.equals(t[u].NFC, t[u].NFD)) {
0102:                        errln("Failure: codePoint \\u"
0103:                                + Integer.toHexString(t[u].u)
0104:                                + " fails TestComposeDecompose in the UCA");
0105:                        CollationTest.doTest(this , coll, t[u].NFC, t[u].NFD, 0);
0106:                    }
0107:                }
0108:
0109:                logln("Testing locales, number of cases = " + noCases);
0110:                Locale loc[] = Collator.getAvailableLocales();
0111:                for (int i = 0; i < loc.length; i++) {
0112:                    if (hasCollationElements(loc[i])) {
0113:                        logln("Testing locale " + loc[i].getDisplayName());
0114:                        coll = (RuleBasedCollator) Collator.getInstance(loc[i]);
0115:                        coll.setStrength(Collator.IDENTICAL);
0116:
0117:                        for (int u = 0; u < noCases; u++) {
0118:                            if (!coll.equals(t[u].NFC, t[u].NFD)) {
0119:                                errln("Failure: codePoint \\u"
0120:                                        + Integer.toHexString(t[u].u)
0121:                                        + " fails TestComposeDecompose for locale "
0122:                                        + loc[i].getDisplayName());
0123:                                // this tests for the iterators too
0124:                                CollationTest.doTest(this , coll, t[u].NFC,
0125:                                        t[u].NFD, 0);
0126:                            }
0127:                        }
0128:                    }
0129:                }
0130:            }
0131:
0132:            public void TestRuleOptions() {
0133:                // values here are hardcoded and are correct for the current UCA when 
0134:                // the UCA changes, one might be forced to change these values. 
0135:                // (\\u02d0, \\U00010FFFC etc...) 
0136:                String[] rules = {
0137:                        // cannot test this anymore, as [last primary ignorable] doesn't 
0138:                        // have a  code point associated to it anymore  
0139:                        // "&[before 3][last primary ignorable]<<<k",
0140:                        // - all befores here amount to zero       
0141:                        "&[before 3][first tertiary ignorable]<<<a",
0142:                        "&[before 3][last tertiary ignorable]<<<a",
0143:                        "&[before 3][first secondary ignorable]<<<a",
0144:                        "&[before 3][last secondary ignorable]<<<a",
0145:                        // 'normal' befores  
0146:                        "&[before 3][first primary ignorable]<<<c<<<b &[first primary ignorable]<a",
0147:                        // we don't have a code point that corresponds to the last primary 
0148:                        // ignorable 
0149:                        "&[before 3][last primary ignorable]<<<c<<<b &[last primary ignorable]<a",
0150:                        "&[before 3][first variable]<<<c<<<b &[first variable]<a",
0151:                        "&[last variable]<a &[before 3][last variable]<<<c<<<b ",
0152:                        "&[first regular]<a &[before 1][first regular]<b",
0153:                        "&[before 1][last regular]<b &[last regular]<a",
0154:                        "&[before 1][first implicit]<b &[first implicit]<a",
0155:                        "&[before 1][last implicit]<b &[last implicit]<a",
0156:                        "&[last variable]<z&[last primary ignorable]<x&[last secondary ignorable]<<y&[last tertiary ignorable]<<<w&[top]<u", };
0157:                String[][] data = {
0158:                        // {"k", "\u20e3"},
0159:                        { "\\u0000", "a" }, // you cannot go before first tertiary ignorable 
0160:                        { "\\u0000", "a" }, // you cannot go before last tertiary ignorable 
0161:                        { "\\u0000", "a" }, // you cannot go before first secondary ignorable
0162:                        { "\\u0000", "a" }, // you cannot go before first secondary ignorable
0163:                        { "c", "b", "\\u0332", "a" },
0164:                        { "\\u0332", "\\u20e3", "c", "b", "a" },
0165:                        { "c", "b", "\\u0009", "a", "\\u000a" },
0166:                        { "c", "b", "\\uD834\\uDF71", "a", "\\u02d0" },
0167:                        { "b", "\\u02d0", "a", "\\u02d1" },
0168:                        { "b", "\\ud808\\udf6e", "a", "\\u4e00" },
0169:                        { "b", "\\u4e00", "a", "\\u4e01" },
0170:                        { "b", "\\U0010FFFD", "a" },
0171:                        { "\ufffb", "w", "y", "\u20e3", "x", "\u137c", "z", "u" }, };
0172:
0173:                for (int i = 0; i < rules.length; i++) {
0174:                    genericRulesStarter(rules[i], data[i]);
0175:                }
0176:            }
0177:
0178:            void genericRulesStarter(String rules, String[] s) {
0179:                genericRulesStarterWithResult(rules, s, -1);
0180:            }
0181:
0182:            void genericRulesStarterWithResult(String rules, String[] s,
0183:                    int result) {
0184:
0185:                RuleBasedCollator coll = null;
0186:                try {
0187:                    coll = new RuleBasedCollator(rules);
0188:                    // logln("Rules starter for " + rules);
0189:                    genericOrderingTestWithResult(coll, s, result);
0190:                } catch (Exception e) {
0191:                    warnln("Unable to open collator with rules " + rules);
0192:                }
0193:            }
0194:
0195:            void genericRulesStarterWithOptionsAndResult(String rules,
0196:                    String[] s, String[] atts, Object[] attVals, int result) {
0197:                RuleBasedCollator coll = null;
0198:                try {
0199:                    coll = new RuleBasedCollator(rules);
0200:                    genericOptionsSetter(coll, atts, attVals);
0201:                    genericOrderingTestWithResult(coll, s, result);
0202:                } catch (Exception e) {
0203:                    warnln("Unable to open collator with rules " + rules);
0204:                }
0205:            }
0206:
0207:            void genericOrderingTestWithResult(Collator coll, String[] s,
0208:                    int result) {
0209:                String t1 = "";
0210:                String t2 = "";
0211:
0212:                for (int i = 0; i < s.length - 1; i++) {
0213:                    for (int j = i + 1; j < s.length; j++) {
0214:                        t1 = Utility.unescape(s[i]);
0215:                        t2 = Utility.unescape(s[j]);
0216:                        // System.out.println(i + " " + j);
0217:                        CollationTest.doTest(this , (RuleBasedCollator) coll,
0218:                                t1, t2, result);
0219:                    }
0220:                }
0221:            }
0222:
0223:            void reportCResult(String source, String target,
0224:                    CollationKey sourceKey, CollationKey targetKey,
0225:                    int compareResult, int keyResult, int incResult,
0226:                    int expectedResult) {
0227:                if (expectedResult < -1 || expectedResult > 1) {
0228:                    errln("***** invalid call to reportCResult ****");
0229:                    return;
0230:                }
0231:                boolean ok1 = (compareResult == expectedResult);
0232:                boolean ok2 = (keyResult == expectedResult);
0233:                boolean ok3 = (incResult == expectedResult);
0234:                if (ok1 && ok2 && ok3 /* synwee to undo && !isVerbose()*/) {
0235:                    return;
0236:                } else {
0237:                    String msg1 = ok1 ? "Ok: compare(\"" : "FAIL: compare(\"";
0238:                    String msg2 = "\", \"";
0239:                    String msg3 = "\") returned ";
0240:                    String msg4 = "; expected ";
0241:                    String sExpect = new String("");
0242:                    String sResult = new String("");
0243:                    sResult = appendCompareResult(compareResult, sResult);
0244:                    sExpect = appendCompareResult(expectedResult, sExpect);
0245:                    if (ok1) {
0246:                        // logln(msg1 + source + msg2 + target + msg3 + sResult);
0247:                    } else {
0248:                        errln(msg1 + source + msg2 + target + msg3 + sResult
0249:                                + msg4 + sExpect);
0250:                    }
0251:                    msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
0252:                    msg2 = "\").compareTo(key(\"";
0253:                    msg3 = "\")) returned ";
0254:                    sResult = appendCompareResult(keyResult, sResult);
0255:                    if (ok2) {
0256:                        // logln(msg1 + source + msg2 + target + msg3 + sResult);
0257:                    } else {
0258:                        errln(msg1 + source + msg2 + target + msg3 + sResult
0259:                                + msg4 + sExpect);
0260:                        msg1 = "  ";
0261:                        msg2 = " vs. ";
0262:                        errln(msg1 + prettify(sourceKey) + msg2
0263:                                + prettify(targetKey));
0264:                    }
0265:                    msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
0266:                    msg2 = "\", \"";
0267:                    msg3 = "\") returned ";
0268:                    sResult = appendCompareResult(incResult, sResult);
0269:                    if (ok3) {
0270:                        // logln(msg1 + source + msg2 + target + msg3 + sResult);
0271:                    } else {
0272:                        errln(msg1 + source + msg2 + target + msg3 + sResult
0273:                                + msg4 + sExpect);
0274:                    }
0275:                }
0276:            }
0277:
0278:            String appendCompareResult(int result, String target) {
0279:                if (result == -1) { //LESS
0280:                    target += "LESS";
0281:                } else if (result == 0) { //EQUAL
0282:                    target += "EQUAL";
0283:                } else if (result == 1) { //GREATER
0284:                    target += "GREATER";
0285:                } else {
0286:                    String huh = "?";
0287:                    target += huh + result;
0288:                }
0289:                return target;
0290:            }
0291:
0292:            String prettify(CollationKey sourceKey) {
0293:                int i;
0294:                byte[] bytes = sourceKey.toByteArray();
0295:                String target = "[";
0296:
0297:                for (i = 0; i < bytes.length; i++) {
0298:                    target += Integer.toHexString(bytes[i]);
0299:                    target += " ";
0300:                }
0301:                target += "]";
0302:                return target;
0303:            }
0304:
0305:            public void TestBeforePrefixFailure() {
0306:                String[] rules = {
0307:                        "&g <<< a&[before 3]\uff41 <<< x",
0308:                        "&\u30A7=\u30A7=\u3047=\uff6a&\u30A8=\u30A8=\u3048=\uff74&[before 3]\u30a7<<<\u30a9",
0309:                        "&[before 3]\u30a7<<<\u30a9&\u30A7=\u30A7=\u3047=\uff6a&\u30A8=\u30A8=\u3048=\uff74", };
0310:                String[][] data = { { "x", "\uff41" }, { "\u30a9", "\u30a7" },
0311:                        { "\u30a9", "\u30a7" }, };
0312:
0313:                for (int i = 0; i < rules.length; i++) {
0314:                    genericRulesStarter(rules[i], data[i]);
0315:                }
0316:            }
0317:
0318:            public void TestContractionClosure() {
0319:                String[] rules = { "&b=\u00e4\u00e4", "&b=\u00C5", };
0320:                String[][] data = {
0321:                        { "b", "\u00e4\u00e4", "a\u0308a\u0308",
0322:                                "\u00e4a\u0308", "a\u0308\u00e4" },
0323:                        { "b", "\u00C5", "A\u030A", "\u212B" }, };
0324:
0325:                for (int i = 0; i < rules.length; i++) {
0326:                    genericRulesStarterWithResult(rules[i], data[i], 0);
0327:                }
0328:            }
0329:
0330:            public void TestPrefixCompose() {
0331:                String rule1 = "&\u30a7<<<\u30ab|\u30fc=\u30ac|\u30fc";
0332:
0333:                String string = rule1;
0334:                try {
0335:                    RuleBasedCollator coll = new RuleBasedCollator(string);
0336:                    logln("rule:" + coll.getRules());
0337:                } catch (Exception e) {
0338:                    warnln("Error open RuleBasedCollator rule = " + string);
0339:                }
0340:            }
0341:
0342:            public void TestStrCollIdenticalPrefix() {
0343:                String rule = "&\ud9b0\udc70=\ud9b0\udc71";
0344:                String test[] = { "ab\ud9b0\udc70", "ab\ud9b0\udc71" };
0345:                genericRulesStarterWithResult(rule, test, 0);
0346:            }
0347:
0348:            public void TestPrefix() {
0349:                String[] rules = { "&z <<< z|a", "&z <<< z|   a",
0350:                        "[strength I]&a=\ud900\udc25&z<<<\ud900\udc25|a", };
0351:                String[][] data = { { "zz", "za" }, { "zz", "za" },
0352:                        { "aa", "az", "\ud900\udc25z", "\ud900\udc25a", "zz" }, };
0353:
0354:                for (int i = 0; i < rules.length; i++) {
0355:                    genericRulesStarter(rules[i], data[i]);
0356:                }
0357:            }
0358:
0359:            public void TestNewJapanese() {
0360:
0361:                String test1[] = { "\u30b7\u30e3\u30fc\u30ec",
0362:                        "\u30b7\u30e3\u30a4", "\u30b7\u30e4\u30a3",
0363:                        "\u30b7\u30e3\u30ec", "\u3061\u3087\u3053",
0364:                        "\u3061\u3088\u3053",
0365:                        "\u30c1\u30e7\u30b3\u30ec\u30fc\u30c8",
0366:                        "\u3066\u30fc\u305f", "\u30c6\u30fc\u30bf",
0367:                        "\u30c6\u30a7\u30bf", "\u3066\u3048\u305f",
0368:                        "\u3067\u30fc\u305f", "\u30c7\u30fc\u30bf",
0369:                        "\u30c7\u30a7\u30bf", "\u3067\u3048\u305f",
0370:                        "\u3066\u30fc\u305f\u30fc", "\u30c6\u30fc\u30bf\u30a1",
0371:                        "\u30c6\u30a7\u30bf\u30fc", "\u3066\u3047\u305f\u3041",
0372:                        "\u3066\u3048\u305f\u30fc", "\u3067\u30fc\u305f\u30fc",
0373:                        "\u30c7\u30fc\u30bf\u30a1", "\u3067\u30a7\u305f\u30a1",
0374:                        "\u30c7\u3047\u30bf\u3041", "\u30c7\u30a8\u30bf\u30a2",
0375:                        "\u3072\u3086", "\u3073\u3085\u3042",
0376:                        "\u3074\u3085\u3042", "\u3073\u3085\u3042\u30fc",
0377:                        "\u30d3\u30e5\u30a2\u30fc", "\u3074\u3085\u3042\u30fc",
0378:                        "\u30d4\u30e5\u30a2\u30fc", "\u30d2\u30e5\u30a6",
0379:                        "\u30d2\u30e6\u30a6", "\u30d4\u30e5\u30a6\u30a2",
0380:                        "\u3073\u3085\u30fc\u3042\u30fc",
0381:                        "\u30d3\u30e5\u30fc\u30a2\u30fc",
0382:                        "\u30d3\u30e5\u30a6\u30a2\u30fc", "\u3072\u3085\u3093",
0383:                        "\u3074\u3085\u3093", "\u3075\u30fc\u308a",
0384:                        "\u30d5\u30fc\u30ea", "\u3075\u3045\u308a",
0385:                        "\u3075\u30a5\u308a", "\u3075\u30a5\u30ea",
0386:                        "\u30d5\u30a6\u30ea", "\u3076\u30fc\u308a",
0387:                        "\u30d6\u30fc\u30ea", "\u3076\u3045\u308a",
0388:                        "\u30d6\u30a5\u308a", "\u3077\u3046\u308a",
0389:                        "\u30d7\u30a6\u30ea", "\u3075\u30fc\u308a\u30fc",
0390:                        "\u30d5\u30a5\u30ea\u30fc", "\u3075\u30a5\u308a\u30a3",
0391:                        "\u30d5\u3045\u308a\u3043", "\u30d5\u30a6\u30ea\u30fc",
0392:                        "\u3075\u3046\u308a\u3043", "\u30d6\u30a6\u30ea\u30a4",
0393:                        "\u3077\u30fc\u308a\u30fc", "\u3077\u30a5\u308a\u30a4",
0394:                        "\u3077\u3046\u308a\u30fc", "\u30d7\u30a6\u30ea\u30a4",
0395:                        "\u30d5\u30fd", "\u3075\u309e", "\u3076\u309d",
0396:                        "\u3076\u3075", "\u3076\u30d5", "\u30d6\u3075",
0397:                        "\u30d6\u30d5", "\u3076\u309e", "\u3076\u3077",
0398:                        "\u30d6\u3077", "\u3077\u309d", "\u30d7\u30fd",
0399:                        "\u3077\u3075", };
0400:
0401:                String test2[] = { "\u306f\u309d", // H\u309d 
0402:                        "\u30cf\u30fd", // K\u30fd 
0403:                        "\u306f\u306f", // HH 
0404:                        "\u306f\u30cf", // HK 
0405:                        "\u30cf\u30cf", // KK 
0406:                        "\u306f\u309e", // H\u309e 
0407:                        "\u30cf\u30fe", // K\u30fe 
0408:                        "\u306f\u3070", // HH\u309b 
0409:                        "\u30cf\u30d0", // KK\u309b 
0410:                        "\u306f\u3071", // HH\u309c 
0411:                        "\u30cf\u3071", // KH\u309c 
0412:                        "\u30cf\u30d1", // KK\u309c 
0413:                        "\u3070\u309d", // H\u309b\u309d 
0414:                        "\u30d0\u30fd", // K\u309b\u30fd 
0415:                        "\u3070\u306f", // H\u309bH 
0416:                        "\u30d0\u30cf", // K\u309bK 
0417:                        "\u3070\u309e", // H\u309b\u309e 
0418:                        "\u30d0\u30fe", // K\u309b\u30fe 
0419:                        "\u3070\u3070", // H\u309bH\u309b 
0420:                        "\u30d0\u3070", // K\u309bH\u309b 
0421:                        "\u30d0\u30d0", // K\u309bK\u309b 
0422:                        "\u3070\u3071", // H\u309bH\u309c 
0423:                        "\u30d0\u30d1", // K\u309bK\u309c 
0424:                        "\u3071\u309d", // H\u309c\u309d 
0425:                        "\u30d1\u30fd", // K\u309c\u30fd 
0426:                        "\u3071\u306f", // H\u309cH 
0427:                        "\u30d1\u30cf", // K\u309cK 
0428:                        "\u3071\u3070", // H\u309cH\u309b 
0429:                        "\u3071\u30d0", // H\u309cK\u309b 
0430:                        "\u30d1\u30d0", // K\u309cK\u309b
0431:                        "\u3071\u3071", // H\u309cH\u309c
0432:                        "\u30d1\u30d1", // K\u309cK\u309c
0433:                };
0434:
0435:                String[] att = { "strength", };
0436:                Object[] val = { new Integer(Collator.QUATERNARY), };
0437:
0438:                String[] attShifted = { "strength", "AlternateHandling" };
0439:                Object valShifted[] = { new Integer(Collator.QUATERNARY),
0440:                        new Boolean(true) };
0441:
0442:                genericLocaleStarterWithOptions(Locale.JAPANESE, test1, att,
0443:                        val);
0444:                genericLocaleStarterWithOptions(Locale.JAPANESE, test2, att,
0445:                        val);
0446:
0447:                genericLocaleStarterWithOptions(Locale.JAPANESE, test1,
0448:                        attShifted, valShifted);
0449:                genericLocaleStarterWithOptions(Locale.JAPANESE, test2,
0450:                        attShifted, valShifted);
0451:            }
0452:
0453:            void genericLocaleStarter(Locale locale, String s[]) {
0454:                RuleBasedCollator coll = null;
0455:                try {
0456:                    coll = (RuleBasedCollator) Collator.getInstance(locale);
0457:
0458:                } catch (Exception e) {
0459:                    warnln("Unable to open collator for locale " + locale);
0460:                    return;
0461:                }
0462:                // logln("Locale starter for " + locale);
0463:                genericOrderingTest(coll, s);
0464:            }
0465:
0466:            void genericLocaleStarterWithOptions(Locale locale, String[] s,
0467:                    String[] attrs, Object[] values) {
0468:                genericLocaleStarterWithOptionsAndResult(locale, s, attrs,
0469:                        values, -1);
0470:            }
0471:
0472:            private void genericOptionsSetter(RuleBasedCollator coll,
0473:                    String[] attrs, Object[] values) {
0474:                for (int i = 0; i < attrs.length; i++) {
0475:                    if (attrs[i].equals("strength")) {
0476:                        coll.setStrength(((Integer) values[i]).intValue());
0477:                    } else if (attrs[i].equals("decomp")) {
0478:                        coll.setDecomposition(((Integer) values[i]).intValue());
0479:                    } else if (attrs[i].equals("AlternateHandling")) {
0480:                        coll.setAlternateHandlingShifted(((Boolean) values[i])
0481:                                .booleanValue());
0482:                    } else if (attrs[i].equals("NumericCollation")) {
0483:                        coll.setNumericCollation(((Boolean) values[i])
0484:                                .booleanValue());
0485:                    } else if (attrs[i].equals("UpperFirst")) {
0486:                        coll.setUpperCaseFirst(((Boolean) values[i])
0487:                                .booleanValue());
0488:                    } else if (attrs[i].equals("LowerFirst")) {
0489:                        coll.setLowerCaseFirst(((Boolean) values[i])
0490:                                .booleanValue());
0491:                    } else if (attrs[i].equals("CaseLevel")) {
0492:                        coll.setCaseLevel(((Boolean) values[i]).booleanValue());
0493:                    }
0494:                }
0495:            }
0496:
0497:            void genericLocaleStarterWithOptionsAndResult(Locale locale,
0498:                    String[] s, String[] attrs, Object[] values, int result) {
0499:                RuleBasedCollator coll = null;
0500:                try {
0501:                    coll = (RuleBasedCollator) Collator.getInstance(locale);
0502:                } catch (Exception e) {
0503:                    warnln("Unable to open collator for locale " + locale);
0504:                    return;
0505:                }
0506:                // logln("Locale starter for " +locale);
0507:
0508:                // logln("Setting attributes");
0509:                genericOptionsSetter(coll, attrs, values);
0510:
0511:                genericOrderingTestWithResult(coll, s, result);
0512:            }
0513:
0514:            void genericOrderingTest(Collator coll, String[] s) {
0515:                genericOrderingTestWithResult(coll, s, -1);
0516:            }
0517:
0518:            public void TestNonChars() {
0519:                String test[] = { "\u0000", "\uFFFE", "\uFFFF", "\\U0001FFFE",
0520:                        "\\U0001FFFF", "\\U0002FFFE", "\\U0002FFFF",
0521:                        "\\U0003FFFE", "\\U0003FFFF", "\\U0004FFFE",
0522:                        "\\U0004FFFF", "\\U0005FFFE", "\\U0005FFFF",
0523:                        "\\U0006FFFE", "\\U0006FFFF", "\\U0007FFFE",
0524:                        "\\U0007FFFF", "\\U0008FFFE", "\\U0008FFFF",
0525:                        "\\U0009FFFE", "\\U0009FFFF", "\\U000AFFFE",
0526:                        "\\U000AFFFF", "\\U000BFFFE", "\\U000BFFFF",
0527:                        "\\U000CFFFE", "\\U000CFFFF", "\\U000DFFFE",
0528:                        "\\U000DFFFF", "\\U000EFFFE", "\\U000EFFFF",
0529:                        "\\U000FFFFE", "\\U000FFFFF", "\\U0010FFFE",
0530:                        "\\U0010FFFF" };
0531:                Collator coll = null;
0532:                try {
0533:                    coll = Collator.getInstance(new Locale("en", "US"));
0534:                } catch (Exception e) {
0535:                    warnln("Unable to open collator");
0536:                    return;
0537:                }
0538:                // logln("Test non characters");
0539:
0540:                genericOrderingTestWithResult(coll, test, 0);
0541:            }
0542:
0543:            public void TestExtremeCompression() {
0544:                String[] test = new String[4];
0545:
0546:                for (int i = 0; i < 4; i++) {
0547:                    StringBuffer temp = new StringBuffer();
0548:                    for (int j = 0; j < 2047; j++) {
0549:                        temp.append('a');
0550:                    }
0551:                    temp.append((char) ('a' + i));
0552:                    test[i] = temp.toString();
0553:                }
0554:
0555:                genericLocaleStarter(new Locale("en", "US"), test);
0556:            }
0557:
0558:            /**
0559:             * Tests surrogate support.
0560:             */
0561:            public void TestSurrogates() {
0562:                String test[] = { "z", "\ud900\udc25", "\ud805\udc50",
0563:                        "\ud800\udc00y", "\ud800\udc00r", "\ud800\udc00f",
0564:                        "\ud800\udc00", "\ud800\udc00c", "\ud800\udc00b",
0565:                        "\ud800\udc00fa", "\ud800\udc00fb", "\ud800\udc00a",
0566:                        "c", "b" };
0567:
0568:                String rule = "&z < \ud900\udc25 < \ud805\udc50 < \ud800\udc00y "
0569:                        + "< \ud800\udc00r < \ud800\udc00f << \ud800\udc00 "
0570:                        + "< \ud800\udc00fa << \ud800\udc00fb < \ud800\udc00a "
0571:                        + "< c < b";
0572:                genericRulesStarter(rule, test);
0573:            }
0574:
0575:            public void TestBocsuCoverage() {
0576:                String test = "\u0041\u0441\u4441\\U00044441\u4441\u0441\u0041";
0577:                Collator coll = Collator.getInstance();
0578:                coll.setStrength(Collator.IDENTICAL);
0579:                CollationKey key = coll.getCollationKey(test);
0580:                logln("source:" + key.getSourceString());
0581:            }
0582:
0583:            public void TestCyrillicTailoring() {
0584:                String test[] = { "\u0410b", "\u0410\u0306a", "\u04d0A" };
0585:                genericLocaleStarter(new Locale("en", ""), test);
0586:                genericRulesStarter("&\u0410 = \u0410", test);
0587:                genericRulesStarter("&Z < \u0410", test);
0588:                genericRulesStarter("&\u0410 = \u0410 < \u04d0", test);
0589:                genericRulesStarter("&Z < \u0410 < \u04d0", test);
0590:                genericRulesStarter("&\u0410 = \u0410 < \u0410\u0301", test);
0591:                genericRulesStarter("&Z < \u0410 < \u0410\u0301", test);
0592:            }
0593:
0594:            public void TestSuppressContractions() {
0595:                String testNoCont2[] = { "\u0410\u0302a", "\u0410\u0306b",
0596:                        "\u0410c" };
0597:                String testNoCont[] = { "a\u0410", "A\u0410\u0306",
0598:                        "\uFF21\u0410\u0302" };
0599:
0600:                genericRulesStarter("[suppressContractions [\u0400-\u047f]]",
0601:                        testNoCont);
0602:                genericRulesStarter("[suppressContractions [\u0400-\u047f]]",
0603:                        testNoCont2);
0604:            }
0605:
0606:            public void TestCase() {
0607:                String gRules = "\u0026\u0030\u003C\u0031\u002C\u2460\u003C\u0061\u002C\u0041";
0608:                String[] testCase = { "1a", "1A", "\u2460a", "\u2460A" };
0609:                int[][] caseTestResults = {
0610:                        { -1, -1, -1, 0, -1, -1, 0, 0, -1 },
0611:                        { 1, -1, -1, 0, -1, -1, 0, 0, 1 },
0612:                        { -1, -1, -1, 0, 1, -1, 0, 0, -1 },
0613:                        { 1, -1, 1, 0, -1, -1, 0, 0, 1 }
0614:
0615:                };
0616:                boolean[][] caseTestAttributes = { { false, false },
0617:                        { true, false }, { false, true }, { true, true } };
0618:
0619:                int i, j, k;
0620:                Collator myCollation;
0621:                try {
0622:                    myCollation = Collator.getInstance(new Locale("en", "US"));
0623:                } catch (Exception e) {
0624:                    warnln("ERROR: in creation of rule based collator ");
0625:                    return;
0626:                }
0627:                // logln("Testing different case settings");
0628:                myCollation.setStrength(Collator.TERTIARY);
0629:
0630:                for (k = 0; k < 4; k++) {
0631:                    if (caseTestAttributes[k][0] == true) {
0632:                        // upper case first
0633:                        ((RuleBasedCollator) myCollation)
0634:                                .setUpperCaseFirst(true);
0635:                    } else {
0636:                        // upper case first
0637:                        ((RuleBasedCollator) myCollation)
0638:                                .setLowerCaseFirst(true);
0639:                    }
0640:                    ((RuleBasedCollator) myCollation)
0641:                            .setCaseLevel(caseTestAttributes[k][1]);
0642:
0643:                    // logln("Case first = " + caseTestAttributes[k][0] + ", Case level = " + caseTestAttributes[k][1]);
0644:                    for (i = 0; i < 3; i++) {
0645:                        for (j = i + 1; j < 4; j++) {
0646:                            CollationTest.doTest(this ,
0647:                                    (RuleBasedCollator) myCollation,
0648:                                    testCase[i], testCase[j],
0649:                                    caseTestResults[k][3 * i + j - 1]);
0650:                        }
0651:                    }
0652:                }
0653:                try {
0654:                    myCollation = new RuleBasedCollator(gRules);
0655:                } catch (Exception e) {
0656:                    warnln("ERROR: in creation of rule based collator");
0657:                    return;
0658:                }
0659:                // logln("Testing different case settings with custom rules");
0660:                myCollation.setStrength(Collator.TERTIARY);
0661:
0662:                for (k = 0; k < 4; k++) {
0663:                    if (caseTestAttributes[k][0] == true) {
0664:                        ((RuleBasedCollator) myCollation)
0665:                                .setUpperCaseFirst(true);
0666:                    } else {
0667:                        ((RuleBasedCollator) myCollation)
0668:                                .setUpperCaseFirst(false);
0669:                    }
0670:                    ((RuleBasedCollator) myCollation)
0671:                            .setCaseLevel(caseTestAttributes[k][1]);
0672:                    for (i = 0; i < 3; i++) {
0673:                        for (j = i + 1; j < 4; j++) {
0674:                            CollationTest.doTest(this ,
0675:                                    (RuleBasedCollator) myCollation,
0676:                                    testCase[i], testCase[j],
0677:                                    caseTestResults[k][3 * i + j - 1]);
0678:                        }
0679:                    }
0680:                }
0681:
0682:                {
0683:                    String[] lowerFirst = { "h", "H", "ch", "Ch", "CH", "cha",
0684:                            "chA", "Cha", "ChA", "CHa", "CHA", "i", "I" };
0685:
0686:                    String[] upperFirst = { "H", "h", "CH", "Ch", "ch", "CHA",
0687:                            "CHa", "ChA", "Cha", "chA", "cha", "I", "i" };
0688:                    // logln("mixed case test");
0689:                    // logln("lower first, case level off");
0690:                    genericRulesStarter("[casefirst lower]&H<ch<<<Ch<<<CH",
0691:                            lowerFirst);
0692:                    // logln("upper first, case level off");
0693:                    genericRulesStarter("[casefirst upper]&H<ch<<<Ch<<<CH",
0694:                            upperFirst);
0695:                    // logln("lower first, case level on");
0696:                    genericRulesStarter(
0697:                            "[casefirst lower][caselevel on]&H<ch<<<Ch<<<CH",
0698:                            lowerFirst);
0699:                    // logln("upper first, case level on");
0700:                    genericRulesStarter(
0701:                            "[casefirst upper][caselevel on]&H<ch<<<Ch<<<CH",
0702:                            upperFirst);
0703:                }
0704:            }
0705:
0706:            public void TestIncompleteCnt() {
0707:                String[] cnt1 = { "AA", "AC", "AZ", "AQ", "AB", "ABZ", "ABQ",
0708:                        "Z", "ABC", "Q", "B" };
0709:
0710:                String[] cnt2 = { "DA", "DAD", "DAZ", "MAR", "Z", "DAVIS",
0711:                        "MARK", "DAV", "DAVI" };
0712:                RuleBasedCollator coll = null;
0713:                String temp = " & Z < ABC < Q < B";
0714:                try {
0715:                    coll = new RuleBasedCollator(temp);
0716:                } catch (Exception e) {
0717:                    warnln("fail to create RuleBasedCollator");
0718:                    return;
0719:                }
0720:
0721:                int size = cnt1.length;
0722:                for (int i = 0; i < size - 1; i++) {
0723:                    for (int j = i + 1; j < size; j++) {
0724:                        String t1 = cnt1[i];
0725:                        String t2 = cnt1[j];
0726:                        CollationTest.doTest(this , coll, t1, t2, -1);
0727:                    }
0728:                }
0729:
0730:                temp = " & Z < DAVIS < MARK <DAV";
0731:                try {
0732:                    coll = new RuleBasedCollator(temp);
0733:                } catch (Exception e) {
0734:                    warnln("fail to create RuleBasedCollator");
0735:                    return;
0736:                }
0737:
0738:                size = cnt2.length;
0739:                for (int i = 0; i < size - 1; i++) {
0740:                    for (int j = i + 1; j < size; j++) {
0741:                        String t1 = cnt2[i];
0742:                        String t2 = cnt2[j];
0743:                        CollationTest.doTest(this , coll, t1, t2, -1);
0744:                    }
0745:                }
0746:            }
0747:
0748:            public void TestBlackBird() {
0749:                String[] shifted = { "black bird", "black-bird", "blackbird",
0750:                        "black Bird", "black-Bird", "blackBird", "black birds",
0751:                        "black-birds", "blackbirds" };
0752:                int[] shiftedTert = { 0, 0, 0, -1, 0, 0, -1, 0, 0 };
0753:                String[] nonignorable = { "black bird", "black Bird",
0754:                        "black birds", "black-bird", "black-Bird",
0755:                        "black-birds", "blackbird", "blackBird", "blackbirds" };
0756:                int i = 0, j = 0;
0757:                int size = 0;
0758:                Collator coll = Collator.getInstance(new Locale("en", "US"));
0759:                //ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_OFF, &status);
0760:                //ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &status);
0761:                ((RuleBasedCollator) coll).setAlternateHandlingShifted(false);
0762:                size = nonignorable.length;
0763:                for (i = 0; i < size - 1; i++) {
0764:                    for (j = i + 1; j < size; j++) {
0765:                        String t1 = nonignorable[i];
0766:                        String t2 = nonignorable[j];
0767:                        CollationTest.doTest(this , (RuleBasedCollator) coll,
0768:                                t1, t2, -1);
0769:                    }
0770:                }
0771:                ((RuleBasedCollator) coll).setAlternateHandlingShifted(true);
0772:                coll.setStrength(Collator.QUATERNARY);
0773:                size = shifted.length;
0774:                for (i = 0; i < size - 1; i++) {
0775:                    for (j = i + 1; j < size; j++) {
0776:                        String t1 = shifted[i];
0777:                        String t2 = shifted[j];
0778:                        CollationTest.doTest(this , (RuleBasedCollator) coll,
0779:                                t1, t2, -1);
0780:                    }
0781:                }
0782:                coll.setStrength(Collator.TERTIARY);
0783:                size = shifted.length;
0784:                for (i = 1; i < size; i++) {
0785:                    String t1 = shifted[i - 1];
0786:                    String t2 = shifted[i];
0787:                    CollationTest.doTest(this , (RuleBasedCollator) coll, t1,
0788:                            t2, shiftedTert[i]);
0789:                }
0790:            }
0791:
0792:            public void TestFunkyA() {
0793:                String[] testSourceCases = { "\u0041\u0300\u0301",
0794:                        "\u0041\u0300\u0316", "\u0041\u0300", "\u00C0\u0301",
0795:                        // this would work with forced normalization 
0796:                        "\u00C0\u0316", };
0797:
0798:                String[] testTargetCases = { "\u0041\u0301\u0300",
0799:                        "\u0041\u0316\u0300", "\u00C0", "\u0041\u0301\u0300",
0800:                        // this would work with forced normalization 
0801:                        "\u0041\u0316\u0300", };
0802:
0803:                int[] results = { 1, 0, 0, 1, 0 };
0804:
0805:                Collator myCollation;
0806:                try {
0807:                    myCollation = Collator.getInstance(new Locale("en", "US"));
0808:                } catch (Exception e) {
0809:                    warnln("ERROR: in creation of rule based collator");
0810:                    return;
0811:                }
0812:                // logln("Testing some A letters, for some reason");
0813:                myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
0814:                myCollation.setStrength(Collator.TERTIARY);
0815:                for (int i = 0; i < 4; i++) {
0816:                    CollationTest.doTest(this , (RuleBasedCollator) myCollation,
0817:                            testSourceCases[i], testTargetCases[i], results[i]);
0818:                }
0819:            }
0820:
0821:            public void TestChMove() {
0822:                String[] chTest = { "c", "C", "ca", "cb", "cx", "cy", "CZ",
0823:                        "c\u030C", "C\u030C", "h", "H", "ha", "Ha", "harly",
0824:                        "hb", "HB", "hx", "HX", "hy", "HY", "ch", "cH", "Ch",
0825:                        "CH", "cha", "charly", "che", "chh", "chch", "chr",
0826:                        "i", "I", "iarly", "r", "R", "r\u030C", "R\u030C", "s",
0827:                        "S", "s\u030C", "S\u030C", "z", "Z", "z\u030C",
0828:                        "Z\u030C" };
0829:                Collator coll = null;
0830:                try {
0831:                    coll = Collator.getInstance(new Locale("cs", ""));
0832:                } catch (Exception e) {
0833:                    warnln("Cannot create Collator");
0834:                    return;
0835:                }
0836:                int size = chTest.length;
0837:                for (int i = 0; i < size - 1; i++) {
0838:                    for (int j = i + 1; j < size; j++) {
0839:                        String t1 = chTest[i];
0840:                        String t2 = chTest[j];
0841:                        CollationTest.doTest(this , (RuleBasedCollator) coll,
0842:                                t1, t2, -1);
0843:                    }
0844:                }
0845:            }
0846:
0847:            public void TestImplicitTailoring() {
0848:                String rules[] = {
0849:                        "&[before 1]\u4e00 < b < c &[before 1]\u4e00 < d < e",
0850:                        "&\u4e00 < a <<< A < b <<< B",
0851:                        "&[before 1]\u4e00 < \u4e01 < \u4e02",
0852:                        "&[before 1]\u4e01 < \u4e02 < \u4e03", };
0853:                String cases[][] = { { "d", "e", "b", "c", "\u4e00" },
0854:                        { "\u4e00", "a", "A", "b", "B", "\u4e01" },
0855:                        { "\u4e01", "\u4e02", "\u4e00" },
0856:                        { "\u4e02", "\u4e03", "\u4e01" }, };
0857:
0858:                int i = 0;
0859:
0860:                for (i = 0; i < rules.length; i++) {
0861:                    genericRulesStarter(rules[i], cases[i]);
0862:                }
0863:
0864:            }
0865:
0866:            public void TestFCDProblem() {
0867:                String s1 = "\u0430\u0306\u0325";
0868:                String s2 = "\u04D1\u0325";
0869:                Collator coll = null;
0870:                try {
0871:                    coll = Collator.getInstance();
0872:                } catch (Exception e) {
0873:                    warnln("Can't create collator");
0874:                    return;
0875:                }
0876:
0877:                coll.setDecomposition(Collator.NO_DECOMPOSITION);
0878:                CollationTest.doTest(this , (RuleBasedCollator) coll, s1, s2, 0);
0879:                coll.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
0880:                CollationTest.doTest(this , (RuleBasedCollator) coll, s1, s2, 0);
0881:            }
0882:
0883:            public void TestEmptyRule() {
0884:                String rulez = "";
0885:                try {
0886:                    RuleBasedCollator coll = new RuleBasedCollator(rulez);
0887:                    logln("rule:" + coll.getRules());
0888:                } catch (Exception e) {
0889:                    warnln(e.getMessage());
0890:                }
0891:            }
0892:
0893:            /* superseded by TestBeforePinyin, since Chinese collation rules have changed */
0894:            /*
0895:            public void TestJ784() {
0896:                String[] data = {
0897:                    "A", "\u0101", "\u00e1", "\u01ce", "\u00e0",
0898:                    "E", "\u0113", "\u00e9", "\u011b", "\u00e8",
0899:                    "I", "\u012b", "\u00ed", "\u01d0", "\u00ec",
0900:                    "O", "\u014d", "\u00f3", "\u01d2", "\u00f2",
0901:                    "U", "\u016b", "\u00fa", "\u01d4", "\u00f9",
0902:                    "\u00fc", "\u01d6", "\u01d8", "\u01da", "\u01dc"
0903:                };
0904:                genericLocaleStarter(new Locale("zh", ""), data);
0905:            }
0906:             */
0907:
0908:            public void TestJ815() {
0909:                String data[] = { "aa", "Aa", "ab", "Ab", "ad", "Ad", "ae",
0910:                        "Ae", "\u00e6", "\u00c6", "af", "Af", "b", "B" };
0911:                genericLocaleStarter(new Locale("fr", ""), data);
0912:                genericRulesStarter("[backwards 2]&A<<\u00e6/e<<<\u00c6/E",
0913:                        data);
0914:            }
0915:
0916:            public void TestJ3087() {
0917:                String rule[] = { "&h<H&CH=\u0427", "&CH=\u0427&h<H",
0918:                        "&CH=\u0427" };
0919:                RuleBasedCollator rbc = null;
0920:                CollationElementIterator iter1;
0921:                CollationElementIterator iter2;
0922:                for (int i = 0; i < rule.length; i++) {
0923:                    try {
0924:                        rbc = new RuleBasedCollator(rule[i]);
0925:                    } catch (Exception e) {
0926:                        warnln(e.getMessage());
0927:                        return;
0928:                    }
0929:                    iter1 = rbc.getCollationElementIterator("CH");
0930:                    iter2 = rbc.getCollationElementIterator("\u0427");
0931:                    int ce1 = CollationElementIterator.IGNORABLE;
0932:                    int ce2 = CollationElementIterator.IGNORABLE;
0933:                    while (ce1 != CollationElementIterator.NULLORDER
0934:                            && ce2 != CollationElementIterator.NULLORDER) {
0935:                        ce1 = iter1.next();
0936:                        ce2 = iter2.next();
0937:                        if (ce1 != ce2) {
0938:                            errln("Error generating RuleBasedCollator with the rule "
0939:                                    + rule[i]);
0940:                            errln("CH != \\u0427");
0941:                        }
0942:                    }
0943:                }
0944:            }
0945:
0946:            public void DontTestJ831() { // Latvian does not use upper first
0947:                String[] data = { "I", "i", "Y", "y" };
0948:                genericLocaleStarter(new Locale("lv", ""), data);
0949:            }
0950:
0951:            public void TestBefore() {
0952:                String data[] = { "\u0101", "\u00e1", "\u01ce", "\u00e0", "A",
0953:                        "\u0113", "\u00e9", "\u011b", "\u00e8", "E", "\u012b",
0954:                        "\u00ed", "\u01d0", "\u00ec", "I", "\u014d", "\u00f3",
0955:                        "\u01d2", "\u00f2", "O", "\u016b", "\u00fa", "\u01d4",
0956:                        "\u00f9", "U", "\u01d6", "\u01d8", "\u01da", "\u01dc",
0957:                        "\u00fc" };
0958:                genericRulesStarter("&[before 1]a<\u0101<\u00e1<\u01ce<\u00e0"
0959:                        + "&[before 1]e<\u0113<\u00e9<\u011b<\u00e8"
0960:                        + "&[before 1]i<\u012b<\u00ed<\u01d0<\u00ec"
0961:                        + "&[before 1]o<\u014d<\u00f3<\u01d2<\u00f2"
0962:                        + "&[before 1]u<\u016b<\u00fa<\u01d4<\u00f9"
0963:                        + "&u<\u01d6<\u01d8<\u01da<\u01dc<\u00fc", data);
0964:            }
0965:
0966:            public void TestRedundantRules() {
0967:                String[] rules = {
0968:                        //"& a <<< b <<< c << d <<< e& [before 1] e <<< x",
0969:                        "& b <<< c <<< d << e <<< f& [before 3] f <<< x",
0970:                        "& a < b <<< c << d <<< e& [before 1] e <<< x",
0971:                        "& a < b < c < d& [before 1] c < m",
0972:                        "& a < b <<< c << d <<< e& [before 3] e <<< x",
0973:                        "& a < b <<< c << d <<< e& [before 2] e <<< x",
0974:                        "& a < b <<< c << d <<< e <<< f < g& [before 1] g < x",
0975:                        "& a <<< b << c < d& a < m", "&a<b<<b\u0301 &z<b",
0976:                        "&z<m<<<q<<<m", "&z<<<m<q<<<m",
0977:                        "& a < b < c < d& r < c", "& a < b < c < d& r < c",
0978:                        "& a < b < c < d& c < m", "& a < b < c < d& a < m" };
0979:
0980:                String[] expectedRules = {
0981:                        //"&\u2089<<<x",
0982:                        "&\u0252<<<x", "& a <<< x < b <<< c << d <<< e",
0983:                        "& a < b < m < c < d",
0984:                        "& a < b <<< c << d <<< x <<< e",
0985:                        "& a < b <<< c <<< x << d <<< e",
0986:                        "& a < b <<< c << d <<< e <<< f < x < g",
0987:                        "& a <<< b << c < m < d", "&a<b\u0301 &z<b",
0988:                        "&z<q<<<m", "&z<q<<<m", "& a < b < d& r < c",
0989:                        "& a < b < d& r < c", "& a < b < c < m < d",
0990:                        "& a < m < b < c < d" };
0991:
0992:                String[][] testdata = {
0993:                        //            {"\u2089", "x"},
0994:                        { "\u0252", "x" }, { "a", "x", "b", "c", "d", "e" },
0995:                        { "a", "b", "m", "c", "d" },
0996:                        { "a", "b", "c", "d", "x", "e" },
0997:                        { "a", "b", "c", "x", "d", "e" },
0998:                        { "a", "b", "c", "d", "e", "f", "x", "g" },
0999:                        { "a", "b", "c", "m", "d" },
1000:                        { "a", "b\u0301", "z", "b" }, { "z", "q", "m" },
1001:                        { "z", "q", "m" }, { "a", "b", "d" }, { "r", "c" },
1002:                        { "a", "b", "c", "m", "d" },
1003:                        { "a", "m", "b", "c", "d" } };
1004:
1005:                String rlz = "";
1006:                for (int i = 0; i < rules.length; i++) {
1007:                    logln("testing rule " + rules[i] + ", expected to be"
1008:                            + expectedRules[i]);
1009:                    try {
1010:                        rlz = rules[i];
1011:                        Collator credundant = new RuleBasedCollator(rlz);
1012:                        rlz = expectedRules[i];
1013:                        Collator cresulting = new RuleBasedCollator(rlz);
1014:                        logln(" credundant Rule:"
1015:                                + ((RuleBasedCollator) credundant).getRules());
1016:                        logln(" cresulting Rule:"
1017:                                + ((RuleBasedCollator) cresulting).getRules());
1018:                    } catch (Exception e) {
1019:                        warnln("Cannot create RuleBasedCollator");
1020:                    }
1021:                    //testAgainstUCA(cresulting, credundant, "expected", TRUE, &status);
1022:                    // logln("testing using data\n");
1023:                    genericRulesStarter(rules[i], testdata[i]);
1024:                }
1025:            }
1026:
1027:            public void TestExpansionSyntax() {
1028:                String[] rules = { "&AE <<< a << b <<< c &d <<< f",
1029:                        "&AE <<< a <<< b << c << d < e < f <<< g",
1030:                        "&AE <<< B <<< C / D <<< F" };
1031:
1032:                String[] expectedRules = {
1033:                        "&A <<< a / E << b / E <<< c /E  &d <<< f",
1034:                        "&A <<< a / E <<< b / E << c / E << d / E < e < f <<< g",
1035:                        "&A <<< B / E <<< C / ED <<< F / E" };
1036:
1037:                String[][] testdata = { { "AE", "a", "b", "c" },
1038:                        { "AE", "a", "b", "c", "d", "e", "f", "g" },
1039:                        { "AE", "B", "C" } // / ED <<< F / E"},
1040:                };
1041:
1042:                for (int i = 0; i < rules.length; i++) {
1043:                    // logln("testing rule " + rules[i] + ", expected to be " + expectedRules[i]);
1044:                    try {
1045:                        String rlz = rules[i];
1046:                        Collator credundant = new RuleBasedCollator(rlz);
1047:                        rlz = expectedRules[i];
1048:                        Collator cresulting = new RuleBasedCollator(rlz);
1049:                        logln(" credundant Rule:"
1050:                                + ((RuleBasedCollator) credundant).getRules());
1051:                        logln(" cresulting Rule:"
1052:                                + ((RuleBasedCollator) cresulting).getRules());
1053:                    } catch (Exception e) {
1054:                        warnln(e.getMessage());
1055:                    }
1056:                    // testAgainstUCA still doesn't handle expansions correctly, so this is not run 
1057:                    // as a hard error test, but only in information mode 
1058:                    //testAgainstUCA(cresulting, credundant, "expected", FALSE, &status);
1059:
1060:                    // logln("testing using data");
1061:                    genericRulesStarter(rules[i], testdata[i]);
1062:                }
1063:            }
1064:
1065:            public void TestHangulTailoring() {
1066:                String[] koreanData = { "\uac00", "\u4f3d", "\u4f73", "\u5047",
1067:                        "\u50f9", "\u52a0", "\u53ef", "\u5475", "\u54e5",
1068:                        "\u5609", "\u5ac1", "\u5bb6", "\u6687", "\u67b6",
1069:                        "\u67b7", "\u67ef", "\u6b4c", "\u73c2", "\u75c2",
1070:                        "\u7a3c", "\u82db", "\u8304", "\u8857", "\u8888",
1071:                        "\u8a36", "\u8cc8", "\u8dcf", "\u8efb", "\u8fe6",
1072:                        "\u99d5", "\u4EEE", "\u50A2", "\u5496", "\u54FF",
1073:                        "\u5777", "\u5B8A", "\u659D", "\u698E", "\u6A9F",
1074:                        "\u73C8", "\u7B33", "\u801E", "\u8238", "\u846D",
1075:                        "\u8B0C" };
1076:
1077:                String rules = "&\uac00 <<< \u4f3d <<< \u4f73 <<< \u5047 <<< \u50f9 <<< \u52a0 <<< \u53ef <<< \u5475 "
1078:                        + "<<< \u54e5 <<< \u5609 <<< \u5ac1 <<< \u5bb6 <<< \u6687 <<< \u67b6 <<< \u67b7 <<< \u67ef "
1079:                        + "<<< \u6b4c <<< \u73c2 <<< \u75c2 <<< \u7a3c <<< \u82db <<< \u8304 <<< \u8857 <<< \u8888 "
1080:                        + "<<< \u8a36 <<< \u8cc8 <<< \u8dcf <<< \u8efb <<< \u8fe6 <<< \u99d5 "
1081:                        + "<<< \u4EEE <<< \u50A2 <<< \u5496 <<< \u54FF <<< \u5777 <<< \u5B8A <<< \u659D <<< \u698E "
1082:                        + "<<< \u6A9F <<< \u73C8 <<< \u7B33 <<< \u801E <<< \u8238 <<< \u846D <<< \u8B0C";
1083:
1084:                String rlz = rules;
1085:
1086:                Collator coll = null;
1087:                try {
1088:                    coll = new RuleBasedCollator(rlz);
1089:                } catch (Exception e) {
1090:                    warnln("Unable to open collator with rules" + rules);
1091:                    return;
1092:                }
1093:                // logln("Using start of korean rules\n");
1094:                genericOrderingTest(coll, koreanData);
1095:                // logln("Setting jamoSpecial to TRUE and testing once more\n");
1096:
1097:                // can't set jamo in icu4j 
1098:                // ((UCATableHeader *)coll->image)->jamoSpecial = TRUE; // don't try this at home
1099:                // genericOrderingTest(coll, koreanData);
1100:
1101:                // no such locale in icu4j
1102:                // logln("Using ko__LOTUS locale\n");
1103:                // genericLocaleStarter(new Locale("ko__LOTUS", ""), koreanData);
1104:            }
1105:
1106:            public void TestIncrementalNormalize() {
1107:                Collator coll = null;
1108:                // logln("Test 1 ....");
1109:                {
1110:                    /* Test 1.  Run very long unnormalized strings, to force overflow of*/
1111:                    /*          most buffers along the way.*/
1112:
1113:                    try {
1114:                        coll = Collator.getInstance(new Locale("en", "US"));
1115:                    } catch (Exception e) {
1116:                        warnln("Cannot get default instance!");
1117:                        return;
1118:                    }
1119:                    char baseA = 0x41;
1120:                    char ccMix[] = { 0x316, 0x321, 0x300 };
1121:                    int sLen;
1122:                    int i;
1123:                    StringBuffer strA = new StringBuffer();
1124:                    StringBuffer strB = new StringBuffer();
1125:
1126:                    coll.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
1127:
1128:                    for (sLen = 1000; sLen < 1001; sLen++) {
1129:                        strA.delete(0, strA.length());
1130:                        strA.append(baseA);
1131:                        strB.delete(0, strB.length());
1132:                        strB.append(baseA);
1133:                        for (i = 1; i < sLen; i++) {
1134:                            strA.append(ccMix[i % 3]);
1135:                            strB.insert(1, ccMix[i % 3]);
1136:                        }
1137:                        coll.setStrength(Collator.TERTIARY); // Do test with default strength, which runs
1138:                        CollationTest.doTest(this , (RuleBasedCollator) coll,
1139:                                strA.toString(), strB.toString(), 0); //   optimized functions in the impl
1140:                        coll.setStrength(Collator.IDENTICAL); // Do again with the slow, general impl.
1141:                        CollationTest.doTest(this , (RuleBasedCollator) coll,
1142:                                strA.toString(), strB.toString(), 0);
1143:                    }
1144:                }
1145:                /*  Test 2:  Non-normal sequence in a string that extends to the last character*/
1146:                /*         of the string.  Checks a couple of edge cases.*/
1147:                // logln("Test 2 ....");    
1148:                {
1149:                    String strA = "AA\u0300\u0316";
1150:                    String strB = "A\u00c0\u0316";
1151:                    coll.setStrength(Collator.TERTIARY);
1152:                    CollationTest.doTest(this , (RuleBasedCollator) coll, strA,
1153:                            strB, 0);
1154:                }
1155:                /*  Test 3:  Non-normal sequence is terminated by a surrogate pair.*/
1156:                // logln("Test 3 ....");
1157:                {
1158:                    String strA = "AA\u0300\u0316\uD800\uDC01";
1159:                    String strB = "A\u00c0\u0316\uD800\uDC00";
1160:                    coll.setStrength(Collator.TERTIARY);
1161:                    CollationTest.doTest(this , (RuleBasedCollator) coll, strA,
1162:                            strB, 1);
1163:                }
1164:                /*  Test 4:  Imbedded nulls do not terminate a string when length is specified.*/
1165:                // logln("Test 4 ....");
1166:                /*
1167:                 * not a valid test since string are null-terminated in java{
1168:                 char strA[] = {0x41, 0x00, 0x42};
1169:                 char strB[] = {0x41, 0x00, 0x00};
1170:                    
1171:                 int result = coll.compare(new String(strA), new String(strB));
1172:                 if (result != 1) {
1173:                 errln("ERROR 1 in test 4\n");
1174:                 }
1175:                    
1176:                 result = coll.compare(new String(strA, 0, 1), new String(strB, 0, 1));
1177:                 if (result != 0) {
1178:                 errln("ERROR 1 in test 4\n");
1179:                 }
1180:                    
1181:                 CollationKey sortKeyA = coll.getCollationKey(new String(strA));
1182:                 CollationKey sortKeyB = coll.getCollationKey(new String(strB));
1183:                    
1184:                 int r = sortKeyA.compareTo(sortKeyB);
1185:                 if (r <= 0) {
1186:                 errln("Error 4 in test 4\n");
1187:                 }
1188:                
1189:                 coll.setStrength(Collator.IDENTICAL);
1190:                 sortKeyA = coll.getCollationKey(new String(strA));
1191:                 sortKeyB = coll.getCollationKey(new String(strB));
1192:                
1193:                 r = sortKeyA.compareTo(sortKeyB);
1194:                 if (r <= 0) {
1195:                 errln("Error 7 in test 4\n");
1196:                 }
1197:                    
1198:                 coll.setStrength(Collator.TERTIARY);
1199:                 }
1200:                 */
1201:                /*  Test 5:  Null characters in non-normal source strings.*/
1202:                // logln("Test 5 ....");
1203:                /*
1204:                 * not a valid test since string are null-terminated in java{
1205:                 {
1206:                 char strA[] = {0x41, 0x41, 0x300, 0x316, 0x00, 0x42,};
1207:                 char strB[] = {0x41, 0x41, 0x300, 0x316, 0x00, 0x00,};
1208:                   
1209:                
1210:                 int result = coll.compare(new String(strA, 0, 6), new String(strB, 0, 6));
1211:                 if (result < 0) {
1212:                 errln("ERROR 1 in test 5\n");
1213:                 }
1214:                 result = coll.compare(new String(strA, 0, 4), new String(strB, 0, 4));
1215:                 if (result != 0) {
1216:                 errln("ERROR 2 in test 5\n");
1217:                 }
1218:                
1219:                 CollationKey sortKeyA = coll.getCollationKey(new String(strA));
1220:                 CollationKey sortKeyB = coll.getCollationKey(new String(strB));
1221:                 int r = sortKeyA.compareTo(sortKeyB);
1222:                 if (r <= 0) {
1223:                 errln("Error 4 in test 5\n");
1224:                 }
1225:                
1226:                 coll.setStrength(Collator.IDENTICAL);
1227:                    
1228:                 sortKeyA = coll.getCollationKey(new String(strA));
1229:                 sortKeyB = coll.getCollationKey(new String(strB));
1230:                 r = sortKeyA.compareTo(sortKeyB);
1231:                 if (r <= 0) {
1232:                 errln("Error 7 in test 5\n");
1233:                 }
1234:                    
1235:                 coll.setStrength(Collator.TERTIARY);
1236:                 }
1237:                 */
1238:                /*  Test 6:  Null character as base of a non-normal combining sequence.*/
1239:                // logln("Test 6 ....");
1240:                /*
1241:                 * not a valid test since string are null-terminated in java{
1242:                 {
1243:                 char strA[] = {0x41, 0x0, 0x300, 0x316, 0x41, 0x302,};
1244:                 char strB[] = {0x41, 0x0, 0x302, 0x316, 0x41, 0x300,};
1245:                
1246:                 int result = coll.compare(new String(strA, 0, 5), new String(strB, 0, 5));
1247:                 if (result != -1) {
1248:                 errln("Error 1 in test 6\n");
1249:                 }
1250:                 result = coll.compare(new String(strA, 0, 1), new String(strB, 0, 1));
1251:                 if (result != 0) {
1252:                 errln("Error 2 in test 6\n");
1253:                 }
1254:                 }
1255:                 */
1256:            }
1257:
1258:            public void TestContraction() {
1259:                String[] testrules = { "&A = AB / B", "&A = A\\u0306/\\u0306",
1260:                        "&c = ch / h", };
1261:                String[] testdata = { "AB", "AB", "A\u0306", "ch" };
1262:                String[] testdata2 = { "\u0063\u0067", "\u0063\u0068",
1263:                        "\u0063\u006C", };
1264:                String[] testrules3 = { "&z < xyz &xyzw << B",
1265:                        "&z < xyz &xyz << B / w", "&z < ch &achm << B",
1266:                        "&z < ch &a << B / chm", "&\ud800\udc00w << B",
1267:                        "&\ud800\udc00 << B / w", "&a\ud800\udc00m << B",
1268:                        "&a << B / \ud800\udc00m", };
1269:
1270:                RuleBasedCollator coll = null;
1271:                for (int i = 0; i < testrules.length; i++) {
1272:                    CollationElementIterator iter1 = null;
1273:                    int j = 0;
1274:                    // logln("Rule " + testrules[i] + " for testing\n");
1275:                    String rule = testrules[i];
1276:                    try {
1277:                        coll = new RuleBasedCollator(rule);
1278:                    } catch (Exception e) {
1279:                        warnln("Collator creation failed " + testrules[i]);
1280:                        return;
1281:                    }
1282:                    try {
1283:                        iter1 = coll.getCollationElementIterator(testdata[i]);
1284:                    } catch (Exception e) {
1285:                        errln("Collation iterator creation failed\n");
1286:                        return;
1287:                    }
1288:                    while (j < 2) {
1289:                        CollationElementIterator iter2;
1290:                        int ce;
1291:                        try {
1292:                            iter2 = coll.getCollationElementIterator(String
1293:                                    .valueOf(testdata[i].charAt(j)));
1294:
1295:                        } catch (Exception e) {
1296:                            errln("Collation iterator creation failed\n");
1297:                            return;
1298:                        }
1299:                        ce = iter2.next();
1300:                        while (ce != CollationElementIterator.NULLORDER) {
1301:                            if (iter1.next() != ce) {
1302:                                errln("Collation elements in contraction split does not match\n");
1303:                                return;
1304:                            }
1305:                            ce = iter2.next();
1306:                        }
1307:                        j++;
1308:                    }
1309:                    if (iter1.next() != CollationElementIterator.NULLORDER) {
1310:                        errln("Collation elements not exhausted\n");
1311:                        return;
1312:                    }
1313:                }
1314:                String rule = "& a < b < c < ch < d & c = ch / h";
1315:                try {
1316:                    coll = new RuleBasedCollator(rule);
1317:                } catch (Exception e) {
1318:                    errln("cannot create rulebased collator");
1319:                    return;
1320:                }
1321:
1322:                if (coll.compare(testdata2[0], testdata2[1]) != -1) {
1323:                    errln("Expected " + testdata2[0] + " < " + testdata2[1]);
1324:                    return;
1325:                }
1326:                if (coll.compare(testdata2[1], testdata2[2]) != -1) {
1327:                    errln("Expected " + testdata2[1] + " < " + testdata2[2]);
1328:                    return;
1329:                }
1330:                for (int i = 0; i < testrules3.length; i += 2) {
1331:                    RuleBasedCollator coll1, coll2;
1332:                    CollationElementIterator iter1, iter2;
1333:                    char ch = 0x0042;
1334:                    int ce;
1335:                    rule = testrules3[i];
1336:                    try {
1337:                        coll1 = new RuleBasedCollator(rule);
1338:                    } catch (Exception e) {
1339:                        errln("Fail: cannot create rulebased collator, rule:"
1340:                                + rule);
1341:                        return;
1342:                    }
1343:                    rule = testrules3[i + 1];
1344:                    try {
1345:                        coll2 = new RuleBasedCollator(rule);
1346:                    } catch (Exception e) {
1347:                        errln("Collator creation failed " + testrules[i]);
1348:                        return;
1349:                    }
1350:                    try {
1351:                        iter1 = coll1.getCollationElementIterator(String
1352:                                .valueOf(ch));
1353:                        iter2 = coll2.getCollationElementIterator(String
1354:                                .valueOf(ch));
1355:                    } catch (Exception e) {
1356:                        errln("Collation iterator creation failed\n");
1357:                        return;
1358:                    }
1359:                    ce = iter1.next();
1360:
1361:                    while (ce != CollationElementIterator.NULLORDER) {
1362:                        if (ce != iter2.next()) {
1363:                            errln("CEs does not match\n");
1364:                            return;
1365:                        }
1366:                        ce = iter1.next();
1367:                    }
1368:                    if (iter2.next() != CollationElementIterator.NULLORDER) {
1369:                        errln("CEs not exhausted\n");
1370:                        return;
1371:                    }
1372:                }
1373:            }
1374:
1375:            public void TestExpansion() {
1376:                String[] testrules = { "&J << K / B & K << M",
1377:                        "&J << K / B << M" };
1378:                String[] testdata = { "JA", "MA", "KA", "KC", "JC", "MC", };
1379:
1380:                Collator coll;
1381:                for (int i = 0; i < testrules.length; i++) {
1382:                    // logln("Rule " + testrules[i] + " for testing\n");
1383:                    String rule = testrules[i];
1384:                    try {
1385:                        coll = new RuleBasedCollator(rule);
1386:                    } catch (Exception e) {
1387:                        warnln("Collator creation failed " + testrules[i]);
1388:                        return;
1389:                    }
1390:
1391:                    for (int j = 0; j < 5; j++) {
1392:                        CollationTest.doTest(this , (RuleBasedCollator) coll,
1393:                                testdata[j], testdata[j + 1], -1);
1394:                    }
1395:                }
1396:            }
1397:
1398:            public void TestContractionEndCompare() {
1399:                String rules = "&b=ch";
1400:                String src = "bec";
1401:                String tgt = "bech";
1402:                Collator coll = null;
1403:                try {
1404:                    coll = new RuleBasedCollator(rules);
1405:                } catch (Exception e) {
1406:                    warnln("Collator creation failed " + rules);
1407:                    return;
1408:                }
1409:                CollationTest.doTest(this , (RuleBasedCollator) coll, src, tgt,
1410:                        1);
1411:            }
1412:
1413:            public void TestLocaleRuleBasedCollators() {
1414:                if (getInclusion() < 5) {
1415:                    // not serious enough to run this
1416:                    return;
1417:                }
1418:                Locale locale[] = Collator.getAvailableLocales();
1419:                String prevrule = null;
1420:                for (int i = 0; i < locale.length; i++) {
1421:                    Locale l = locale[i];
1422:                    try {
1423:                        ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle
1424:                                .getBundleInstance(
1425:                                        ICUResourceBundle.ICU_COLLATION_BASE_NAME,
1426:                                        l);
1427:                        String collkey = rb
1428:                                .getStringWithFallback("collations/default");
1429:                        ICUResourceBundle elements = rb
1430:                                .getWithFallback("collations/" + collkey);
1431:                        if (elements == null) {
1432:                            continue;
1433:                        }
1434:                        String rule = null;
1435:                        /*
1436:                          Object[][] colldata = (Object[][])elements;
1437:                          // %%CollationBin
1438:                          if (colldata[0][1] instanceof byte[]){
1439:                          rule = (String)colldata[1][1];
1440:                          }
1441:                          else {
1442:                          rule = (String)colldata[0][1];
1443:                          }
1444:                         */
1445:                        rule = elements.getString("Sequence");
1446:
1447:                        RuleBasedCollator col1 = (RuleBasedCollator) Collator
1448:                                .getInstance(l);
1449:                        if (!rule.equals(col1.getRules())) {
1450:                            errln("Rules should be the same in the RuleBasedCollator and Locale");
1451:                        }
1452:                        if (rule != null && rule.length() > 0
1453:                                && !rule.equals(prevrule)) {
1454:                            RuleBasedCollator col2 = new RuleBasedCollator(rule);
1455:                            if (!col1.equals(col2)) {
1456:                                errln("Error creating RuleBasedCollator from "
1457:                                        + "locale rules for " + l.toString());
1458:                            }
1459:                        }
1460:                        prevrule = rule;
1461:                    } catch (Exception e) {
1462:                        warnln("Error retrieving resource bundle for testing: "
1463:                                + e.toString());
1464:                    }
1465:                }
1466:            }
1467:
1468:            public void TestOptimize() {
1469:                /* this is not really a test - just trying out 
1470:                 * whether copying of UCA contents will fail 
1471:                 * Cannot really test, since the functionality 
1472:                 * remains the same.
1473:                 */
1474:                String rules[] = { "[optimize [\\uAC00-\\uD7FF]]" };
1475:                String data[][] = { { "a", "b" } };
1476:                int i = 0;
1477:
1478:                for (i = 0; i < rules.length; i++) {
1479:                    genericRulesStarter(rules[i], data[i]);
1480:                }
1481:            }
1482:
1483:            public void TestIdenticalCompare() {
1484:                try {
1485:                    RuleBasedCollator coll = new RuleBasedCollator(
1486:                            "& \uD800\uDC00 = \uD800\uDC01");
1487:                    String strA = "AA\u0300\u0316\uD800\uDC01";
1488:                    String strB = "A\u00c0\u0316\uD800\uDC00";
1489:                    coll.setStrength(Collator.IDENTICAL);
1490:                    CollationTest.doTest(this , coll, strA, strB, 1);
1491:                } catch (Exception e) {
1492:                    warnln(e.getMessage());
1493:                }
1494:            }
1495:
1496:            public void TestMergeSortKeys() {
1497:                String cases[] = { "abc", "abcd", "abcde" };
1498:                String prefix = "foo";
1499:                String suffix = "egg";
1500:                CollationKey mergedPrefixKeys[] = new CollationKey[cases.length];
1501:                CollationKey mergedSuffixKeys[] = new CollationKey[cases.length];
1502:
1503:                Collator coll = Collator.getInstance(Locale.ENGLISH);
1504:                genericLocaleStarter(Locale.ENGLISH, cases);
1505:
1506:                int strength = Collator.PRIMARY;
1507:                while (strength <= Collator.IDENTICAL) {
1508:                    coll.setStrength(strength);
1509:                    CollationKey prefixKey = coll.getCollationKey(prefix);
1510:                    CollationKey suffixKey = coll.getCollationKey(suffix);
1511:                    for (int i = 0; i < cases.length; i++) {
1512:                        CollationKey key = coll.getCollationKey(cases[i]);
1513:                        mergedPrefixKeys[i] = prefixKey.merge(key);
1514:                        mergedSuffixKeys[i] = suffixKey.merge(key);
1515:                        if (mergedPrefixKeys[i].getSourceString() != null
1516:                                || mergedSuffixKeys[i].getSourceString() != null) {
1517:                            errln("Merged source string error: expected null");
1518:                        }
1519:                        if (i > 0) {
1520:                            if (mergedPrefixKeys[i - 1]
1521:                                    .compareTo(mergedPrefixKeys[i]) >= 0) {
1522:                                errln("Error while comparing prefixed keys @ strength "
1523:                                        + strength);
1524:                                errln(prettify(mergedPrefixKeys[i - 1]));
1525:                                errln(prettify(mergedPrefixKeys[i]));
1526:                            }
1527:                            if (mergedSuffixKeys[i - 1]
1528:                                    .compareTo(mergedSuffixKeys[i]) >= 0) {
1529:                                errln("Error while comparing suffixed keys @ strength "
1530:                                        + strength);
1531:                                errln(prettify(mergedSuffixKeys[i - 1]));
1532:                                errln(prettify(mergedSuffixKeys[i]));
1533:                            }
1534:                        }
1535:                    }
1536:                    if (strength == Collator.QUATERNARY) {
1537:                        strength = Collator.IDENTICAL;
1538:                    } else {
1539:                        strength++;
1540:                    }
1541:                }
1542:            }
1543:
1544:            public void TestVariableTop() {
1545:                // parseNextToken is not released as public so i create my own rules
1546:                String rules = "& a < b < c < de < fg & hi = j";
1547:                try {
1548:                    RuleBasedCollator coll = new RuleBasedCollator(rules);
1549:                    String tokens[] = { "a", "b", "c", "de", "fg", "hi", "j",
1550:                            "ab" };
1551:                    coll.setAlternateHandlingShifted(true);
1552:                    for (int i = 0; i < tokens.length; i++) {
1553:                        int varTopOriginal = coll.getVariableTop();
1554:                        try {
1555:                            int varTop = coll.setVariableTop(tokens[i]);
1556:                            if (i > 4) {
1557:                                errln("Token " + tokens[i]
1558:                                        + " expected to fail");
1559:                            }
1560:                            if (varTop != coll.getVariableTop()) {
1561:                                errln("Error setting and getting variable top");
1562:                            }
1563:                            CollationKey key1 = coll.getCollationKey(tokens[i]);
1564:                            for (int j = 0; j < i; j++) {
1565:                                CollationKey key2 = coll
1566:                                        .getCollationKey(tokens[j]);
1567:                                if (key2.compareTo(key1) < 0) {
1568:                                    errln("Setting variable top shouldn't change the comparison sequence");
1569:                                }
1570:                                byte sortorder[] = key2.toByteArray();
1571:                                if (sortorder.length > 0
1572:                                        && (key2.toByteArray())[0] > 1) {
1573:                                    errln("Primary sort order should be 0");
1574:                                }
1575:                            }
1576:                        } catch (Exception e) {
1577:                            CollationElementIterator iter = coll
1578:                                    .getCollationElementIterator(tokens[i]);
1579:                            /*int ce =*/iter.next();
1580:                            int ce2 = iter.next();
1581:                            if (ce2 == CollationElementIterator.NULLORDER) {
1582:                                errln("Token " + tokens[i]
1583:                                        + " not expected to fail");
1584:                            }
1585:                            if (coll.getVariableTop() != varTopOriginal) {
1586:                                errln("When exception is thrown variable top should "
1587:                                        + "not be changed");
1588:                            }
1589:                        }
1590:                        coll.setVariableTop(varTopOriginal);
1591:                        if (varTopOriginal != coll.getVariableTop()) {
1592:                            errln("Couldn't restore old variable top\n");
1593:                        }
1594:                    }
1595:
1596:                    // Testing calling with error set
1597:                    try {
1598:                        coll.setVariableTop("");
1599:                        errln("Empty string should throw an IllegalArgumentException");
1600:                    } catch (IllegalArgumentException e) {
1601:                        logln("PASS: Empty string failed as expected");
1602:                    }
1603:                    try {
1604:                        coll.setVariableTop(null);
1605:                        errln("Null string should throw an IllegalArgumentException");
1606:                    } catch (IllegalArgumentException e) {
1607:                        logln("PASS: null string failed as expected");
1608:                    }
1609:                } catch (Exception e) {
1610:                    warnln("Error creating RuleBasedCollator");
1611:                }
1612:            }
1613:
1614:            public void TestUCARules() {
1615:                try {
1616:                    // only root locale can have empty tailorings .. not English!
1617:                    RuleBasedCollator coll = (RuleBasedCollator) Collator
1618:                            .getInstance(new Locale("", "", ""));
1619:                    String rule = coll.getRules(false);
1620:                    if (!rule.equals("")) {
1621:                        errln("Empty rule string should have empty rules "
1622:                                + rule);
1623:                    }
1624:                    rule = coll.getRules(true);
1625:                    if (rule.equals("")) {
1626:                        errln("UCA rule string should not be empty");
1627:                    }
1628:                    coll = new RuleBasedCollator(rule);
1629:                } catch (Exception e) {
1630:                    warnln(e.getMessage());
1631:                }
1632:            }
1633:
1634:            /**
1635:             * Jitterbug 2726
1636:             */
1637:            public void TestShifted() {
1638:                RuleBasedCollator collator = (RuleBasedCollator) Collator
1639:                        .getInstance();
1640:                collator.setStrength(Collator.PRIMARY);
1641:                collator.setAlternateHandlingShifted(true);
1642:                CollationTest.doTest(this , collator, " a", "a", 0); // works properly
1643:                CollationTest.doTest(this , collator, "a", "a ", 0); // inconsistent results
1644:            }
1645:
1646:            /**
1647:             * Test for CollationElementIterator previous and next for the whole set of
1648:             * unicode characters with normalization on.
1649:             */
1650:            public void TestNumericCollation() {
1651:                String basicTestStrings[] = { "hello1", "hello2", "hello123456" };
1652:                String preZeroTestStrings[] = { "avery1", "avery01",
1653:                        "avery001", "avery0001" };
1654:                String thirtyTwoBitNumericStrings[] = { "avery42949672960",
1655:                        "avery42949672961", "avery42949672962",
1656:                        "avery429496729610" };
1657:
1658:                String supplementaryDigits[] = { "\uD835\uDFCE", // 0 
1659:                        "\uD835\uDFCF", // 1 
1660:                        "\uD835\uDFD0", // 2 
1661:                        "\uD835\uDFD1", // 3 
1662:                        "\uD835\uDFCF\uD835\uDFCE", // 10 
1663:                        "\uD835\uDFCF\uD835\uDFCF", // 11 
1664:                        "\uD835\uDFCF\uD835\uDFD0", // 12 
1665:                        "\uD835\uDFD0\uD835\uDFCE", // 20 
1666:                        "\uD835\uDFD0\uD835\uDFCF", // 21 
1667:                        "\uD835\uDFD0\uD835\uDFD0" // 22 
1668:                };
1669:
1670:                String foreignDigits[] = { "\u0661", "\u0662", "\u0663",
1671:                        "\u0661\u0660", "\u0661\u0662", "\u0661\u0663",
1672:                        "\u0662\u0660", "\u0662\u0662", "\u0662\u0663",
1673:                        "\u0663\u0660", "\u0663\u0662", "\u0663\u0663" };
1674:
1675:                // Open our collator.
1676:                RuleBasedCollator coll = (RuleBasedCollator) Collator
1677:                        .getInstance(Locale.ENGLISH);
1678:                String att[] = { "NumericCollation" };
1679:                Boolean val[] = { new Boolean(true) };
1680:                genericLocaleStarterWithOptions(Locale.ENGLISH,
1681:                        basicTestStrings, att, val);
1682:                genericLocaleStarterWithOptions(Locale.ENGLISH,
1683:                        thirtyTwoBitNumericStrings, att, val);
1684:                genericLocaleStarterWithOptions(Locale.ENGLISH, foreignDigits,
1685:                        att, val);
1686:                genericLocaleStarterWithOptions(Locale.ENGLISH,
1687:                        supplementaryDigits, att, val);
1688:
1689:                // Setting up our collator to do digits.
1690:                coll.setNumericCollation(true);
1691:
1692:                // Testing that prepended zeroes still yield the correct collation 
1693:                // behavior. 
1694:                // We expect that every element in our strings array will be equal.
1695:                for (int i = 0; i < preZeroTestStrings.length - 1; i++) {
1696:                    for (int j = i + 1; j < preZeroTestStrings.length; j++) {
1697:                        CollationTest.doTest(this , coll, preZeroTestStrings[i],
1698:                                preZeroTestStrings[j], 0);
1699:                    }
1700:                }
1701:
1702:                //cover setNumericCollationDefault, getNumericCollation
1703:                assertTrue("The Numeric Collation setting is on", coll
1704:                        .getNumericCollation());
1705:                coll.setNumericCollationDefault();
1706:                logln("After set Numeric to default, the setting is: "
1707:                        + coll.getNumericCollation());
1708:            }
1709:
1710:            public void Test3249() {
1711:                String rule = "&x < a &z < a";
1712:                try {
1713:                    RuleBasedCollator coll = new RuleBasedCollator(rule);
1714:                    if (coll != null) {
1715:                        logln("Collator did not throw an exception");
1716:                    }
1717:                } catch (Exception e) {
1718:                    warnln("Error creating RuleBasedCollator with " + rule
1719:                            + " failed");
1720:                }
1721:            }
1722:
1723:            public void TestTibetanConformance() {
1724:                String test[] = { "\u0FB2\u0591\u0F71\u0061",
1725:                        "\u0FB2\u0F71\u0061" };
1726:                try {
1727:                    Collator coll = Collator.getInstance();
1728:                    coll.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
1729:                    if (coll.compare(test[0], test[1]) != 0) {
1730:                        errln("Tibetan comparison error");
1731:                    }
1732:                    CollationTest.doTest(this , (RuleBasedCollator) coll,
1733:                            test[0], test[1], 0);
1734:                } catch (Exception e) {
1735:                    warnln("Error creating UCA collator");
1736:                }
1737:            }
1738:
1739:            public void TestJ3347() {
1740:                try {
1741:                    Collator coll = Collator.getInstance(Locale.FRENCH);
1742:                    ((RuleBasedCollator) coll)
1743:                            .setAlternateHandlingShifted(true);
1744:                    if (coll.compare("6", "!6") != 0) {
1745:                        errln("Jitterbug 3347 failed");
1746:                    }
1747:                } catch (Exception e) {
1748:                    warnln("Error creating UCA collator");
1749:                }
1750:            }
1751:
1752:            public void TestPinyinProblem() {
1753:                String test[] = { "\u4E56\u4E56\u7761", "\u4E56\u5B69\u5B50" };
1754:                genericLocaleStarter(new Locale("zh", "", "PINYIN"), test);
1755:            }
1756:
1757:            static final long topByte = 0xFF000000L;
1758:            static final long bottomByte = 0xFFL;
1759:            static final long fourBytes = 0xFFFFFFFFL;
1760:
1761:            static final int MAX_INPUT = 0x220001; // 2 * Unicode range + 2
1762:
1763:            private void show(int i, ImplicitCEGenerator imp) {
1764:                if (i >= 0 && i <= MAX_INPUT) {
1765:                    logln(Utility.hex(i)
1766:                            + "\t"
1767:                            + Utility
1768:                                    .hex(imp.getImplicitFromRaw(i) & fourBytes));
1769:                }
1770:            }
1771:
1772:            private void throwError(String title, int cp,
1773:                    ImplicitCEGenerator imp) {
1774:                throw new IllegalArgumentException(title + "\t"
1775:                        + Utility.hex(cp, 6) + "\t"
1776:                        + Utility.hex(imp.getImplicitFromRaw(cp) & fourBytes));
1777:            }
1778:
1779:            private void throwError(String title, long ce) {
1780:                errln(title + "\t" + Utility.hex(ce & fourBytes));
1781:            }
1782:
1783:            public void TestImplicitGeneration() {
1784:                logln("Start");
1785:                try {
1786:                    ImplicitCEGenerator foo = new ImplicitCEGenerator(0xE0,
1787:                            0xE4);
1788:
1789:                    //int x = foo.getRawImplicit(0xF810);
1790:                    foo.getRawFromImplicit(0xE20303E7);
1791:
1792:                    int gap4 = foo.getGap4();
1793:                    logln("Gap4: " + gap4);
1794:                    int gap3 = foo.getGap3();
1795:                    int minTrail = foo.getMinTrail();
1796:                    int maxTrail = foo.getMaxTrail();
1797:                    long last = 0;
1798:                    long current;
1799:                    for (int i = 0; i <= MAX_INPUT; ++i) {
1800:                        current = foo.getImplicitFromRaw(i) & fourBytes;
1801:
1802:                        // check that it round-trips AND that all intervening ones are illegal
1803:                        int roundtrip = foo.getRawFromImplicit((int) current);
1804:                        if (roundtrip != i) {
1805:                            throwError("No roundtrip", i, foo);
1806:                        }
1807:                        if (last != 0) {
1808:                            for (long j = last + 1; j < current; ++j) {
1809:                                roundtrip = foo.getRawFromImplicit((int) j);
1810:                                // raise an error if it *doesn't* find an error
1811:                                if (roundtrip != -1) {
1812:                                    throwError("Fails to recognize illegal", j);
1813:                                }
1814:                            }
1815:                        }
1816:                        // now do other consistency checks
1817:                        long lastBottom = last & bottomByte;
1818:                        long currentBottom = current & bottomByte;
1819:                        long lastTop = last & topByte;
1820:                        long currentTop = current & topByte;
1821:
1822:                        // do some consistency checks
1823:                        /*
1824:                          long gap = current - last;               
1825:                          if (currentBottom != 0) { // if we are a 4-byte
1826:                          // gap has to be at least gap4
1827:                          // and gap from minTrail, maxTrail has to be at least gap4
1828:                          if (gap <= gap4) foo.throwError("Failed gap4 between", i);
1829:                          if (currentBottom < minTrail + gap4) foo.throwError("Failed gap4 before", i);
1830:                          if (currentBottom > maxTrail - gap4) foo.throwError("Failed gap4 after", i);
1831:                          } else { // we are a three-byte
1832:                          gap = gap >> 8; // move gap down for comparison.
1833:                          long current3Bottom = (current >> 8) & bottomByte;
1834:                          if (gap <= gap3) foo.throwError("Failed gap3 between ", i);
1835:                          if (current3Bottom < minTrail + gap3) foo.throwError("Failed gap3 before", i);
1836:                          if (current3Bottom > maxTrail - gap3) foo.throwError("Failed gap3 after", i);
1837:                          }
1838:                         */
1839:                        // print out some values for spot-checking
1840:                        if (lastTop != currentTop || i == 0x10000
1841:                                || i == 0x110000) {
1842:                            show(i - 3, foo);
1843:                            show(i - 2, foo);
1844:                            show(i - 1, foo);
1845:                            if (i == 0) {
1846:                                // do nothing
1847:                            } else if (lastBottom == 0 && currentBottom != 0) {
1848:                                logln("+ primary boundary, 4-byte CE's below");
1849:                            } else if (lastTop != currentTop) {
1850:                                logln("+ primary boundary");
1851:                            }
1852:                            show(i, foo);
1853:                            show(i + 1, foo);
1854:                            show(i + 2, foo);
1855:                            logln("...");
1856:                        }
1857:                        last = current;
1858:                        if (foo.getCodePointFromRaw(foo.getRawFromCodePoint(i)) != i) {
1859:                            errln("No raw <-> code point roundtrip for "
1860:                                    + Utility.hex(i));
1861:                        }
1862:                    }
1863:                    show(MAX_INPUT - 2, foo);
1864:                    show(MAX_INPUT - 1, foo);
1865:                    show(MAX_INPUT, foo);
1866:                } catch (Exception e) {
1867:                    e.printStackTrace();
1868:                    warnln(e.getMessage());
1869:                } finally {
1870:                    logln("End");
1871:                }
1872:            }
1873:
1874:            /* supercedes TestJ784 */
1875:            public void TestBeforePinyin() {
1876:                String rules = "&[before 2]A << \u0101  <<< \u0100 << \u00E1 <<< \u00C1 << \u01CE <<< \u01CD << \u00E0 <<< \u00C0"
1877:                        + "&[before 2]e << \u0113 <<< \u0112 << \u00E9 <<< \u00C9 << \u011B <<< \u011A << \u00E8 <<< \u00C8"
1878:                        + "&[before 2] i << \u012B <<< \u012A << \u00ED <<< \u00CD << \u01D0 <<< \u01CF << \u00EC <<< \u00CC"
1879:                        + "&[before 2] o << \u014D <<< \u014C << \u00F3 <<< \u00D3 << \u01D2 <<< \u01D1 << \u00F2 <<< \u00D2"
1880:                        + "&[before 2]u << \u016B <<< \u016A << \u00FA <<< \u00DA << \u01D4 <<< \u01D3 << \u00F9 <<< \u00D9"
1881:                        + "&U << \u01D6 <<< \u01D5 << \u01D8 <<< \u01D7 << \u01DA <<< \u01D9 << \u01DC <<< \u01DB << \u00FC";
1882:
1883:                String test[] = { "l\u0101", "la", "l\u0101n", "lan ",
1884:                        "l\u0113", "le", "l\u0113n", "len" };
1885:
1886:                String test2[] = { "x\u0101", "x\u0100", "X\u0101", "X\u0100",
1887:                        "x\u00E1", "x\u00C1", "X\u00E1", "X\u00C1", "x\u01CE",
1888:                        "x\u01CD", "X\u01CE", "X\u01CD", "x\u00E0", "x\u00C0",
1889:                        "X\u00E0", "X\u00C0", "xa", "xA", "Xa", "XA",
1890:                        "x\u0101x", "x\u0100x", "x\u00E1x", "x\u00C1x",
1891:                        "x\u01CEx", "x\u01CDx", "x\u00E0x", "x\u00C0x", "xax",
1892:                        "xAx" };
1893:                /* TODO: port builder fixes to before */
1894:                genericRulesStarter(rules, test);
1895:                genericLocaleStarter(new Locale("zh", "", ""), test);
1896:                genericRulesStarter(rules, test2);
1897:                genericLocaleStarter(new Locale("zh", "", ""), test2);
1898:            }
1899:
1900:            public void TestUpperFirstQuaternary() {
1901:                String tests[] = { "B", "b", "Bb", "bB" };
1902:                String[] att = { "strength", "UpperFirst" };
1903:                Object attVals[] = { new Integer(Collator.QUATERNARY),
1904:                        new Boolean(true) };
1905:                genericLocaleStarterWithOptions(new Locale("root", "", ""),
1906:                        tests, att, attVals);
1907:            }
1908:
1909:            public void TestJ4960() {
1910:                String tests[] = { "\\u00e2T", "aT" };
1911:                String att[] = { "strength", "CaseLevel" };
1912:                Object attVals[] = { new Integer(Collator.PRIMARY),
1913:                        new Boolean(true) };
1914:                String tests2[] = { "a", "A" };
1915:                String rule = "&[first tertiary ignorable]=A=a";
1916:                String att2[] = { "CaseLevel" };
1917:                Object attVals2[] = { new Boolean(true) };
1918:                // Test whether we correctly ignore primary ignorables on case level when
1919:                // we have only primary & case level
1920:                genericLocaleStarterWithOptionsAndResult(
1921:                        new Locale("root", ""), tests, att, attVals, 0);
1922:                // Test whether ICU4J will make case level for sortkeys that have primary strength
1923:                // and case level
1924:                genericLocaleStarterWithOptions(new Locale("root", ""), tests2,
1925:                        att, attVals);
1926:                // Test whether completely ignorable letters have case level info (they shouldn't)
1927:                genericRulesStarterWithOptionsAndResult(rule, tests2, att2,
1928:                        attVals2, 0);
1929:            }
1930:
1931:            public void TestJB5298() {
1932:                ULocale[] locales = Collator.getAvailableULocales();
1933:                logln("Number of collator locales returned : " + locales.length);
1934:                // double-check keywords
1935:                String[] keywords = Collator.getKeywords();
1936:                if (keywords.length != 1 || !keywords[0].equals("collation")) {
1937:                    throw new IllegalArgumentException(
1938:                            "internal collation error");
1939:                }
1940:
1941:                String[] values = Collator.getKeywordValues("collation");
1942:                log("Collator.getKeywordValues returned: ");
1943:                for (int i = 0; i < values.length; i++) {
1944:                    log(values[i] + ", ");
1945:                }
1946:                logln("");
1947:                logln("Number of collator values returned : " + values.length);
1948:
1949:                Set foundValues = new TreeSet(Arrays.asList(values));
1950:
1951:                for (int i = 0; i < locales.length; ++i) {
1952:                    for (int j = 0; j < values.length; ++j) {
1953:                        ULocale tryLocale = values[j].equals("standard") ? locales[i]
1954:                                : new ULocale(locales[i] + "@collation="
1955:                                        + values[j]);
1956:                        // only append if not standard
1957:                        ULocale canon = Collator.getFunctionalEquivalent(
1958:                                "collation", tryLocale);
1959:                        if (!canon.equals(tryLocale)) {
1960:                            continue; // has a different 
1961:                        } else {// functional equivalent, so skip
1962:                            logln(tryLocale + " : " + canon + ", ");
1963:                        }
1964:                        String can = canon.toString();
1965:                        int idx = can.indexOf("@collation=");
1966:                        String val = idx >= 0 ? can.substring(idx + 11, can
1967:                                .length()) : "";
1968:                        if (val.length() > 0 && !foundValues.contains(val)) {
1969:                            errln("Unknown collation found " + can);
1970:                        }
1971:                    }
1972:                }
1973:                logln(" ");
1974:            }
1975:
1976:            public void TestJ5367() {
1977:                String[] test = { "a", "y" };
1978:                String rules = "&Ny << Y &[first secondary ignorable] <<< a";
1979:                genericRulesStarter(rules, test);
1980:            }
1981:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.