1 package org.apache.lucene.util.collections;
5 import org.apache.lucene.util.LuceneTestCase;
6 import org.apache.lucene.util.collections.FloatIterator;
7 import org.apache.lucene.util.collections.ObjectToFloatMap;
9 import java.util.HashSet;
10 import java.util.Iterator;
11 import java.util.Random;
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 ObjectToFloatMapTest extends LuceneTestCase {
34 ObjectToFloatMap<Integer> map = new ObjectToFloatMap<Integer>();
36 assertNaN(map.get(0));
38 for (int i = 0; i < 100; ++i) {
40 assertFalse(map.containsValue(value));
42 assertTrue(map.containsValue(value));
43 assertNotNaN(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 assertNaN(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 assertNaN(float f) {
82 assertTrue(Float.isNaN(f));
85 private static void assertNotNaN(float f) {
86 assertFalse(Float.isNaN(f));
91 ObjectToFloatMap<Integer> map = new ObjectToFloatMap<Integer>();
93 for (int i = 0; i < 100; ++i) {
94 map.put(i, Integer.valueOf(100 + i));
97 HashSet<Float> set = new HashSet<Float>();
99 for (FloatIterator 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(Float.valueOf(100+i)));
109 for (FloatIterator iterator = map.iterator(); iterator.hasNext();) {
110 Float 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(Float.valueOf(100+i)));
124 public void test2() {
125 ObjectToFloatMap<Integer> map = new ObjectToFloatMap<Integer>();
127 assertTrue(map.isEmpty());
128 assertNaN(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 assertNotNaN(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 ObjectToFloatMap<Integer> map = new ObjectToFloatMap<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<Float> valueSet = new HashSet<Float>();
173 for (FloatIterator iit = map.iterator(); iit.hasNext(); ) {
174 valueSet.add(iit.next());
176 assertEquals(length, valueSet.size());
177 float[] array = map.toArray();
178 assertEquals(length, array.length);
179 for (float value: array) {
180 assertTrue(valueSet.contains(value));
183 float[] array2 = new float[80];
184 array2 = map.toArray(array2);
185 assertEquals(80, array2.length);
186 for (float value: array2) {
187 assertTrue(valueSet.contains(value));
190 float[] array3 = new float[120];
191 array3 = map.toArray(array3);
192 for (int i = 0 ;i < length; ++i) {
193 assertTrue(valueSet.contains(array3[i]));
195 assertNaN(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 ObjectToFloatMap<Integer> map = new ObjectToFloatMap<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);
234 assertTrue(map.containsValue(value));
235 assertTrue(map.containsKey(i*128));
236 assertEquals(0, Float.compare(value, map.remove(i*128)));
238 assertEquals(0, map.size());
239 assertTrue(map.isEmpty());
243 public void testEquals() {
244 ObjectToFloatMap<Integer> map1 = new ObjectToFloatMap<Integer>(100);
245 ObjectToFloatMap<Integer> map2 = new ObjectToFloatMap<Integer>(100);
246 assertEquals("Empty maps should be equal", map1, map2);
247 assertEquals("hashCode() for empty maps should be equal",
248 map1.hashCode(), map2.hashCode());
250 for (int i = 0; i < 100; ++i) {
251 map1.put(i, Float.valueOf(1f/i));
252 map2.put(i, Float.valueOf(1f/i));
254 assertEquals("Identical maps should be equal", map1, map2);
255 assertEquals("hashCode() for identical maps should be equal",
256 map1.hashCode(), map2.hashCode());
258 for (int i = 10; i < 20; i++) {
261 assertFalse("Different maps should not be equal", map1.equals(map2));
263 for (int i = 19; i >=10; --i) {
266 assertEquals("Identical maps should be equal", map1, map2);
267 assertEquals("hashCode() for identical maps should be equal",
268 map1.hashCode(), map2.hashCode());
272 assertFalse("Different maps should not be equal", map1.equals(map2));
275 assertEquals("Identical maps should be equal", map1, map2);
276 assertEquals("hashCode() for identical maps should be equal",
277 map1.hashCode(), map2.hashCode());