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.IntIterator;
11 import org.apache.lucene.util.collections.IntToObjectMap;
14 * Licensed to the Apache Software Foundation (ASF) under one or more
15 * contributor license agreements. See the NOTICE file distributed with
16 * this work for additional information regarding copyright ownership.
17 * The ASF licenses this file to You under the Apache License, Version 2.0
18 * (the "License"); you may not use this file except in compliance with
19 * the License. You may obtain a copy of the License at
21 * http://www.apache.org/licenses/LICENSE-2.0
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS,
25 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
30 public class IntToObjectMapTest extends LuceneTestCase {
34 IntToObjectMap<Integer> map = new IntToObjectMap<Integer>();
36 assertNull(map.get(0));
38 for (int i = 0; i < 100; ++i) {
40 assertFalse(map.containsValue(value));
42 assertTrue(map.containsValue(value));
43 assertNotNull(map.get(i));
46 assertEquals(100, map.size());
47 for (int i = 0; i < 100; ++i) {
48 assertTrue(map.containsKey(i));
49 assertEquals(100 + i, map.get(i).intValue());
53 for (int i = 10; i < 90; ++i) {
55 assertNull(map.get(i));
58 assertEquals(20, map.size());
59 for (int i = 0; i < 100; ++i) {
60 assertEquals(map.containsKey(i), !(i >= 10 && i < 90));
63 for (int i = 5; i < 85; ++i) {
64 map.put(i, Integer.valueOf(5 + i));
66 assertEquals(95, map.size());
67 for (int i = 0; i < 100; ++i) {
68 assertEquals(map.containsKey(i), !(i >= 85 && i < 90));
70 for (int i = 0; i < 5; ++i) {
71 assertEquals(map.get(i).intValue(), (100 + i));
73 for (int i = 5; i < 85; ++i) {
74 assertEquals(map.get(i).intValue(), (5 + i));
76 for (int i = 90; i < 100; ++i) {
77 assertEquals(map.get(i).intValue(), (100 + i));
83 IntToObjectMap<Integer> map = new IntToObjectMap<Integer>();
85 for (int i = 0; i < 100; ++i) {
86 map.put(i, Integer.valueOf(100 + i));
89 HashSet<Integer> set = new HashSet<Integer>();
91 for (Iterator<Integer> iterator = map.iterator(); iterator.hasNext();) {
92 set.add(iterator.next());
95 assertEquals(set.size(), map.size());
96 for (int i = 0; i < 100; ++i) {
97 assertTrue(set.contains(Integer.valueOf(100 + i)));
101 for (Iterator<Integer> iterator = map.iterator(); iterator.hasNext();) {
102 Integer integer = iterator.next();
103 if (integer % 2 == 1) {
109 assertEquals(set.size(), map.size());
110 for (int i = 0; i < 100; i += 2) {
111 assertTrue(set.contains(Integer.valueOf(100 + i)));
116 public void test2() {
117 IntToObjectMap<Integer> map = new IntToObjectMap<Integer>();
119 assertTrue(map.isEmpty());
120 assertNull(map.get(0));
121 for (int i = 0; i < 128; ++i) {
122 int value = i * 4096;
123 assertFalse(map.containsValue(value));
125 assertTrue(map.containsValue(value));
126 assertNotNull(map.get(i));
127 assertFalse(map.isEmpty());
130 assertEquals(128, map.size());
131 for (int i = 0; i < 128; ++i) {
132 assertTrue(map.containsKey(i));
133 assertEquals(i * 4096, map.get(i).intValue());
136 for (int i = 0; i < 200; i += 2) {
139 assertEquals(64, map.size());
140 for (int i = 1; i < 128; i += 2) {
141 assertTrue(map.containsKey(i));
142 assertEquals(i * 4096, map.get(i).intValue());
145 assertTrue(map.isEmpty());
149 public void test3() {
150 IntToObjectMap<Integer> map = new IntToObjectMap<Integer>();
152 for (int i = 0; i < length; ++i) {
153 map.put(i * 64, 100 + i);
155 HashSet<Integer> keySet = new HashSet<Integer>();
156 for (IntIterator iit = map.keyIterator(); iit.hasNext();) {
157 keySet.add(iit.next());
159 assertEquals(length, keySet.size());
160 for (int i = 0; i < length; ++i) {
161 assertTrue(keySet.contains(i * 64));
164 HashSet<Integer> valueSet = new HashSet<Integer>();
165 for (Iterator<Integer> iit = map.iterator(); iit.hasNext();) {
166 valueSet.add(iit.next());
168 assertEquals(length, valueSet.size());
169 Object[] array = map.toArray();
170 assertEquals(length, array.length);
171 for (Object value : array) {
172 assertTrue(valueSet.contains(value));
175 Integer[] array2 = new Integer[80];
176 array2 = map.toArray(array2);
177 for (int value : array2) {
178 assertTrue(valueSet.contains(value));
180 Integer[] array3 = new Integer[120];
181 array3 = map.toArray(array3);
182 for (int i = 0; i < length; ++i) {
183 assertTrue(valueSet.contains(array3[i]));
185 assertNull(array3[length]);
187 for (int i = 0; i < length; ++i) {
188 assertTrue(map.containsValue(i + 100));
189 assertTrue(map.containsKey(i * 64));
192 for (IntIterator iit = map.keyIterator(); iit.hasNext();) {
196 assertTrue(map.isEmpty());
197 assertEquals(0, map.size());
201 // now with random data.. and lots of it
203 public void test4() {
204 IntToObjectMap<Integer> map = new IntToObjectMap<Integer>();
205 int length = ArrayHashMapTest.RANDOM_TEST_NUM_ITERATIONS;
207 // for a repeatable random sequence
208 long seed = random.nextLong();
209 Random random = new Random(seed);
211 for (int i = 0; i < length; ++i) {
212 int value = random.nextInt(Integer.MAX_VALUE);
213 map.put(i * 128, value);
216 assertEquals(length, map.size());
219 random.setSeed(seed);
221 for (int i = 0; i < length; ++i) {
222 int value = random.nextInt(Integer.MAX_VALUE);
223 assertTrue(map.containsValue(value));
224 assertTrue(map.containsKey(i * 128));
225 assertEquals(Integer.valueOf(value), map.remove(i * 128));
227 assertEquals(0, map.size());
228 assertTrue(map.isEmpty());
232 public void testEquals() {
233 IntToObjectMap<Double> map1 = new IntToObjectMap<Double>(100);
234 IntToObjectMap<Double> map2 = new IntToObjectMap<Double>(100);
235 assertEquals("Empty maps should be equal", map1, map2);
236 assertEquals("hashCode() for empty maps should be equal",
237 map1.hashCode(), map2.hashCode());
239 for (int i = 0; i < 100; ++i) {
240 map1.put(i, Double.valueOf(1f/i));
241 map2.put(i, Double.valueOf(1f/i));
243 assertEquals("Identical maps should be equal", map1, map2);
244 assertEquals("hashCode() for identical maps should be equal",
245 map1.hashCode(), map2.hashCode());
247 for (int i = 10; i < 20; i++) {
250 assertFalse("Different maps should not be equal", map1.equals(map2));
252 for (int i = 19; i >=10; --i) {
255 assertEquals("Identical maps should be equal", map1, map2);
256 assertEquals("hashCode() for identical maps should be equal",
257 map1.hashCode(), map2.hashCode());
261 assertFalse("Different maps should not be equal", map1.equals(map2));
264 assertEquals("Identical maps should be equal", map1, map2);
265 assertEquals("hashCode() for identical maps should be equal",
266 map1.hashCode(), map2.hashCode());