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.addTest( 57 SetTestSuiteBuilder.using( 58 new TestStringSetGenerator() { 59 @Override 60 protected Set<String> create(String[] elements) { 61 return CompactHashSet.create(Arrays.asList(elements)); 62 } 63 }) 64 .named("CompactHashSet") 65 .withFeatures(allFeatures) 66 .createTestSuite()); 67 suite.addTest( 68 SetTestSuiteBuilder.using( 69 new TestStringSetGenerator() { 70 @Override 71 protected Set<String> create(String[] elements) { 72 CompactHashSet<String> set = CompactHashSet.create(); 73 set.convertToHashFloodingResistantImplementation(); 74 Collections.addAll(set, elements); 75 return set; 76 } 77 }) 78 .named("CompactHashSet with flooding protection") 79 .withFeatures(allFeatures) 80 .createTestSuite()); 81 suite.addTest( 82 SetTestSuiteBuilder.using( 83 new TestStringSetGenerator() { 84 @Override 85 protected Set<String> create(String[] elements) { 86 CompactHashSet<String> set = CompactHashSet.create(Arrays.asList(elements)); 87 for (int i = 0; i < 100; i++) { 88 set.add("extra" + i); 89 } 90 for (int i = 0; i < 100; i++) { 91 set.remove("extra" + i); 92 } 93 set.trimToSize(); 94 return set; 95 } 96 }) 97 .named("CompactHashSet#TrimToSize") 98 .withFeatures(allFeatures) 99 .createTestSuite()); 100 return suite; 101 } 102 testAllocArraysDefault()103 public void testAllocArraysDefault() { 104 CompactHashSet<Integer> set = CompactHashSet.create(); 105 assertThat(set.needsAllocArrays()).isTrue(); 106 assertThat(set.elements).isNull(); 107 108 set.add(1); 109 assertThat(set.needsAllocArrays()).isFalse(); 110 assertThat(set.elements).hasLength(CompactHashing.DEFAULT_SIZE); 111 } 112 testAllocArraysExpectedSize()113 public void testAllocArraysExpectedSize() { 114 for (int i = 0; i <= CompactHashing.DEFAULT_SIZE; i++) { 115 CompactHashSet<Integer> set = CompactHashSet.createWithExpectedSize(i); 116 assertThat(set.needsAllocArrays()).isTrue(); 117 assertThat(set.elements).isNull(); 118 119 set.add(1); 120 assertThat(set.needsAllocArrays()).isFalse(); 121 int expectedSize = Math.max(1, i); 122 assertThat(set.elements).hasLength(expectedSize); 123 } 124 } 125 126 } 127