pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / test / org / apache / lucene / util / collections / ObjectToFloatMapTest.java
1 package org.apache.lucene.util.collections;
2
3 import org.junit.Test;
4
5 import org.apache.lucene.util.LuceneTestCase;
6 import org.apache.lucene.util.collections.FloatIterator;
7 import org.apache.lucene.util.collections.ObjectToFloatMap;
8
9 import java.util.HashSet;
10 import java.util.Iterator;
11 import java.util.Random;
12
13 /**
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
20  *
21  *     http://www.apache.org/licenses/LICENSE-2.0
22  *
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.
28  */
29
30 public class ObjectToFloatMapTest extends LuceneTestCase {
31
32   @Test
33   public void test0() {
34     ObjectToFloatMap<Integer> map = new ObjectToFloatMap<Integer>();
35
36     assertNaN(map.get(0));
37     
38     for (int i = 0; i < 100; ++i) {
39       int value = 100 + i;
40       assertFalse(map.containsValue(value));
41       map.put(i, value);
42       assertTrue(map.containsValue(value));
43       assertNotNaN(map.get(i));
44     }
45
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);
50
51     }
52
53     for (int i = 10; i < 90; ++i) {
54       map.remove(i);
55       assertNaN(map.get(i));
56     }
57
58     assertEquals(20, map.size());
59     for (int i = 0; i < 100; ++i) {
60       assertEquals(map.containsKey(i), !(i >= 10 && i < 90));
61     }
62
63     for (int i = 5; i < 85; ++i) {
64       map.put(i, Integer.valueOf(5 + i));
65     }
66     assertEquals(95, map.size());
67     for (int i = 0; i < 100; ++i) {
68       assertEquals(map.containsKey(i), !(i >= 85 && i < 90));
69     }
70     for (int i = 0; i < 5; ++i) {
71       assertEquals(map.get(i), (100 + i), 1E-5);
72     }
73     for (int i = 5; i < 85; ++i) {
74       assertEquals(map.get(i), (5 + i), 1E-5);
75     }
76     for (int i = 90; i < 100; ++i) {
77       assertEquals(map.get(i), (100 + i), 1E-5);
78     }
79   }
80
81   private static void assertNaN(float f) {
82     assertTrue(Float.isNaN(f));
83   }
84   
85   private static void assertNotNaN(float f) {
86     assertFalse(Float.isNaN(f));
87   }
88
89   @Test
90   public void test1() {
91     ObjectToFloatMap<Integer> map = new ObjectToFloatMap<Integer>();
92
93     for (int i = 0; i < 100; ++i) {
94       map.put(i, Integer.valueOf(100 + i));
95     }
96     
97     HashSet<Float> set = new HashSet<Float>();
98     
99     for (FloatIterator iterator = map.iterator(); iterator.hasNext();) {
100       set.add(iterator.next());
101     }
102
103     assertEquals(set.size(), map.size());
104     for (int i = 0; i < 100; ++i) {
105       assertTrue(set.contains(Float.valueOf(100+i)));
106     }
107
108     set.clear();
109     for (FloatIterator iterator = map.iterator(); iterator.hasNext();) {
110       Float value = iterator.next();
111       if (value % 2 == 1) {
112         iterator.remove();
113         continue;
114       }
115       set.add(value);
116     }
117     assertEquals(set.size(), map.size());
118     for (int i = 0; i < 100; i+=2) {
119       assertTrue(set.contains(Float.valueOf(100+i)));
120     }
121   }
122   
123   @Test
124   public void test2() {
125     ObjectToFloatMap<Integer> map = new ObjectToFloatMap<Integer>();
126
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));
132       map.put(i, value);
133       assertTrue(map.containsValue(value));
134       assertNotNaN(map.get(i));
135       assertFalse(map.isEmpty());
136     }
137
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);
142     }
143     
144     for (int i = 0 ; i < 200; i+=2) {
145       map.remove(i);
146     }
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);
151       map.remove(i);
152     }
153     assertTrue(map.isEmpty());
154   }
155   
156   @Test
157   public void test3() {
158     ObjectToFloatMap<Integer> map = new ObjectToFloatMap<Integer>();
159     int length = 100;
160     for (int i = 0; i < length; ++i) {
161       map.put(i*64, 100 + i);
162     }
163     HashSet<Integer> keySet = new HashSet<Integer>();
164     for (Iterator<Integer> iit = map.keyIterator(); iit.hasNext(); ) {
165       keySet.add(iit.next());
166     }
167     assertEquals(length, keySet.size());
168     for (int i = 0; i < length; ++i) {
169       assertTrue(keySet.contains(i * 64));
170     }
171     
172     HashSet<Float> valueSet = new HashSet<Float>();
173     for (FloatIterator iit = map.iterator(); iit.hasNext(); ) {
174       valueSet.add(iit.next());
175     }
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));
181     }
182     
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));
188     }
189     
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]));
194     }
195     assertNaN(array3[length]);
196     
197     for (int i = 0; i < length; ++i) {
198       assertTrue(map.containsValue(i + 100));
199       assertTrue(map.containsKey(i*64));
200     }
201     
202     for (Iterator<Integer> iit = map.keyIterator(); iit.hasNext(); ) {
203       iit.next();
204       iit.remove();
205     }
206     assertTrue(map.isEmpty());
207     assertEquals(0, map.size());
208     
209   }
210
211   // now with random data.. and lots of it
212   @Test
213   public void test4() {
214     ObjectToFloatMap<Integer> map = new ObjectToFloatMap<Integer>();
215     int length = ArrayHashMapTest.RANDOM_TEST_NUM_ITERATIONS;
216     
217     // for a repeatable random sequence
218     long seed = random.nextLong();
219     Random random = new Random(seed);
220
221     for (int i = 0; i < length; ++i) {
222       int value = random.nextInt(Integer.MAX_VALUE);
223       map.put(i*128, value);
224     }
225
226     assertEquals(length, map.size());
227
228     // now repeat
229     random.setSeed(seed);
230
231     for (int i = 0; i < length; ++i) {
232       int value = random.nextInt(Integer.MAX_VALUE);
233       
234       assertTrue(map.containsValue(value));
235       assertTrue(map.containsKey(i*128));
236       assertEquals(0, Float.compare(value, map.remove(i*128)));
237     }
238     assertEquals(0, map.size());
239     assertTrue(map.isEmpty());
240   }
241   
242   @Test
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());
249     
250     for (int i = 0; i < 100; ++i) {
251       map1.put(i, Float.valueOf(1f/i));
252       map2.put(i, Float.valueOf(1f/i));
253     }
254     assertEquals("Identical maps should be equal", map1, map2);
255     assertEquals("hashCode() for identical maps should be equal", 
256         map1.hashCode(), map2.hashCode());
257
258     for (int i = 10; i < 20; i++) {
259       map1.remove(i);
260     }
261     assertFalse("Different maps should not be equal", map1.equals(map2));
262     
263     for (int i = 19; i >=10; --i) {
264       map2.remove(i);
265     }
266     assertEquals("Identical maps should be equal", map1, map2);
267     assertEquals("hashCode() for identical maps should be equal", 
268         map1.hashCode(), map2.hashCode());
269     
270     map1.put(-1,-1f);
271     map2.put(-1,-1.1f);
272     assertFalse("Different maps should not be equal", map1.equals(map2));
273     
274     map2.put(-1,-1f);
275     assertEquals("Identical maps should be equal", map1, map2);
276     assertEquals("hashCode() for identical maps should be equal", 
277         map1.hashCode(), map2.hashCode());
278   }
279   
280 }