2 //#if defined(FOUNDATION10) || defined(J2SE13)
\r
5 *******************************************************************************
\r
6 * Copyright (C) 1996-2009, International Business Machines Corporation and *
\r
7 * others. All Rights Reserved. *
\r
8 *******************************************************************************
\r
10 package com.ibm.icu.dev.test.util;
\r
12 import java.text.NumberFormat;
\r
13 import java.util.ArrayList;
\r
14 import java.util.Arrays;
\r
15 import java.util.Collection;
\r
16 import java.util.Comparator;
\r
17 import java.util.HashMap;
\r
18 import java.util.HashSet;
\r
19 import java.util.Iterator;
\r
20 import java.util.List;
\r
21 import java.util.Map;
\r
22 import java.util.Random;
\r
23 import java.util.Set;
\r
24 import java.util.SortedSet;
\r
25 import java.util.TreeMap;
\r
26 import java.util.TreeSet;
\r
28 import com.ibm.icu.dev.test.TestBoilerplate;
\r
29 import com.ibm.icu.dev.test.TestFmwk;
\r
30 import com.ibm.icu.impl.Utility;
\r
31 import com.ibm.icu.lang.UCharacter;
\r
32 import com.ibm.icu.lang.UProperty;
\r
33 import com.ibm.icu.text.UnicodeSet;
\r
35 public class TestUtilities extends TestFmwk {
\r
36 static final int LIMIT = 0x15; // limit to make testing more realistic in terms of collisions
\r
37 static final int ITERATIONS = 1000000;
\r
38 static final boolean SHOW_PROGRESS = false;
\r
39 static final boolean DEBUG = false;
\r
41 public static void main(String[] args) throws Exception {
\r
42 new TestUtilities().run(args);
\r
45 UnicodeMap map1 = new UnicodeMap();
\r
46 Map map2 = new HashMap();
\r
47 Map map3 = new TreeMap();
\r
48 SortedSet log = new TreeSet();
\r
49 static String[] TEST_VALUES = {null, "A", "B", "C", "D", "E", "F"};
\r
50 static Random random = new Random(12345);
\r
52 public void TestUnicodeMap() {
\r
53 random.setSeed(12345);
\r
54 // do random change to both, then compare
\r
55 logln("Comparing against HashMap");
\r
56 for (int counter = 0; counter < ITERATIONS; ++counter) {
\r
57 int start = random.nextInt(LIMIT);
\r
58 String value = TEST_VALUES[random.nextInt(TEST_VALUES.length)];
\r
59 String logline = Utility.hex(start) + "\t" + value;
\r
60 if (SHOW_PROGRESS) logln(counter + "\t" + logline);
\r
62 if (DEBUG && counter == 144) {
\r
63 System.out.println(" debug");
\r
65 map1.put(start, value);
\r
66 map2.put(new Integer(start), value);
\r
71 logln("Setting General Category");
\r
72 map1 = new UnicodeMap();
\r
73 map2 = new TreeMap();
\r
74 for (int cp = 0; cp <= SET_LIMIT; ++cp) {
\r
75 int enumValue = UCharacter.getIntPropertyValue(cp, propEnum);
\r
76 //if (enumValue <= 0) continue; // for smaller set
\r
77 String value = UCharacter.getPropertyValueName(propEnum,enumValue, UProperty.NameChoice.LONG);
\r
78 map1.put(cp, value);
\r
79 map2.put(new Integer(cp), value);
\r
81 checkNext(Integer.MAX_VALUE);
\r
84 logln("Comparing General Category");
\r
86 logln("Comparing Values");
\r
87 Set values1 = (Set) map1.getAvailableValues(new TreeSet());
\r
88 Set values2 = new TreeSet(map2.values());
\r
89 if (!TestBoilerplate.verifySetsIdentical(this, values1, values2)) {
\r
90 throw new IllegalArgumentException("Halting");
\r
92 logln("Comparing Sets");
\r
93 for (Iterator it = values1.iterator(); it.hasNext();) {
\r
94 Object value = it.next();
\r
95 logln(value == null ? "null" : value.toString());
\r
96 UnicodeSet set1 = map1.getSet(value);
\r
97 UnicodeSet set2 = TestBoilerplate.getSet(map2, value);
\r
98 if (!TestBoilerplate.verifySetsIdentical(this, set1, set2)) {
\r
99 throw new IllegalArgumentException("Halting");
\r
103 logln("Getting Scripts");
\r
104 UnicodeMap scripts = ICUPropertyFactory.make().getProperty("script").getUnicodeMap_internal();
\r
105 UnicodeMap.Composer composer = new UnicodeMap.Composer() {
\r
106 public Object compose(int codePoint, Object a, Object b) {
\r
107 return a.toString() + "_" + b.toString();
\r
111 logln("Trying Compose");
\r
112 UnicodeMap composed = ((UnicodeMap)scripts.cloneAsThawed()).composeWith(map1, composer);
\r
114 for (int i = 0; i < 0x10FFFF; ++i) {
\r
115 Object comp = composed.getValue(i);
\r
116 Object gc = map1.getValue(i);
\r
117 Object sc = scripts.getValue(i);
\r
118 if (!comp.equals(composer.compose(i, gc, sc))) {
\r
119 errln("Failed compose at: " + i);
\r
121 if (!last.equals(comp)) {
\r
122 logln(Utility.hex(i) + "\t" + comp);
\r
127 // check boilerplate
\r
128 List argList = new ArrayList();
\r
129 argList.add("TestMain");
\r
130 if (params.nothrow) argList.add("-nothrow");
\r
131 if (params.verbose) argList.add("-verbose");
\r
132 String[] args = new String[argList.size()];
\r
133 argList.toArray(args);
\r
134 new UnicodeMapBoilerplate().run(args);
\r
135 // TODO: the following is not being reached
\r
136 new UnicodeSetBoilerplate().run(args);
\r
139 public void TestCollectionUtilitySpeed() {
\r
140 HashSet hs1 = new HashSet();
\r
141 HashSet hs2 = new HashSet();
\r
143 int iterations = 100;
\r
144 String prefix = "abcde";
\r
145 String postfix = "abcde";
\r
146 int start1 = 0; // 1 for some, 0 for all
\r
147 for (int i = 0; i < size; i += 2) hs1.add(prefix + String.valueOf(i) + postfix);
\r
148 for (int i = start1; i < size; i += 2) hs2.add(prefix + String.valueOf(i) + postfix);
\r
149 TreeSet ts1 = new TreeSet(hs1);
\r
150 TreeSet ts2 = new TreeSet(hs2);
\r
151 CollectionUtilities.containsAll(hs1, hs2);
\r
152 CollectionUtilities.containsAll(ts1, ts2);
\r
154 boolean temp = false;
\r
155 start = System.currentTimeMillis();
\r
156 for (int i = 0; i < iterations; ++i) temp = CollectionUtilities.containsAll(hs1, hs2);
\r
157 end = System.currentTimeMillis();
\r
158 logln(temp + " " + (end - start)/1000.0);
\r
159 start = System.currentTimeMillis();
\r
160 for (int i = 0; i < iterations; ++i) temp = CollectionUtilities.containsAll(ts1, ts2);
\r
161 end = System.currentTimeMillis();
\r
162 logln(temp + " " + (end - start)/1000.0);
\r
165 public void TestCollectionUtilities() {
\r
166 String[][] test = {{"a", "c", "e", "g", "h", "z"}, {"b", "d", "f", "h", "w"}, { "a", "b" }, { "a", "d" }, {"d"}, {}}; //
\r
167 int resultMask = 0;
\r
168 for (int i = 0; i < test.length; ++i) {
\r
169 Collection a = new TreeSet(Arrays.asList(test[i]));
\r
170 for (int j = 0; j < test.length; ++j) {
\r
171 Collection b = new TreeSet(Arrays.asList(test[j]));
\r
172 int relation = CollectionUtilities.getContainmentRelation(a, b);
\r
173 resultMask |= (1 << relation);
\r
174 switch (relation) {
\r
175 case CollectionUtilities.ALL_EMPTY:
\r
176 checkContainment(a.size() == 0 && b.size() == 0, a, relation, b);
\r
178 case CollectionUtilities.NOT_A_SUPERSET_B:
\r
179 checkContainment(a.size() == 0 && b.size() != 0, a, relation, b);
\r
181 case CollectionUtilities.NOT_A_DISJOINT_B:
\r
182 checkContainment(a.equals(b) && a.size() != 0, a, relation, b);
\r
184 case CollectionUtilities.NOT_A_SUBSET_B:
\r
185 checkContainment(a.size() != 0 && b.size() == 0, a, relation, b);
\r
187 case CollectionUtilities.A_PROPER_SUBSET_OF_B:
\r
188 checkContainment(b.containsAll(a) && !a.equals(b), a, relation, b);
\r
190 case CollectionUtilities.NOT_A_EQUALS_B:
\r
191 checkContainment(!CollectionUtilities.containsSome(a, b) && a.size() != 0 && b.size() != 0, a, relation, b);
\r
193 case CollectionUtilities.A_PROPER_SUPERSET_B:
\r
194 checkContainment(a.containsAll(b) && !a.equals(b), a, relation, b);
\r
196 case CollectionUtilities.A_PROPER_OVERLAPS_B:
\r
197 checkContainment(!b.containsAll(a) && !a.containsAll(b) && CollectionUtilities.containsSome(a, b), a, relation, b);
\r
202 if (resultMask != 0xFF) {
\r
203 String missing = "";
\r
204 for (int i = 0; i < 8; ++i) {
\r
205 if ((resultMask & (1 << i)) == 0) {
\r
206 if (missing.length() != 0) missing += ", ";
\r
207 missing += RelationName[i];
\r
210 errln("Not all ContainmentRelations checked: " + missing);
\r
214 static final String[] RelationName = {"ALL_EMPTY",
\r
215 "NOT_A_SUPERSET_B",
\r
216 "NOT_A_DISJOINT_B",
\r
218 "A_PROPER_SUBSET_OF_B",
\r
219 "A_PROPER_DISJOINT_B",
\r
220 "A_PROPER_SUPERSET_B",
\r
221 "A_PROPER_OVERLAPS_B"};
\r
226 private void checkContainment(boolean c, Collection a, int relation, Collection b) {
\r
228 errln("Fails relation: " + a + " \t" + RelationName[relation] + " \t" + b);
\r
232 private void checkNext(int limit) {
\r
233 logln("Comparing nextRange");
\r
234 UnicodeMap.MapIterator mi = new UnicodeMap.MapIterator(map1);
\r
235 Map localMap = new TreeMap();
\r
236 while (mi.nextRange()) {
\r
237 logln(Utility.hex(mi.codepoint) + ".." + Utility.hex(mi.codepointEnd) + " => " + mi.value);
\r
238 for (int i = mi.codepoint; i <= mi.codepointEnd; ++i) {
\r
239 if (i >= limit) continue;
\r
240 localMap.put(new Integer(i), mi.value);
\r
243 checkMap(map2, localMap);
\r
245 logln("Comparing next");
\r
247 localMap = new TreeMap();
\r
248 Object lastValue = new Object();
\r
249 while (mi.next()) {
\r
250 if (!UnicodeMap.areEqual(lastValue, mi.value)) {
\r
251 // System.out.println("Change: " + Utility.hex(mi.codepoint) + " => " + mi.value);
\r
252 lastValue = mi.value;
\r
254 if (mi.codepoint >= limit) continue;
\r
255 localMap.put(new Integer(mi.codepoint), mi.value);
\r
257 checkMap(map2, localMap);
\r
260 public void check(int counter) {
\r
261 for (int i = 0; i < LIMIT; ++i) {
\r
262 Object value1 = map1.getValue(i);
\r
263 Object value2 = map2.get(new Integer(i));
\r
264 if (!UnicodeMap.areEqual(value1, value2)) {
\r
265 errln(counter + " Difference at " + Utility.hex(i)
\r
266 + "\t UnicodeMap: " + value1
\r
267 + "\t HashMap: " + value2);
\r
268 errln("UnicodeMap: " + map1);
\r
269 errln("Log: " + TestBoilerplate.show(log));
\r
270 errln("HashMap: " + TestBoilerplate.show(map2));
\r
275 void checkMap(Map m1, Map m2) {
\r
276 if (m1.equals(m2)) return;
\r
277 StringBuffer buffer = new StringBuffer();
\r
278 Set m1entries = m1.entrySet();
\r
279 Set m2entries = m2.entrySet();
\r
280 getEntries("\r\nIn First, and not Second", m1entries, m2entries, buffer, 20);
\r
281 getEntries("\r\nIn Second, and not First", m2entries, m1entries, buffer, 20);
\r
282 errln(buffer.toString());
\r
285 static Comparator ENTRY_COMPARATOR = new Comparator() {
\r
286 public int compare(Object o1, Object o2) {
\r
287 if (o1 == o2) return 0;
\r
288 if (o1 == null) return -1;
\r
289 if (o2 == null) return 1;
\r
290 Map.Entry a = (Map.Entry) o1;
\r
291 Map.Entry b = (Map.Entry) o2;
\r
292 int result = compare2(a.getKey(), b.getKey());
\r
293 if (result != 0) return result;
\r
294 return compare2(a.getValue(), b.getValue());
\r
296 private int compare2(Object o1, Object o2) {
\r
297 if (o1 == o2) return 0;
\r
298 if (o1 == null) return -1;
\r
299 if (o2 == null) return 1;
\r
300 return ((Comparable)o1).compareTo(o2);
\r
304 private void getEntries(String title, Set m1entries, Set m2entries, StringBuffer buffer, int limit) {
\r
305 Set m1_m2 = new TreeSet(ENTRY_COMPARATOR);
\r
306 m1_m2.addAll(m1entries);
\r
307 m1_m2.removeAll(m2entries);
\r
308 buffer.append(title + ": " + m1_m2.size() + "\r\n");
\r
309 for (Iterator it = m1_m2.iterator(); it.hasNext();) {
\r
310 if (limit-- < 0) return;
\r
311 Map.Entry entry = (Map.Entry) it.next();
\r
312 buffer.append(entry.getKey()).append(" => ")
\r
313 .append(entry.getValue()).append("\r\n");
\r
317 static final int SET_LIMIT = 0x10FFFF;
\r
318 static final int CHECK_LIMIT = 0xFFFF;
\r
319 static final NumberFormat pf = NumberFormat.getPercentInstance();
\r
320 static final NumberFormat nf = NumberFormat.getInstance();
\r
322 public void TestTime() {
\r
323 double hashTime, umTime, icuTime, treeTime;
\r
324 umTime = checkSetTime(20, 0);
\r
325 hashTime = checkSetTime(20, 1);
\r
326 logln("Percentage: " + pf.format(hashTime/umTime));
\r
327 treeTime = checkSetTime(20, 3);
\r
328 logln("Percentage: " + pf.format(treeTime/umTime));
\r
329 //logln(map1.toString());
\r
331 umTime = checkGetTime(1000, 0);
\r
332 hashTime = checkGetTime(1000, 1);
\r
333 logln("Percentage: " + pf.format(hashTime/umTime));
\r
334 icuTime = checkGetTime(1000, 2);
\r
335 logln("Percentage: " + pf.format(icuTime/umTime));
\r
336 treeTime = checkGetTime(1000, 3);
\r
337 logln("Percentage: " + pf.format(treeTime/umTime));
\r
340 int propEnum = UProperty.GENERAL_CATEGORY;
\r
342 double checkSetTime(int iterations, int type) {
\r
343 _checkSetTime(1,type);
\r
344 double result = _checkSetTime(iterations, type);
\r
345 logln((type == 0 ? "UnicodeMap" : type == 1 ? "HashMap" : type == 2 ? "ICU" : "TreeMap") + "\t" + nf.format(result));
\r
348 double _checkSetTime(int iterations, int type) {
\r
349 map1 = new UnicodeMap();
\r
350 map2 = new HashMap();
\r
352 double start = System.currentTimeMillis();
\r
353 for (int j = 0; j < iterations; ++j)
\r
354 for (int cp = 0; cp <= SET_LIMIT; ++cp) {
\r
355 int enumValue = UCharacter.getIntPropertyValue(cp, propEnum);
\r
356 if (enumValue <= 0) continue; // for smaller set
\r
357 String value = UCharacter.getPropertyValueName(propEnum,enumValue, UProperty.NameChoice.LONG);
\r
359 case 0: map1.put(cp, value); break;
\r
360 case 1: map2.put(new Integer(cp), value); break;
\r
361 case 3: map3.put(new Integer(cp), value); break;
\r
364 double end = System.currentTimeMillis();
\r
365 return (end-start)/1000/iterations;
\r
368 double checkGetTime(int iterations, int type) {
\r
369 _checkGetTime(1,type);
\r
370 double result = _checkGetTime(iterations, type);
\r
371 logln((type == 0 ? "UnicodeMap" : type == 1 ? "HashMap" : type == 2 ? "ICU" : "TreeMap") + "\t" + nf.format(result));
\r
374 double _checkGetTime(int iterations, int type) {
\r
376 double start = System.currentTimeMillis();
\r
377 for (int j = 0; j < iterations; ++j)
\r
378 for (int cp = 0; cp < CHECK_LIMIT; ++cp) {
\r
380 case 0: map1.getValue(cp); break;
\r
381 case 1: map2.get(new Integer(cp)); break;
\r
383 int enumValue = UCharacter.getIntPropertyValue(cp, propEnum);
\r
384 //if (enumValue <= 0) continue;
\r
385 UCharacter.getPropertyValueName(propEnum,enumValue, UProperty.NameChoice.LONG);
\r
387 case 3: map3.get(new Integer(cp)); break;
\r
390 double end = System.currentTimeMillis();
\r
391 return (end-start)/1000/iterations;
\r
394 static class UnicodeMapBoilerplate extends TestBoilerplate {
\r
397 * @see com.ibm.icu.dev.test.TestBoilerplate#_hasSameBehavior(java.lang.Object, java.lang.Object)
\r
399 protected boolean _hasSameBehavior(Object a, Object b) {
\r
400 // we are pretty confident in the equals method, so won't bother with this right now.
\r
405 * @see com.ibm.icu.dev.test.TestBoilerplate#_createTestObject()
\r
407 protected boolean _addTestObject(List list) {
\r
408 if (list.size() > 30) return false;
\r
409 UnicodeMap result = new UnicodeMap();
\r
410 for (int i = 0; i < 50; ++i) {
\r
411 int start = random.nextInt(25);
\r
412 String value = TEST_VALUES[random.nextInt(TEST_VALUES.length)];
\r
413 result.put(start, value);
\r
420 static class StringBoilerplate extends TestBoilerplate {
\r
423 * @see com.ibm.icu.dev.test.TestBoilerplate#_hasSameBehavior(java.lang.Object, java.lang.Object)
\r
425 protected boolean _hasSameBehavior(Object a, Object b) {
\r
426 // we are pretty confident in the equals method, so won't bother with this right now.
\r
431 * @see com.ibm.icu.dev.test.TestBoilerplate#_createTestObject()
\r
433 protected boolean _addTestObject(List list) {
\r
434 if (list.size() > 31) return false;
\r
435 StringBuffer result = new StringBuffer();
\r
436 for (int i = 0; i < 10; ++i) {
\r
437 result.append((char)random.nextInt(0xFF));
\r
439 list.add(result.toString());
\r
444 static class UnicodeSetBoilerplate extends TestBoilerplate {
\r
447 * @see com.ibm.icu.dev.test.TestBoilerplate#_hasSameBehavior(java.lang.Object, java.lang.Object)
\r
449 protected boolean _hasSameBehavior(Object a, Object b) {
\r
450 // we are pretty confident in the equals method, so won't bother with this right now.
\r
455 * @see com.ibm.icu.dev.test.TestBoilerplate#_createTestObject()
\r
457 protected boolean _addTestObject(List list) {
\r
458 if (list.size() > 32) return false;
\r
459 UnicodeSet result = new UnicodeSet();
\r
460 for (int i = 0; i < 50; ++i) {
\r
461 result.add(random.nextInt(100));
\r
463 list.add(result.toString());
\r