-//##header\r
-//#if defined(FOUNDATION10) || defined(J2SE13)\r
-//#else\r
-/*\r
- *******************************************************************************\r
- * Copyright (C) 1996-2009, International Business Machines Corporation and *\r
- * others. All Rights Reserved. *\r
- *******************************************************************************\r
- */\r
-package com.ibm.icu.dev.test.util;\r
-\r
-import java.text.NumberFormat;\r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-import java.util.Collection;\r
-import java.util.Comparator;\r
-import java.util.HashMap;\r
-import java.util.HashSet;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Random;\r
-import java.util.Set;\r
-import java.util.SortedSet;\r
-import java.util.TreeMap;\r
-import java.util.TreeSet;\r
-\r
-import com.ibm.icu.dev.test.TestBoilerplate;\r
-import com.ibm.icu.dev.test.TestFmwk;\r
-import com.ibm.icu.impl.Utility;\r
-import com.ibm.icu.lang.UCharacter;\r
-import com.ibm.icu.lang.UProperty;\r
-import com.ibm.icu.text.UnicodeSet;\r
-\r
-public class TestUtilities extends TestFmwk {\r
- static final int LIMIT = 0x15; // limit to make testing more realistic in terms of collisions\r
- static final int ITERATIONS = 1000000;\r
- static final boolean SHOW_PROGRESS = false;\r
- static final boolean DEBUG = false;\r
- \r
- public static void main(String[] args) throws Exception {\r
- new TestUtilities().run(args);\r
- }\r
- \r
- UnicodeMap map1 = new UnicodeMap();\r
- Map map2 = new HashMap();\r
- Map map3 = new TreeMap();\r
- SortedSet log = new TreeSet();\r
- static String[] TEST_VALUES = {null, "A", "B", "C", "D", "E", "F"};\r
- static Random random = new Random(12345);\r
- \r
- public void TestUnicodeMap() {\r
- random.setSeed(12345);\r
- // do random change to both, then compare\r
- logln("Comparing against HashMap");\r
- for (int counter = 0; counter < ITERATIONS; ++counter) {\r
- int start = random.nextInt(LIMIT);\r
- String value = TEST_VALUES[random.nextInt(TEST_VALUES.length)];\r
- String logline = Utility.hex(start) + "\t" + value;\r
- if (SHOW_PROGRESS) logln(counter + "\t" + logline);\r
- log.add(logline);\r
- if (DEBUG && counter == 144) {\r
- System.out.println(" debug");\r
- }\r
- map1.put(start, value);\r
- map2.put(new Integer(start), value);\r
- check(counter);\r
- }\r
- checkNext(LIMIT);\r
- \r
- logln("Setting General Category");\r
- map1 = new UnicodeMap();\r
- map2 = new TreeMap();\r
- for (int cp = 0; cp <= SET_LIMIT; ++cp) {\r
- int enumValue = UCharacter.getIntPropertyValue(cp, propEnum);\r
- //if (enumValue <= 0) continue; // for smaller set\r
- String value = UCharacter.getPropertyValueName(propEnum,enumValue, UProperty.NameChoice.LONG);\r
- map1.put(cp, value);\r
- map2.put(new Integer(cp), value);\r
- } \r
- checkNext(Integer.MAX_VALUE);\r
-\r
-\r
- logln("Comparing General Category");\r
- check(-1);\r
- logln("Comparing Values");\r
- Set values1 = (Set) map1.getAvailableValues(new TreeSet());\r
- Set values2 = new TreeSet(map2.values());\r
- if (!TestBoilerplate.verifySetsIdentical(this, values1, values2)) {\r
- throw new IllegalArgumentException("Halting");\r
- }\r
- logln("Comparing Sets");\r
- for (Iterator it = values1.iterator(); it.hasNext();) {\r
- Object value = it.next();\r
- logln(value == null ? "null" : value.toString());\r
- UnicodeSet set1 = map1.getSet(value);\r
- UnicodeSet set2 = TestBoilerplate.getSet(map2, value);\r
- if (!TestBoilerplate.verifySetsIdentical(this, set1, set2)) {\r
- throw new IllegalArgumentException("Halting");\r
- }\r
- } \r
- \r
- logln("Getting Scripts");\r
- UnicodeMap scripts = ICUPropertyFactory.make().getProperty("script").getUnicodeMap_internal();\r
- UnicodeMap.Composer composer = new UnicodeMap.Composer() {\r
- public Object compose(int codePoint, Object a, Object b) {\r
- return a.toString() + "_" + b.toString();\r
- }\r
- };\r
- \r
- logln("Trying Compose");\r
- UnicodeMap composed = ((UnicodeMap)scripts.cloneAsThawed()).composeWith(map1, composer);\r
- Object last = "";\r
- for (int i = 0; i < 0x10FFFF; ++i) {\r
- Object comp = composed.getValue(i);\r
- Object gc = map1.getValue(i);\r
- Object sc = scripts.getValue(i);\r
- if (!comp.equals(composer.compose(i, gc, sc))) {\r
- errln("Failed compose at: " + i);\r
- }\r
- if (!last.equals(comp)) {\r
- logln(Utility.hex(i) + "\t" + comp);\r
- last = comp;\r
- }\r
- }\r
-\r
- // check boilerplate\r
- List argList = new ArrayList();\r
- argList.add("TestMain");\r
- if (params.nothrow) argList.add("-nothrow");\r
- if (params.verbose) argList.add("-verbose");\r
- String[] args = new String[argList.size()];\r
- argList.toArray(args);\r
- new UnicodeMapBoilerplate().run(args);\r
- // TODO: the following is not being reached\r
- new UnicodeSetBoilerplate().run(args); \r
- }\r
- \r
- public void TestCollectionUtilitySpeed() {\r
- HashSet hs1 = new HashSet();\r
- HashSet hs2 = new HashSet();\r
- int size = 100000;\r
- int iterations = 100;\r
- String prefix = "abcde";\r
- String postfix = "abcde";\r
- int start1 = 0; // 1 for some, 0 for all\r
- for (int i = 0; i < size; i += 2) hs1.add(prefix + String.valueOf(i) + postfix);\r
- for (int i = start1; i < size; i += 2) hs2.add(prefix + String.valueOf(i) + postfix);\r
- TreeSet ts1 = new TreeSet(hs1);\r
- TreeSet ts2 = new TreeSet(hs2);\r
- CollectionUtilities.containsAll(hs1, hs2);\r
- CollectionUtilities.containsAll(ts1, ts2);\r
- long start, end;\r
- boolean temp = false;\r
- start = System.currentTimeMillis();\r
- for (int i = 0; i < iterations; ++i) temp = CollectionUtilities.containsAll(hs1, hs2);\r
- end = System.currentTimeMillis();\r
- logln(temp + " " + (end - start)/1000.0);\r
- start = System.currentTimeMillis();\r
- for (int i = 0; i < iterations; ++i) temp = CollectionUtilities.containsAll(ts1, ts2);\r
- end = System.currentTimeMillis();\r
- logln(temp + " " + (end - start)/1000.0);\r
- }\r
- \r
- public void TestCollectionUtilities() {\r
- String[][] test = {{"a", "c", "e", "g", "h", "z"}, {"b", "d", "f", "h", "w"}, { "a", "b" }, { "a", "d" }, {"d"}, {}}; // \r
- int resultMask = 0;\r
- for (int i = 0; i < test.length; ++i) {\r
- Collection a = new TreeSet(Arrays.asList(test[i]));\r
- for (int j = 0; j < test.length; ++j) {\r
- Collection b = new TreeSet(Arrays.asList(test[j]));\r
- int relation = CollectionUtilities.getContainmentRelation(a, b);\r
- resultMask |= (1 << relation);\r
- switch (relation) {\r
- case CollectionUtilities.ALL_EMPTY:\r
- checkContainment(a.size() == 0 && b.size() == 0, a, relation, b);\r
- break;\r
- case CollectionUtilities.NOT_A_SUPERSET_B:\r
- checkContainment(a.size() == 0 && b.size() != 0, a, relation, b);\r
- break;\r
- case CollectionUtilities.NOT_A_DISJOINT_B:\r
- checkContainment(a.equals(b) && a.size() != 0, a, relation, b);\r
- break;\r
- case CollectionUtilities.NOT_A_SUBSET_B:\r
- checkContainment(a.size() != 0 && b.size() == 0, a, relation, b);\r
- break;\r
- case CollectionUtilities.A_PROPER_SUBSET_OF_B:\r
- checkContainment(b.containsAll(a) && !a.equals(b), a, relation, b);\r
- break;\r
- case CollectionUtilities.NOT_A_EQUALS_B:\r
- checkContainment(!CollectionUtilities.containsSome(a, b) && a.size() != 0 && b.size() != 0, a, relation, b);\r
- break;\r
- case CollectionUtilities.A_PROPER_SUPERSET_B:\r
- checkContainment(a.containsAll(b) && !a.equals(b), a, relation, b);\r
- break;\r
- case CollectionUtilities.A_PROPER_OVERLAPS_B:\r
- checkContainment(!b.containsAll(a) && !a.containsAll(b) && CollectionUtilities.containsSome(a, b), a, relation, b);\r
- break;\r
- }\r
- }\r
- }\r
- if (resultMask != 0xFF) {\r
- String missing = "";\r
- for (int i = 0; i < 8; ++i) {\r
- if ((resultMask & (1 << i)) == 0) {\r
- if (missing.length() != 0) missing += ", ";\r
- missing += RelationName[i];\r
- }\r
- }\r
- errln("Not all ContainmentRelations checked: " + missing);\r
- }\r
- }\r
-\r
- static final String[] RelationName = {"ALL_EMPTY",\r
- "NOT_A_SUPERSET_B",\r
- "NOT_A_DISJOINT_B",\r
- "NOT_A_SUBSET_B",\r
- "A_PROPER_SUBSET_OF_B",\r
- "A_PROPER_DISJOINT_B",\r
- "A_PROPER_SUPERSET_B",\r
- "A_PROPER_OVERLAPS_B"};\r
-\r
- /**\r
- * \r
- */\r
- private void checkContainment(boolean c, Collection a, int relation, Collection b) {\r
- if (!c) {\r
- errln("Fails relation: " + a + " \t" + RelationName[relation] + " \t" + b);\r
- }\r
- }\r
-\r
- private void checkNext(int limit) {\r
- logln("Comparing nextRange");\r
- UnicodeMap.MapIterator mi = new UnicodeMap.MapIterator(map1);\r
- Map localMap = new TreeMap();\r
- while (mi.nextRange()) {\r
- logln(Utility.hex(mi.codepoint) + ".." + Utility.hex(mi.codepointEnd) + " => " + mi.value);\r
- for (int i = mi.codepoint; i <= mi.codepointEnd; ++i) {\r
- if (i >= limit) continue;\r
- localMap.put(new Integer(i), mi.value);\r
- }\r
- }\r
- checkMap(map2, localMap);\r
- \r
- logln("Comparing next");\r
- mi.reset();\r
- localMap = new TreeMap();\r
- Object lastValue = new Object();\r
- while (mi.next()) {\r
- if (!UnicodeMap.areEqual(lastValue, mi.value)) {\r
- // System.out.println("Change: " + Utility.hex(mi.codepoint) + " => " + mi.value);\r
- lastValue = mi.value;\r
- }\r
- if (mi.codepoint >= limit) continue;\r
- localMap.put(new Integer(mi.codepoint), mi.value);\r
- }\r
- checkMap(map2, localMap);\r
- }\r
- \r
- public void check(int counter) {\r
- for (int i = 0; i < LIMIT; ++i) {\r
- Object value1 = map1.getValue(i);\r
- Object value2 = map2.get(new Integer(i));\r
- if (!UnicodeMap.areEqual(value1, value2)) {\r
- errln(counter + " Difference at " + Utility.hex(i)\r
- + "\t UnicodeMap: " + value1\r
- + "\t HashMap: " + value2);\r
- errln("UnicodeMap: " + map1);\r
- errln("Log: " + TestBoilerplate.show(log));\r
- errln("HashMap: " + TestBoilerplate.show(map2));\r
- }\r
- }\r
- }\r
- \r
- void checkMap(Map m1, Map m2) {\r
- if (m1.equals(m2)) return;\r
- StringBuffer buffer = new StringBuffer();\r
- Set m1entries = m1.entrySet();\r
- Set m2entries = m2.entrySet();\r
- getEntries("\r\nIn First, and not Second", m1entries, m2entries, buffer, 20);\r
- getEntries("\r\nIn Second, and not First", m2entries, m1entries, buffer, 20);\r
- errln(buffer.toString());\r
- }\r
- \r
- static Comparator ENTRY_COMPARATOR = new Comparator() {\r
- public int compare(Object o1, Object o2) {\r
- if (o1 == o2) return 0;\r
- if (o1 == null) return -1;\r
- if (o2 == null) return 1;\r
- Map.Entry a = (Map.Entry) o1;\r
- Map.Entry b = (Map.Entry) o2;\r
- int result = compare2(a.getKey(), b.getKey());\r
- if (result != 0) return result;\r
- return compare2(a.getValue(), b.getValue());\r
- }\r
- private int compare2(Object o1, Object o2) {\r
- if (o1 == o2) return 0;\r
- if (o1 == null) return -1;\r
- if (o2 == null) return 1;\r
- return ((Comparable)o1).compareTo(o2);\r
- }\r
- };\r
-\r
- private void getEntries(String title, Set m1entries, Set m2entries, StringBuffer buffer, int limit) {\r
- Set m1_m2 = new TreeSet(ENTRY_COMPARATOR);\r
- m1_m2.addAll(m1entries);\r
- m1_m2.removeAll(m2entries);\r
- buffer.append(title + ": " + m1_m2.size() + "\r\n");\r
- for (Iterator it = m1_m2.iterator(); it.hasNext();) {\r
- if (limit-- < 0) return;\r
- Map.Entry entry = (Map.Entry) it.next();\r
- buffer.append(entry.getKey()).append(" => ")\r
- .append(entry.getValue()).append("\r\n");\r
- }\r
- }\r
- \r
- static final int SET_LIMIT = 0x10FFFF;\r
- static final int CHECK_LIMIT = 0xFFFF;\r
- static final NumberFormat pf = NumberFormat.getPercentInstance();\r
- static final NumberFormat nf = NumberFormat.getInstance();\r
- \r
- public void TestTime() {\r
- double hashTime, umTime, icuTime, treeTime;\r
- umTime = checkSetTime(20, 0);\r
- hashTime = checkSetTime(20, 1);\r
- logln("Percentage: " + pf.format(hashTime/umTime));\r
- treeTime = checkSetTime(20, 3);\r
- logln("Percentage: " + pf.format(treeTime/umTime));\r
- //logln(map1.toString());\r
- \r
- umTime = checkGetTime(1000, 0);\r
- hashTime = checkGetTime(1000, 1);\r
- logln("Percentage: " + pf.format(hashTime/umTime));\r
- icuTime = checkGetTime(1000, 2);\r
- logln("Percentage: " + pf.format(icuTime/umTime));\r
- treeTime = checkGetTime(1000, 3);\r
- logln("Percentage: " + pf.format(treeTime/umTime));\r
- }\r
- \r
- int propEnum = UProperty.GENERAL_CATEGORY;\r
- \r
- double checkSetTime(int iterations, int type) {\r
- _checkSetTime(1,type);\r
- double result = _checkSetTime(iterations, type);\r
- logln((type == 0 ? "UnicodeMap" : type == 1 ? "HashMap" : type == 2 ? "ICU" : "TreeMap") + "\t" + nf.format(result));\r
- return result;\r
- }\r
- double _checkSetTime(int iterations, int type) {\r
- map1 = new UnicodeMap();\r
- map2 = new HashMap();\r
- System.gc();\r
- double start = System.currentTimeMillis();\r
- for (int j = 0; j < iterations; ++j)\r
- for (int cp = 0; cp <= SET_LIMIT; ++cp) {\r
- int enumValue = UCharacter.getIntPropertyValue(cp, propEnum);\r
- if (enumValue <= 0) continue; // for smaller set\r
- String value = UCharacter.getPropertyValueName(propEnum,enumValue, UProperty.NameChoice.LONG);\r
- switch(type) {\r
- case 0: map1.put(cp, value); break;\r
- case 1: map2.put(new Integer(cp), value); break;\r
- case 3: map3.put(new Integer(cp), value); break;\r
- }\r
- }\r
- double end = System.currentTimeMillis();\r
- return (end-start)/1000/iterations;\r
- }\r
- \r
- double checkGetTime(int iterations, int type) {\r
- _checkGetTime(1,type);\r
- double result = _checkGetTime(iterations, type);\r
- logln((type == 0 ? "UnicodeMap" : type == 1 ? "HashMap" : type == 2 ? "ICU" : "TreeMap") + "\t" + nf.format(result));\r
- return result;\r
- }\r
- double _checkGetTime(int iterations, int type) {\r
- System.gc();\r
- double start = System.currentTimeMillis();\r
- for (int j = 0; j < iterations; ++j)\r
- for (int cp = 0; cp < CHECK_LIMIT; ++cp) {\r
- switch (type) {\r
- case 0: map1.getValue(cp); break;\r
- case 1: map2.get(new Integer(cp)); break;\r
- case 2:\r
- int enumValue = UCharacter.getIntPropertyValue(cp, propEnum);\r
- //if (enumValue <= 0) continue;\r
- UCharacter.getPropertyValueName(propEnum,enumValue, UProperty.NameChoice.LONG);\r
- break; \r
- case 3: map3.get(new Integer(cp)); break;\r
- }\r
- }\r
- double end = System.currentTimeMillis();\r
- return (end-start)/1000/iterations;\r
- }\r
- \r
- static class UnicodeMapBoilerplate extends TestBoilerplate {\r
-\r
- /* \r
- * @see com.ibm.icu.dev.test.TestBoilerplate#_hasSameBehavior(java.lang.Object, java.lang.Object)\r
- */\r
- protected boolean _hasSameBehavior(Object a, Object b) {\r
- // we are pretty confident in the equals method, so won't bother with this right now.\r
- return true;\r
- }\r
-\r
- /*\r
- * @see com.ibm.icu.dev.test.TestBoilerplate#_createTestObject()\r
- */\r
- protected boolean _addTestObject(List list) {\r
- if (list.size() > 30) return false;\r
- UnicodeMap result = new UnicodeMap();\r
- for (int i = 0; i < 50; ++i) {\r
- int start = random.nextInt(25);\r
- String value = TEST_VALUES[random.nextInt(TEST_VALUES.length)];\r
- result.put(start, value);\r
- }\r
- list.add(result);\r
- return true;\r
- }\r
- }\r
- \r
- static class StringBoilerplate extends TestBoilerplate {\r
-\r
- /* \r
- * @see com.ibm.icu.dev.test.TestBoilerplate#_hasSameBehavior(java.lang.Object, java.lang.Object)\r
- */\r
- protected boolean _hasSameBehavior(Object a, Object b) {\r
- // we are pretty confident in the equals method, so won't bother with this right now.\r
- return true;\r
- }\r
-\r
- /*\r
- * @see com.ibm.icu.dev.test.TestBoilerplate#_createTestObject()\r
- */\r
- protected boolean _addTestObject(List list) {\r
- if (list.size() > 31) return false;\r
- StringBuffer result = new StringBuffer();\r
- for (int i = 0; i < 10; ++i) {\r
- result.append((char)random.nextInt(0xFF));\r
- }\r
- list.add(result.toString());\r
- return true;\r
- }\r
- }\r
- \r
- static class UnicodeSetBoilerplate extends TestBoilerplate {\r
-\r
- /* \r
- * @see com.ibm.icu.dev.test.TestBoilerplate#_hasSameBehavior(java.lang.Object, java.lang.Object)\r
- */\r
- protected boolean _hasSameBehavior(Object a, Object b) {\r
- // we are pretty confident in the equals method, so won't bother with this right now.\r
- return true;\r
- }\r
-\r
- /*\r
- * @see com.ibm.icu.dev.test.TestBoilerplate#_createTestObject()\r
- */\r
- protected boolean _addTestObject(List list) {\r
- if (list.size() > 32) return false;\r
- UnicodeSet result = new UnicodeSet();\r
- for (int i = 0; i < 50; ++i) {\r
- result.add(random.nextInt(100));\r
- }\r
- list.add(result.toString());\r
- return true;\r
- }\r
- }\r
-\r
-}\r
-//#endif\r
+//##header J2SE15
+//#if defined(FOUNDATION10) || defined(J2SE13)
+//#else
+/*
+ *******************************************************************************
+ * Copyright (C) 1996-2009, International Business Machines Corporation and *
+ * others. All Rights Reserved. *
+ *******************************************************************************
+ */
+package com.ibm.icu.dev.test.util;
+
+import java.text.NumberFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import com.ibm.icu.dev.test.TestBoilerplate;
+import com.ibm.icu.dev.test.TestFmwk;
+import com.ibm.icu.impl.Utility;
+import com.ibm.icu.lang.UCharacter;
+import com.ibm.icu.lang.UProperty;
+import com.ibm.icu.text.UnicodeSet;
+
+public class TestUtilities extends TestFmwk {
+ static final int LIMIT = 0x15; // limit to make testing more realistic in terms of collisions
+ static final int ITERATIONS = 1000000;
+ static final boolean SHOW_PROGRESS = false;
+ static final boolean DEBUG = false;
+
+ public static void main(String[] args) throws Exception {
+ new TestUtilities().run(args);
+ }
+
+ UnicodeMap map1 = new UnicodeMap();
+ Map map2 = new HashMap();
+ Map map3 = new TreeMap();
+ SortedSet log = new TreeSet();
+ static String[] TEST_VALUES = {null, "A", "B", "C", "D", "E", "F"};
+ static Random random = new Random(12345);
+
+ public void TestUnicodeMap() {
+ random.setSeed(12345);
+ // do random change to both, then compare
+ logln("Comparing against HashMap");
+ for (int counter = 0; counter < ITERATIONS; ++counter) {
+ int start = random.nextInt(LIMIT);
+ String value = TEST_VALUES[random.nextInt(TEST_VALUES.length)];
+ String logline = Utility.hex(start) + "\t" + value;
+ if (SHOW_PROGRESS) logln(counter + "\t" + logline);
+ log.add(logline);
+ if (DEBUG && counter == 144) {
+ System.out.println(" debug");
+ }
+ map1.put(start, value);
+ map2.put(new Integer(start), value);
+ check(counter);
+ }
+ checkNext(LIMIT);
+
+ logln("Setting General Category");
+ map1 = new UnicodeMap();
+ map2 = new TreeMap();
+ for (int cp = 0; cp <= SET_LIMIT; ++cp) {
+ int enumValue = UCharacter.getIntPropertyValue(cp, propEnum);
+ //if (enumValue <= 0) continue; // for smaller set
+ String value = UCharacter.getPropertyValueName(propEnum,enumValue, UProperty.NameChoice.LONG);
+ map1.put(cp, value);
+ map2.put(new Integer(cp), value);
+ }
+ checkNext(Integer.MAX_VALUE);
+
+
+ logln("Comparing General Category");
+ check(-1);
+ logln("Comparing Values");
+ Set values1 = (Set) map1.getAvailableValues(new TreeSet());
+ Set values2 = new TreeSet(map2.values());
+ if (!TestBoilerplate.verifySetsIdentical(this, values1, values2)) {
+ throw new IllegalArgumentException("Halting");
+ }
+ logln("Comparing Sets");
+ for (Iterator it = values1.iterator(); it.hasNext();) {
+ Object value = it.next();
+ logln(value == null ? "null" : value.toString());
+ UnicodeSet set1 = map1.getSet(value);
+ UnicodeSet set2 = TestBoilerplate.getSet(map2, value);
+ if (!TestBoilerplate.verifySetsIdentical(this, set1, set2)) {
+ throw new IllegalArgumentException("Halting");
+ }
+ }
+
+ logln("Getting Scripts");
+ UnicodeMap scripts = ICUPropertyFactory.make().getProperty("script").getUnicodeMap_internal();
+ UnicodeMap.Composer composer = new UnicodeMap.Composer() {
+ public Object compose(int codePoint, Object a, Object b) {
+ return a.toString() + "_" + b.toString();
+ }
+ };
+
+ logln("Trying Compose");
+ UnicodeMap composed = ((UnicodeMap)scripts.cloneAsThawed()).composeWith(map1, composer);
+ Object last = "";
+ for (int i = 0; i < 0x10FFFF; ++i) {
+ Object comp = composed.getValue(i);
+ Object gc = map1.getValue(i);
+ Object sc = scripts.getValue(i);
+ if (!comp.equals(composer.compose(i, gc, sc))) {
+ errln("Failed compose at: " + i);
+ }
+ if (!last.equals(comp)) {
+ logln(Utility.hex(i) + "\t" + comp);
+ last = comp;
+ }
+ }
+
+ // check boilerplate
+ List argList = new ArrayList();
+ argList.add("TestMain");
+ if (params.nothrow) argList.add("-nothrow");
+ if (params.verbose) argList.add("-verbose");
+ String[] args = new String[argList.size()];
+ argList.toArray(args);
+ new UnicodeMapBoilerplate().run(args);
+ // TODO: the following is not being reached
+ new UnicodeSetBoilerplate().run(args);
+ }
+
+ public void TestCollectionUtilitySpeed() {
+ HashSet hs1 = new HashSet();
+ HashSet hs2 = new HashSet();
+ int size = 100000;
+ int iterations = 100;
+ String prefix = "abcde";
+ String postfix = "abcde";
+ int start1 = 0; // 1 for some, 0 for all
+ for (int i = 0; i < size; i += 2) hs1.add(prefix + String.valueOf(i) + postfix);
+ for (int i = start1; i < size; i += 2) hs2.add(prefix + String.valueOf(i) + postfix);
+ TreeSet ts1 = new TreeSet(hs1);
+ TreeSet ts2 = new TreeSet(hs2);
+ CollectionUtilities.containsAll(hs1, hs2);
+ CollectionUtilities.containsAll(ts1, ts2);
+ long start, end;
+ boolean temp = false;
+ start = System.currentTimeMillis();
+ for (int i = 0; i < iterations; ++i) temp = CollectionUtilities.containsAll(hs1, hs2);
+ end = System.currentTimeMillis();
+ logln(temp + " " + (end - start)/1000.0);
+ start = System.currentTimeMillis();
+ for (int i = 0; i < iterations; ++i) temp = CollectionUtilities.containsAll(ts1, ts2);
+ end = System.currentTimeMillis();
+ logln(temp + " " + (end - start)/1000.0);
+ }
+
+ public void TestCollectionUtilities() {
+ String[][] test = {{"a", "c", "e", "g", "h", "z"}, {"b", "d", "f", "h", "w"}, { "a", "b" }, { "a", "d" }, {"d"}, {}}; //
+ int resultMask = 0;
+ for (int i = 0; i < test.length; ++i) {
+ Collection a = new TreeSet(Arrays.asList(test[i]));
+ for (int j = 0; j < test.length; ++j) {
+ Collection b = new TreeSet(Arrays.asList(test[j]));
+ int relation = CollectionUtilities.getContainmentRelation(a, b);
+ resultMask |= (1 << relation);
+ switch (relation) {
+ case CollectionUtilities.ALL_EMPTY:
+ checkContainment(a.size() == 0 && b.size() == 0, a, relation, b);
+ break;
+ case CollectionUtilities.NOT_A_SUPERSET_B:
+ checkContainment(a.size() == 0 && b.size() != 0, a, relation, b);
+ break;
+ case CollectionUtilities.NOT_A_DISJOINT_B:
+ checkContainment(a.equals(b) && a.size() != 0, a, relation, b);
+ break;
+ case CollectionUtilities.NOT_A_SUBSET_B:
+ checkContainment(a.size() != 0 && b.size() == 0, a, relation, b);
+ break;
+ case CollectionUtilities.A_PROPER_SUBSET_OF_B:
+ checkContainment(b.containsAll(a) && !a.equals(b), a, relation, b);
+ break;
+ case CollectionUtilities.NOT_A_EQUALS_B:
+ checkContainment(!CollectionUtilities.containsSome(a, b) && a.size() != 0 && b.size() != 0, a, relation, b);
+ break;
+ case CollectionUtilities.A_PROPER_SUPERSET_B:
+ checkContainment(a.containsAll(b) && !a.equals(b), a, relation, b);
+ break;
+ case CollectionUtilities.A_PROPER_OVERLAPS_B:
+ checkContainment(!b.containsAll(a) && !a.containsAll(b) && CollectionUtilities.containsSome(a, b), a, relation, b);
+ break;
+ }
+ }
+ }
+ if (resultMask != 0xFF) {
+ String missing = "";
+ for (int i = 0; i < 8; ++i) {
+ if ((resultMask & (1 << i)) == 0) {
+ if (missing.length() != 0) missing += ", ";
+ missing += RelationName[i];
+ }
+ }
+ errln("Not all ContainmentRelations checked: " + missing);
+ }
+ }
+
+ static final String[] RelationName = {"ALL_EMPTY",
+ "NOT_A_SUPERSET_B",
+ "NOT_A_DISJOINT_B",
+ "NOT_A_SUBSET_B",
+ "A_PROPER_SUBSET_OF_B",
+ "A_PROPER_DISJOINT_B",
+ "A_PROPER_SUPERSET_B",
+ "A_PROPER_OVERLAPS_B"};
+
+ /**
+ *
+ */
+ private void checkContainment(boolean c, Collection a, int relation, Collection b) {
+ if (!c) {
+ errln("Fails relation: " + a + " \t" + RelationName[relation] + " \t" + b);
+ }
+ }
+
+ private void checkNext(int limit) {
+ logln("Comparing nextRange");
+ UnicodeMap.MapIterator mi = new UnicodeMap.MapIterator(map1);
+ Map localMap = new TreeMap();
+ while (mi.nextRange()) {
+ logln(Utility.hex(mi.codepoint) + ".." + Utility.hex(mi.codepointEnd) + " => " + mi.value);
+ for (int i = mi.codepoint; i <= mi.codepointEnd; ++i) {
+ if (i >= limit) continue;
+ localMap.put(new Integer(i), mi.value);
+ }
+ }
+ checkMap(map2, localMap);
+
+ logln("Comparing next");
+ mi.reset();
+ localMap = new TreeMap();
+ Object lastValue = new Object();
+ while (mi.next()) {
+ if (!UnicodeMap.areEqual(lastValue, mi.value)) {
+ // System.out.println("Change: " + Utility.hex(mi.codepoint) + " => " + mi.value);
+ lastValue = mi.value;
+ }
+ if (mi.codepoint >= limit) continue;
+ localMap.put(new Integer(mi.codepoint), mi.value);
+ }
+ checkMap(map2, localMap);
+ }
+
+ public void check(int counter) {
+ for (int i = 0; i < LIMIT; ++i) {
+ Object value1 = map1.getValue(i);
+ Object value2 = map2.get(new Integer(i));
+ if (!UnicodeMap.areEqual(value1, value2)) {
+ errln(counter + " Difference at " + Utility.hex(i)
+ + "\t UnicodeMap: " + value1
+ + "\t HashMap: " + value2);
+ errln("UnicodeMap: " + map1);
+ errln("Log: " + TestBoilerplate.show(log));
+ errln("HashMap: " + TestBoilerplate.show(map2));
+ }
+ }
+ }
+
+ void checkMap(Map m1, Map m2) {
+ if (m1.equals(m2)) return;
+ StringBuffer buffer = new StringBuffer();
+ Set m1entries = m1.entrySet();
+ Set m2entries = m2.entrySet();
+ getEntries("\r\nIn First, and not Second", m1entries, m2entries, buffer, 20);
+ getEntries("\r\nIn Second, and not First", m2entries, m1entries, buffer, 20);
+ errln(buffer.toString());
+ }
+
+ static Comparator ENTRY_COMPARATOR = new Comparator() {
+ public int compare(Object o1, Object o2) {
+ if (o1 == o2) return 0;
+ if (o1 == null) return -1;
+ if (o2 == null) return 1;
+ Map.Entry a = (Map.Entry) o1;
+ Map.Entry b = (Map.Entry) o2;
+ int result = compare2(a.getKey(), b.getKey());
+ if (result != 0) return result;
+ return compare2(a.getValue(), b.getValue());
+ }
+ private int compare2(Object o1, Object o2) {
+ if (o1 == o2) return 0;
+ if (o1 == null) return -1;
+ if (o2 == null) return 1;
+ return ((Comparable)o1).compareTo(o2);
+ }
+ };
+
+ private void getEntries(String title, Set m1entries, Set m2entries, StringBuffer buffer, int limit) {
+ Set m1_m2 = new TreeSet(ENTRY_COMPARATOR);
+ m1_m2.addAll(m1entries);
+ m1_m2.removeAll(m2entries);
+ buffer.append(title + ": " + m1_m2.size() + "\r\n");
+ for (Iterator it = m1_m2.iterator(); it.hasNext();) {
+ if (limit-- < 0) return;
+ Map.Entry entry = (Map.Entry) it.next();
+ buffer.append(entry.getKey()).append(" => ")
+ .append(entry.getValue()).append("\r\n");
+ }
+ }
+
+ static final int SET_LIMIT = 0x10FFFF;
+ static final int CHECK_LIMIT = 0xFFFF;
+ static final NumberFormat pf = NumberFormat.getPercentInstance();
+ static final NumberFormat nf = NumberFormat.getInstance();
+
+ public void TestTime() {
+ double hashTime, umTime, icuTime, treeTime;
+ umTime = checkSetTime(20, 0);
+ hashTime = checkSetTime(20, 1);
+ logln("Percentage: " + pf.format(hashTime/umTime));
+ treeTime = checkSetTime(20, 3);
+ logln("Percentage: " + pf.format(treeTime/umTime));
+ //logln(map1.toString());
+
+ umTime = checkGetTime(1000, 0);
+ hashTime = checkGetTime(1000, 1);
+ logln("Percentage: " + pf.format(hashTime/umTime));
+ icuTime = checkGetTime(1000, 2);
+ logln("Percentage: " + pf.format(icuTime/umTime));
+ treeTime = checkGetTime(1000, 3);
+ logln("Percentage: " + pf.format(treeTime/umTime));
+ }
+
+ int propEnum = UProperty.GENERAL_CATEGORY;
+
+ double checkSetTime(int iterations, int type) {
+ _checkSetTime(1,type);
+ double result = _checkSetTime(iterations, type);
+ logln((type == 0 ? "UnicodeMap" : type == 1 ? "HashMap" : type == 2 ? "ICU" : "TreeMap") + "\t" + nf.format(result));
+ return result;
+ }
+ double _checkSetTime(int iterations, int type) {
+ map1 = new UnicodeMap();
+ map2 = new HashMap();
+ System.gc();
+ double start = System.currentTimeMillis();
+ for (int j = 0; j < iterations; ++j)
+ for (int cp = 0; cp <= SET_LIMIT; ++cp) {
+ int enumValue = UCharacter.getIntPropertyValue(cp, propEnum);
+ if (enumValue <= 0) continue; // for smaller set
+ String value = UCharacter.getPropertyValueName(propEnum,enumValue, UProperty.NameChoice.LONG);
+ switch(type) {
+ case 0: map1.put(cp, value); break;
+ case 1: map2.put(new Integer(cp), value); break;
+ case 3: map3.put(new Integer(cp), value); break;
+ }
+ }
+ double end = System.currentTimeMillis();
+ return (end-start)/1000/iterations;
+ }
+
+ double checkGetTime(int iterations, int type) {
+ _checkGetTime(1,type);
+ double result = _checkGetTime(iterations, type);
+ logln((type == 0 ? "UnicodeMap" : type == 1 ? "HashMap" : type == 2 ? "ICU" : "TreeMap") + "\t" + nf.format(result));
+ return result;
+ }
+ double _checkGetTime(int iterations, int type) {
+ System.gc();
+ double start = System.currentTimeMillis();
+ for (int j = 0; j < iterations; ++j)
+ for (int cp = 0; cp < CHECK_LIMIT; ++cp) {
+ switch (type) {
+ case 0: map1.getValue(cp); break;
+ case 1: map2.get(new Integer(cp)); break;
+ case 2:
+ int enumValue = UCharacter.getIntPropertyValue(cp, propEnum);
+ //if (enumValue <= 0) continue;
+ UCharacter.getPropertyValueName(propEnum,enumValue, UProperty.NameChoice.LONG);
+ break;
+ case 3: map3.get(new Integer(cp)); break;
+ }
+ }
+ double end = System.currentTimeMillis();
+ return (end-start)/1000/iterations;
+ }
+
+ static class UnicodeMapBoilerplate extends TestBoilerplate {
+
+ /*
+ * @see com.ibm.icu.dev.test.TestBoilerplate#_hasSameBehavior(java.lang.Object, java.lang.Object)
+ */
+ protected boolean _hasSameBehavior(Object a, Object b) {
+ // we are pretty confident in the equals method, so won't bother with this right now.
+ return true;
+ }
+
+ /*
+ * @see com.ibm.icu.dev.test.TestBoilerplate#_createTestObject()
+ */
+ protected boolean _addTestObject(List list) {
+ if (list.size() > 30) return false;
+ UnicodeMap result = new UnicodeMap();
+ for (int i = 0; i < 50; ++i) {
+ int start = random.nextInt(25);
+ String value = TEST_VALUES[random.nextInt(TEST_VALUES.length)];
+ result.put(start, value);
+ }
+ list.add(result);
+ return true;
+ }
+ }
+
+ static class StringBoilerplate extends TestBoilerplate {
+
+ /*
+ * @see com.ibm.icu.dev.test.TestBoilerplate#_hasSameBehavior(java.lang.Object, java.lang.Object)
+ */
+ protected boolean _hasSameBehavior(Object a, Object b) {
+ // we are pretty confident in the equals method, so won't bother with this right now.
+ return true;
+ }
+
+ /*
+ * @see com.ibm.icu.dev.test.TestBoilerplate#_createTestObject()
+ */
+ protected boolean _addTestObject(List list) {
+ if (list.size() > 31) return false;
+ StringBuffer result = new StringBuffer();
+ for (int i = 0; i < 10; ++i) {
+ result.append((char)random.nextInt(0xFF));
+ }
+ list.add(result.toString());
+ return true;
+ }
+ }
+
+ static class UnicodeSetBoilerplate extends TestBoilerplate {
+
+ /*
+ * @see com.ibm.icu.dev.test.TestBoilerplate#_hasSameBehavior(java.lang.Object, java.lang.Object)
+ */
+ protected boolean _hasSameBehavior(Object a, Object b) {
+ // we are pretty confident in the equals method, so won't bother with this right now.
+ return true;
+ }
+
+ /*
+ * @see com.ibm.icu.dev.test.TestBoilerplate#_createTestObject()
+ */
+ protected boolean _addTestObject(List list) {
+ if (list.size() > 32) return false;
+ UnicodeSet result = new UnicodeSet();
+ for (int i = 0; i < 50; ++i) {
+ result.add(random.nextInt(100));
+ }
+ list.add(result.toString());
+ return true;
+ }
+ }
+
+}
+//#endif