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.addTestSuite(FloodingTest.class); 58 suite.addTest( 59 SetTestSuiteBuilder.using( 60 new TestStringSetGenerator() { 61 @Override 62 protected Set<String> create(String[] elements) { 63 return CompactLinkedHashSet.create(Arrays.asList(elements)); 64 } 65 }) 66 .named("CompactLinkedHashSet") 67 .withFeatures(allFeatures) 68 .createTestSuite()); 69 suite.addTest( 70 SetTestSuiteBuilder.using( 71 new TestStringSetGenerator() { 72 @Override 73 protected Set<String> create(String[] elements) { 74 CompactLinkedHashSet<String> set = CompactLinkedHashSet.create(); 75 set.convertToHashFloodingResistantImplementation(); 76 Collections.addAll(set, elements); 77 return set; 78 } 79 }) 80 .named("CompactLinkedHashSet with flooding protection") 81 .withFeatures(allFeatures) 82 .createTestSuite()); 83 return suite; 84 } 85 testAllocArraysDefault()86 public void testAllocArraysDefault() { 87 CompactHashSet<Integer> set = CompactHashSet.create(); 88 assertThat(set.needsAllocArrays()).isTrue(); 89 assertThat(set.elements).isNull(); 90 91 set.add(1); 92 assertThat(set.needsAllocArrays()).isFalse(); 93 assertThat(set.elements).hasLength(CompactHashing.DEFAULT_SIZE); 94 } 95 testAllocArraysExpectedSize()96 public void testAllocArraysExpectedSize() { 97 for (int i = 0; i <= CompactHashing.DEFAULT_SIZE; i++) { 98 CompactHashSet<Integer> set = CompactHashSet.createWithExpectedSize(i); 99 assertThat(set.needsAllocArrays()).isTrue(); 100 assertThat(set.elements).isNull(); 101 102 set.add(1); 103 assertThat(set.needsAllocArrays()).isFalse(); 104 int expectedSize = Math.max(1, i); 105 assertThat(set.elements).hasLength(expectedSize); 106 } 107 } 108 109 public static class FloodingTest extends AbstractHashFloodingTest<Set<Object>> { FloodingTest()110 public FloodingTest() { 111 super( 112 ImmutableList.of(Construction.setFromElements(CompactLinkedHashSet::create)), 113 n -> n * Math.log(n), 114 ImmutableList.of(QueryOp.SET_CONTAINS)); 115 } 116 } 117 } 118