1 package org.apache.lucene.util.collections;
3 import java.util.HashSet;
7 import org.apache.lucene.util.LuceneTestCase;
8 import org.apache.lucene.util.collections.IntHashSet;
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
18 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 public class IntHashSetTest extends LuceneTestCase {
31 IntHashSet set0 = new IntHashSet();
33 assertEquals(0, set0.size());
34 assertTrue(set0.isEmpty());
36 assertEquals(1, set0.size());
37 assertFalse(set0.isEmpty());
39 assertEquals(0, set0.size());
40 assertTrue(set0.isEmpty());
45 IntHashSet set0 = new IntHashSet();
47 assertEquals(0, set0.size());
48 assertTrue(set0.isEmpty());
49 for (int i = 0; i < 1000; ++i) {
52 assertEquals(1000, set0.size());
53 assertFalse(set0.isEmpty());
54 for (int i = 0; i < 1000; ++i) {
55 assertTrue(set0.contains(i));
59 assertEquals(0, set0.size());
60 assertTrue(set0.isEmpty());
66 IntHashSet set0 = new IntHashSet();
68 assertEquals(0, set0.size());
69 assertTrue(set0.isEmpty());
70 for (int i = 0; i < 1000; ++i) {
74 assertEquals(2, set0.size());
75 assertFalse(set0.isEmpty());
78 assertEquals(0, set0.size());
79 assertTrue(set0.isEmpty());
85 IntHashSet set0 = new IntHashSet();
87 assertEquals(0, set0.size());
88 assertTrue(set0.isEmpty());
89 for (int i = 0; i < 1000; ++i) {
93 for (int i = 0; i < 1000; i += 2) {
97 assertEquals(500, set0.size());
98 for (int i = 0; i < 1000; ++i) {
100 assertFalse(set0.contains(i));
102 assertTrue(set0.contains(i));
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();
123 assertEquals(set2.size(), set1.size());
125 for (int value : set2) {
126 assertTrue(set1.contains(value));
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();
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();
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();
178 int[] vals = set.toArray();
179 assertEquals(set.size(), vals.length);
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));
187 for (int i = 0; i < vals.length; ++i) {
188 assertEquals(vals[i], realValues[i]);
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();
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();