1 package org.apache.lucene.util.collections;
4 import java.util.HashSet;
5 import java.util.Iterator;
6 import java.util.Random;
8 import org.apache.lucene.util.LuceneTestCase;
9 import org.apache.lucene.util.collections.FloatIterator;
10 import org.apache.lucene.util.collections.FloatToObjectMap;
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 FloatToObjectMapTest extends LuceneTestCase {
33 FloatToObjectMap<Integer> map = new FloatToObjectMap<Integer>();
35 assertNull(map.get(0));
37 for (int i = 0; i < 100; ++i) {
39 assertFalse(map.containsValue(value));
40 map.put(i * 1.1f, value);
41 assertTrue(map.containsValue(value));
42 assertNotNull(map.get(i * 1.1f));
45 assertEquals(100, map.size());
46 for (int i = 0; i < 100; ++i) {
47 assertTrue(map.containsKey(i*1.1f));
48 assertEquals(100 + i, map.get(i*1.1f).intValue());
52 for (int i = 10; i < 90; ++i) {
54 assertNull(map.get(i*1.1f));
57 assertEquals(20, map.size());
58 for (int i = 0; i < 100; ++i) {
59 assertEquals(map.containsKey(i*1.1f), !(i >= 10 && i < 90));
62 for (int i = 5; i < 85; ++i) {
63 map.put(i*1.1f, Integer.valueOf(5 + i));
65 assertEquals(95, map.size());
66 for (int i = 0; i < 100; ++i) {
67 assertEquals(map.containsKey(i*1.1f), !(i >= 85 && i < 90));
69 for (int i = 0; i < 5; ++i) {
70 assertEquals(map.get(i*1.1f).intValue(), (100 + i));
72 for (int i = 5; i < 85; ++i) {
73 assertEquals(map.get(i*1.1f).intValue(), (5 + i));
75 for (int i = 90; i < 100; ++i) {
76 assertEquals(map.get(i*1.1f).intValue(), (100 + i));
82 FloatToObjectMap<Integer> map = new FloatToObjectMap<Integer>();
84 for (int i = 0; i < 100; ++i) {
85 map.put(i*1.1f, Integer.valueOf(100 + i));
88 HashSet<Integer> set = new HashSet<Integer>();
90 for (Iterator<Integer> iterator = map.iterator(); iterator.hasNext();) {
91 set.add(iterator.next());
94 assertEquals(set.size(), map.size());
95 for (int i = 0; i < 100; ++i) {
96 assertTrue(set.contains(Integer.valueOf(100 + i)));
100 for (Iterator<Integer> iterator = map.iterator(); iterator.hasNext();) {
101 Integer integer = iterator.next();
102 if (integer % 2 == 1) {
108 assertEquals(set.size(), map.size());
109 for (int i = 0; i < 100; i += 2) {
110 assertTrue(set.contains(Integer.valueOf(100 + i)));
115 public void test2() {
116 FloatToObjectMap<Integer> map = new FloatToObjectMap<Integer>();
118 assertTrue(map.isEmpty());
119 assertNull(map.get(0));
120 for (int i = 0; i < 128; ++i) {
121 int value = i * 4096;
122 assertFalse(map.containsValue(value));
123 map.put(i*1.1f, value);
124 assertTrue(map.containsValue(value));
125 assertNotNull(map.get(i*1.1f));
126 assertFalse(map.isEmpty());
129 assertEquals(128, map.size());
130 for (int i = 0; i < 128; ++i) {
131 assertTrue(map.containsKey(i*1.1f));
132 assertEquals(i * 4096, map.get(i*1.1f).intValue());
135 for (int i = 0; i < 200; i += 2) {
138 assertEquals(64, map.size());
139 for (int i = 1; i < 128; i += 2) {
140 assertTrue(map.containsKey(i*1.1f));
141 assertEquals(i * 4096, map.get(i*1.1f).intValue());
144 assertTrue(map.isEmpty());
148 public void test3() {
149 FloatToObjectMap<Integer> map = new FloatToObjectMap<Integer>();
151 for (int i = 0; i < length; ++i) {
152 map.put(i * 64*1.1f, 100 + i);
154 HashSet<Float> keySet = new HashSet<Float>();
155 for (FloatIterator iit = map.keyIterator(); iit.hasNext();) {
156 keySet.add(iit.next());
158 assertEquals(length, keySet.size());
159 for (int i = 0; i < length; ++i) {
160 assertTrue(keySet.contains(i * 64*1.1f));
163 HashSet<Integer> valueSet = new HashSet<Integer>();
164 for (Iterator<Integer> iit = map.iterator(); iit.hasNext();) {
165 valueSet.add(iit.next());
167 assertEquals(length, valueSet.size());
168 Object[] array = map.toArray();
169 assertEquals(length, array.length);
170 for (Object value : array) {
171 assertTrue(valueSet.contains(value));
174 Integer[] array2 = new Integer[80];
175 array2 = map.toArray(array2);
176 for (int value : array2) {
177 assertTrue(valueSet.contains(value));
179 Integer[] array3 = new Integer[120];
180 array3 = map.toArray(array3);
181 for (int i = 0; i < length; ++i) {
182 assertTrue(valueSet.contains(array3[i]));
184 assertNull(array3[length]);
186 for (int i = 0; i < length; ++i) {
187 assertTrue(map.containsValue(i + 100));
188 assertTrue(map.containsKey(i * 64*1.1f));
191 for (FloatIterator iit = map.keyIterator(); iit.hasNext();) {
195 assertTrue(map.isEmpty());
196 assertEquals(0, map.size());
200 // now with random data.. and lots of it
202 public void test4() {
203 FloatToObjectMap<Integer> map = new FloatToObjectMap<Integer>();
204 int length = ArrayHashMapTest.RANDOM_TEST_NUM_ITERATIONS;
206 // for a repeatable random sequence
207 long seed = random.nextLong();
208 Random random = new Random(seed);
210 for (int i = 0; i < length; ++i) {
211 int value = random.nextInt(Integer.MAX_VALUE);
212 map.put(i * 128*1.1f, value);
215 assertEquals(length, map.size());
218 random.setSeed(seed);
220 for (int i = 0; i < length; ++i) {
221 int value = random.nextInt(Integer.MAX_VALUE);
222 assertTrue(map.containsValue(value));
223 assertTrue(map.containsKey(i * 128*1.1f));
224 assertEquals(Integer.valueOf(value), map.remove(i * 128*1.1f));
226 assertEquals(0, map.size());
227 assertTrue(map.isEmpty());
231 public void testEquals() {
232 FloatToObjectMap<Integer> map1 = new FloatToObjectMap<Integer>(100);
233 FloatToObjectMap<Integer> map2 = new FloatToObjectMap<Integer>(100);
234 assertEquals("Empty maps should be equal", map1, map2);
235 assertEquals("hashCode() for empty maps should be equal",
236 map1.hashCode(), map2.hashCode());
238 for (int i = 0; i < 100; ++i) {
239 map1.put(i * 1.1f, 100 + i);
240 map2.put(i * 1.1f, 100 + i);
242 assertEquals("Identical maps should be equal", map1, map2);
243 assertEquals("hashCode() for identical maps should be equal",
244 map1.hashCode(), map2.hashCode());
246 for (int i = 10; i < 20; i++) {
247 map1.remove(i * 1.1f);
249 assertFalse("Different maps should not be equal", map1.equals(map2));
251 for (int i = 19; i >=10; --i) {
252 map2.remove(i * 1.1f);
254 assertEquals("Identical maps should be equal", map1, map2);
255 assertEquals("hashCode() for identical maps should be equal",
256 map1.hashCode(), map2.hashCode());
260 assertFalse("Different maps should not be equal", map1.equals(map2));
263 assertEquals("Identical maps should be equal", map1, map2);
264 assertEquals("hashCode() for identical maps should be equal",
265 map1.hashCode(), map2.hashCode());