1
package org.apache.lucene.util.collections;
5
import org.apache.lucene.util.LuceneTestCase;
6
import org.apache.lucene.util.collections.DoubleIterator;
7
import org.apache.lucene.util.collections.IntIterator;
8
import org.apache.lucene.util.collections.IntToDoubleMap;
10
import java.util.HashSet;
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 IntToDoubleMapTest extends LuceneTestCase {
31
private static void assertGround(double value) {
32
assertEquals(IntToDoubleMap.GROUND, value, Double.MAX_VALUE);
37
IntToDoubleMap map = new IntToDoubleMap();
39
assertGround(map.get(0));
41
for (int i = 0; i < 100; ++i) {
43
assertFalse(map.containsValue(value));
45
assertTrue(map.containsValue(value));
46
assertNotNull(map.get(i));
49
assertEquals(100, map.size());
50
for (int i = 0; i < 100; ++i) {
51
assertTrue(map.containsKey(i));
52
assertEquals(100 + i, map.get(i), Double.MAX_VALUE);
56
for (int i = 10; i < 90; ++i) {
58
assertGround(map.get(i));
61
assertEquals(20, map.size());
62
for (int i = 0; i < 100; ++i) {
63
assertEquals(map.containsKey(i), !(i >= 10 && i < 90));
66
for (int i = 5; i < 85; ++i) {
67
map.put(i, Integer.valueOf(5 + i));
69
assertEquals(95, map.size());
70
for (int i = 0; i < 100; ++i) {
71
assertEquals(map.containsKey(i), !(i >= 85 && i < 90));
73
for (int i = 0; i < 5; ++i) {
74
assertEquals(map.get(i), (100 + i), Double.MAX_VALUE);
76
for (int i = 5; i < 85; ++i) {
77
assertEquals(map.get(i), (5 + i), Double.MAX_VALUE);
79
for (int i = 90; i < 100; ++i) {
80
assertEquals(map.get(i), (100 + i), Double.MAX_VALUE);
86
IntToDoubleMap map = new IntToDoubleMap();
88
for (int i = 0; i < 100; ++i) {
89
map.put(i, Integer.valueOf(100 + i));
92
HashSet<Double> set = new HashSet<Double>();
94
for (DoubleIterator iterator = map.iterator(); iterator.hasNext();) {
95
set.add(iterator.next());
98
assertEquals(set.size(), map.size());
99
for (int i = 0; i < 100; ++i) {
100
assertTrue(set.contains(Double.valueOf(100+i)));
104
for (DoubleIterator iterator = map.iterator(); iterator.hasNext();) {
105
double d = iterator.next();
112
assertEquals(set.size(), map.size());
113
for (int i = 0; i < 100; i+=2) {
114
assertTrue(set.contains(Double.valueOf(100+i)));
119
public void test2() {
120
IntToDoubleMap map = new IntToDoubleMap();
122
assertTrue(map.isEmpty());
123
assertGround(map.get(0));
124
for (int i = 0; i < 128; ++i) {
125
int value = i * 4096;
126
assertFalse(map.containsValue(value));
128
assertTrue(map.containsValue(value));
129
assertNotNull(map.get(i));
130
assertFalse(map.isEmpty());
133
assertEquals(128, map.size());
134
for (int i = 0; i < 128; ++i) {
135
assertTrue(map.containsKey(i));
136
assertEquals(i * 4096, map.get(i), Double.MAX_VALUE);
139
for (int i = 0 ; i < 200; i+=2) {
142
assertEquals(64, map.size());
143
for (int i = 1; i < 128; i+=2) {
144
assertTrue(map.containsKey(i));
145
assertEquals(i * 4096, map.get(i), Double.MAX_VALUE);
148
assertTrue(map.isEmpty());
152
public void test3() {
153
IntToDoubleMap map = new IntToDoubleMap();
155
for (int i = 0; i < length; ++i) {
156
map.put(i*64, 100 + i);
158
HashSet<Integer> keySet = new HashSet<Integer>();
159
for (IntIterator iit = map.keyIterator(); iit.hasNext(); ) {
160
keySet.add(iit.next());
162
assertEquals(length, keySet.size());
163
for (int i = 0; i < length; ++i) {
164
assertTrue(keySet.contains(i * 64));
167
HashSet<Double> valueSet = new HashSet<Double>();
168
for (DoubleIterator iit = map.iterator(); iit.hasNext(); ) {
169
valueSet.add(iit.next());
171
assertEquals(length, valueSet.size());
172
double[] array = map.toArray();
173
assertEquals(length, array.length);
174
for (double value: array) {
175
assertTrue(valueSet.contains(value));
178
double[] array2 = new double[80];
179
array2 = map.toArray(array2);
180
assertEquals(length, array2.length);
181
for (double value: array2) {
182
assertTrue(valueSet.contains(value));
185
double[] array3 = new double[120];
186
array3 = map.toArray(array3);
187
for (int i = 0 ;i < length; ++i) {
188
assertTrue(valueSet.contains(array3[i]));
191
for (int i = 0; i < length; ++i) {
192
assertTrue(map.containsValue(i + 100));
193
assertTrue(map.containsKey(i*64));
196
for (IntIterator iit = map.keyIterator(); iit.hasNext(); ) {
200
assertTrue(map.isEmpty());
201
assertEquals(0, map.size());
205
// now with random data.. and lots of it
207
public void test4() {
208
IntToDoubleMap map = new IntToDoubleMap();
209
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);
226
assertTrue(map.containsValue(value));
227
assertTrue(map.containsKey(i*128));
228
assertEquals(0, Double.compare(value, map.remove(i*128)));
230
assertEquals(0, map.size());
231
assertTrue(map.isEmpty());
235
public void testEquals() {
236
IntToDoubleMap map1 = new IntToDoubleMap(100);
237
IntToDoubleMap map2 = new IntToDoubleMap(100);
238
assertEquals("Empty maps should be equal", map1, map2);
239
assertEquals("hashCode() for empty maps should be equal",
240
map1.hashCode(), map2.hashCode());
242
for (int i = 0; i < 100; ++i) {
243
map1.put(i, Float.valueOf(1f/i));
244
map2.put(i, Float.valueOf(1f/i));
246
assertEquals("Identical maps should be equal", map1, map2);
247
assertEquals("hashCode() for identical maps should be equal",
248
map1.hashCode(), map2.hashCode());
250
for (int i = 10; i < 20; i++) {
253
assertFalse("Different maps should not be equal", map1.equals(map2));
255
for (int i = 19; i >=10; --i) {
258
assertEquals("Identical maps should be equal", map1, map2);
259
assertEquals("hashCode() for identical maps should be equal",
260
map1.hashCode(), map2.hashCode());
264
assertFalse("Different maps should not be equal", map1.equals(map2));
267
assertEquals("Identical maps should be equal", map1, map2);
268
assertEquals("hashCode() for identical maps should be equal",
269
map1.hashCode(), map2.hashCode());