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 17 package com.google.common.collect; 18 19 import static org.junit.contrib.truth.Truth.ASSERT; 20 21 import com.google.common.annotations.GwtCompatible; 22 import com.google.common.annotations.GwtIncompatible; 23 import com.google.common.collect.ImmutableSet.Builder; 24 import com.google.common.testing.NullPointerTester; 25 import com.google.common.testing.SerializableTester; 26 27 import java.util.Collection; 28 import java.util.Collections; 29 import java.util.Iterator; 30 import java.util.Set; 31 32 /** 33 * Unit test for {@link ImmutableSet}. 34 * 35 * @author Kevin Bourrillion 36 * @author Jared Levy 37 * @author Nick Kralevich 38 */ 39 @GwtCompatible(emulated = true) 40 public class ImmutableSetTest extends AbstractImmutableSetTest { 41 of()42 @Override protected Set<String> of() { 43 return ImmutableSet.of(); 44 } 45 of(String e)46 @Override protected Set<String> of(String e) { 47 return ImmutableSet.of(e); 48 } 49 of(String e1, String e2)50 @Override protected Set<String> of(String e1, String e2) { 51 return ImmutableSet.of(e1, e2); 52 } 53 of(String e1, String e2, String e3)54 @Override protected Set<String> of(String e1, String e2, String e3) { 55 return ImmutableSet.of(e1, e2, e3); 56 } 57 of( String e1, String e2, String e3, String e4)58 @Override protected Set<String> of( 59 String e1, String e2, String e3, String e4) { 60 return ImmutableSet.of(e1, e2, e3, e4); 61 } 62 of( String e1, String e2, String e3, String e4, String e5)63 @Override protected Set<String> of( 64 String e1, String e2, String e3, String e4, String e5) { 65 return ImmutableSet.of(e1, e2, e3, e4, e5); 66 } 67 of(String e1, String e2, String e3, String e4, String e5, String e6, String... rest)68 @Override protected Set<String> of(String e1, String e2, String e3, 69 String e4, String e5, String e6, String... rest) { 70 return ImmutableSet.of(e1, e2, e3, e4, e5, e6, rest); 71 } 72 copyOf(String[] elements)73 @Override protected Set<String> copyOf(String[] elements) { 74 return ImmutableSet.copyOf(elements); 75 } 76 copyOf(Collection<String> elements)77 @Override protected Set<String> copyOf(Collection<String> elements) { 78 return ImmutableSet.copyOf(elements); 79 } 80 copyOf(Iterable<String> elements)81 @Override protected Set<String> copyOf(Iterable<String> elements) { 82 return ImmutableSet.copyOf(elements); 83 } 84 copyOf(Iterator<String> elements)85 @Override protected Set<String> copyOf(Iterator<String> elements) { 86 return ImmutableSet.copyOf(elements); 87 } 88 testCreation_allDuplicates()89 public void testCreation_allDuplicates() { 90 ImmutableSet<String> set = ImmutableSet.copyOf(Lists.newArrayList("a", "a")); 91 assertTrue(set instanceof SingletonImmutableSet); 92 assertEquals(Lists.newArrayList("a"), Lists.newArrayList(set)); 93 } 94 testCreation_oneDuplicate()95 public void testCreation_oneDuplicate() { 96 // now we'll get the varargs overload 97 ImmutableSet<String> set = ImmutableSet.of( 98 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "a"); 99 assertEquals(Lists.newArrayList( 100 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"), 101 Lists.newArrayList(set)); 102 } 103 testCreation_manyDuplicates()104 public void testCreation_manyDuplicates() { 105 // now we'll get the varargs overload 106 ImmutableSet<String> set = ImmutableSet.of( 107 "a", "b", "c", "c", "c", "c", "b", "b", "a", "a", "c", "c", "c", "a"); 108 ASSERT.that(set).hasContentsInOrder("a", "b", "c"); 109 } 110 testCreation_arrayOfArray()111 public void testCreation_arrayOfArray() { 112 String[] array = new String[] { "a" }; 113 Set<String[]> set = ImmutableSet.<String[]>of(array); 114 assertEquals(Collections.singleton(array), set); 115 } 116 117 @GwtIncompatible("NullPointerTester") testNullPointers()118 public void testNullPointers() throws Exception { 119 NullPointerTester tester = new NullPointerTester(); 120 tester.testAllPublicStaticMethods(ImmutableSet.class); 121 } 122 123 @GwtIncompatible("ImmutableSet.chooseTableSize") testChooseTableSize()124 public void testChooseTableSize() { 125 assertEquals(8, ImmutableSet.chooseTableSize(3)); 126 assertEquals(16, ImmutableSet.chooseTableSize(4)); 127 128 assertEquals(1 << 30, ImmutableSet.chooseTableSize(1 << 28)); 129 assertEquals(1 << 30, ImmutableSet.chooseTableSize(1 << 29 - 1)); 130 131 // Now we hit the cap 132 assertEquals(1 << 30, ImmutableSet.chooseTableSize(1 << 29)); 133 assertEquals(1 << 30, ImmutableSet.chooseTableSize(1 << 30 - 1)); 134 135 // Now we've gone too far 136 try { 137 ImmutableSet.chooseTableSize(1 << 30); 138 fail(); 139 } catch (IllegalArgumentException expected) { 140 } 141 } 142 143 @GwtIncompatible("RegularImmutableSet.table not in emulation") testResizeTable()144 public void testResizeTable() { 145 verifyTableSize(100, 2, 8); 146 verifyTableSize(100, 5, 16); 147 verifyTableSize(100, 33, 256); 148 verifyTableSize(17, 17, 64); 149 verifyTableSize(17, 16, 64); 150 verifyTableSize(17, 15, 64); 151 } 152 153 @GwtIncompatible("RegularImmutableSet.table not in emulation") verifyTableSize(int inputSize, int setSize, int tableSize)154 private void verifyTableSize(int inputSize, int setSize, int tableSize) { 155 Builder<Integer> builder = ImmutableSet.builder(); 156 for (int i = 0; i < inputSize; i++) { 157 builder.add(i % setSize); 158 } 159 ImmutableSet<Integer> set = builder.build(); 160 assertTrue(set instanceof RegularImmutableSet); 161 assertEquals("Input size " + inputSize + " and set size " + setSize, 162 tableSize, ((RegularImmutableSet<Integer>) set).table.length); 163 } 164 testCopyOf_copiesImmutableSortedSet()165 public void testCopyOf_copiesImmutableSortedSet() { 166 ImmutableSortedSet<String> sortedSet = ImmutableSortedSet.of("a"); 167 ImmutableSet<String> copy = ImmutableSet.copyOf(sortedSet); 168 assertNotSame(sortedSet, copy); 169 } 170 171 @GwtIncompatible("GWT is single threaded") testCopyOf_threadSafe()172 public void testCopyOf_threadSafe() { 173 verifyThreadSafe(); 174 } 175 testAsList()176 public void testAsList() { 177 ImmutableSet<String> set = ImmutableSet.of("a", "b", "c", "d", "e"); 178 ImmutableList<String> list = set.asList(); 179 assertEquals(ImmutableList.of("a", "b", "c", "d", "e"), list); 180 } 181 182 @GwtIncompatible("SerializableTester, ImmutableAsList") testAsListReturnTypeAndSerialization()183 public void testAsListReturnTypeAndSerialization() { 184 ImmutableSet<String> set = ImmutableSet.of("a", "b", "c", "d", "e"); 185 ImmutableList<String> list = set.asList(); 186 assertTrue(list instanceof ImmutableAsList); 187 ImmutableList<String> copy = SerializableTester.reserializeAndAssert(list); 188 assertTrue(copy instanceof ImmutableAsList); 189 } 190 builder()191 @Override <E extends Comparable<E>> Builder<E> builder() { 192 return ImmutableSet.builder(); 193 } 194 getComplexBuilderSetLastElement()195 @Override int getComplexBuilderSetLastElement() { 196 return LAST_COLOR_ADDED; 197 } 198 } 199