ImmutableSetTest.java revision 1d580d0f6ee4f21eb309ba7b509d2c6d671c4044
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 static org.junit.contrib.truth.Truth.ASSERT;
20
21import com.google.common.annotations.GwtCompatible;
22import com.google.common.annotations.GwtIncompatible;
23import com.google.common.collect.ImmutableSet.Builder;
24import com.google.common.testing.NullPointerTester;
25import com.google.common.testing.SerializableTester;
26
27import java.util.Collection;
28import java.util.Collections;
29import java.util.Iterator;
30import 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)
40public class ImmutableSetTest extends AbstractImmutableSetTest {
41
42  @Override protected Set<String> of() {
43    return ImmutableSet.of();
44  }
45
46  @Override protected Set<String> of(String e) {
47    return ImmutableSet.of(e);
48  }
49
50  @Override protected Set<String> of(String e1, String e2) {
51    return ImmutableSet.of(e1, e2);
52  }
53
54  @Override protected Set<String> of(String e1, String e2, String e3) {
55    return ImmutableSet.of(e1, e2, e3);
56  }
57
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
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
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
73  @Override protected Set<String> copyOf(String[] elements) {
74    return ImmutableSet.copyOf(elements);
75  }
76
77  @Override protected Set<String> copyOf(Collection<String> elements) {
78    return ImmutableSet.copyOf(elements);
79  }
80
81  @Override protected Set<String> copyOf(Iterable<String> elements) {
82    return ImmutableSet.copyOf(elements);
83  }
84
85  @Override protected Set<String> copyOf(Iterator<String> elements) {
86    return ImmutableSet.copyOf(elements);
87  }
88
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
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
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
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")
118  public void testNullPointers() throws Exception {
119    NullPointerTester tester = new NullPointerTester();
120    tester.testAllPublicStaticMethods(ImmutableSet.class);
121  }
122
123  @GwtIncompatible("ImmutableSet.chooseTableSize")
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")
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")
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
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")
172  public void testCopyOf_threadSafe() {
173    verifyThreadSafe();
174  }
175
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")
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
191  @Override <E extends Comparable<E>> Builder<E> builder() {
192    return ImmutableSet.builder();
193  }
194
195  @Override int getComplexBuilderSetLastElement() {
196    return LAST_COLOR_ADDED;
197  }
198}
199