2 *******************************************************************************
3 * Copyright (C) 1996-2012, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.test.translit;
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;
15 import java.util.SortedMap;
16 import java.util.TreeMap;
17 import java.util.TreeSet;
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;
27 * @summary General test of UnicodeSet
29 public class UnicodeMapTest extends TestFmwk {
31 static final int MODIFY_TEST_LIMIT = 32;
32 static final int MODIFY_TEST_ITERATIONS = 100000;
34 public static void main(String[] args) throws Exception {
35 new UnicodeMapTest().run(args);
38 public void TestAMonkey() {
39 SortedMap<String,Integer> stayWithMe = new TreeMap<String,Integer>(OneFirstComparator);
41 UnicodeMap<Integer> me = new UnicodeMap<Integer>().putAll(stayWithMe);
42 // check one special case, removal near end
43 me.putAll(0x10FFFE, 0x10FFFF, 666);
46 int iterations = 100000;
47 SortedMap<String,Integer> test = new TreeMap();
49 Random rand = new Random(0);
53 for (int i = 0; i < iterations ; ++i) {
54 switch(rand.nextInt(20)) {
61 fillRandomMap(rand, 5, test);
62 logln("putAll\t" + test);
63 stayWithMe.putAll(test);
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);
71 logln("remove\t" + other);
72 stayWithMe.remove(other);
75 } catch (IllegalArgumentException e) {
76 errln("remove\t" + other + "\tfailed: " + e.getMessage() + "\n" + me);
82 other = getRandomKey(rand);
83 value = rand.nextInt(50)+50;
84 logln("put\t" + other + " = " + value);
85 stayWithMe.put(other, value);
89 checkEquals(me, stayWithMe);
99 private SortedMap<String, Integer> fillRandomMap(Random rand, int max, SortedMap<String, Integer> test) {
101 max = rand.nextInt(max);
102 for (int i = 0; i < max; ++i) {
103 test.put(getRandomKey(rand), rand.nextInt(50)+50);
108 Set temp = new HashSet();
113 private void checkEquals(UnicodeMap<Integer> me, SortedMap<String, Integer> stayWithMe) {
115 for (Entry<String, Integer> e : me.entrySet()) {
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
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));
138 Set<String> nonCodePointStrings = stayWithMe.tailMap("").keySet();
139 if (nonCodePointStrings.size() == 0) nonCodePointStrings = null; // for parallel api
140 assertEquals("getNonRangeStrings", nonCodePointStrings, me.getNonRangeStrings());
142 TreeSet<Integer> values = new TreeSet<Integer>(stayWithMe.values());
143 TreeSet<Integer> myValues = new TreeSet<Integer>(me.values());
144 assertEquals("values", myValues, values);
146 for (String key : stayWithMe.keySet()) {
147 assertEquals("containsKey", stayWithMe.containsKey(key), me.containsKey(key));
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;
159 if (cp1 == Integer.MAX_VALUE) {
160 return o1.compareTo(o2);
172 private String getRandomKey(Random rand) {
173 int r = rand.nextInt(30);
175 return UTF16.valueOf(r);
177 return UTF16.valueOf('A'-1+r);
179 return UTF16.valueOf(0x10FFFF - (r-10));
180 // } else if (r == 20) {
183 return "a" + UTF16.valueOf(r + 'a'-1);
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);
200 int modCount = count & 0xFF;
201 if (modCount == 0 && isVerbose()) {
203 logln(unicodeMap.toString());
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());
210 for (int i = start; i <= end; ++i) {
211 hashMap.put(new Integer(i), value);
213 if (!hasSameValues(unicodeMap, hashMap)) {
214 errln("Failed at " + count);
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) {