1 package org.apache.lucene.util.collections;
3 import java.util.HashSet;
4 import java.util.Iterator;
5 import java.util.Random;
9 import org.apache.lucene.util.LuceneTestCase;
10 import org.apache.lucene.util.collections.ArrayHashMap;
13 * Licensed to the Apache Software Foundation (ASF) under one or more
14 * contributor license agreements. See the NOTICE file distributed with
15 * this work for additional information regarding copyright ownership.
16 * The ASF licenses this file to You under the Apache License, Version 2.0
17 * (the "License"); you may not use this file except in compliance with
18 * the License. You may obtain a copy of the License at
20 * http://www.apache.org/licenses/LICENSE-2.0
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS,
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
29 public class ArrayHashMapTest extends LuceneTestCase {
31 public static final int RANDOM_TEST_NUM_ITERATIONS = 100; // set to 100,000 for deeper test
35 ArrayHashMap<Integer,Integer> map = new ArrayHashMap<Integer,Integer>();
37 assertNull(map.get(0));
39 for (int i = 0; i < 100; ++i) {
41 assertFalse(map.containsValue(value));
43 assertTrue(map.containsValue(value));
44 assertNotNull(map.get(i));
47 assertEquals(100, map.size());
48 for (int i = 0; i < 100; ++i) {
49 assertTrue(map.containsKey(i));
50 assertEquals(100 + i, map.get(i).intValue());
54 for (int i = 10; i < 90; ++i) {
56 assertNull(map.get(i));
59 assertEquals(20, map.size());
60 for (int i = 0; i < 100; ++i) {
61 assertEquals(map.containsKey(i), !(i >= 10 && i < 90));
64 for (int i = 5; i < 85; ++i) {
65 map.put(i, Integer.valueOf(5 + i));
67 assertEquals(95, map.size());
68 for (int i = 0; i < 100; ++i) {
69 assertEquals(map.containsKey(i), !(i >= 85 && i < 90));
71 for (int i = 0; i < 5; ++i) {
72 assertEquals(map.get(i).intValue(), (100 + i));
74 for (int i = 5; i < 85; ++i) {
75 assertEquals(map.get(i).intValue(), (5 + i));
77 for (int i = 90; i < 100; ++i) {
78 assertEquals(map.get(i).intValue(), (100 + i));
84 ArrayHashMap<Integer,Integer> map = new ArrayHashMap<Integer,Integer>();
86 for (int i = 0; i < 100; ++i) {
87 map.put(i, Integer.valueOf(100 + i));
90 HashSet<Integer> set = new HashSet<Integer>();
92 for (Iterator<Integer> iterator = map.iterator(); iterator.hasNext();) {
93 set.add(iterator.next());
96 assertEquals(set.size(), map.size());
97 for (int i = 0; i < 100; ++i) {
98 assertTrue(set.contains(Integer.valueOf(100 + i)));
102 for (Iterator<Integer> iterator = map.iterator(); iterator.hasNext();) {
103 Integer integer = iterator.next();
104 if (integer % 2 == 1) {
110 assertEquals(set.size(), map.size());
111 for (int i = 0; i < 100; i += 2) {
112 assertTrue(set.contains(Integer.valueOf(100 + i)));
117 public void test2() {
118 ArrayHashMap<Integer,Integer> map = new ArrayHashMap<Integer,Integer>();
120 assertTrue(map.isEmpty());
121 assertNull(map.get(0));
122 for (int i = 0; i < 128; ++i) {
123 int value = i * 4096;
124 assertFalse(map.containsValue(value));
126 assertTrue(map.containsValue(value));
127 assertNotNull(map.get(i));
128 assertFalse(map.isEmpty());
131 assertEquals(128, map.size());
132 for (int i = 0; i < 128; ++i) {
133 assertTrue(map.containsKey(i));
134 assertEquals(i * 4096, map.get(i).intValue());
137 for (int i = 0; i < 200; i += 2) {
140 assertEquals(64, map.size());
141 for (int i = 1; i < 128; i += 2) {
142 assertTrue(map.containsKey(i));
143 assertEquals(i * 4096, map.get(i).intValue());
146 assertTrue(map.isEmpty());
150 public void test3() {
151 ArrayHashMap<Integer,Integer> map = new ArrayHashMap<Integer,Integer>();
153 for (int i = 0; i < length; ++i) {
154 map.put(i * 64, 100 + i);
156 HashSet<Integer> keySet = new HashSet<Integer>();
157 for (Iterator<Integer> iit = map.keyIterator(); iit.hasNext();) {
158 keySet.add(iit.next());
160 assertEquals(length, keySet.size());
161 for (int i = 0; i < length; ++i) {
162 assertTrue(keySet.contains(i * 64));
165 HashSet<Integer> valueSet = new HashSet<Integer>();
166 for (Iterator<Integer> iit = map.iterator(); iit.hasNext();) {
167 valueSet.add(iit.next());
169 assertEquals(length, valueSet.size());
170 Object[] array = map.toArray();
171 assertEquals(length, array.length);
172 for (Object value : array) {
173 assertTrue(valueSet.contains(value));
176 Integer[] array2 = new Integer[80];
177 array2 = map.toArray(array2);
178 for (int value : array2) {
179 assertTrue(valueSet.contains(value));
181 Integer[] array3 = new Integer[120];
182 array3 = map.toArray(array3);
183 for (int i = 0; i < length; ++i) {
184 assertTrue(valueSet.contains(array3[i]));
186 assertNull(array3[length]);
188 for (int i = 0; i < length; ++i) {
189 assertTrue(map.containsValue(i + 100));
190 assertTrue(map.containsKey(i * 64));
193 for (Iterator<Integer> iit = map.keyIterator(); iit.hasNext();) {
197 assertTrue(map.isEmpty());
198 assertEquals(0, map.size());
202 // now with random data.. and lots of it
204 public void test4() {
205 ArrayHashMap<Integer,Integer> map = new ArrayHashMap<Integer,Integer>();
206 int length = RANDOM_TEST_NUM_ITERATIONS;
208 // for a repeatable random sequence
209 long seed = random.nextLong();
210 Random random = new Random(seed);
212 for (int i = 0; i < length; ++i) {
213 int value = random.nextInt(Integer.MAX_VALUE);
214 map.put(i * 128, value);
217 assertEquals(length, map.size());
220 random.setSeed(seed);
222 for (int i = 0; i < length; ++i) {
223 int value = random.nextInt(Integer.MAX_VALUE);
224 assertTrue(map.containsValue(value));
225 assertTrue(map.containsKey(i * 128));
226 assertEquals(Integer.valueOf(value), map.remove(i * 128));
228 assertEquals(0, map.size());
229 assertTrue(map.isEmpty());
233 public void testEquals() {
234 ArrayHashMap<Integer,Float> map1 = new ArrayHashMap<Integer,Float>(100);
235 ArrayHashMap<Integer,Float> map2 = new ArrayHashMap<Integer,Float>(100);
236 assertEquals("Empty maps should be equal", map1, map2);
237 assertEquals("hashCode() for empty maps should be equal",
238 map1.hashCode(), map2.hashCode());
240 for (int i = 0; i < 100; ++i) {
241 map1.put(i, Float.valueOf(1f/i));
242 map2.put(i, Float.valueOf(1f/i));
244 assertEquals("Identical maps should be equal", map1, map2);
245 assertEquals("hashCode() for identical maps should be equal",
246 map1.hashCode(), map2.hashCode());
248 for (int i = 10; i < 20; i++) {
251 assertFalse("Different maps should not be equal", map1.equals(map2));
253 for (int i = 19; i >=10; --i) {
256 assertEquals("Identical maps should be equal", map1, map2);
257 assertEquals("hashCode() for identical maps should be equal",
258 map1.hashCode(), map2.hashCode());
262 assertFalse("Different maps should not be equal", map1.equals(map2));
265 assertEquals("Identical maps should be equal", map1, map2);
266 assertEquals("hashCode() for identical maps should be equal",
267 map1.hashCode(), map2.hashCode());