Java
97 строк · 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.
37@SuppressWarnings("JUnit4ClassUsedInJUnit3")
38public class SetEqualsTester<E> extends AbstractSetTester<E> {
39public void testEquals_otherSetWithSameElements() {
40assertTrue(
41"A Set should equal any other Set containing the same elements.",
42getSet().equals(MinimalSet.from(getSampleElements())));
43}
44
45@CollectionSize.Require(absent = CollectionSize.ZERO)
46public void testEquals_otherSetWithDifferentElements() {
47Collection<E> elements = getSampleElements(getNumElements() - 1);
48elements.add(getSubjectGenerator().samples().e3());
49
50assertFalse(
51"A Set should not equal another Set containing different elements.",
52getSet().equals(MinimalSet.from(elements)));
53}
54
55@CollectionSize.Require(absent = CollectionSize.ZERO)
56@CollectionFeature.Require(ALLOWS_NULL_VALUES)
57public void testEquals_containingNull() {
58Collection<E> elements = getSampleElements(getNumElements() - 1);
59elements.add(null);
60
61collection = getSubjectGenerator().create(elements.toArray());
62assertTrue(
63"A Set should equal any other Set containing the same elements,"
64+ " even if some elements are null.",
65getSet().equals(MinimalSet.from(elements)));
66}
67
68@CollectionSize.Require(absent = CollectionSize.ZERO)
69public void testEquals_otherContainsNull() {
70Collection<E> elements = getSampleElements(getNumElements() - 1);
71elements.add(null);
72Set<E> other = MinimalSet.from(elements);
73
74assertFalse(
75"Two Sets should not be equal if exactly one of them contains null.",
76getSet().equals(other));
77}
78
79@CollectionSize.Require(absent = CollectionSize.ZERO)
80public void testEquals_smallerSet() {
81Collection<E> fewerElements = getSampleElements(getNumElements() - 1);
82assertFalse(
83"Sets of different sizes should not be equal.",
84getSet().equals(MinimalSet.from(fewerElements)));
85}
86
87public void testEquals_largerSet() {
88Collection<E> moreElements = getSampleElements(getNumElements() + 1);
89assertFalse(
90"Sets of different sizes should not be equal.",
91getSet().equals(MinimalSet.from(moreElements)));
92}
93
94public void testEquals_list() {
95assertFalse("A List should never equal a Set.", getSet().equals(Helpers.copyToList(getSet())));
96}
97}
98