View Javadoc
1   /*
2    * Copyright (C) 2008 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  import static java.util.Arrays.asList;
21  
22  import com.google.common.annotations.GwtCompatible;
23  import com.google.common.collect.testing.MinimalCollection;
24  import com.google.common.collect.testing.MinimalIterable;
25  
26  import junit.framework.TestCase;
27  
28  import java.util.Collection;
29  import java.util.Collections;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Set;
33  
34  /**
35   * Base class for {@link ImmutableSet} and  {@link ImmutableSortedSet} tests.
36   *
37   * @author Kevin Bourrillion
38   * @author Jared Levy
39   */
40  @GwtCompatible(emulated = true)
41  public abstract class AbstractImmutableSetTest extends TestCase {
42  
43    protected abstract Set<String> of();
44    protected abstract Set<String> of(String e);
45    protected abstract Set<String> of(String e1, String e2);
46    protected abstract Set<String> of(String e1, String e2, String e3);
47    protected abstract Set<String> of(String e1, String e2, String e3, String e4);
48    protected abstract Set<String> of(String e1, String e2, String e3, String e4,
49        String e5);
50    protected abstract Set<String> of(String e1, String e2, String e3, String e4,
51        String e5, String e6, String... rest);
52    protected abstract Set<String> copyOf(String[] elements);
53    protected abstract Set<String> copyOf(Collection<String> elements);
54    protected abstract Set<String> copyOf(Iterable<String> elements);
55    protected abstract Set<String> copyOf(Iterator<String> elements);
56  
57    public void testCreation_noArgs() {
58      Set<String> set = of();
59      assertEquals(Collections.<String>emptySet(), set);
60      assertSame(of(), set);
61    }
62  
63    public void testCreation_oneElement() {
64      Set<String> set = of("a");
65      assertEquals(Collections.singleton("a"), set);
66    }
67  
68    public void testCreation_twoElements() {
69      Set<String> set = of("a", "b");
70      assertEquals(Sets.newHashSet("a", "b"), set);
71    }
72  
73    public void testCreation_threeElements() {
74      Set<String> set = of("a", "b", "c");
75      assertEquals(Sets.newHashSet("a", "b", "c"), set);
76    }
77  
78    public void testCreation_fourElements() {
79      Set<String> set = of("a", "b", "c", "d");
80      assertEquals(Sets.newHashSet("a", "b", "c", "d"), set);
81    }
82  
83    public void testCreation_fiveElements() {
84      Set<String> set = of("a", "b", "c", "d", "e");
85      assertEquals(Sets.newHashSet("a", "b", "c", "d", "e"), set);
86    }
87  
88    public void testCreation_sixElements() {
89      Set<String> set = of("a", "b", "c", "d", "e", "f");
90      assertEquals(Sets.newHashSet("a", "b", "c", "d", "e", "f"), set);
91    }
92  
93    public void testCreation_sevenElements() {
94      Set<String> set = of("a", "b", "c", "d", "e", "f", "g");
95      assertEquals(Sets.newHashSet("a", "b", "c", "d", "e", "f", "g"), set);
96    }
97  
98    public void testCreation_eightElements() {
99      Set<String> set = of("a", "b", "c", "d", "e", "f", "g", "h");
100     assertEquals(Sets.newHashSet("a", "b", "c", "d", "e", "f", "g", "h"), set);
101   }
102 
103   public void testCopyOf_emptyArray() {
104     String[] array = new String[0];
105     Set<String> set = copyOf(array);
106     assertEquals(Collections.<String>emptySet(), set);
107     assertSame(of(), set);
108   }
109 
110   public void testCopyOf_arrayOfOneElement() {
111     String[] array = new String[] { "a" };
112     Set<String> set = copyOf(array);
113     assertEquals(Collections.singleton("a"), set);
114   }
115 
116   public void testCopyOf_nullArray() {
117     try {
118       copyOf((String[]) null);
119       fail();
120     } catch (NullPointerException expected) {
121     }
122   }
123 
124   public void testCopyOf_arrayContainingOnlyNull() {
125     String[] array = new String[] { null };
126     try {
127       copyOf(array);
128       fail();
129     } catch (NullPointerException expected) {
130     }
131   }
132 
133   public void testCopyOf_collection_empty() {
134     // "<String>" is required to work around a javac 1.5 bug.
135     Collection<String> c = MinimalCollection.<String>of();
136     Set<String> set = copyOf(c);
137     assertEquals(Collections.<String>emptySet(), set);
138     assertSame(of(), set);
139   }
140 
141   public void testCopyOf_collection_oneElement() {
142     Collection<String> c = MinimalCollection.of("a");
143     Set<String> set = copyOf(c);
144     assertEquals(Collections.singleton("a"), set);
145   }
146 
147   public void testCopyOf_collection_oneElementRepeated() {
148     Collection<String> c = MinimalCollection.of("a", "a", "a");
149     Set<String> set = copyOf(c);
150     assertEquals(Collections.singleton("a"), set);
151   }
152 
153   public void testCopyOf_collection_general() {
154     Collection<String> c = MinimalCollection.of("a", "b", "a");
155     Set<String> set = copyOf(c);
156     assertEquals(2, set.size());
157     assertTrue(set.contains("a"));
158     assertTrue(set.contains("b"));
159   }
160 
161   public void testCopyOf_collectionContainingNull() {
162     Collection<String> c = MinimalCollection.of("a", null, "b");
163     try {
164       copyOf(c);
165       fail();
166     } catch (NullPointerException expected) {
167     }
168   }
169 
170   public void testCopyOf_iterator_empty() {
171     Iterator<String> iterator = Iterators.emptyIterator();
172     Set<String> set = copyOf(iterator);
173     assertEquals(Collections.<String>emptySet(), set);
174     assertSame(of(), set);
175   }
176 
177   public void testCopyOf_iterator_oneElement() {
178     Iterator<String> iterator = Iterators.singletonIterator("a");
179     Set<String> set = copyOf(iterator);
180     assertEquals(Collections.singleton("a"), set);
181   }
182 
183   public void testCopyOf_iterator_oneElementRepeated() {
184     Iterator<String> iterator = Iterators.forArray("a", "a", "a");
185     Set<String> set = copyOf(iterator);
186     assertEquals(Collections.singleton("a"), set);
187   }
188 
189   public void testCopyOf_iterator_general() {
190     Iterator<String> iterator = Iterators.forArray("a", "b", "a");
191     Set<String> set = copyOf(iterator);
192     assertEquals(2, set.size());
193     assertTrue(set.contains("a"));
194     assertTrue(set.contains("b"));
195   }
196 
197   public void testCopyOf_iteratorContainingNull() {
198     Iterator<String> c = Iterators.forArray("a", null, "b");
199     try {
200       copyOf(c);
201       fail();
202     } catch (NullPointerException expected) {
203     }
204   }
205 
206   private static class CountingIterable implements Iterable<String> {
207     int count = 0;
208     @Override
209     public Iterator<String> iterator() {
210       count++;
211       return Iterators.forArray("a", "b", "a");
212     }
213   }
214 
215   public void testCopyOf_plainIterable() {
216     CountingIterable iterable = new CountingIterable();
217     Set<String> set = copyOf(iterable);
218     assertEquals(2, set.size());
219     assertTrue(set.contains("a"));
220     assertTrue(set.contains("b"));
221   }
222 
223   public void testCopyOf_plainIterable_iteratesOnce() {
224     CountingIterable iterable = new CountingIterable();
225     copyOf(iterable);
226     assertEquals(1, iterable.count);
227   }
228 
229   public void testCopyOf_shortcut_empty() {
230     Collection<String> c = of();
231     assertEquals(Collections.<String>emptySet(), copyOf(c));
232     assertSame(c, copyOf(c));
233   }
234 
235   public void testCopyOf_shortcut_singleton() {
236     Collection<String> c = of("a");
237     assertEquals(Collections.singleton("a"), copyOf(c));
238     assertSame(c, copyOf(c));
239   }
240 
241   public void testCopyOf_shortcut_sameType() {
242     Collection<String> c = of("a", "b", "c");
243     assertSame(c, copyOf(c));
244   }
245 
246   public void testToString() {
247     Set<String> set = of("a", "b", "c", "d", "e", "f", "g");
248     assertEquals("[a, b, c, d, e, f, g]", set.toString());
249   }
250 
251   public void testContainsAll_sameType() {
252     Collection<String> c = of("a", "b", "c");
253     assertFalse(c.containsAll(of("a", "b", "c", "d")));
254     assertFalse(c.containsAll(of("a", "d")));
255     assertTrue(c.containsAll(of("a", "c")));
256     assertTrue(c.containsAll(of("a", "b", "c")));
257   }
258 
259   public void testEquals_sameType() {
260     Collection<String> c = of("a", "b", "c");
261     assertTrue(c.equals(of("a", "b", "c")));
262     assertFalse(c.equals(of("a", "b", "d")));
263   }
264 
265   abstract <E extends Comparable<E>> ImmutableSet.Builder<E> builder();
266 
267   public void testBuilderWithNonDuplicateElements() {
268     ImmutableSet<String> set = this.<String>builder()
269         .add("a")
270         .add("b", "c")
271         .add("d", "e", "f")
272         .add("g", "h", "i", "j")
273         .build();
274     assertThat(set).has().exactly(
275         "a", "b", "c", "d", "e", "f", "g", "h", "i", "j").inOrder();
276   }
277 
278   public void testReuseBuilderWithNonDuplicateElements() {
279     ImmutableSet.Builder<String> builder = this.<String>builder()
280         .add("a")
281         .add("b");
282     assertThat(builder.build()).has().exactly("a", "b").inOrder();
283     builder.add("c", "d");
284     assertThat(builder.build()).has().exactly("a", "b", "c", "d").inOrder();
285   }
286 
287   public void testBuilderWithDuplicateElements() {
288     ImmutableSet<String> set = this.<String>builder()
289         .add("a")
290         .add("a", "a")
291         .add("a", "a", "a")
292         .add("a", "a", "a", "a")
293         .build();
294     assertTrue(set.contains("a"));
295     assertFalse(set.contains("b"));
296     assertEquals(1, set.size());
297   }
298 
299   public void testReuseBuilderWithDuplicateElements() {
300     ImmutableSet.Builder<String> builder = this.<String>builder()
301         .add("a")
302         .add("a", "a")
303         .add("b");
304     assertThat(builder.build()).has().exactly("a", "b").inOrder();
305     builder.add("a", "b", "c", "c");
306     assertThat(builder.build()).has().exactly("a", "b", "c").inOrder();
307   }
308 
309   public void testBuilderAddAll() {
310     List<String> a = asList("a", "b", "c");
311     List<String> b = asList("c", "d", "e");
312     ImmutableSet<String> set = this.<String>builder()
313         .addAll(a)
314         .addAll(b)
315         .build();
316     assertThat(set).has().exactly("a", "b", "c", "d", "e").inOrder();
317   }
318 
319   static final int LAST_COLOR_ADDED = 0x00BFFF;
320 
321   public void testComplexBuilder() {
322     List<Integer> colorElem = asList(0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF);
323     // javac won't compile this without "this.<Integer>"
324     ImmutableSet.Builder<Integer> webSafeColorsBuilder
325         = this.<Integer>builder();
326     for (Integer red : colorElem) {
327       for (Integer green : colorElem) {
328         for (Integer blue : colorElem) {
329           webSafeColorsBuilder.add((red << 16) + (green << 8) + blue);
330         }
331       }
332     }
333     ImmutableSet<Integer> webSafeColors = webSafeColorsBuilder.build();
334     assertEquals(216, webSafeColors.size());
335     Integer[] webSafeColorArray =
336         webSafeColors.toArray(new Integer[webSafeColors.size()]);
337     assertEquals(0x000000, (int) webSafeColorArray[0]);
338     assertEquals(0x000033, (int) webSafeColorArray[1]);
339     assertEquals(0x000066, (int) webSafeColorArray[2]);
340     assertEquals(0x003300, (int) webSafeColorArray[6]);
341     assertEquals(0x330000, (int) webSafeColorArray[36]);
342     ImmutableSet<Integer> addedColor
343         = webSafeColorsBuilder.add(LAST_COLOR_ADDED).build();
344     assertEquals(
345         "Modifying the builder should not have changed any already built sets",
346         216, webSafeColors.size());
347     assertEquals("the new array should be one bigger than webSafeColors",
348         217, addedColor.size());
349     Integer[] appendColorArray =
350         addedColor.toArray(new Integer[addedColor.size()]);
351     assertEquals(
352         getComplexBuilderSetLastElement(), (int) appendColorArray[216]);
353   }
354 
355   abstract int getComplexBuilderSetLastElement();
356 
357   public void testBuilderAddHandlesNullsCorrectly() {
358     ImmutableSet.Builder<String> builder = this.<String>builder();
359     try {
360       builder.add((String) null);
361       fail("expected NullPointerException");  // COV_NF_LINE
362     } catch (NullPointerException expected) {
363     }
364 
365     builder = this.<String>builder();
366     try {
367       builder.add((String[]) null);
368       fail("expected NullPointerException");  // COV_NF_LINE
369     } catch (NullPointerException expected) {
370     }
371 
372     builder = this.<String>builder();
373     try {
374       builder.add("a", (String) null);
375       fail("expected NullPointerException");  // COV_NF_LINE
376     } catch (NullPointerException expected) {
377     }
378 
379     builder = this.<String>builder();
380     try {
381       builder.add("a", "b", (String) null);
382       fail("expected NullPointerException");  // COV_NF_LINE
383     } catch (NullPointerException expected) {
384     }
385 
386     builder = this.<String>builder();
387     try {
388       builder.add("a", "b", "c", null);
389       fail("expected NullPointerException");  // COV_NF_LINE
390     } catch (NullPointerException expected) {
391     }
392 
393     builder = this.<String>builder();
394     try {
395       builder.add("a", "b", null, "c");
396       fail("expected NullPointerException");  // COV_NF_LINE
397     } catch (NullPointerException expected) {
398     }
399   }
400 
401   public void testBuilderAddAllHandlesNullsCorrectly() {
402     ImmutableSet.Builder<String> builder = this.<String>builder();
403     try {
404       builder.addAll((Iterable<String>) null);
405       fail("expected NullPointerException");  // COV_NF_LINE
406     } catch (NullPointerException expected) {
407     }
408 
409     try {
410       builder.addAll((Iterator<String>) null);
411       fail("expected NullPointerException");  // COV_NF_LINE
412     } catch (NullPointerException expected) {
413     }
414 
415     builder = this.<String>builder();
416     List<String> listWithNulls = asList("a", null, "b");
417     try {
418       builder.addAll(listWithNulls);
419       fail("expected NullPointerException");  // COV_NF_LINE
420     } catch (NullPointerException expected) {
421     }
422 
423     Iterable<String> iterableWithNulls = MinimalIterable.of("a", null, "b");
424     try {
425       builder.addAll(iterableWithNulls);
426       fail("expected NullPointerException");  // COV_NF_LINE
427     } catch (NullPointerException expected) {
428     }
429   }
430 }
431