• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.base.Preconditions.checkArgument;
20 import static java.util.Arrays.asList;
21 
22 import com.google.common.annotations.GwtCompatible;
23 import com.google.common.annotations.GwtIncompatible;
24 import com.google.common.annotations.J2ktIncompatible;
25 import com.google.common.collect.testing.SetTestSuiteBuilder;
26 import com.google.common.collect.testing.TestStringSetGenerator;
27 import com.google.common.collect.testing.features.CollectionFeature;
28 import com.google.common.collect.testing.features.CollectionSize;
29 import java.util.HashSet;
30 import java.util.Set;
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34 
35 /**
36  * Unit tests for {@link Sets#union}, {@link Sets#intersection} and {@link Sets#difference}.
37  *
38  * @author Kevin Bourrillion
39  */
40 @GwtCompatible(emulated = true)
41 @ElementTypesAreNonnullByDefault
42 public class SetOperationsTest extends TestCase {
43   @J2ktIncompatible
44   @GwtIncompatible // suite
suite()45   public static Test suite() {
46     TestSuite suite = new TestSuite();
47 
48     suite.addTest(
49         SetTestSuiteBuilder.using(
50                 new TestStringSetGenerator() {
51                   @Override
52                   protected Set<String> create(String[] elements) {
53                     return Sets.union(Sets.<String>newHashSet(), Sets.<String>newHashSet());
54                   }
55                 })
56             .named("empty U empty")
57             .withFeatures(
58                 CollectionSize.ZERO, CollectionFeature.NONE, CollectionFeature.ALLOWS_NULL_VALUES)
59             .createTestSuite());
60 
61     suite.addTest(
62         SetTestSuiteBuilder.using(
63                 new TestStringSetGenerator() {
64                   @Override
65                   protected Set<String> create(String[] elements) {
66                     checkArgument(elements.length == 1);
67                     return Sets.union(Sets.<String>newHashSet(elements), Sets.newHashSet(elements));
68                   }
69                 })
70             .named("singleton U itself")
71             .withFeatures(CollectionSize.ONE, CollectionFeature.ALLOWS_NULL_VALUES)
72             .createTestSuite());
73 
74     suite.addTest(
75         SetTestSuiteBuilder.using(
76                 new TestStringSetGenerator() {
77                   @Override
78                   protected Set<String> create(String[] elements) {
79                     return Sets.union(Sets.<String>newHashSet(), Sets.newHashSet(elements));
80                   }
81                 })
82             .named("empty U set")
83             .withFeatures(
84                 CollectionSize.ONE, CollectionSize.SEVERAL, CollectionFeature.ALLOWS_NULL_VALUES)
85             .createTestSuite());
86 
87     suite.addTest(
88         SetTestSuiteBuilder.using(
89                 new TestStringSetGenerator() {
90                   @Override
91                   protected Set<String> create(String[] elements) {
92                     return Sets.union(Sets.newHashSet(elements), Sets.<String>newHashSet());
93                   }
94                 })
95             .named("set U empty")
96             .withFeatures(
97                 CollectionSize.ONE, CollectionSize.SEVERAL, CollectionFeature.ALLOWS_NULL_VALUES)
98             .createTestSuite());
99 
100     suite.addTest(
101         SetTestSuiteBuilder.using(
102                 new TestStringSetGenerator() {
103                   @Override
104                   protected Set<String> create(String[] elements) {
105                     checkArgument(elements.length == 3);
106                     // Put the sets in different orders for the hell of it
107                     return Sets.union(
108                         Sets.newLinkedHashSet(asList(elements)),
109                         Sets.newLinkedHashSet(asList(elements[1], elements[0], elements[2])));
110                   }
111                 })
112             .named("set U itself")
113             .withFeatures(CollectionSize.SEVERAL, CollectionFeature.ALLOWS_NULL_VALUES)
114             .createTestSuite());
115 
116     suite.addTest(
117         SetTestSuiteBuilder.using(
118                 new TestStringSetGenerator() {
119                   @Override
120                   protected Set<String> create(String[] elements) {
121                     checkArgument(elements.length == 3);
122                     return Sets.union(
123                         Sets.newHashSet(elements[0]), Sets.newHashSet(elements[1], elements[2]));
124                   }
125                 })
126             .named("union of disjoint")
127             .withFeatures(CollectionSize.SEVERAL, CollectionFeature.ALLOWS_NULL_VALUES)
128             .createTestSuite());
129 
130     suite.addTest(
131         SetTestSuiteBuilder.using(
132                 new TestStringSetGenerator() {
133                   @Override
134                   protected Set<String> create(String[] elements) {
135                     return Sets.union(
136                         Sets.<String>newHashSet(elements[0], elements[1]),
137                         Sets.newHashSet(elements[1], elements[2]));
138                   }
139                 })
140             .named("venn")
141             .withFeatures(CollectionSize.SEVERAL, CollectionFeature.ALLOWS_NULL_VALUES)
142             .createTestSuite());
143 
144     suite.addTest(
145         SetTestSuiteBuilder.using(
146                 new TestStringSetGenerator() {
147                   @Override
148                   protected Set<String> create(String[] elements) {
149                     return Sets.intersection(Sets.<String>newHashSet(), Sets.<String>newHashSet());
150                   }
151                 })
152             .named("empty & empty")
153             .withFeatures(
154                 CollectionSize.ZERO, CollectionFeature.NONE, CollectionFeature.ALLOWS_NULL_VALUES)
155             .createTestSuite());
156 
157     suite.addTest(
158         SetTestSuiteBuilder.using(
159                 new TestStringSetGenerator() {
160                   @Override
161                   protected Set<String> create(String[] elements) {
162                     return Sets.intersection(
163                         Sets.<String>newHashSet(), Sets.newHashSet((String) null));
164                   }
165                 })
166             .named("empty & singleton")
167             .withFeatures(
168                 CollectionSize.ZERO, CollectionFeature.NONE, CollectionFeature.ALLOWS_NULL_VALUES)
169             .createTestSuite());
170 
171     suite.addTest(
172         SetTestSuiteBuilder.using(
173                 new TestStringSetGenerator() {
174                   @Override
175                   protected Set<String> create(String[] elements) {
176                     return Sets.intersection(Sets.newHashSet("a", "b"), Sets.newHashSet("c", "d"));
177                   }
178                 })
179             .named("intersection of disjoint")
180             .withFeatures(
181                 CollectionSize.ZERO, CollectionFeature.NONE, CollectionFeature.ALLOWS_NULL_VALUES)
182             .createTestSuite());
183 
184     suite.addTest(
185         SetTestSuiteBuilder.using(
186                 new TestStringSetGenerator() {
187                   @Override
188                   protected Set<String> create(String[] elements) {
189                     return Sets.intersection(Sets.newHashSet(elements), Sets.newHashSet(elements));
190                   }
191                 })
192             .named("set & itself")
193             .withFeatures(
194                 CollectionSize.ONE, CollectionSize.SEVERAL, CollectionFeature.ALLOWS_NULL_VALUES)
195             .createTestSuite());
196 
197     suite.addTest(
198         SetTestSuiteBuilder.using(
199                 new TestStringSetGenerator() {
200                   @Override
201                   protected Set<String> create(String[] elements) {
202                     return Sets.intersection(
203                         Sets.newHashSet("a", elements[0], "b"),
204                         Sets.newHashSet("c", elements[0], "d"));
205                   }
206                 })
207             .named("intersection with overlap of one")
208             .withFeatures(CollectionSize.ONE, CollectionFeature.ALLOWS_NULL_VALUES)
209             .createTestSuite());
210 
211     suite.addTest(
212         SetTestSuiteBuilder.using(
213                 new TestStringSetGenerator() {
214                   @Override
215                   protected Set<String> create(String[] elements) {
216                     return Sets.difference(Sets.<String>newHashSet(), Sets.<String>newHashSet());
217                   }
218                 })
219             .named("empty - empty")
220             .withFeatures(
221                 CollectionSize.ZERO, CollectionFeature.NONE, CollectionFeature.ALLOWS_NULL_VALUES)
222             .createTestSuite());
223 
224     suite.addTest(
225         SetTestSuiteBuilder.using(
226                 new TestStringSetGenerator() {
227                   @Override
228                   protected Set<String> create(String[] elements) {
229                     return Sets.difference(Sets.newHashSet("a"), Sets.newHashSet("a"));
230                   }
231                 })
232             .named("singleton - itself")
233             .withFeatures(
234                 CollectionSize.ZERO, CollectionFeature.NONE, CollectionFeature.ALLOWS_NULL_VALUES)
235             .createTestSuite());
236 
237     suite.addTest(
238         SetTestSuiteBuilder.using(
239                 new TestStringSetGenerator() {
240                   @Override
241                   protected Set<String> create(String[] elements) {
242                     Set<String> set = Sets.newHashSet("b", "c");
243                     Set<String> other = Sets.newHashSet("a", "b", "c", "d");
244                     return Sets.difference(set, other);
245                   }
246                 })
247             .named("set - superset")
248             .withFeatures(
249                 CollectionSize.ZERO, CollectionFeature.NONE, CollectionFeature.ALLOWS_NULL_VALUES)
250             .createTestSuite());
251 
252     suite.addTest(
253         SetTestSuiteBuilder.using(
254                 new TestStringSetGenerator() {
255                   @Override
256                   protected Set<String> create(String[] elements) {
257                     Set<String> set = Sets.newHashSet(elements);
258                     Set<String> other = Sets.newHashSet("wz", "xq");
259                     set.addAll(other);
260                     other.add("pq");
261                     return Sets.difference(set, other);
262                   }
263                 })
264             .named("set - set")
265             .withFeatures(
266                 CollectionSize.ANY,
267                 CollectionFeature.ALLOWS_NULL_VALUES,
268                 CollectionFeature.ALLOWS_NULL_VALUES)
269             .createTestSuite());
270 
271     suite.addTest(
272         SetTestSuiteBuilder.using(
273                 new TestStringSetGenerator() {
274                   @Override
275                   protected Set<String> create(String[] elements) {
276                     return Sets.difference(Sets.newHashSet(elements), Sets.newHashSet());
277                   }
278                 })
279             .named("set - empty")
280             .withFeatures(
281                 CollectionSize.ONE, CollectionSize.SEVERAL, CollectionFeature.ALLOWS_NULL_VALUES)
282             .createTestSuite());
283 
284     suite.addTest(
285         SetTestSuiteBuilder.using(
286                 new TestStringSetGenerator() {
287                   @Override
288                   protected Set<String> create(String[] elements) {
289                     return Sets.difference(
290                         Sets.<String>newHashSet(elements), Sets.newHashSet("xx", "xq"));
291                   }
292                 })
293             .named("set - disjoint")
294             .withFeatures(CollectionSize.ANY, CollectionFeature.ALLOWS_NULL_VALUES)
295             .createTestSuite());
296 
297     suite.addTestSuite(SetOperationsTest.class);
298     return suite;
299   }
300 
301   Set<String> friends;
302   Set<String> enemies;
303 
304   @Override
setUp()305   public void setUp() {
306     friends = Sets.newHashSet("Tom", "Joe", "Dave");
307     enemies = Sets.newHashSet("Dick", "Harry", "Tom");
308   }
309 
testUnion()310   public void testUnion() {
311     Set<String> all = Sets.union(friends, enemies);
312     assertEquals(5, all.size());
313 
314     ImmutableSet<String> immut = Sets.union(friends, enemies).immutableCopy();
315     HashSet<String> mut = Sets.union(friends, enemies).copyInto(new HashSet<String>());
316 
317     enemies.add("Buck");
318     assertEquals(6, all.size());
319     assertEquals(5, immut.size());
320     assertEquals(5, mut.size());
321   }
322 
testIntersection()323   public void testIntersection() {
324     Set<String> friends = Sets.newHashSet("Tom", "Joe", "Dave");
325     Set<String> enemies = Sets.newHashSet("Dick", "Harry", "Tom");
326 
327     Set<String> frenemies = Sets.intersection(friends, enemies);
328     assertEquals(1, frenemies.size());
329 
330     ImmutableSet<String> immut = Sets.intersection(friends, enemies).immutableCopy();
331     HashSet<String> mut = Sets.intersection(friends, enemies).copyInto(new HashSet<String>());
332 
333     enemies.add("Joe");
334     assertEquals(2, frenemies.size());
335     assertEquals(1, immut.size());
336     assertEquals(1, mut.size());
337   }
338 
testDifference()339   public void testDifference() {
340     Set<String> friends = Sets.newHashSet("Tom", "Joe", "Dave");
341     Set<String> enemies = Sets.newHashSet("Dick", "Harry", "Tom");
342 
343     Set<String> goodFriends = Sets.difference(friends, enemies);
344     assertEquals(2, goodFriends.size());
345 
346     ImmutableSet<String> immut = Sets.difference(friends, enemies).immutableCopy();
347     HashSet<String> mut = Sets.difference(friends, enemies).copyInto(new HashSet<String>());
348 
349     enemies.add("Dave");
350     assertEquals(1, goodFriends.size());
351     assertEquals(2, immut.size());
352     assertEquals(2, mut.size());
353   }
354 
testSymmetricDifference()355   public void testSymmetricDifference() {
356     Set<String> friends = Sets.newHashSet("Tom", "Joe", "Dave");
357     Set<String> enemies = Sets.newHashSet("Dick", "Harry", "Tom");
358 
359     Set<String> symmetricDifferenceFriendsFirst = Sets.symmetricDifference(friends, enemies);
360     assertEquals(4, symmetricDifferenceFriendsFirst.size());
361 
362     Set<String> symmetricDifferenceEnemiesFirst = Sets.symmetricDifference(enemies, friends);
363     assertEquals(4, symmetricDifferenceEnemiesFirst.size());
364 
365     assertEquals(symmetricDifferenceFriendsFirst, symmetricDifferenceEnemiesFirst);
366 
367     ImmutableSet<String> immut = Sets.symmetricDifference(friends, enemies).immutableCopy();
368     HashSet<String> mut =
369         Sets.symmetricDifference(friends, enemies).copyInto(new HashSet<String>());
370 
371     enemies.add("Dave");
372     assertEquals(3, symmetricDifferenceFriendsFirst.size());
373     assertEquals(4, immut.size());
374     assertEquals(4, mut.size());
375 
376     immut = Sets.symmetricDifference(enemies, friends).immutableCopy();
377     mut = Sets.symmetricDifference(enemies, friends).copyInto(new HashSet<String>());
378     friends.add("Harry");
379     assertEquals(2, symmetricDifferenceEnemiesFirst.size());
380     assertEquals(3, immut.size());
381     assertEquals(3, mut.size());
382   }
383 }
384