pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / test / org / apache / lucene / util / collections / IntToIntMapTest.java
1 package org.apache.lucene.util.collections;
2
3 import org.junit.Test;
4 import java.util.HashSet;
5 import java.util.Random;
6
7 import org.apache.lucene.util.LuceneTestCase;
8 import org.apache.lucene.util.collections.IntIterator;
9 import org.apache.lucene.util.collections.IntToIntMap;
10
11 /**
12  * Licensed to the Apache Software Foundation (ASF) under one or more
13  * contributor license agreements.  See the NOTICE file distributed with
14  * this work for additional information regarding copyright ownership.
15  * The ASF licenses this file to You under the Apache License, Version 2.0
16  * (the "License"); you may not use this file except in compliance with
17  * the License.  You may obtain a copy of the License at
18  *
19  *     http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  */
27
28 public class IntToIntMapTest extends LuceneTestCase {
29   
30   private static void assertGround(int value) {
31     assertEquals(IntToIntMap.GROUD, value);
32   }
33   
34   @Test
35   public void test0() {
36     IntToIntMap map = new IntToIntMap();
37
38     assertGround(map.get(0));
39     
40     for (int i = 0; i < 100; ++i) {
41       int value = 100 + i;
42       assertFalse(map.containsValue(value));
43       map.put(i, value);
44       assertTrue(map.containsValue(value));
45       assertNotNull(map.get(i));
46     }
47
48     assertEquals(100, map.size());
49     for (int i = 0; i < 100; ++i) {
50       assertTrue(map.containsKey(i));
51       assertEquals(100 + i, map.get(i));
52
53     }
54
55     for (int i = 10; i < 90; ++i) {
56       map.remove(i);
57       assertGround(map.get(i));
58     }
59
60     assertEquals(20, map.size());
61     for (int i = 0; i < 100; ++i) {
62       assertEquals(map.containsKey(i), !(i >= 10 && i < 90));
63     }
64
65     for (int i = 5; i < 85; ++i) {
66       map.put(i, Integer.valueOf(5 + i));
67     }
68     assertEquals(95, map.size());
69     for (int i = 0; i < 100; ++i) {
70       assertEquals(map.containsKey(i), !(i >= 85 && i < 90));
71     }
72     for (int i = 0; i < 5; ++i) {
73       assertEquals(map.get(i), (100 + i));
74     }
75     for (int i = 5; i < 85; ++i) {
76       assertEquals(map.get(i), (5 + i));
77     }
78     for (int i = 90; i < 100; ++i) {
79       assertEquals(map.get(i), (100 + i));
80     }
81   }
82
83   @Test
84   public void test1() {
85     IntToIntMap map = new IntToIntMap();
86
87     for (int i = 0; i < 100; ++i) {
88       map.put(i, Integer.valueOf(100 + i));
89     }
90     
91     HashSet<Integer> set = new HashSet<Integer>();
92     
93     for (IntIterator iterator = map.iterator(); iterator.hasNext();) {
94       set.add(iterator.next());
95     }
96
97     assertEquals(set.size(), map.size());
98     for (int i = 0; i < 100; ++i) {
99       assertTrue(set.contains(Integer.valueOf(100+i)));
100     }
101
102     set.clear();
103     for (IntIterator iterator = map.iterator(); iterator.hasNext();) {
104       Integer integer = iterator.next();
105       if (integer % 2 == 1) {
106         iterator.remove();
107         continue;
108       }
109       set.add(integer);
110     }
111     assertEquals(set.size(), map.size());
112     for (int i = 0; i < 100; i+=2) {
113       assertTrue(set.contains(Integer.valueOf(100+i)));
114     }
115   }
116   
117   @Test
118   public void test2() {
119     IntToIntMap map = new IntToIntMap();
120
121     assertTrue(map.isEmpty());
122     assertGround(map.get(0));
123     for (int i = 0; i < 128; ++i) {
124       int value = i * 4096;
125       assertFalse(map.containsValue(value));
126       map.put(i, value);
127       assertTrue(map.containsValue(value));
128       assertNotNull(map.get(i));
129       assertFalse(map.isEmpty());
130     }
131
132     assertEquals(128, map.size());
133     for (int i = 0; i < 128; ++i) {
134       assertTrue(map.containsKey(i));
135       assertEquals(i * 4096, map.get(i));
136     }
137     
138     for (int i = 0 ; i < 200; i+=2) {
139       map.remove(i);
140     }
141     assertEquals(64, map.size());
142     for (int i = 1; i < 128; i+=2) {
143       assertTrue(map.containsKey(i));
144       assertEquals(i * 4096, map.get(i));
145       map.remove(i);
146     }
147     assertTrue(map.isEmpty());
148   }
149   
150   @Test
151   public void test3() {
152     IntToIntMap map = new IntToIntMap();
153     int length = 100;
154     for (int i = 0; i < length; ++i) {
155       map.put(i*64, 100 + i);
156     }
157     HashSet<Integer> keySet = new HashSet<Integer>();
158     for (IntIterator iit = map.keyIterator(); iit.hasNext(); ) {
159       keySet.add(iit.next());
160     }
161     assertEquals(length, keySet.size());
162     for (int i = 0; i < length; ++i) {
163       assertTrue(keySet.contains(i * 64));
164     }
165     
166     HashSet<Integer> valueSet = new HashSet<Integer>();
167     for (IntIterator iit = map.iterator(); iit.hasNext(); ) {
168       valueSet.add(iit.next());
169     }
170     assertEquals(length, valueSet.size());
171     int[] array = map.toArray();
172     assertEquals(length, array.length);
173     for (int value: array) {
174       assertTrue(valueSet.contains(value));
175     }
176     
177     int[] array2 = new int[80];
178     array2 = map.toArray(array2);
179     assertEquals(length, array2.length);
180     for (int value: array2) {
181       assertTrue(valueSet.contains(value));
182     }
183     
184     int[] array3 = new int[120];
185     array3 = map.toArray(array3);
186     for (int i = 0 ;i < length; ++i) {
187       assertTrue(valueSet.contains(array3[i]));
188     }
189     
190     for (int i = 0; i < length; ++i) {
191       assertTrue(map.containsValue(i + 100));
192       assertTrue(map.containsKey(i*64));
193     }
194     
195     for (IntIterator iit = map.keyIterator(); iit.hasNext(); ) {
196       iit.next();
197       iit.remove();
198     }
199     assertTrue(map.isEmpty());
200     assertEquals(0, map.size());
201     
202   }
203
204   // now with random data.. and lots of it
205   @Test
206   public void test4() {
207     IntToIntMap map = new IntToIntMap();
208     int length = ArrayHashMapTest.RANDOM_TEST_NUM_ITERATIONS;
209     
210     // for a repeatable random sequence
211     long seed = random.nextLong();
212     Random random = new Random(seed);
213     
214     for (int i = 0; i < length; ++i) {
215       int value = random.nextInt(Integer.MAX_VALUE);
216       map.put(i*128, value);
217     }
218
219     assertEquals(length, map.size());
220
221     // now repeat
222     random.setSeed(seed);
223
224     for (int i = 0; i < length; ++i) {
225       int value = random.nextInt(Integer.MAX_VALUE);
226       
227       assertTrue(map.containsValue(value));
228       assertTrue(map.containsKey(i*128));
229       assertEquals(value, map.remove(i*128));
230     }
231     assertEquals(0, map.size());
232     assertTrue(map.isEmpty());
233   }
234   
235   @Test
236   public void testEquals() {
237     IntToIntMap map1 = new IntToIntMap(100);
238     IntToIntMap map2 = new IntToIntMap(100);
239     assertEquals("Empty maps should be equal", map1, map2);
240     assertEquals("hashCode() for empty maps should be equal", 
241         map1.hashCode(), map2.hashCode());
242     
243     for (int i = 0; i < 100; ++i) {
244       map1.put(i, 100 + i);
245       map2.put(i, 100 + i);
246     }
247     assertEquals("Identical maps should be equal", map1, map2);
248     assertEquals("hashCode() for identical maps should be equal", 
249         map1.hashCode(), map2.hashCode());
250
251     for (int i = 10; i < 20; i++) {
252       map1.remove(i);
253     }
254     assertFalse("Different maps should not be equal", map1.equals(map2));
255     
256     for (int i = 19; i >=10; --i) {
257       map2.remove(i);
258     }
259     assertEquals("Identical maps should be equal", map1, map2);
260     assertEquals("hashCode() for identical maps should be equal", 
261         map1.hashCode(), map2.hashCode());
262     
263     map1.put(-1,-1);
264     map2.put(-1,-2);
265     assertFalse("Different maps should not be equal", map1.equals(map2));
266     
267     map2.put(-1,-1);
268     assertEquals("Identical maps should be equal", map1, map2);
269     assertEquals("hashCode() for identical maps should be equal", 
270         map1.hashCode(), map2.hashCode());
271   }
272 }