2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.lucene.analysis;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.Locale;
26 import org.apache.lucene.util.LuceneTestCase;
28 public class TestCharArrayMap extends LuceneTestCase {
30 public void doRandom(int iter, boolean ignoreCase) {
31 CharArrayMap<Integer> map = new CharArrayMap<Integer>(TEST_VERSION_CURRENT, 1, ignoreCase);
32 HashMap<String,Integer> hmap = new HashMap<String,Integer>();
35 for (int i=0; i<iter; i++) {
36 int len = random.nextInt(5);
38 for (int j=0; j<key.length; j++) {
39 key[j] = (char)random.nextInt(127);
41 String keyStr = new String(key);
42 String hmapKey = ignoreCase ? keyStr.toLowerCase(Locale.ENGLISH) : keyStr;
44 int val = random.nextInt();
46 Object o1 = map.put(key, val);
47 Object o2 = hmap.put(hmapKey,val);
50 // add it again with the string method
51 assertEquals(val, map.put(keyStr,val).intValue());
53 assertEquals(val, map.get(key,0,key.length).intValue());
54 assertEquals(val, map.get(key).intValue());
55 assertEquals(val, map.get(keyStr).intValue());
57 assertEquals(hmap.size(), map.size());
61 public void testCharArrayMap() {
62 int num = 5 * RANDOM_MULTIPLIER;
63 for (int i = 0; i < num; i++) { // pump this up for more random testing
69 public void testMethods() {
70 CharArrayMap<Integer> cm = new CharArrayMap<Integer>(TEST_VERSION_CURRENT, 2, false);
71 HashMap<String,Integer> hm = new HashMap<String,Integer>();
75 assertEquals(hm.size(), cm.size());
78 assertEquals(hm.size(), cm.size());
80 CharArraySet cs = cm.keySet();
83 assertTrue(cm.containsKey(o));
84 char[] co = (char[]) o;
85 assertTrue(cm.containsKey(co, 0, co.length));
88 assertEquals(hm.size(), n);
89 assertEquals(hm.size(), cs.size());
90 assertEquals(cm.size(), cs.size());
92 assertEquals(0, cs.size());
93 assertEquals(0, cm.size());
96 fail("keySet() allows adding new keys");
97 } catch (UnsupportedOperationException ue) {
101 assertEquals(hm.size(), cs.size());
102 assertEquals(cm.size(), cs.size());
104 Iterator<Map.Entry<Object,Integer>> iter1 = cm.entrySet().iterator();
106 while (iter1.hasNext()) {
107 Map.Entry<Object,Integer> entry = iter1.next();
108 Object key = entry.getKey();
109 Integer val = entry.getValue();
110 assertEquals(cm.get(key), val);
111 entry.setValue(val*100);
112 assertEquals(val*100, (int)cm.get(key));
115 assertEquals(hm.size(), n);
118 assertEquals(cm.size(), n);
120 CharArrayMap<Integer>.EntryIterator iter2 = cm.entrySet().iterator();
122 while (iter2.hasNext()) {
123 char[] keyc = iter2.nextKey();
124 Integer val = iter2.currentValue();
125 assertEquals(hm.get(new String(keyc)), val);
126 iter2.setValue(val*100);
127 assertEquals(val*100, (int)cm.get(keyc));
130 assertEquals(hm.size(), n);
132 cm.entrySet().clear();
133 assertEquals(0, cm.size());
134 assertEquals(0, cm.entrySet().size());
135 assertTrue(cm.isEmpty());
138 public void testModifyOnUnmodifiable(){
139 CharArrayMap<Integer> map = new CharArrayMap<Integer>(TEST_VERSION_CURRENT, 2, false);
142 final int size = map.size();
143 assertEquals(2, size);
144 assertTrue(map.containsKey("foo"));
145 assertEquals(1, map.get("foo").intValue());
146 assertTrue(map.containsKey("bar"));
147 assertEquals(2, map.get("bar").intValue());
149 map = CharArrayMap.unmodifiableMap(map);
150 assertEquals("Map size changed due to unmodifiableMap call" , size, map.size());
151 String NOT_IN_MAP = "SirGallahad";
152 assertFalse("Test String already exists in map", map.containsKey(NOT_IN_MAP));
153 assertNull("Test String already exists in map", map.get(NOT_IN_MAP));
156 map.put(NOT_IN_MAP.toCharArray(), 3);
157 fail("Modified unmodifiable map");
158 }catch (UnsupportedOperationException e) {
160 assertFalse("Test String has been added to unmodifiable map", map.containsKey(NOT_IN_MAP));
161 assertNull("Test String has been added to unmodifiable map", map.get(NOT_IN_MAP));
162 assertEquals("Size of unmodifiable map has changed", size, map.size());
166 map.put(NOT_IN_MAP, 3);
167 fail("Modified unmodifiable map");
168 }catch (UnsupportedOperationException e) {
170 assertFalse("Test String has been added to unmodifiable map", map.containsKey(NOT_IN_MAP));
171 assertNull("Test String has been added to unmodifiable map", map.get(NOT_IN_MAP));
172 assertEquals("Size of unmodifiable map has changed", size, map.size());
176 map.put(new StringBuilder(NOT_IN_MAP), 3);
177 fail("Modified unmodifiable map");
178 }catch (UnsupportedOperationException e) {
180 assertFalse("Test String has been added to unmodifiable map", map.containsKey(NOT_IN_MAP));
181 assertNull("Test String has been added to unmodifiable map", map.get(NOT_IN_MAP));
182 assertEquals("Size of unmodifiable map has changed", size, map.size());
187 fail("Modified unmodifiable map");
188 }catch (UnsupportedOperationException e) {
190 assertEquals("Size of unmodifiable map has changed", size, map.size());
194 map.entrySet().clear();
195 fail("Modified unmodifiable map");
196 }catch (UnsupportedOperationException e) {
198 assertEquals("Size of unmodifiable map has changed", size, map.size());
202 map.keySet().clear();
203 fail("Modified unmodifiable map");
204 }catch (UnsupportedOperationException e) {
206 assertEquals("Size of unmodifiable map has changed", size, map.size());
210 map.put((Object) NOT_IN_MAP, 3);
211 fail("Modified unmodifiable map");
212 }catch (UnsupportedOperationException e) {
214 assertFalse("Test String has been added to unmodifiable map", map.containsKey(NOT_IN_MAP));
215 assertNull("Test String has been added to unmodifiable map", map.get(NOT_IN_MAP));
216 assertEquals("Size of unmodifiable map has changed", size, map.size());
220 map.putAll(Collections.singletonMap(NOT_IN_MAP, 3));
221 fail("Modified unmodifiable map");
222 }catch (UnsupportedOperationException e) {
224 assertFalse("Test String has been added to unmodifiable map", map.containsKey(NOT_IN_MAP));
225 assertNull("Test String has been added to unmodifiable map", map.get(NOT_IN_MAP));
226 assertEquals("Size of unmodifiable map has changed", size, map.size());
229 assertTrue(map.containsKey("foo"));
230 assertEquals(1, map.get("foo").intValue());
231 assertTrue(map.containsKey("bar"));
232 assertEquals(2, map.get("bar").intValue());
235 public void testToString() {
236 CharArrayMap<Integer> cm = new CharArrayMap<Integer>(TEST_VERSION_CURRENT, Collections.singletonMap("test",1), false);
237 assertEquals("[test]",cm.keySet().toString());
238 assertEquals("[1]",cm.values().toString());
239 assertEquals("[test=1]",cm.entrySet().toString());
240 assertEquals("{test=1}",cm.toString());
242 assertTrue(cm.keySet().toString().contains(", "));
243 assertTrue(cm.values().toString().contains(", "));
244 assertTrue(cm.entrySet().toString().contains(", "));
245 assertTrue(cm.toString().contains(", "));