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.testing.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20  
21  import com.google.common.annotations.GwtCompatible;
22  import com.google.common.collect.testing.Helpers;
23  import com.google.common.collect.testing.MinimalSet;
24  import com.google.common.collect.testing.features.CollectionFeature;
25  import com.google.common.collect.testing.features.CollectionSize;
26  import java.util.Collection;
27  import java.util.Set;
28  
29  /**
30   * Tests {@link java.util.Set#equals}.
31   *
32   * @author George van den Driessche
33   */
34  @GwtCompatible
35  public class SetEqualsTester<E> extends AbstractSetTester<E> {
36    public void testEquals_otherSetWithSameElements() {
37      assertTrue(
38          "A Set should equal any other Set containing the same elements.",
39          getSet().equals(MinimalSet.from(getSampleElements())));
40    }
41  
42    @CollectionSize.Require(absent = CollectionSize.ZERO)
43    public void testEquals_otherSetWithDifferentElements() {
44      Collection<E> elements = getSampleElements(getNumElements() - 1);
45      elements.add(getSubjectGenerator().samples().e3());
46  
47      assertFalse(
48          "A Set should not equal another Set containing different elements.",
49          getSet().equals(MinimalSet.from(elements)));
50    }
51  
52    @CollectionSize.Require(absent = CollectionSize.ZERO)
53    @CollectionFeature.Require(ALLOWS_NULL_VALUES)
54    public void testEquals_containingNull() {
55      Collection<E> elements = getSampleElements(getNumElements() - 1);
56      elements.add(null);
57  
58      collection = getSubjectGenerator().create(elements.toArray());
59      assertTrue(
60          "A Set should equal any other Set containing the same elements,"
61              + " even if some elements are null.",
62          getSet().equals(MinimalSet.from(elements)));
63    }
64  
65    @CollectionSize.Require(absent = CollectionSize.ZERO)
66    public void testEquals_otherContainsNull() {
67      Collection<E> elements = getSampleElements(getNumElements() - 1);
68      elements.add(null);
69      Set<E> other = MinimalSet.from(elements);
70  
71      assertFalse(
72          "Two Sets should not be equal if exactly one of them contains null.",
73          getSet().equals(other));
74    }
75  
76    @CollectionSize.Require(absent = CollectionSize.ZERO)
77    public void testEquals_smallerSet() {
78      Collection<E> fewerElements = getSampleElements(getNumElements() - 1);
79      assertFalse(
80          "Sets of different sizes should not be equal.",
81          getSet().equals(MinimalSet.from(fewerElements)));
82    }
83  
84    public void testEquals_largerSet() {
85      Collection<E> moreElements = getSampleElements(getNumElements() + 1);
86      assertFalse(
87          "Sets of different sizes should not be equal.",
88          getSet().equals(MinimalSet.from(moreElements)));
89    }
90  
91    public void testEquals_list() {
92      assertFalse("A List should never equal a Set.", getSet().equals(Helpers.copyToList(getSet())));
93    }
94  }