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 com.google.common.truth.Truth.assertThat; 20 21import com.google.common.annotations.GwtCompatible; 22import com.google.common.collect.ImmutableSet.Builder; 23import com.google.common.testing.EqualsTester; 24 25import java.util.Collection; 26import java.util.Collections; 27import java.util.Iterator; 28import java.util.Set; 29 30/** 31 * Unit test for {@link ImmutableSet}. 32 * 33 * @author Kevin Bourrillion 34 * @author Jared Levy 35 * @author Nick Kralevich 36 */ 37@GwtCompatible(emulated = true) 38public class ImmutableSetTest extends AbstractImmutableSetTest { 39 40 @Override protected Set<String> of() { 41 return ImmutableSet.of(); 42 } 43 44 @Override protected Set<String> of(String e) { 45 return ImmutableSet.of(e); 46 } 47 48 @Override protected Set<String> of(String e1, String e2) { 49 return ImmutableSet.of(e1, e2); 50 } 51 52 @Override protected Set<String> of(String e1, String e2, String e3) { 53 return ImmutableSet.of(e1, e2, e3); 54 } 55 56 @Override protected Set<String> of( 57 String e1, String e2, String e3, String e4) { 58 return ImmutableSet.of(e1, e2, e3, e4); 59 } 60 61 @Override protected Set<String> of( 62 String e1, String e2, String e3, String e4, String e5) { 63 return ImmutableSet.of(e1, e2, e3, e4, e5); 64 } 65 66 @Override protected Set<String> of(String e1, String e2, String e3, 67 String e4, String e5, String e6, String... rest) { 68 return ImmutableSet.of(e1, e2, e3, e4, e5, e6, rest); 69 } 70 71 @Override protected Set<String> copyOf(String[] elements) { 72 return ImmutableSet.copyOf(elements); 73 } 74 75 @Override protected Set<String> copyOf(Collection<String> elements) { 76 return ImmutableSet.copyOf(elements); 77 } 78 79 @Override protected Set<String> copyOf(Iterable<String> elements) { 80 return ImmutableSet.copyOf(elements); 81 } 82 83 @Override protected Set<String> copyOf(Iterator<String> elements) { 84 return ImmutableSet.copyOf(elements); 85 } 86 87 public void testCreation_allDuplicates() { 88 ImmutableSet<String> set = ImmutableSet.copyOf(Lists.newArrayList("a", "a")); 89 assertTrue(set instanceof SingletonImmutableSet); 90 assertEquals(Lists.newArrayList("a"), Lists.newArrayList(set)); 91 } 92 93 public void testCreation_oneDuplicate() { 94 // now we'll get the varargs overload 95 ImmutableSet<String> set = ImmutableSet.of( 96 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "a"); 97 assertEquals(Lists.newArrayList( 98 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"), 99 Lists.newArrayList(set)); 100 } 101 102 public void testCreation_manyDuplicates() { 103 // now we'll get the varargs overload 104 ImmutableSet<String> set = ImmutableSet.of( 105 "a", "b", "c", "c", "c", "c", "b", "b", "a", "a", "c", "c", "c", "a"); 106 assertThat(set).has().exactly("a", "b", "c").inOrder(); 107 } 108 109 public void testCreation_arrayOfArray() { 110 String[] array = new String[] { "a" }; 111 Set<String[]> set = ImmutableSet.<String[]>of(array); 112 assertEquals(Collections.singleton(array), set); 113 } 114 115 public void testCopyOf_copiesImmutableSortedSet() { 116 ImmutableSortedSet<String> sortedSet = ImmutableSortedSet.of("a"); 117 ImmutableSet<String> copy = ImmutableSet.copyOf(sortedSet); 118 assertNotSame(sortedSet, copy); 119 } 120 121 @Override <E extends Comparable<E>> Builder<E> builder() { 122 return ImmutableSet.builder(); 123 } 124 125 @Override int getComplexBuilderSetLastElement() { 126 return LAST_COLOR_ADDED; 127 } 128 129 public void testEquals() { 130 new EqualsTester() 131 .addEqualityGroup(ImmutableSet.of(), ImmutableSet.of()) 132 .addEqualityGroup(ImmutableSet.of(1), ImmutableSet.of(1), ImmutableSet.of(1, 1)) 133 .addEqualityGroup(ImmutableSet.of(1, 2, 1), ImmutableSet.of(2, 1, 1)) 134 .testEquals(); 135 } 136} 137 138