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