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.List; 29 import java.util.Set; 30 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestSuite; 33 34 /** 35 * Tests for CompactLinkedHashSet. 36 * 37 * @author Dimitris Andreou 38 */ 39 @GwtIncompatible // java.util.Arrays#copyOf(Object[], int), java.lang.reflect.Array 40 public class CompactLinkedHashSetTest extends TestCase { suite()41 public static Test suite() { 42 List<Feature<?>> allFeatures = 43 Arrays.<Feature<?>>asList( 44 CollectionSize.ANY, 45 CollectionFeature.ALLOWS_NULL_VALUES, 46 CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION, 47 CollectionFeature.GENERAL_PURPOSE, 48 CollectionFeature.REMOVE_OPERATIONS, 49 CollectionFeature.SERIALIZABLE, 50 CollectionFeature.KNOWN_ORDER, 51 CollectionFeature.SUPPORTS_ADD, 52 CollectionFeature.SUPPORTS_REMOVE); 53 54 TestSuite suite = new TestSuite(); 55 suite.addTestSuite(CompactLinkedHashSetTest.class); 56 suite.addTest( 57 SetTestSuiteBuilder.using( 58 new TestStringSetGenerator() { 59 @Override 60 protected Set<String> create(String[] elements) { 61 return CompactLinkedHashSet.create(Arrays.asList(elements)); 62 } 63 }) 64 .named("CompactLinkedHashSet") 65 .withFeatures(allFeatures) 66 .createTestSuite()); 67 return suite; 68 } 69 testAllocArraysDefault()70 public void testAllocArraysDefault() { 71 CompactHashSet<Integer> set = CompactHashSet.create(); 72 assertThat(set.needsAllocArrays()).isTrue(); 73 assertThat(set.elements).isNull(); 74 75 set.add(1); 76 assertThat(set.needsAllocArrays()).isFalse(); 77 assertThat(set.elements).hasLength(CompactHashing.DEFAULT_SIZE); 78 } 79 testAllocArraysExpectedSize()80 public void testAllocArraysExpectedSize() { 81 for (int i = 0; i <= CompactHashing.DEFAULT_SIZE; i++) { 82 CompactHashSet<Integer> set = CompactHashSet.createWithExpectedSize(i); 83 assertThat(set.needsAllocArrays()).isTrue(); 84 assertThat(set.elements).isNull(); 85 86 set.add(1); 87 assertThat(set.needsAllocArrays()).isFalse(); 88 int expectedSize = Math.max(1, i); 89 assertThat(set.elements).hasLength(expectedSize); 90 } 91 } 92 } 93