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