]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/translit/src/com/ibm/icu/dev/test/translit/UnicodeMapTest.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / translit / src / com / ibm / icu / dev / test / translit / UnicodeMapTest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2012, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.test.translit;
8
9 import java.util.Comparator;
10 import java.util.HashMap;
11 import java.util.HashSet;
12 import java.util.Map.Entry;
13 import java.util.Random;
14 import java.util.Set;
15 import java.util.SortedMap;
16 import java.util.TreeMap;
17 import java.util.TreeSet;
18
19 import com.ibm.icu.dev.test.TestFmwk;
20 import com.ibm.icu.dev.util.UnicodeMap;
21 import com.ibm.icu.impl.Utility;
22 import com.ibm.icu.text.UTF16;
23 import com.ibm.icu.text.UnicodeSet;
24
25 /**
26  * @test
27  * @summary General test of UnicodeSet
28  */
29 public class UnicodeMapTest extends TestFmwk {
30
31     static final int MODIFY_TEST_LIMIT = 32;
32     static final int MODIFY_TEST_ITERATIONS = 100000;
33
34     public static void main(String[] args) throws Exception {
35         new UnicodeMapTest().run(args);
36     }
37
38     public void TestAMonkey() {
39         SortedMap<String,Integer> stayWithMe = new TreeMap<String,Integer>(OneFirstComparator);
40
41         UnicodeMap<Integer> me = new UnicodeMap<Integer>().putAll(stayWithMe);
42         // check one special case, removal near end
43         me.putAll(0x10FFFE, 0x10FFFF, 666);
44         me.remove(0x10FFFF);
45         
46         int iterations = 100000;
47         SortedMap<String,Integer> test = new TreeMap();
48
49         Random rand = new Random(0);
50         String other;
51         Integer value;
52         // try modifications
53         for (int i = 0; i < iterations ; ++i) {
54             switch(rand.nextInt(20)) {
55             case 0:
56                 logln("clear");
57                 stayWithMe.clear();
58                 me.clear();
59                 break;
60             case 1:
61                 fillRandomMap(rand, 5, test);
62                 logln("putAll\t" + test);
63                 stayWithMe.putAll(test);
64                 me.putAll(test);
65                 break;
66             case 2: case 3: case 4: case 5: case 6: case 7: case 8:
67                 other = getRandomKey(rand);
68 //                if (other.equals("\uDBFF\uDFFF") && me.containsKey(0x10FFFF) && me.get(0x10FFFF).equals(me.get(0x10FFFE))) {
69 //                    System.out.println("Remove\t" + other + "\n" + me);
70 //                }
71                 logln("remove\t" + other);
72                 stayWithMe.remove(other);
73                 try {
74                     me.remove(other);
75                 } catch (IllegalArgumentException e) {
76                     errln("remove\t" + other + "\tfailed: " + e.getMessage() + "\n" + me);
77                     me.clear();
78                     stayWithMe.clear();
79                 }
80                 break;
81             default:
82                 other = getRandomKey(rand);
83                 value = rand.nextInt(50)+50;
84                 logln("put\t" + other + " = " + value);
85                 stayWithMe.put(other, value);
86                 me.put(other,value);
87                 break;
88             }
89             checkEquals(me, stayWithMe);
90         }
91     }
92
93     /**
94      * @param rand 
95      * @param nextInt
96      * @param test 
97      * @return
98      */
99     private SortedMap<String, Integer> fillRandomMap(Random rand, int max, SortedMap<String, Integer> test) {
100         test.clear();
101         max = rand.nextInt(max);
102         for (int i = 0; i < max; ++i) {
103             test.put(getRandomKey(rand), rand.nextInt(50)+50);
104         }
105         return test;
106     }
107
108     Set temp = new HashSet();
109     /**
110      * @param me
111      * @param stayWithMe
112      */
113     private void checkEquals(UnicodeMap<Integer> me, SortedMap<String, Integer> stayWithMe) {
114         temp.clear();
115         for (Entry<String, Integer> e : me.entrySet()) {
116             temp.add(e);
117         }
118         Set<Entry<String, Integer>> entrySet = stayWithMe.entrySet();
119         if (!entrySet.equals(temp)) {
120             logln(me.entrySet().toString());
121             logln(me.toString());
122             assertEquals("are in parallel", entrySet, temp);
123             // we failed. Reset and start again
124             entrySet.clear();
125             temp.clear();
126             return;
127         }
128         for (String key : stayWithMe.keySet()) {
129             assertEquals("containsKey", stayWithMe.containsKey(key), me.containsKey(key));
130             Integer value = stayWithMe.get(key);
131             assertEquals("get", value, me.get(key));
132             assertEquals("containsValue", stayWithMe.containsValue(value), me.containsValue(value));
133             int cp = UnicodeSet.getSingleCodePoint(key);
134             if (cp != Integer.MAX_VALUE) {
135                 assertEquals("get", value, me.get(cp));
136             }
137         }
138         Set<String> nonCodePointStrings = stayWithMe.tailMap("").keySet();
139         if (nonCodePointStrings.size() == 0) nonCodePointStrings = null; // for parallel api
140         assertEquals("getNonRangeStrings", nonCodePointStrings, me.getNonRangeStrings());
141         
142         TreeSet<Integer> values = new TreeSet<Integer>(stayWithMe.values());
143         TreeSet<Integer> myValues = new TreeSet<Integer>(me.values());
144         assertEquals("values", myValues, values);
145
146         for (String key : stayWithMe.keySet()) {
147             assertEquals("containsKey", stayWithMe.containsKey(key), me.containsKey(key));
148         }
149     }
150     
151     static Comparator<String> OneFirstComparator = new Comparator<String>() {
152         public int compare(String o1, String o2) {
153             int cp1 = UnicodeSet.getSingleCodePoint(o1);
154             int cp2 = UnicodeSet.getSingleCodePoint(o2);
155             int result = cp1 - cp2;
156             if (result != 0) {
157                 return result;
158             }
159             if (cp1 == Integer.MAX_VALUE) {
160                 return o1.compareTo(o2);
161             }
162             return 0;
163         }
164         
165     };
166
167     /**
168      * @param rand 
169      * @param others
170      * @return
171      */
172     private String getRandomKey(Random rand) {
173         int r = rand.nextInt(30);
174         if (r == 0) {
175             return UTF16.valueOf(r); 
176         } else if (r < 10) {
177             return UTF16.valueOf('A'-1+r);
178         } else if (r < 20) {
179             return UTF16.valueOf(0x10FFFF - (r-10));
180 //        } else if (r == 20) {
181 //            return "";
182         }
183         return "a" + UTF16.valueOf(r + 'a'-1);
184     }
185
186     public void TestModify() {
187         Random random = new Random(0);
188         UnicodeMap unicodeMap = new UnicodeMap();
189         HashMap hashMap = new HashMap();
190         String[] values = {null, "the", "quick", "brown", "fox"};
191         for (int count = 1; count <= MODIFY_TEST_ITERATIONS; ++count) {
192             String value = values[random.nextInt(values.length)];
193             int start = random.nextInt(MODIFY_TEST_LIMIT); // test limited range
194             int end = random.nextInt(MODIFY_TEST_LIMIT);
195             if (start > end) {
196                 int temp = start;
197                 start = end;
198                 end = temp;
199             }
200             int modCount = count & 0xFF;
201             if (modCount == 0 && isVerbose()) {
202                 logln("***"+count);
203                 logln(unicodeMap.toString());
204             }
205             unicodeMap.putAll(start, end, value);
206             if (modCount == 1 && isVerbose()) {
207                 logln(">>>\t" + Utility.hex(start) + ".." + Utility.hex(end) + "\t" + value);
208                 logln(unicodeMap.toString());
209             }
210             for (int i = start; i <= end; ++i) {
211                 hashMap.put(new Integer(i), value);
212             }
213             if (!hasSameValues(unicodeMap, hashMap)) {
214                 errln("Failed at " + count);
215             }
216         }
217     }
218
219     private boolean hasSameValues(UnicodeMap unicodeMap, HashMap hashMap) {
220         for (int i = 0; i < MODIFY_TEST_LIMIT; ++i) {
221             Object unicodeMapValue = unicodeMap.getValue(i);
222             Object hashMapValue = hashMap.get(new Integer(i));
223             if (unicodeMapValue != hashMapValue) {
224                 return false;
225             }
226         }
227         return true;
228     }
229 }