001/* 002 * Copyright (C) 2008 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing.testers; 018 019import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES; 020 021import com.google.common.annotations.GwtCompatible; 022import com.google.common.collect.testing.Helpers; 023import com.google.common.collect.testing.MinimalSet; 024import com.google.common.collect.testing.features.CollectionFeature; 025import com.google.common.collect.testing.features.CollectionSize; 026import java.util.Collection; 027import java.util.Set; 028 029/** 030 * Tests {@link java.util.Set#equals}. 031 * 032 * @author George van den Driessche 033 */ 034@GwtCompatible 035public class SetEqualsTester<E> extends AbstractSetTester<E> { 036 public void testEquals_otherSetWithSameElements() { 037 assertTrue( 038 "A Set should equal any other Set containing the same elements.", 039 getSet().equals(MinimalSet.from(getSampleElements()))); 040 } 041 042 @CollectionSize.Require(absent = CollectionSize.ZERO) 043 public void testEquals_otherSetWithDifferentElements() { 044 Collection<E> elements = getSampleElements(getNumElements() - 1); 045 elements.add(getSubjectGenerator().samples().e3()); 046 047 assertFalse( 048 "A Set should not equal another Set containing different elements.", 049 getSet().equals(MinimalSet.from(elements))); 050 } 051 052 @CollectionSize.Require(absent = CollectionSize.ZERO) 053 @CollectionFeature.Require(ALLOWS_NULL_VALUES) 054 public void testEquals_containingNull() { 055 Collection<E> elements = getSampleElements(getNumElements() - 1); 056 elements.add(null); 057 058 collection = getSubjectGenerator().create(elements.toArray()); 059 assertTrue( 060 "A Set should equal any other Set containing the same elements," 061 + " even if some elements are null.", 062 getSet().equals(MinimalSet.from(elements))); 063 } 064 065 @CollectionSize.Require(absent = CollectionSize.ZERO) 066 public void testEquals_otherContainsNull() { 067 Collection<E> elements = getSampleElements(getNumElements() - 1); 068 elements.add(null); 069 Set<E> other = MinimalSet.from(elements); 070 071 assertFalse( 072 "Two Sets should not be equal if exactly one of them contains null.", 073 getSet().equals(other)); 074 } 075 076 @CollectionSize.Require(absent = CollectionSize.ZERO) 077 public void testEquals_smallerSet() { 078 Collection<E> fewerElements = getSampleElements(getNumElements() - 1); 079 assertFalse( 080 "Sets of different sizes should not be equal.", 081 getSet().equals(MinimalSet.from(fewerElements))); 082 } 083 084 public void testEquals_largerSet() { 085 Collection<E> moreElements = getSampleElements(getNumElements() + 1); 086 assertFalse( 087 "Sets of different sizes should not be equal.", 088 getSet().equals(MinimalSet.from(moreElements))); 089 } 090 091 public void testEquals_list() { 092 assertFalse("A List should never equal a Set.", getSet().equals(Helpers.copyToList(getSet()))); 093 } 094}