pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / test / org / apache / lucene / util / collections / IntHashSetTest.java
1 package org.apache.lucene.util.collections;
2
3 import java.util.HashSet;
4
5 import org.junit.Test;
6
7 import org.apache.lucene.util.LuceneTestCase;
8 import org.apache.lucene.util.collections.IntHashSet;
9
10 /**
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements.  See the NOTICE file distributed with
13  * this work for additional information regarding copyright ownership.
14  * The ASF licenses this file to You under the Apache License, Version 2.0
15  * (the "License"); you may not use this file except in compliance with
16  * the License.  You may obtain a copy of the License at
17  *
18  *     http://www.apache.org/licenses/LICENSE-2.0
19  *
20  * Unless required by applicable law or agreed to in writing, software
21  * distributed under the License is distributed on an "AS IS" BASIS,
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23  * See the License for the specific language governing permissions and
24  * limitations under the License.
25  */
26
27 public class IntHashSetTest extends LuceneTestCase {
28
29   @Test
30   public void test0() {
31     IntHashSet set0 = new IntHashSet();
32
33     assertEquals(0, set0.size());
34     assertTrue(set0.isEmpty());
35     set0.add(0);
36     assertEquals(1, set0.size());
37     assertFalse(set0.isEmpty());
38     set0.remove(0);
39     assertEquals(0, set0.size());
40     assertTrue(set0.isEmpty());
41   }
42
43   @Test
44   public void test1() {
45     IntHashSet set0 = new IntHashSet();
46
47     assertEquals(0, set0.size());
48     assertTrue(set0.isEmpty());
49     for (int i = 0; i < 1000; ++i) {
50       set0.add(i);
51     }
52     assertEquals(1000, set0.size());
53     assertFalse(set0.isEmpty());
54     for (int i = 0; i < 1000; ++i) {
55       assertTrue(set0.contains(i));
56     }
57
58     set0.clear();
59     assertEquals(0, set0.size());
60     assertTrue(set0.isEmpty());
61
62   }
63
64   @Test
65   public void test2() {
66     IntHashSet set0 = new IntHashSet();
67
68     assertEquals(0, set0.size());
69     assertTrue(set0.isEmpty());
70     for (int i = 0; i < 1000; ++i) {
71       set0.add(1);
72       set0.add(-382);
73     }
74     assertEquals(2, set0.size());
75     assertFalse(set0.isEmpty());
76     set0.remove(-382);
77     set0.remove(1);
78     assertEquals(0, set0.size());
79     assertTrue(set0.isEmpty());
80
81   }
82
83   @Test
84   public void test3() {
85     IntHashSet set0 = new IntHashSet();
86
87     assertEquals(0, set0.size());
88     assertTrue(set0.isEmpty());
89     for (int i = 0; i < 1000; ++i) {
90       set0.add(i);
91     }
92
93     for (int i = 0; i < 1000; i += 2) {
94       set0.remove(i);
95     }
96
97     assertEquals(500, set0.size());
98     for (int i = 0; i < 1000; ++i) {
99       if (i % 2 == 0) {
100         assertFalse(set0.contains(i));
101       } else {
102         assertTrue(set0.contains(i));
103       }
104     }
105
106   }
107
108   @Test
109   public void test4() {
110     IntHashSet set1 = new IntHashSet();
111     HashSet<Integer> set2 = new HashSet<Integer>();
112     for (int i = 0; i < ArrayHashMapTest.RANDOM_TEST_NUM_ITERATIONS; ++i) {
113       int value = random.nextInt() % 500;
114       boolean shouldAdd = random.nextBoolean();
115       if (shouldAdd) {
116         set1.add(value);
117         set2.add(value);
118       } else {
119         set1.remove(value);
120         set2.remove(value);
121       }
122     }
123     assertEquals(set2.size(), set1.size());
124     int i = 0;
125     for (int value : set2) {
126       assertTrue(set1.contains(value));
127       i++;
128     }
129   }
130
131   @Test
132   public void testRegularJavaSet() {
133     HashSet<Integer> set = new HashSet<Integer>();
134     for (int j = 0; j < 100; ++j) {
135       for (int i = 0; i < ArrayHashMapTest.RANDOM_TEST_NUM_ITERATIONS; ++i) {
136         int value = random.nextInt() % 5000;
137         boolean shouldAdd = random.nextBoolean();
138         if (shouldAdd) {
139           set.add(value);
140         } else {
141           set.remove(value);
142         }
143       }
144       set.clear();
145     }
146   }
147
148   @Test
149   public void testMySet() {
150     IntHashSet set = new IntHashSet();
151     for (int j = 0; j < 100; ++j) {
152       for (int i = 0; i < ArrayHashMapTest.RANDOM_TEST_NUM_ITERATIONS; ++i) {
153         int value = random.nextInt() % 5000;
154         boolean shouldAdd = random.nextBoolean();
155         if (shouldAdd) {
156           set.add(value);
157         } else {
158           set.remove(value);
159         }
160       }
161       set.clear();
162     }
163   }
164
165   @Test
166   public void testToArray() {
167     IntHashSet set = new IntHashSet();
168     for (int j = 0; j < 100; ++j) {
169       for (int i = 0; i < ArrayHashMapTest.RANDOM_TEST_NUM_ITERATIONS; ++i) {
170         int value = random.nextInt() % 5000;
171         boolean shouldAdd = random.nextBoolean();
172         if (shouldAdd) {
173           set.add(value);
174         } else {
175           set.remove(value);
176         }
177       }
178       int[] vals = set.toArray();
179       assertEquals(set.size(), vals.length);
180
181       int[] realValues = new int[set.size()];
182       int[] unrealValues = set.toArray(realValues);
183       assertEquals(realValues, unrealValues);
184       for (int value : vals) {
185         assertTrue(set.remove(value));
186       }
187       for (int i = 0; i < vals.length; ++i) {
188         assertEquals(vals[i], realValues[i]);
189       }
190     }
191   }
192
193   @Test
194   public void testZZRegularJavaSet() {
195     HashSet<Integer> set = new HashSet<Integer>();
196     for (int j = 0; j < 100; ++j) {
197       for (int i = 0; i < ArrayHashMapTest.RANDOM_TEST_NUM_ITERATIONS; ++i) {
198         int value = random.nextInt() % 5000;
199         boolean shouldAdd = random.nextBoolean();
200         if (shouldAdd) {
201           set.add(value);
202         } else {
203           set.remove(value);
204         }
205       }
206       set.clear();
207     }
208   }
209
210   @Test
211   public void testZZMySet() {
212     IntHashSet set = new IntHashSet();
213     for (int j = 0; j < 100; ++j) {
214       for (int i = 0; i < ArrayHashMapTest.RANDOM_TEST_NUM_ITERATIONS; ++i) {
215         int value = random.nextInt() % 5000;
216         boolean shouldAdd = random.nextBoolean();
217         if (shouldAdd) {
218           set.add(value);
219         } else {
220           set.remove(value);
221         }
222       }
223       set.clear();
224     }
225   }
226 }