2 *******************************************************************************
\r
3 * Copyright (C) 1996-2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.translit;
\r
9 import java.util.Comparator;
\r
10 import java.util.HashMap;
\r
11 import java.util.HashSet;
\r
12 import java.util.Random;
\r
13 import java.util.Set;
\r
14 import java.util.SortedMap;
\r
15 import java.util.TreeMap;
\r
16 import java.util.TreeSet;
\r
17 import java.util.Map.Entry;
\r
19 import com.ibm.icu.dev.test.TestFmwk;
\r
20 import com.ibm.icu.dev.test.util.UnicodeMap;
\r
21 import com.ibm.icu.impl.Utility;
\r
22 import com.ibm.icu.text.UTF16;
\r
23 import com.ibm.icu.text.UnicodeSet;
\r
27 * @summary General test of UnicodeSet
\r
29 public class UnicodeMapTest extends TestFmwk {
\r
31 static final int MODIFY_TEST_LIMIT = 32;
\r
32 static final int MODIFY_TEST_ITERATIONS = 100000;
\r
34 public static void main(String[] args) throws Exception {
\r
35 new UnicodeMapTest().run(args);
\r
38 public void TestAMonkey() {
\r
39 SortedMap<String,Integer> stayWithMe = new TreeMap<String,Integer>(OneFirstComparator);
\r
41 UnicodeMap<Integer> me = new UnicodeMap<Integer>().putAll(stayWithMe);
\r
42 // check one special case, removal near end
\r
43 me.putAll(0x10FFFE, 0x10FFFF, 666);
\r
44 me.remove(0x10FFFF);
\r
46 int iterations = 100000;
\r
47 SortedMap<String,Integer> test = new TreeMap();
\r
49 Random rand = new Random(0);
\r
52 // try modifications
\r
53 for (int i = 0; i < iterations ; ++i) {
\r
54 switch(rand.nextInt(20)) {
\r
61 fillRandomMap(rand, 5, test);
\r
62 logln("putAll\t" + test);
\r
63 stayWithMe.putAll(test);
\r
66 case 2: case 3: case 4: case 5: case 6: case 7: case 8:
\r
67 other = getRandomKey(rand);
\r
68 // if (other.equals("\uDBFF\uDFFF") && me.containsKey(0x10FFFF) && me.get(0x10FFFF).equals(me.get(0x10FFFE))) {
\r
69 // System.out.println("Remove\t" + other + "\n" + me);
\r
71 logln("remove\t" + other);
\r
72 stayWithMe.remove(other);
\r
75 } catch (IllegalArgumentException e) {
\r
76 errln("remove\t" + other + "\tfailed: " + e.getMessage() + "\n" + me);
\r
82 other = getRandomKey(rand);
\r
83 value = rand.nextInt(50)+50;
\r
84 logln("put\t" + other + " = " + value);
\r
85 stayWithMe.put(other, value);
\r
86 me.put(other,value);
\r
89 checkEquals(me, stayWithMe);
\r
99 private SortedMap<String, Integer> fillRandomMap(Random rand, int max, SortedMap<String, Integer> test) {
\r
101 max = rand.nextInt(max);
\r
102 for (int i = 0; i < max; ++i) {
\r
103 test.put(getRandomKey(rand), rand.nextInt(50)+50);
\r
108 Set temp = new HashSet();
\r
111 * @param stayWithMe
\r
113 private void checkEquals(UnicodeMap<Integer> me, SortedMap<String, Integer> stayWithMe) {
\r
115 for (Entry<String, Integer> e : me.entrySet()) {
\r
118 Set<Entry<String, Integer>> entrySet = stayWithMe.entrySet();
\r
119 if (!entrySet.equals(temp)) {
\r
120 logln(me.entrySet().toString());
\r
121 logln(me.toString());
\r
122 assertEquals("are in parallel", entrySet, temp);
\r
123 // we failed. Reset and start again
\r
128 for (String key : stayWithMe.keySet()) {
\r
129 assertEquals("containsKey", stayWithMe.containsKey(key), me.containsKey(key));
\r
130 Integer value = stayWithMe.get(key);
\r
131 assertEquals("get", value, me.get(key));
\r
132 assertEquals("containsValue", stayWithMe.containsValue(value), me.containsValue(value));
\r
133 int cp = UnicodeSet.getSingleCodePoint(key);
\r
134 if (cp != Integer.MAX_VALUE) {
\r
135 assertEquals("get", value, me.get(cp));
\r
138 Set<String> nonCodePointStrings = stayWithMe.tailMap("").keySet();
\r
139 if (nonCodePointStrings.size() == 0) nonCodePointStrings = null; // for parallel api
\r
140 assertEquals("getNonRangeStrings", nonCodePointStrings, me.getNonRangeStrings());
\r
142 TreeSet<Integer> values = new TreeSet<Integer>(stayWithMe.values());
\r
143 TreeSet<Integer> myValues = new TreeSet<Integer>(me.values());
\r
144 assertEquals("values", myValues, values);
\r
146 for (String key : stayWithMe.keySet()) {
\r
147 assertEquals("containsKey", stayWithMe.containsKey(key), me.containsKey(key));
\r
151 static Comparator<String> OneFirstComparator = new Comparator<String>() {
\r
152 public int compare(String o1, String o2) {
\r
153 int cp1 = UnicodeSet.getSingleCodePoint(o1);
\r
154 int cp2 = UnicodeSet.getSingleCodePoint(o2);
\r
155 int result = cp1 - cp2;
\r
159 if (cp1 == Integer.MAX_VALUE) {
\r
160 return o1.compareTo(o2);
\r
172 private String getRandomKey(Random rand) {
\r
173 int r = rand.nextInt(30);
\r
175 return UTF16.valueOf(r);
\r
176 } else if (r < 10) {
\r
177 return UTF16.valueOf('A'-1+r);
\r
178 } else if (r < 20) {
\r
179 return UTF16.valueOf(0x10FFFF - (r-10));
\r
180 // } else if (r == 20) {
\r
183 return "a" + UTF16.valueOf(r + 'a'-1);
\r
186 public void TestModify() {
\r
187 Random random = new Random(0);
\r
188 UnicodeMap unicodeMap = new UnicodeMap();
\r
189 HashMap hashMap = new HashMap();
\r
190 String[] values = {null, "the", "quick", "brown", "fox"};
\r
191 for (int count = 1; count <= MODIFY_TEST_ITERATIONS; ++count) {
\r
192 String value = values[random.nextInt(values.length)];
\r
193 int start = random.nextInt(MODIFY_TEST_LIMIT); // test limited range
\r
194 int end = random.nextInt(MODIFY_TEST_LIMIT);
\r
200 int modCount = count & 0xFF;
\r
201 if (modCount == 0 && isVerbose()) {
\r
202 logln("***"+count);
\r
203 logln(unicodeMap.toString());
\r
205 unicodeMap.putAll(start, end, value);
\r
206 if (modCount == 1 && isVerbose()) {
\r
207 logln(">>>\t" + Utility.hex(start) + ".." + Utility.hex(end) + "\t" + value);
\r
208 logln(unicodeMap.toString());
\r
210 for (int i = start; i <= end; ++i) {
\r
211 hashMap.put(new Integer(i), value);
\r
213 if (!hasSameValues(unicodeMap, hashMap)) {
\r
214 errln("Failed at " + count);
\r
219 private boolean hasSameValues(UnicodeMap unicodeMap, HashMap hashMap) {
\r
220 for (int i = 0; i < MODIFY_TEST_LIMIT; ++i) {
\r
221 Object unicodeMapValue = unicodeMap.getValue(i);
\r
222 Object hashMapValue = hashMap.get(new Integer(i));
\r
223 if (unicodeMapValue != hashMapValue) {
\r