]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/util/TestUtilities.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / util / TestUtilities.java
1 //##header\r
2 //#if defined(FOUNDATION10) || defined(J2SE13)\r
3 //#else\r
4 /*\r
5  *******************************************************************************\r
6  * Copyright (C) 1996-2009, International Business Machines Corporation and    *\r
7  * others. All Rights Reserved.                                                *\r
8  *******************************************************************************\r
9  */\r
10 package com.ibm.icu.dev.test.util;\r
11 \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
27 \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
34 \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
40     \r
41     public static void main(String[] args) throws Exception {\r
42         new TestUtilities().run(args);\r
43     }\r
44     \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
51     \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
61             log.add(logline);\r
62             if (DEBUG && counter == 144) {\r
63                 System.out.println(" debug");\r
64             }\r
65             map1.put(start, value);\r
66             map2.put(new Integer(start), value);\r
67             check(counter);\r
68         }\r
69         checkNext(LIMIT);\r
70         \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
80         }       \r
81         checkNext(Integer.MAX_VALUE);\r
82 \r
83 \r
84         logln("Comparing General Category");\r
85         check(-1);\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
91         }\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
100             }\r
101         } \r
102         \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
108             }\r
109         };\r
110         \r
111         logln("Trying Compose");\r
112         UnicodeMap composed = ((UnicodeMap)scripts.cloneAsThawed()).composeWith(map1, composer);\r
113         Object last = "";\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
120             }\r
121             if (!last.equals(comp)) {\r
122                 logln(Utility.hex(i) + "\t" + comp);\r
123                 last = comp;\r
124             }\r
125         }\r
126 \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
137     }\r
138     \r
139     public void TestCollectionUtilitySpeed() {\r
140         HashSet hs1 = new HashSet();\r
141         HashSet hs2 = new HashSet();\r
142         int size = 100000;\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
153         long start, end;\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
163     }\r
164     \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
177                     break;\r
178                 case CollectionUtilities.NOT_A_SUPERSET_B:\r
179                     checkContainment(a.size() == 0 && b.size() != 0, a, relation, b);\r
180                     break;\r
181                 case CollectionUtilities.NOT_A_DISJOINT_B:\r
182                     checkContainment(a.equals(b) && a.size() != 0, a, relation, b);\r
183                     break;\r
184                 case CollectionUtilities.NOT_A_SUBSET_B:\r
185                     checkContainment(a.size() != 0 && b.size() == 0, a, relation, b);\r
186                     break;\r
187                 case CollectionUtilities.A_PROPER_SUBSET_OF_B:\r
188                     checkContainment(b.containsAll(a) && !a.equals(b), a, relation, b);\r
189                     break;\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
192                     break;\r
193                 case CollectionUtilities.A_PROPER_SUPERSET_B:\r
194                     checkContainment(a.containsAll(b) && !a.equals(b), a, relation, b);\r
195                 break;\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
198                 break;\r
199                 }\r
200             }\r
201         }\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
208                 }\r
209             }\r
210             errln("Not all ContainmentRelations checked: " + missing);\r
211         }\r
212     }\r
213 \r
214     static final String[] RelationName = {"ALL_EMPTY",\r
215             "NOT_A_SUPERSET_B",\r
216             "NOT_A_DISJOINT_B",\r
217             "NOT_A_SUBSET_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
222 \r
223     /**\r
224      *  \r
225      */\r
226     private void checkContainment(boolean c, Collection a, int relation, Collection b) {\r
227         if (!c) {\r
228             errln("Fails relation: " + a + " \t" + RelationName[relation] + " \t" + b);\r
229         }\r
230     }\r
231 \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
241             }\r
242         }\r
243         checkMap(map2, localMap);\r
244         \r
245         logln("Comparing next");\r
246         mi.reset();\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
253             }\r
254             if (mi.codepoint >= limit) continue;\r
255             localMap.put(new Integer(mi.codepoint), mi.value);\r
256         }\r
257         checkMap(map2, localMap);\r
258     }\r
259     \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
271             }\r
272         }\r
273     }\r
274     \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
283     }\r
284     \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
295         }\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
301         }\r
302     };\r
303 \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
314         }\r
315     }\r
316     \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
321     \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
330         \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
338     }\r
339     \r
340     int propEnum = UProperty.GENERAL_CATEGORY;\r
341     \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
346         return result;\r
347     }\r
348     double _checkSetTime(int iterations, int type) {\r
349         map1 = new UnicodeMap();\r
350         map2 = new HashMap();\r
351         System.gc();\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
358             switch(type) {\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
362             }\r
363         }\r
364         double end = System.currentTimeMillis();\r
365         return (end-start)/1000/iterations;\r
366     }\r
367     \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
372         return result;\r
373     }\r
374     double _checkGetTime(int iterations, int type) {\r
375         System.gc();\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
379             switch (type) {\r
380             case 0: map1.getValue(cp); break;\r
381             case 1: map2.get(new Integer(cp)); break;\r
382             case 2:\r
383                 int enumValue = UCharacter.getIntPropertyValue(cp, propEnum);\r
384                 //if (enumValue <= 0) continue;\r
385                 UCharacter.getPropertyValueName(propEnum,enumValue, UProperty.NameChoice.LONG);\r
386                 break;                \r
387             case 3: map3.get(new Integer(cp)); break;\r
388             }\r
389         }\r
390         double end = System.currentTimeMillis();\r
391         return (end-start)/1000/iterations;\r
392     }\r
393     \r
394     static class UnicodeMapBoilerplate extends TestBoilerplate {\r
395 \r
396         /* \r
397          * @see com.ibm.icu.dev.test.TestBoilerplate#_hasSameBehavior(java.lang.Object, java.lang.Object)\r
398          */\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
401             return true;\r
402         }\r
403 \r
404         /*\r
405          * @see com.ibm.icu.dev.test.TestBoilerplate#_createTestObject()\r
406          */\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
414             }\r
415             list.add(result);\r
416             return true;\r
417         }\r
418     }\r
419     \r
420     static class StringBoilerplate extends TestBoilerplate {\r
421 \r
422         /* \r
423          * @see com.ibm.icu.dev.test.TestBoilerplate#_hasSameBehavior(java.lang.Object, java.lang.Object)\r
424          */\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
427             return true;\r
428         }\r
429 \r
430         /*\r
431          * @see com.ibm.icu.dev.test.TestBoilerplate#_createTestObject()\r
432          */\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
438             }\r
439             list.add(result.toString());\r
440             return true;\r
441         }\r
442     }\r
443     \r
444     static class UnicodeSetBoilerplate extends TestBoilerplate {\r
445 \r
446         /* \r
447          * @see com.ibm.icu.dev.test.TestBoilerplate#_hasSameBehavior(java.lang.Object, java.lang.Object)\r
448          */\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
451             return true;\r
452         }\r
453 \r
454         /*\r
455          * @see com.ibm.icu.dev.test.TestBoilerplate#_createTestObject()\r
456          */\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
462             }\r
463             list.add(result.toString());\r
464             return true;\r
465         }\r
466     }\r
467 \r
468 }\r
469 //#endif\r