add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / test / org / apache / lucene / util / collections / ObjectToIntMapTest.java
1 package org.apache.lucene.util.collections;
2
3 import java.util.HashSet;
4 import java.util.Iterator;
5 import java.util.Random;
6
7 import org.junit.Test;
8
9 import org.apache.lucene.util.LuceneTestCase;
10 import org.apache.lucene.util.collections.IntIterator;
11 import org.apache.lucene.util.collections.ObjectToIntMap;
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 ObjectToIntMapTest extends LuceneTestCase {
31
32   @Test
33   public void test0() {
34     ObjectToIntMap<Integer> map = new ObjectToIntMap<Integer>();
35
36     assertIntegerMaxValue(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       assertNotIntegerMaxValue(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       assertIntegerMaxValue(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 assertIntegerMaxValue(int i) {
82     assertTrue(i == Integer.MAX_VALUE);
83   }
84   
85   private static void assertNotIntegerMaxValue(int i) {
86     assertFalse(i == Integer.MAX_VALUE);
87   }
88
89   @Test
90   public void test1() {
91     ObjectToIntMap<Integer> map = new ObjectToIntMap<Integer>();
92
93     for (int i = 0; i < 100; ++i) {
94       map.put(i, Integer.valueOf(100 + i));
95     }
96     
97     HashSet<Integer> set = new HashSet<Integer>();
98     
99     for (IntIterator 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(Integer.valueOf(100+i)));
106     }
107
108     set.clear();
109     for (IntIterator iterator = map.iterator(); iterator.hasNext();) {
110       Integer 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(Integer.valueOf(100+i)));
120     }
121   }
122   
123   @Test
124   public void test2() {
125     ObjectToIntMap<Integer> map = new ObjectToIntMap<Integer>();
126
127     assertTrue(map.isEmpty());
128     assertIntegerMaxValue(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       assertNotIntegerMaxValue(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     ObjectToIntMap<Integer> map = new ObjectToIntMap<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<Integer> valueSet = new HashSet<Integer>();
173     for (IntIterator iit = map.iterator(); iit.hasNext(); ) {
174       valueSet.add(iit.next());
175     }
176     assertEquals(length, valueSet.size());
177     int[] array = map.toArray();
178     assertEquals(length, array.length);
179     for (int value: array) {
180       assertTrue(valueSet.contains(value));
181     }
182     
183     int[] array2 = new int[80];
184     array2 = map.toArray(array2);
185     assertEquals(80, array2.length);
186     for (int value: array2) {
187       assertTrue(valueSet.contains(value));
188     }
189     
190     int[] array3 = new int[120];
191     array3 = map.toArray(array3);
192     for (int i = 0 ;i < length; ++i) {
193       assertTrue(valueSet.contains(array3[i]));
194     }
195     assertIntegerMaxValue(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     ObjectToIntMap<Integer> map = new ObjectToIntMap<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       assertTrue(map.containsValue(value));
234       assertTrue(map.containsKey(i*128));
235       assertEquals(value, map.remove(i*128));
236     }
237     assertEquals(0, map.size());
238     assertTrue(map.isEmpty());
239   }
240   
241   @Test
242   public void testEquals() {
243     ObjectToIntMap<Float> map1 = new ObjectToIntMap<Float>(100);
244     ObjectToIntMap<Float> map2 = new ObjectToIntMap<Float>(100);
245     assertEquals("Empty maps should be equal", map1, map2);
246     assertEquals("hashCode() for empty maps should be equal", 
247         map1.hashCode(), map2.hashCode());
248     
249     for (int i = 0; i < 100; ++i) {
250       map1.put(i * 1.1f, 100 + i);
251       map2.put(i * 1.1f, 100 + i);
252     }
253     assertEquals("Identical maps should be equal", map1, map2);
254     assertEquals("hashCode() for identical maps should be equal", 
255         map1.hashCode(), map2.hashCode());
256
257     for (int i = 10; i < 20; i++) {
258       map1.remove(i * 1.1f);
259     }
260     assertFalse("Different maps should not be equal", map1.equals(map2));
261     
262     for (int i = 19; i >=10; --i) {
263       map2.remove(i * 1.1f);
264     }
265     assertEquals("Identical maps should be equal", map1, map2);
266     assertEquals("hashCode() for identical maps should be equal", 
267         map1.hashCode(), map2.hashCode());
268     
269     map1.put(-1.1f,-1);
270     map2.put(-1.1f,-2);
271     assertFalse("Different maps should not be equal", map1.equals(map2));
272     
273     map2.put(-1.1f,-1);
274     assertEquals("Identical maps should be equal", map1, map2);
275     assertEquals("hashCode() for identical maps should be equal", 
276         map1.hashCode(), map2.hashCode());
277   }
278 }