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 import static java.util.stream.Collectors.*; 21 22 import com.google.common.annotations.GwtIncompatible; 23 import com.google.common.collect.testing.SetTestSuiteBuilder; 24 import com.google.common.collect.testing.TestStringSetGenerator; 25 import com.google.common.collect.testing.features.CollectionFeature; 26 import com.google.common.collect.testing.features.CollectionSize; 27 import com.google.common.collect.testing.features.Feature; 28 import java.util.Arrays; 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 set = CompactHashSet.create(Arrays.asList(elements)); 73 for (int i = 0; i < 100; i++) { 74 set.add(i); 75 } 76 for (int i = 0; i < 100; i++) { 77 set.remove(i); 78 } 79 set.trimToSize(); 80 return set; 81 } 82 }) 83 .named("CompactHashSet#TrimToSize") 84 .withFeatures(allFeatures) 85 .createTestSuite()); 86 return suite; 87 } 88 testAllocArraysDefault()89 public void testAllocArraysDefault() { 90 CompactHashSet<Integer> set = CompactHashSet.create(); 91 assertThat(set.needsAllocArrays()).isTrue(); 92 assertThat(set.elements).isNull(); 93 94 set.add(1); 95 assertThat(set.needsAllocArrays()).isFalse(); 96 assertThat(set.elements).hasLength(CompactHashing.DEFAULT_SIZE); 97 } 98 testAllocArraysExpectedSize()99 public void testAllocArraysExpectedSize() { 100 for (int i = 0; i <= CompactHashing.DEFAULT_SIZE; i++) { 101 CompactHashSet<Integer> set = CompactHashSet.createWithExpectedSize(i); 102 assertThat(set.needsAllocArrays()).isTrue(); 103 assertThat(set.elements).isNull(); 104 105 set.add(1); 106 assertThat(set.needsAllocArrays()).isFalse(); 107 int expectedSize = Math.max(1, i); 108 assertThat(set.elements).hasLength(expectedSize); 109 } 110 } 111 } 112