pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / test / org / apache / lucene / util / collections / FloatToObjectMapTest.java
1 package org.apache.lucene.util.collections;
2
3 import org.junit.Test;
4 import java.util.HashSet;
5 import java.util.Iterator;
6 import java.util.Random;
7
8 import org.apache.lucene.util.LuceneTestCase;
9 import org.apache.lucene.util.collections.FloatIterator;
10 import org.apache.lucene.util.collections.FloatToObjectMap;
11
12 /**
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
19  *
20  *     http://www.apache.org/licenses/LICENSE-2.0
21  *
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.
27  */
28
29 public class FloatToObjectMapTest extends LuceneTestCase {
30
31   @Test
32   public void test0() {
33     FloatToObjectMap<Integer> map = new FloatToObjectMap<Integer>();
34
35     assertNull(map.get(0));
36
37     for (int i = 0; i < 100; ++i) {
38       int value = 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));
43     }
44
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());
49
50     }
51
52     for (int i = 10; i < 90; ++i) {
53       map.remove(i*1.1f);
54       assertNull(map.get(i*1.1f));
55     }
56
57     assertEquals(20, map.size());
58     for (int i = 0; i < 100; ++i) {
59       assertEquals(map.containsKey(i*1.1f), !(i >= 10 && i < 90));
60     }
61
62     for (int i = 5; i < 85; ++i) {
63       map.put(i*1.1f, Integer.valueOf(5 + i));
64     }
65     assertEquals(95, map.size());
66     for (int i = 0; i < 100; ++i) {
67       assertEquals(map.containsKey(i*1.1f), !(i >= 85 && i < 90));
68     }
69     for (int i = 0; i < 5; ++i) {
70       assertEquals(map.get(i*1.1f).intValue(), (100 + i));
71     }
72     for (int i = 5; i < 85; ++i) {
73       assertEquals(map.get(i*1.1f).intValue(), (5 + i));
74     }
75     for (int i = 90; i < 100; ++i) {
76       assertEquals(map.get(i*1.1f).intValue(), (100 + i));
77     }
78   }
79
80   @Test
81   public void test1() {
82     FloatToObjectMap<Integer> map = new FloatToObjectMap<Integer>();
83
84     for (int i = 0; i < 100; ++i) {
85       map.put(i*1.1f, Integer.valueOf(100 + i));
86     }
87
88     HashSet<Integer> set = new HashSet<Integer>();
89
90     for (Iterator<Integer> iterator = map.iterator(); iterator.hasNext();) {
91       set.add(iterator.next());
92     }
93
94     assertEquals(set.size(), map.size());
95     for (int i = 0; i < 100; ++i) {
96       assertTrue(set.contains(Integer.valueOf(100 + i)));
97     }
98
99     set.clear();
100     for (Iterator<Integer> iterator = map.iterator(); iterator.hasNext();) {
101       Integer integer = iterator.next();
102       if (integer % 2 == 1) {
103         iterator.remove();
104         continue;
105       }
106       set.add(integer);
107     }
108     assertEquals(set.size(), map.size());
109     for (int i = 0; i < 100; i += 2) {
110       assertTrue(set.contains(Integer.valueOf(100 + i)));
111     }
112   }
113
114   @Test
115   public void test2() {
116     FloatToObjectMap<Integer> map = new FloatToObjectMap<Integer>();
117
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());
127     }
128
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());
133     }
134
135     for (int i = 0; i < 200; i += 2) {
136       map.remove(i*1.1f);
137     }
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());
142       map.remove(i*1.1f);
143     }
144     assertTrue(map.isEmpty());
145   }
146
147   @Test
148   public void test3() {
149     FloatToObjectMap<Integer> map = new FloatToObjectMap<Integer>();
150     int length = 100;
151     for (int i = 0; i < length; ++i) {
152       map.put(i * 64*1.1f, 100 + i);
153     }
154     HashSet<Float> keySet = new HashSet<Float>();
155     for (FloatIterator iit = map.keyIterator(); iit.hasNext();) {
156       keySet.add(iit.next());
157     }
158     assertEquals(length, keySet.size());
159     for (int i = 0; i < length; ++i) {
160       assertTrue(keySet.contains(i * 64*1.1f));
161     }
162
163     HashSet<Integer> valueSet = new HashSet<Integer>();
164     for (Iterator<Integer> iit = map.iterator(); iit.hasNext();) {
165       valueSet.add(iit.next());
166     }
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));
172     }
173
174     Integer[] array2 = new Integer[80];
175     array2 = map.toArray(array2);
176     for (int value : array2) {
177       assertTrue(valueSet.contains(value));
178     }
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]));
183     }
184     assertNull(array3[length]);
185
186     for (int i = 0; i < length; ++i) {
187       assertTrue(map.containsValue(i + 100));
188       assertTrue(map.containsKey(i * 64*1.1f));
189     }
190
191     for (FloatIterator iit = map.keyIterator(); iit.hasNext();) {
192       iit.next();
193       iit.remove();
194     }
195     assertTrue(map.isEmpty());
196     assertEquals(0, map.size());
197
198   }
199
200   // now with random data.. and lots of it
201   @Test
202   public void test4() {
203     FloatToObjectMap<Integer> map = new FloatToObjectMap<Integer>();
204     int length = ArrayHashMapTest.RANDOM_TEST_NUM_ITERATIONS;
205     
206     // for a repeatable random sequence
207     long seed = random.nextLong();
208     Random random = new Random(seed);
209
210     for (int i = 0; i < length; ++i) {
211       int value = random.nextInt(Integer.MAX_VALUE);
212       map.put(i * 128*1.1f, value);
213     }
214
215     assertEquals(length, map.size());
216
217     // now repeat
218     random.setSeed(seed);
219
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));
225     }
226     assertEquals(0, map.size());
227     assertTrue(map.isEmpty());
228   }
229
230   @Test
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());
237     
238     for (int i = 0; i < 100; ++i) {
239       map1.put(i * 1.1f, 100 + i);
240       map2.put(i * 1.1f, 100 + i);
241     }
242     assertEquals("Identical maps should be equal", map1, map2);
243     assertEquals("hashCode() for identical maps should be equal", 
244         map1.hashCode(), map2.hashCode());
245
246     for (int i = 10; i < 20; i++) {
247       map1.remove(i * 1.1f);
248     }
249     assertFalse("Different maps should not be equal", map1.equals(map2));
250     
251     for (int i = 19; i >=10; --i) {
252       map2.remove(i * 1.1f);
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     map1.put(-1.1f,-1);
259     map2.put(-1.1f,-2);
260     assertFalse("Different maps should not be equal", map1.equals(map2));
261     
262     map2.put(-1.1f,-1);
263     assertEquals("Identical maps should be equal", map1, map2);
264     assertEquals("hashCode() for identical maps should be equal", 
265         map1.hashCode(), map2.hashCode());
266   }
267 }