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.ObjectToIntMap;
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 ObjectToIntMapTest extends LuceneTestCase {
34 ObjectToIntMap<Integer> map = new ObjectToIntMap<Integer>();
36 assertIntegerMaxValue(map.get(0));
38 for (int i = 0; i < 100; ++i) {
40 assertFalse(map.containsValue(value));
42 assertTrue(map.containsValue(value));
43 assertNotIntegerMaxValue(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), 1E-5);
53 for (int i = 10; i < 90; ++i) {
55 assertIntegerMaxValue(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), (100 + i), 1E-5);
73 for (int i = 5; i < 85; ++i) {
74 assertEquals(map.get(i), (5 + i), 1E-5);
76 for (int i = 90; i < 100; ++i) {
77 assertEquals(map.get(i), (100 + i), 1E-5);
81 private static void assertIntegerMaxValue(int i) {
82 assertTrue(i == Integer.MAX_VALUE);
85 private static void assertNotIntegerMaxValue(int i) {
86 assertFalse(i == Integer.MAX_VALUE);
91 ObjectToIntMap<Integer> map = new ObjectToIntMap<Integer>();
93 for (int i = 0; i < 100; ++i) {
94 map.put(i, Integer.valueOf(100 + i));
97 HashSet<Integer> set = new HashSet<Integer>();
99 for (IntIterator iterator = map.iterator(); iterator.hasNext();) {
100 set.add(iterator.next());
103 assertEquals(set.size(), map.size());
104 for (int i = 0; i < 100; ++i) {
105 assertTrue(set.contains(Integer.valueOf(100+i)));
109 for (IntIterator iterator = map.iterator(); iterator.hasNext();) {
110 Integer value = iterator.next();
111 if (value % 2 == 1) {
117 assertEquals(set.size(), map.size());
118 for (int i = 0; i < 100; i+=2) {
119 assertTrue(set.contains(Integer.valueOf(100+i)));
124 public void test2() {
125 ObjectToIntMap<Integer> map = new ObjectToIntMap<Integer>();
127 assertTrue(map.isEmpty());
128 assertIntegerMaxValue(map.get(0));
129 for (int i = 0; i < 128; ++i) {
130 int value = i * 4096;
131 assertFalse(map.containsValue(value));
133 assertTrue(map.containsValue(value));
134 assertNotIntegerMaxValue(map.get(i));
135 assertFalse(map.isEmpty());
138 assertEquals(128, map.size());
139 for (int i = 0; i < 128; ++i) {
140 assertTrue(map.containsKey(i));
141 assertEquals(i * 4096, map.get(i), 1E-5);
144 for (int i = 0 ; i < 200; i+=2) {
147 assertEquals(64, map.size());
148 for (int i = 1; i < 128; i+=2) {
149 assertTrue(map.containsKey(i));
150 assertEquals(i * 4096, map.get(i), 1E-5);
153 assertTrue(map.isEmpty());
157 public void test3() {
158 ObjectToIntMap<Integer> map = new ObjectToIntMap<Integer>();
160 for (int i = 0; i < length; ++i) {
161 map.put(i*64, 100 + i);
163 HashSet<Integer> keySet = new HashSet<Integer>();
164 for (Iterator<Integer> iit = map.keyIterator(); iit.hasNext(); ) {
165 keySet.add(iit.next());
167 assertEquals(length, keySet.size());
168 for (int i = 0; i < length; ++i) {
169 assertTrue(keySet.contains(i * 64));
172 HashSet<Integer> valueSet = new HashSet<Integer>();
173 for (IntIterator iit = map.iterator(); iit.hasNext(); ) {
174 valueSet.add(iit.next());
176 assertEquals(length, valueSet.size());
177 int[] array = map.toArray();
178 assertEquals(length, array.length);
179 for (int value: array) {
180 assertTrue(valueSet.contains(value));
183 int[] array2 = new int[80];
184 array2 = map.toArray(array2);
185 assertEquals(80, array2.length);
186 for (int value: array2) {
187 assertTrue(valueSet.contains(value));
190 int[] array3 = new int[120];
191 array3 = map.toArray(array3);
192 for (int i = 0 ;i < length; ++i) {
193 assertTrue(valueSet.contains(array3[i]));
195 assertIntegerMaxValue(array3[length]);
197 for (int i = 0; i < length; ++i) {
198 assertTrue(map.containsValue(i + 100));
199 assertTrue(map.containsKey(i*64));
202 for (Iterator<Integer> iit = map.keyIterator(); iit.hasNext(); ) {
206 assertTrue(map.isEmpty());
207 assertEquals(0, map.size());
211 // now with random data.. and lots of it
213 public void test4() {
214 ObjectToIntMap<Integer> map = new ObjectToIntMap<Integer>();
215 int length = ArrayHashMapTest.RANDOM_TEST_NUM_ITERATIONS;
217 // for a repeatable random sequence
218 long seed = random.nextLong();
219 Random random = new Random(seed);
221 for (int i = 0; i < length; ++i) {
222 int value = random.nextInt(Integer.MAX_VALUE);
223 map.put(i*128, value);
226 assertEquals(length, map.size());
229 random.setSeed(seed);
231 for (int i = 0; i < length; ++i) {
232 int value = random.nextInt(Integer.MAX_VALUE);
233 assertTrue(map.containsValue(value));
234 assertTrue(map.containsKey(i*128));
235 assertEquals(value, map.remove(i*128));
237 assertEquals(0, map.size());
238 assertTrue(map.isEmpty());
242 public void testEquals() {
243 ObjectToIntMap<Float> map1 = new ObjectToIntMap<Float>(100);
244 ObjectToIntMap<Float> map2 = new ObjectToIntMap<Float>(100);
245 assertEquals("Empty maps should be equal", map1, map2);
246 assertEquals("hashCode() for empty maps should be equal",
247 map1.hashCode(), map2.hashCode());
249 for (int i = 0; i < 100; ++i) {
250 map1.put(i * 1.1f, 100 + i);
251 map2.put(i * 1.1f, 100 + i);
253 assertEquals("Identical maps should be equal", map1, map2);
254 assertEquals("hashCode() for identical maps should be equal",
255 map1.hashCode(), map2.hashCode());
257 for (int i = 10; i < 20; i++) {
258 map1.remove(i * 1.1f);
260 assertFalse("Different maps should not be equal", map1.equals(map2));
262 for (int i = 19; i >=10; --i) {
263 map2.remove(i * 1.1f);
265 assertEquals("Identical maps should be equal", map1, map2);
266 assertEquals("hashCode() for identical maps should be equal",
267 map1.hashCode(), map2.hashCode());
271 assertFalse("Different maps should not be equal", map1.equals(map2));
274 assertEquals("Identical maps should be equal", map1, map2);
275 assertEquals("hashCode() for identical maps should be equal",
276 map1.hashCode(), map2.hashCode());