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 CompactLinkedHashSet. 37 * 38 * @author Dimitris Andreou 39 */ 40 @GwtIncompatible // java.util.Arrays#copyOf(Object[], int), java.lang.reflect.Array 41 public class CompactLinkedHashSetTest 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.KNOWN_ORDER, 52 CollectionFeature.SUPPORTS_ADD, 53 CollectionFeature.SUPPORTS_REMOVE); 54 55 TestSuite suite = new TestSuite(); 56 suite.addTestSuite(CompactLinkedHashSetTest.class); 57 suite.addTest( 58 SetTestSuiteBuilder.using( 59 new TestStringSetGenerator() { 60 @Override 61 protected Set<String> create(String[] elements) { 62 return CompactLinkedHashSet.create(Arrays.asList(elements)); 63 } 64 }) 65 .named("CompactLinkedHashSet") 66 .withFeatures(allFeatures) 67 .createTestSuite()); 68 suite.addTest( 69 SetTestSuiteBuilder.using( 70 new TestStringSetGenerator() { 71 @Override 72 protected Set<String> create(String[] elements) { 73 CompactLinkedHashSet<String> set = CompactLinkedHashSet.create(); 74 set.convertToHashFloodingResistantImplementation(); 75 Collections.addAll(set, elements); 76 return set; 77 } 78 }) 79 .named("CompactLinkedHashSet with flooding protection") 80 .withFeatures(allFeatures) 81 .createTestSuite()); 82 return suite; 83 } 84 testAllocArraysDefault()85 public void testAllocArraysDefault() { 86 CompactHashSet<Integer> set = CompactHashSet.create(); 87 assertThat(set.needsAllocArrays()).isTrue(); 88 assertThat(set.elements).isNull(); 89 90 set.add(1); 91 assertThat(set.needsAllocArrays()).isFalse(); 92 assertThat(set.elements).hasLength(CompactHashing.DEFAULT_SIZE); 93 } 94 testAllocArraysExpectedSize()95 public void testAllocArraysExpectedSize() { 96 for (int i = 0; i <= CompactHashing.DEFAULT_SIZE; i++) { 97 CompactHashSet<Integer> set = CompactHashSet.createWithExpectedSize(i); 98 assertThat(set.needsAllocArrays()).isTrue(); 99 assertThat(set.elements).isNull(); 100 101 set.add(1); 102 assertThat(set.needsAllocArrays()).isFalse(); 103 int expectedSize = Math.max(1, i); 104 assertThat(set.elements).hasLength(expectedSize); 105 } 106 } 107 108 } 109