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