add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / test / org / apache / lucene / util / collections / IntToObjectMapTest.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.IntToObjectMap;
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 IntToObjectMapTest extends LuceneTestCase {
31
32   @Test
33   public void test0() {
34     IntToObjectMap<Integer> map = new IntToObjectMap<Integer>();
35
36     assertNull(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       assertNotNull(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).intValue());
50
51     }
52
53     for (int i = 10; i < 90; ++i) {
54       map.remove(i);
55       assertNull(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).intValue(), (100 + i));
72     }
73     for (int i = 5; i < 85; ++i) {
74       assertEquals(map.get(i).intValue(), (5 + i));
75     }
76     for (int i = 90; i < 100; ++i) {
77       assertEquals(map.get(i).intValue(), (100 + i));
78     }
79   }
80
81   @Test
82   public void test1() {
83     IntToObjectMap<Integer> map = new IntToObjectMap<Integer>();
84
85     for (int i = 0; i < 100; ++i) {
86       map.put(i, Integer.valueOf(100 + i));
87     }
88
89     HashSet<Integer> set = new HashSet<Integer>();
90
91     for (Iterator<Integer> iterator = map.iterator(); iterator.hasNext();) {
92       set.add(iterator.next());
93     }
94
95     assertEquals(set.size(), map.size());
96     for (int i = 0; i < 100; ++i) {
97       assertTrue(set.contains(Integer.valueOf(100 + i)));
98     }
99
100     set.clear();
101     for (Iterator<Integer> iterator = map.iterator(); iterator.hasNext();) {
102       Integer integer = iterator.next();
103       if (integer % 2 == 1) {
104         iterator.remove();
105         continue;
106       }
107       set.add(integer);
108     }
109     assertEquals(set.size(), map.size());
110     for (int i = 0; i < 100; i += 2) {
111       assertTrue(set.contains(Integer.valueOf(100 + i)));
112     }
113   }
114
115   @Test
116   public void test2() {
117     IntToObjectMap<Integer> map = new IntToObjectMap<Integer>();
118
119     assertTrue(map.isEmpty());
120     assertNull(map.get(0));
121     for (int i = 0; i < 128; ++i) {
122       int value = i * 4096;
123       assertFalse(map.containsValue(value));
124       map.put(i, value);
125       assertTrue(map.containsValue(value));
126       assertNotNull(map.get(i));
127       assertFalse(map.isEmpty());
128     }
129
130     assertEquals(128, map.size());
131     for (int i = 0; i < 128; ++i) {
132       assertTrue(map.containsKey(i));
133       assertEquals(i * 4096, map.get(i).intValue());
134     }
135
136     for (int i = 0; i < 200; i += 2) {
137       map.remove(i);
138     }
139     assertEquals(64, map.size());
140     for (int i = 1; i < 128; i += 2) {
141       assertTrue(map.containsKey(i));
142       assertEquals(i * 4096, map.get(i).intValue());
143       map.remove(i);
144     }
145     assertTrue(map.isEmpty());
146   }
147
148   @Test
149   public void test3() {
150     IntToObjectMap<Integer> map = new IntToObjectMap<Integer>();
151     int length = 100;
152     for (int i = 0; i < length; ++i) {
153       map.put(i * 64, 100 + i);
154     }
155     HashSet<Integer> keySet = new HashSet<Integer>();
156     for (IntIterator iit = map.keyIterator(); iit.hasNext();) {
157       keySet.add(iit.next());
158     }
159     assertEquals(length, keySet.size());
160     for (int i = 0; i < length; ++i) {
161       assertTrue(keySet.contains(i * 64));
162     }
163
164     HashSet<Integer> valueSet = new HashSet<Integer>();
165     for (Iterator<Integer> iit = map.iterator(); iit.hasNext();) {
166       valueSet.add(iit.next());
167     }
168     assertEquals(length, valueSet.size());
169     Object[] array = map.toArray();
170     assertEquals(length, array.length);
171     for (Object value : array) {
172       assertTrue(valueSet.contains(value));
173     }
174
175     Integer[] array2 = new Integer[80];
176     array2 = map.toArray(array2);
177     for (int value : array2) {
178       assertTrue(valueSet.contains(value));
179     }
180     Integer[] array3 = new Integer[120];
181     array3 = map.toArray(array3);
182     for (int i = 0; i < length; ++i) {
183       assertTrue(valueSet.contains(array3[i]));
184     }
185     assertNull(array3[length]);
186
187     for (int i = 0; i < length; ++i) {
188       assertTrue(map.containsValue(i + 100));
189       assertTrue(map.containsKey(i * 64));
190     }
191
192     for (IntIterator iit = map.keyIterator(); iit.hasNext();) {
193       iit.next();
194       iit.remove();
195     }
196     assertTrue(map.isEmpty());
197     assertEquals(0, map.size());
198
199   }
200
201   // now with random data.. and lots of it
202   @Test
203   public void test4() {
204     IntToObjectMap<Integer> map = new IntToObjectMap<Integer>();
205     int length = ArrayHashMapTest.RANDOM_TEST_NUM_ITERATIONS;
206     
207     // for a repeatable random sequence
208     long seed = random.nextLong();
209     Random random = new Random(seed);
210     
211     for (int i = 0; i < length; ++i) {
212       int value = random.nextInt(Integer.MAX_VALUE);
213       map.put(i * 128, value);
214     }
215
216     assertEquals(length, map.size());
217
218     // now repeat
219     random.setSeed(seed);
220
221     for (int i = 0; i < length; ++i) {
222       int value = random.nextInt(Integer.MAX_VALUE);
223       assertTrue(map.containsValue(value));
224       assertTrue(map.containsKey(i * 128));
225       assertEquals(Integer.valueOf(value), map.remove(i * 128));
226     }
227     assertEquals(0, map.size());
228     assertTrue(map.isEmpty());
229   }
230   
231   @Test
232   public void testEquals() {
233     IntToObjectMap<Double> map1 = new IntToObjectMap<Double>(100);
234     IntToObjectMap<Double> map2 = new IntToObjectMap<Double>(100);
235     assertEquals("Empty maps should be equal", map1, map2);
236     assertEquals("hashCode() for empty maps should be equal", 
237         map1.hashCode(), map2.hashCode());
238     
239     for (int i = 0; i < 100; ++i) {
240       map1.put(i, Double.valueOf(1f/i));
241       map2.put(i, Double.valueOf(1f/i));
242     }
243     assertEquals("Identical maps should be equal", map1, map2);
244     assertEquals("hashCode() for identical maps should be equal", 
245         map1.hashCode(), map2.hashCode());
246   
247     for (int i = 10; i < 20; i++) {
248       map1.remove(i);
249     }
250     assertFalse("Different maps should not be equal", map1.equals(map2));
251     
252     for (int i = 19; i >=10; --i) {
253       map2.remove(i);
254     }
255     assertEquals("Identical maps should be equal", map1, map2);
256     assertEquals("hashCode() for identical maps should be equal", 
257         map1.hashCode(), map2.hashCode());
258     
259     map1.put(-1,-1d);
260     map2.put(-1,-1.1d);
261     assertFalse("Different maps should not be equal", map1.equals(map2));
262     
263     map2.put(-1,-1d);
264     assertEquals("Identical maps should be equal", map1, map2);
265     assertEquals("hashCode() for identical maps should be equal", 
266         map1.hashCode(), map2.hashCode());
267   }
268 }