Home | History | Annotate | Download | only in testers
      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 
     27 import java.util.Collection;
     28 import java.util.Set;
     29 
     30 /**
     31  * Tests {@link java.util.Set#equals}.
     32  *
     33  * @author George van den Driessche
     34  */
     35 @GwtCompatible
     36 public class SetEqualsTester<E> extends AbstractSetTester<E> {
     37   public void testEquals_otherSetWithSameElements() {
     38     assertTrue(
     39         "A Set should equal any other Set containing the same elements.",
     40         getSet().equals(MinimalSet.from(getSampleElements())));
     41   }
     42 
     43   @CollectionSize.Require(absent = CollectionSize.ZERO)
     44   public void testEquals_otherSetWithDifferentElements() {
     45     Collection<E> elements = getSampleElements(getNumElements() - 1);
     46     elements.add(getSubjectGenerator().samples().e3);
     47 
     48     assertFalse(
     49         "A Set should not equal another Set containing different elements.",
     50         getSet().equals(MinimalSet.from(elements))
     51     );
     52   }
     53 
     54   @CollectionSize.Require(absent = CollectionSize.ZERO)
     55   @CollectionFeature.Require(ALLOWS_NULL_VALUES)
     56   public void testEquals_containingNull() {
     57     Collection<E> elements = getSampleElements(getNumElements() - 1);
     58     elements.add(null);
     59 
     60     collection = getSubjectGenerator().create(elements.toArray());
     61     assertTrue("A Set should equal any other Set containing the same elements,"
     62         + " even if some elements are null.",
     63         getSet().equals(MinimalSet.from(elements)));
     64   }
     65 
     66   @CollectionSize.Require(absent = CollectionSize.ZERO)
     67   public void testEquals_otherContainsNull() {
     68     Collection<E> elements = getSampleElements(getNumElements() - 1);
     69     elements.add(null);
     70     Set<E> other = MinimalSet.from(elements);
     71 
     72     assertFalse(
     73         "Two Sets should not be equal if exactly one of them contains null.",
     74         getSet().equals(other));
     75   }
     76 
     77   @CollectionSize.Require(absent = CollectionSize.ZERO)
     78   public void testEquals_smallerSet() {
     79     Collection<E> fewerElements = getSampleElements(getNumElements() - 1);
     80     assertFalse("Sets of different sizes should not be equal.",
     81         getSet().equals(MinimalSet.from(fewerElements)));
     82   }
     83 
     84   public void testEquals_largerSet() {
     85     Collection<E> moreElements = getSampleElements(getNumElements() + 1);
     86     assertFalse("Sets of different sizes should not be equal.",
     87         getSet().equals(MinimalSet.from(moreElements)));
     88   }
     89 
     90   public void testEquals_list() {
     91     assertFalse("A List should never equal a Set.",
     92         getSet().equals(Helpers.copyToList(getSet())));
     93   }
     94 }
     95