ImmutableSetTest.java revision 7dd252788645e940eada959bdde927426e2531c9
1/* 2 * Copyright (C) 2007 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 17package com.google.common.collect; 18 19import com.google.common.annotations.GwtCompatible; 20import com.google.common.annotations.GwtIncompatible; 21import com.google.common.collect.ImmutableSet.Builder; 22import com.google.common.collect.testing.ListTestSuiteBuilder; 23import com.google.common.collect.testing.SetTestSuiteBuilder; 24import com.google.common.collect.testing.features.CollectionFeature; 25import com.google.common.collect.testing.features.CollectionSize; 26import com.google.common.collect.testing.google.SetGenerators.DegeneratedImmutableSetGenerator; 27import com.google.common.collect.testing.google.SetGenerators.ImmutableSetAsListGenerator; 28import com.google.common.collect.testing.google.SetGenerators.ImmutableSetCopyOfGenerator; 29import com.google.common.collect.testing.google.SetGenerators.ImmutableSetWithBadHashesGenerator; 30import com.google.common.testing.EqualsTester; 31import com.google.common.testing.FluentAsserts; 32 33import java.util.Collection; 34import java.util.Collections; 35import java.util.Iterator; 36import java.util.Set; 37 38import junit.framework.Test; 39import junit.framework.TestSuite; 40 41/** 42 * Unit test for {@link ImmutableSet}. 43 * 44 * @author Kevin Bourrillion 45 * @author Jared Levy 46 * @author Nick Kralevich 47 */ 48@GwtCompatible(emulated = true) 49public class ImmutableSetTest extends AbstractImmutableSetTest { 50 51 @GwtIncompatible("suite") 52 public static Test suite() { 53 TestSuite suite = new TestSuite(); 54 55 suite.addTest(SetTestSuiteBuilder.using(new ImmutableSetCopyOfGenerator()) 56 .named(ImmutableSetTest.class.getName()) 57 .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER, 58 CollectionFeature.SERIALIZABLE, 59 CollectionFeature.ALLOWS_NULL_QUERIES) 60 .createTestSuite()); 61 62 suite.addTest(SetTestSuiteBuilder.using( 63 new ImmutableSetWithBadHashesGenerator()) 64 .named(ImmutableSetTest.class.getName() + ", with bad hashes") 65 .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER, 66 CollectionFeature.ALLOWS_NULL_QUERIES) 67 .createTestSuite()); 68 69 suite.addTest(SetTestSuiteBuilder.using( 70 new DegeneratedImmutableSetGenerator()) 71 .named(ImmutableSetTest.class.getName() + ", degenerate") 72 .withFeatures(CollectionSize.ONE, CollectionFeature.KNOWN_ORDER, 73 CollectionFeature.ALLOWS_NULL_QUERIES) 74 .createTestSuite()); 75 76 suite.addTest(ListTestSuiteBuilder.using(new ImmutableSetAsListGenerator()) 77 .named("ImmutableSet.asList") 78 .withFeatures(CollectionSize.ANY, 79 CollectionFeature.REJECTS_DUPLICATES_AT_CREATION, 80 CollectionFeature.SERIALIZABLE, 81 CollectionFeature.ALLOWS_NULL_QUERIES) 82 .createTestSuite()); 83 84 suite.addTestSuite(ImmutableSetTest.class); 85 86 return suite; 87 } 88 89 @Override protected Set<String> of() { 90 return ImmutableSet.of(); 91 } 92 93 @Override protected Set<String> of(String e) { 94 return ImmutableSet.of(e); 95 } 96 97 @Override protected Set<String> of(String e1, String e2) { 98 return ImmutableSet.of(e1, e2); 99 } 100 101 @Override protected Set<String> of(String e1, String e2, String e3) { 102 return ImmutableSet.of(e1, e2, e3); 103 } 104 105 @Override protected Set<String> of( 106 String e1, String e2, String e3, String e4) { 107 return ImmutableSet.of(e1, e2, e3, e4); 108 } 109 110 @Override protected Set<String> of( 111 String e1, String e2, String e3, String e4, String e5) { 112 return ImmutableSet.of(e1, e2, e3, e4, e5); 113 } 114 115 @Override protected Set<String> of(String e1, String e2, String e3, 116 String e4, String e5, String e6, String... rest) { 117 return ImmutableSet.of(e1, e2, e3, e4, e5, e6, rest); 118 } 119 120 @Override protected Set<String> copyOf(String[] elements) { 121 return ImmutableSet.copyOf(elements); 122 } 123 124 @Override protected Set<String> copyOf(Collection<String> elements) { 125 return ImmutableSet.copyOf(elements); 126 } 127 128 @Override protected Set<String> copyOf(Iterable<String> elements) { 129 return ImmutableSet.copyOf(elements); 130 } 131 132 @Override protected Set<String> copyOf(Iterator<String> elements) { 133 return ImmutableSet.copyOf(elements); 134 } 135 136 public void testCreation_allDuplicates() { 137 ImmutableSet<String> set = ImmutableSet.copyOf(Lists.newArrayList("a", "a")); 138 assertTrue(set instanceof SingletonImmutableSet); 139 assertEquals(Lists.newArrayList("a"), Lists.newArrayList(set)); 140 } 141 142 public void testCreation_oneDuplicate() { 143 // now we'll get the varargs overload 144 ImmutableSet<String> set = ImmutableSet.of( 145 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "a"); 146 assertEquals(Lists.newArrayList( 147 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"), 148 Lists.newArrayList(set)); 149 } 150 151 public void testCreation_manyDuplicates() { 152 // now we'll get the varargs overload 153 ImmutableSet<String> set = ImmutableSet.of( 154 "a", "b", "c", "c", "c", "c", "b", "b", "a", "a", "c", "c", "c", "a"); 155 FluentAsserts.assertThat(set).has().allOf("a", "b", "c").inOrder(); 156 } 157 158 public void testCreation_arrayOfArray() { 159 String[] array = new String[] { "a" }; 160 Set<String[]> set = ImmutableSet.<String[]>of(array); 161 assertEquals(Collections.singleton(array), set); 162 } 163 164 @GwtIncompatible("ImmutableSet.chooseTableSize") 165 public void testChooseTableSize() { 166 assertEquals(8, ImmutableSet.chooseTableSize(3)); 167 assertEquals(8, ImmutableSet.chooseTableSize(4)); 168 169 assertEquals(1 << 29, ImmutableSet.chooseTableSize(1 << 28)); 170 assertEquals(1 << 29, ImmutableSet.chooseTableSize(1 << 29 - 1)); 171 172 // Now we hit the cap 173 assertEquals(1 << 30, ImmutableSet.chooseTableSize(1 << 29)); 174 assertEquals(1 << 30, ImmutableSet.chooseTableSize(1 << 30 - 1)); 175 176 // Now we've gone too far 177 try { 178 ImmutableSet.chooseTableSize(1 << 30); 179 fail(); 180 } catch (IllegalArgumentException expected) { 181 } 182 } 183 184 @GwtIncompatible("RegularImmutableSet.table not in emulation") 185 public void testResizeTable() { 186 verifyTableSize(100, 2, 4); 187 verifyTableSize(100, 5, 8); 188 verifyTableSize(100, 33, 64); 189 verifyTableSize(17, 17, 32); 190 verifyTableSize(17, 16, 32); 191 verifyTableSize(17, 15, 32); 192 } 193 194 @GwtIncompatible("RegularImmutableSet.table not in emulation") 195 private void verifyTableSize(int inputSize, int setSize, int tableSize) { 196 Builder<Integer> builder = ImmutableSet.builder(); 197 for (int i = 0; i < inputSize; i++) { 198 builder.add(i % setSize); 199 } 200 ImmutableSet<Integer> set = builder.build(); 201 assertTrue(set instanceof RegularImmutableSet); 202 assertEquals("Input size " + inputSize + " and set size " + setSize, 203 tableSize, ((RegularImmutableSet<Integer>) set).table.length); 204 } 205 206 public void testCopyOf_copiesImmutableSortedSet() { 207 ImmutableSortedSet<String> sortedSet = ImmutableSortedSet.of("a"); 208 ImmutableSet<String> copy = ImmutableSet.copyOf(sortedSet); 209 assertNotSame(sortedSet, copy); 210 } 211 212 @GwtIncompatible("GWT is single threaded") 213 public void testCopyOf_threadSafe() { 214 verifyThreadSafe(); 215 } 216 217 @Override <E extends Comparable<E>> Builder<E> builder() { 218 return ImmutableSet.builder(); 219 } 220 221 @Override int getComplexBuilderSetLastElement() { 222 return LAST_COLOR_ADDED; 223 } 224 225 public void testEquals() { 226 new EqualsTester() 227 .addEqualityGroup(ImmutableSet.of(), ImmutableSet.of()) 228 .addEqualityGroup(ImmutableSet.of(1), ImmutableSet.of(1), ImmutableSet.of(1, 1)) 229 .addEqualityGroup(ImmutableSet.of(1, 2, 1), ImmutableSet.of(2, 1, 1)) 230 .testEquals(); 231 } 232} 233