• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.google.common.annotations.GwtIncompatible;
22 import com.google.common.collect.testing.SetTestSuiteBuilder;
23 import com.google.common.collect.testing.TestStringSetGenerator;
24 import com.google.common.collect.testing.features.CollectionFeature;
25 import com.google.common.collect.testing.features.CollectionSize;
26 import com.google.common.collect.testing.features.Feature;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Set;
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34 
35 /**
36  * Tests for CompactHashSet.
37  *
38  * @author Dimitris Andreou
39  */
40 @GwtIncompatible // java.util.Arrays#copyOf(Object[], int), java.lang.reflect.Array
41 public class CompactHashSetTest extends TestCase {
suite()42   public static Test suite() {
43     List<Feature<?>> allFeatures =
44         Arrays.<Feature<?>>asList(
45             CollectionSize.ANY,
46             CollectionFeature.ALLOWS_NULL_VALUES,
47             CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
48             CollectionFeature.GENERAL_PURPOSE,
49             CollectionFeature.REMOVE_OPERATIONS,
50             CollectionFeature.SERIALIZABLE,
51             CollectionFeature.SUPPORTS_ADD,
52             CollectionFeature.SUPPORTS_REMOVE);
53 
54     TestSuite suite = new TestSuite();
55     suite.addTestSuite(CompactHashSetTest.class);
56     suite.addTestSuite(FloodingTest.class);
57     suite.addTest(
58         SetTestSuiteBuilder.using(
59                 new TestStringSetGenerator() {
60                   @Override
61                   protected Set<String> create(String[] elements) {
62                     return CompactHashSet.create(Arrays.asList(elements));
63                   }
64                 })
65             .named("CompactHashSet")
66             .withFeatures(allFeatures)
67             .createTestSuite());
68     suite.addTest(
69         SetTestSuiteBuilder.using(
70                 new TestStringSetGenerator() {
71                   @Override
72                   protected Set<String> create(String[] elements) {
73                     CompactHashSet<String> set = CompactHashSet.create();
74                     set.convertToHashFloodingResistantImplementation();
75                     Collections.addAll(set, elements);
76                     return set;
77                   }
78                 })
79             .named("CompactHashSet with flooding protection")
80             .withFeatures(allFeatures)
81             .createTestSuite());
82     suite.addTest(
83         SetTestSuiteBuilder.using(
84                 new TestStringSetGenerator() {
85                   @Override
86                   protected Set<String> create(String[] elements) {
87                     CompactHashSet set = CompactHashSet.create(Arrays.asList(elements));
88                     for (int i = 0; i < 100; i++) {
89                       set.add(i);
90                     }
91                     for (int i = 0; i < 100; i++) {
92                       set.remove(i);
93                     }
94                     set.trimToSize();
95                     return set;
96                   }
97                 })
98             .named("CompactHashSet#TrimToSize")
99             .withFeatures(allFeatures)
100             .createTestSuite());
101     return suite;
102   }
103 
testAllocArraysDefault()104   public void testAllocArraysDefault() {
105     CompactHashSet<Integer> set = CompactHashSet.create();
106     assertThat(set.needsAllocArrays()).isTrue();
107     assertThat(set.elements).isNull();
108 
109     set.add(1);
110     assertThat(set.needsAllocArrays()).isFalse();
111     assertThat(set.elements).hasLength(CompactHashing.DEFAULT_SIZE);
112   }
113 
testAllocArraysExpectedSize()114   public void testAllocArraysExpectedSize() {
115     for (int i = 0; i <= CompactHashing.DEFAULT_SIZE; i++) {
116       CompactHashSet<Integer> set = CompactHashSet.createWithExpectedSize(i);
117       assertThat(set.needsAllocArrays()).isTrue();
118       assertThat(set.elements).isNull();
119 
120       set.add(1);
121       assertThat(set.needsAllocArrays()).isFalse();
122       int expectedSize = Math.max(1, i);
123       assertThat(set.elements).hasLength(expectedSize);
124     }
125   }
126 
127   public static class FloodingTest extends AbstractHashFloodingTest<Set<Object>> {
FloodingTest()128     public FloodingTest() {
129       super(
130           ImmutableList.of(Construction.setFromElements(CompactHashSet::create)),
131           n -> n * Math.log(n),
132           ImmutableList.of(QueryOp.SET_CONTAINS));
133     }
134   }
135 }
136