Java

Форк
0
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

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
import 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")
38
public class SetEqualsTester<E> extends AbstractSetTester<E> {
39
  public void testEquals_otherSetWithSameElements() {
40
    assertTrue(
41
        "A Set should equal any other Set containing the same elements.",
42
        getSet().equals(MinimalSet.from(getSampleElements())));
43
  }
44

45
  @CollectionSize.Require(absent = CollectionSize.ZERO)
46
  public void testEquals_otherSetWithDifferentElements() {
47
    Collection<E> elements = getSampleElements(getNumElements() - 1);
48
    elements.add(getSubjectGenerator().samples().e3());
49

50
    assertFalse(
51
        "A Set should not equal another Set containing different elements.",
52
        getSet().equals(MinimalSet.from(elements)));
53
  }
54

55
  @CollectionSize.Require(absent = CollectionSize.ZERO)
56
  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
57
  public void testEquals_containingNull() {
58
    Collection<E> elements = getSampleElements(getNumElements() - 1);
59
    elements.add(null);
60

61
    collection = getSubjectGenerator().create(elements.toArray());
62
    assertTrue(
63
        "A Set should equal any other Set containing the same elements,"
64
            + " even if some elements are null.",
65
        getSet().equals(MinimalSet.from(elements)));
66
  }
67

68
  @CollectionSize.Require(absent = CollectionSize.ZERO)
69
  public void testEquals_otherContainsNull() {
70
    Collection<E> elements = getSampleElements(getNumElements() - 1);
71
    elements.add(null);
72
    Set<E> other = MinimalSet.from(elements);
73

74
    assertFalse(
75
        "Two Sets should not be equal if exactly one of them contains null.",
76
        getSet().equals(other));
77
  }
78

79
  @CollectionSize.Require(absent = CollectionSize.ZERO)
80
  public void testEquals_smallerSet() {
81
    Collection<E> fewerElements = getSampleElements(getNumElements() - 1);
82
    assertFalse(
83
        "Sets of different sizes should not be equal.",
84
        getSet().equals(MinimalSet.from(fewerElements)));
85
  }
86

87
  public void testEquals_largerSet() {
88
    Collection<E> moreElements = getSampleElements(getNumElements() + 1);
89
    assertFalse(
90
        "Sets of different sizes should not be equal.",
91
        getSet().equals(MinimalSet.from(moreElements)));
92
  }
93

94
  public void testEquals_list() {
95
    assertFalse("A List should never equal a Set.", getSet().equals(Helpers.copyToList(getSet())));
96
  }
97
}
98

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.