1 package org.apache.lucene.util.collections;
4 import java.util.HashSet;
5 import java.util.Random;
7 import org.apache.lucene.util.LuceneTestCase;
8 import org.apache.lucene.util.collections.IntIterator;
9 import org.apache.lucene.util.collections.IntToIntMap;
12 * Licensed to the Apache Software Foundation (ASF) under one or more
13 * contributor license agreements. See the NOTICE file distributed with
14 * this work for additional information regarding copyright ownership.
15 * The ASF licenses this file to You under the Apache License, Version 2.0
16 * (the "License"); you may not use this file except in compliance with
17 * the License. You may obtain a copy of the License at
19 * http://www.apache.org/licenses/LICENSE-2.0
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
28 public class IntToIntMapTest extends LuceneTestCase {
30 private static void assertGround(int value) {
31 assertEquals(IntToIntMap.GROUD, value);
36 IntToIntMap map = new IntToIntMap();
38 assertGround(map.get(0));
40 for (int i = 0; i < 100; ++i) {
42 assertFalse(map.containsValue(value));
44 assertTrue(map.containsValue(value));
45 assertNotNull(map.get(i));
48 assertEquals(100, map.size());
49 for (int i = 0; i < 100; ++i) {
50 assertTrue(map.containsKey(i));
51 assertEquals(100 + i, map.get(i));
55 for (int i = 10; i < 90; ++i) {
57 assertGround(map.get(i));
60 assertEquals(20, map.size());
61 for (int i = 0; i < 100; ++i) {
62 assertEquals(map.containsKey(i), !(i >= 10 && i < 90));
65 for (int i = 5; i < 85; ++i) {
66 map.put(i, Integer.valueOf(5 + i));
68 assertEquals(95, map.size());
69 for (int i = 0; i < 100; ++i) {
70 assertEquals(map.containsKey(i), !(i >= 85 && i < 90));
72 for (int i = 0; i < 5; ++i) {
73 assertEquals(map.get(i), (100 + i));
75 for (int i = 5; i < 85; ++i) {
76 assertEquals(map.get(i), (5 + i));
78 for (int i = 90; i < 100; ++i) {
79 assertEquals(map.get(i), (100 + i));
85 IntToIntMap map = new IntToIntMap();
87 for (int i = 0; i < 100; ++i) {
88 map.put(i, Integer.valueOf(100 + i));
91 HashSet<Integer> set = new HashSet<Integer>();
93 for (IntIterator iterator = map.iterator(); iterator.hasNext();) {
94 set.add(iterator.next());
97 assertEquals(set.size(), map.size());
98 for (int i = 0; i < 100; ++i) {
99 assertTrue(set.contains(Integer.valueOf(100+i)));
103 for (IntIterator iterator = map.iterator(); iterator.hasNext();) {
104 Integer integer = iterator.next();
105 if (integer % 2 == 1) {
111 assertEquals(set.size(), map.size());
112 for (int i = 0; i < 100; i+=2) {
113 assertTrue(set.contains(Integer.valueOf(100+i)));
118 public void test2() {
119 IntToIntMap map = new IntToIntMap();
121 assertTrue(map.isEmpty());
122 assertGround(map.get(0));
123 for (int i = 0; i < 128; ++i) {
124 int value = i * 4096;
125 assertFalse(map.containsValue(value));
127 assertTrue(map.containsValue(value));
128 assertNotNull(map.get(i));
129 assertFalse(map.isEmpty());
132 assertEquals(128, map.size());
133 for (int i = 0; i < 128; ++i) {
134 assertTrue(map.containsKey(i));
135 assertEquals(i * 4096, map.get(i));
138 for (int i = 0 ; i < 200; i+=2) {
141 assertEquals(64, map.size());
142 for (int i = 1; i < 128; i+=2) {
143 assertTrue(map.containsKey(i));
144 assertEquals(i * 4096, map.get(i));
147 assertTrue(map.isEmpty());
151 public void test3() {
152 IntToIntMap map = new IntToIntMap();
154 for (int i = 0; i < length; ++i) {
155 map.put(i*64, 100 + i);
157 HashSet<Integer> keySet = new HashSet<Integer>();
158 for (IntIterator iit = map.keyIterator(); iit.hasNext(); ) {
159 keySet.add(iit.next());
161 assertEquals(length, keySet.size());
162 for (int i = 0; i < length; ++i) {
163 assertTrue(keySet.contains(i * 64));
166 HashSet<Integer> valueSet = new HashSet<Integer>();
167 for (IntIterator iit = map.iterator(); iit.hasNext(); ) {
168 valueSet.add(iit.next());
170 assertEquals(length, valueSet.size());
171 int[] array = map.toArray();
172 assertEquals(length, array.length);
173 for (int value: array) {
174 assertTrue(valueSet.contains(value));
177 int[] array2 = new int[80];
178 array2 = map.toArray(array2);
179 assertEquals(length, array2.length);
180 for (int value: array2) {
181 assertTrue(valueSet.contains(value));
184 int[] array3 = new int[120];
185 array3 = map.toArray(array3);
186 for (int i = 0 ;i < length; ++i) {
187 assertTrue(valueSet.contains(array3[i]));
190 for (int i = 0; i < length; ++i) {
191 assertTrue(map.containsValue(i + 100));
192 assertTrue(map.containsKey(i*64));
195 for (IntIterator iit = map.keyIterator(); iit.hasNext(); ) {
199 assertTrue(map.isEmpty());
200 assertEquals(0, map.size());
204 // now with random data.. and lots of it
206 public void test4() {
207 IntToIntMap map = new IntToIntMap();
208 int length = ArrayHashMapTest.RANDOM_TEST_NUM_ITERATIONS;
210 // for a repeatable random sequence
211 long seed = random.nextLong();
212 Random random = new Random(seed);
214 for (int i = 0; i < length; ++i) {
215 int value = random.nextInt(Integer.MAX_VALUE);
216 map.put(i*128, value);
219 assertEquals(length, map.size());
222 random.setSeed(seed);
224 for (int i = 0; i < length; ++i) {
225 int value = random.nextInt(Integer.MAX_VALUE);
227 assertTrue(map.containsValue(value));
228 assertTrue(map.containsKey(i*128));
229 assertEquals(value, map.remove(i*128));
231 assertEquals(0, map.size());
232 assertTrue(map.isEmpty());
236 public void testEquals() {
237 IntToIntMap map1 = new IntToIntMap(100);
238 IntToIntMap map2 = new IntToIntMap(100);
239 assertEquals("Empty maps should be equal", map1, map2);
240 assertEquals("hashCode() for empty maps should be equal",
241 map1.hashCode(), map2.hashCode());
243 for (int i = 0; i < 100; ++i) {
244 map1.put(i, 100 + i);
245 map2.put(i, 100 + i);
247 assertEquals("Identical maps should be equal", map1, map2);
248 assertEquals("hashCode() for identical maps should be equal",
249 map1.hashCode(), map2.hashCode());
251 for (int i = 10; i < 20; i++) {
254 assertFalse("Different maps should not be equal", map1.equals(map2));
256 for (int i = 19; i >=10; --i) {
259 assertEquals("Identical maps should be equal", map1, map2);
260 assertEquals("hashCode() for identical maps should be equal",
261 map1.hashCode(), map2.hashCode());
265 assertFalse("Different maps should not be equal", map1.equals(map2));
268 assertEquals("Identical maps should be equal", map1, map2);
269 assertEquals("hashCode() for identical maps should be equal",
270 map1.hashCode(), map2.hashCode());