SetEqualsTester.java revision 3c77433663281544363151bf284b0240dfd22a42
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; 26 27import java.util.Collection; 28import java.util.Set; 29 30/** 31 * Tests {@link java.util.Set#equals}. 32 * 33 * <p>This class is GWT compatible. 34 * 35 * @author George van den Driessche 36 */ 37@GwtCompatible 38public 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 56 @CollectionSize.Require(absent = CollectionSize.ZERO) 57 @CollectionFeature.Require(ALLOWS_NULL_VALUES) 58 public void testEquals_containingNull() { 59 Collection<E> elements = getSampleElements(getNumElements() - 1); 60 elements.add(null); 61 62 collection = getSubjectGenerator().create(elements.toArray()); 63 assertTrue("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("Sets of different sizes should not be equal.", 83 getSet().equals(MinimalSet.from(fewerElements))); 84 } 85 86 public void testEquals_largerSet() { 87 Collection<E> moreElements = getSampleElements(getNumElements() + 1); 88 assertFalse("Sets of different sizes should not be equal.", 89 getSet().equals(MinimalSet.from(moreElements))); 90 } 91 92 public void testEquals_list() { 93 assertFalse("A List should never equal a Set.", 94 getSet().equals(Helpers.copyToList(getSet()))); 95 } 96} 97