Home | History | Annotate | Download | only in google
      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.google;
     18 
     19 import static com.google.common.collect.testing.features.CollectionSize.ONE;
     20 import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
     21 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
     22 
     23 import com.google.common.annotations.GwtCompatible;
     24 import com.google.common.collect.HashMultiset;
     25 import com.google.common.collect.Multiset;
     26 import com.google.common.collect.Multisets;
     27 import com.google.common.collect.testing.WrongType;
     28 import com.google.common.collect.testing.features.CollectionSize;
     29 
     30 import java.lang.reflect.Method;
     31 import java.util.Arrays;
     32 import java.util.List;
     33 
     34 /**
     35  * A generic JUnit test which tests multiset-specific read operations.
     36  * Can't be invoked directly; please see
     37  * {@link com.google.common.collect.testing.SetTestSuiteBuilder}.
     38  *
     39  * @author Jared Levy
     40  */
     41 @GwtCompatible
     42 public class MultisetReadsTester<E> extends AbstractMultisetTester<E> {
     43   public void testCount_0() {
     44     assertEquals("multiset.count(missing) didn't return 0",
     45         0, getMultiset().count(samples.e3));
     46   }
     47 
     48   @CollectionSize.Require(absent = ZERO)
     49   public void testCount_1() {
     50     assertEquals("multiset.count(present) didn't return 1",
     51         1, getMultiset().count(samples.e0));
     52   }
     53 
     54   @CollectionSize.Require(SEVERAL)
     55   public void testCount_3() {
     56     initThreeCopies();
     57     assertEquals("multiset.count(thriceContained) didn't return 3",
     58         3, getMultiset().count(samples.e0));
     59   }
     60 
     61   public void testCount_null() {
     62     assertEquals("multiset.count(null) didn't return 0",
     63         0, getMultiset().count(null));
     64   }
     65 
     66   public void testCount_wrongType() {
     67     assertEquals("multiset.count(wrongType) didn't return 0",
     68         0, getMultiset().count(WrongType.VALUE));
     69   }
     70 
     71   @CollectionSize.Require(absent = ZERO)
     72   public void testElementSet_contains() {
     73     assertTrue("multiset.elementSet().contains(present) returned false",
     74         getMultiset().elementSet().contains(samples.e0));
     75   }
     76 
     77   @CollectionSize.Require(absent = ZERO)
     78   public void testEntrySet_contains() {
     79     assertTrue("multiset.entrySet() didn't contain [present, 1]",
     80         getMultiset().entrySet().contains(
     81             Multisets.immutableEntry(samples.e0, 1)));
     82   }
     83 
     84   public void testEntrySet_contains_count0() {
     85     assertFalse("multiset.entrySet() contains [missing, 0]",
     86         getMultiset().entrySet().contains(
     87             Multisets.immutableEntry(samples.e3, 0)));
     88   }
     89 
     90   public void testEntrySet_contains_nonentry() {
     91     assertFalse("multiset.entrySet() contains a non-entry",
     92         getMultiset().entrySet().contains(samples.e0));
     93   }
     94 
     95   public void testEntrySet_twice() {
     96     assertEquals("calling multiset.entrySet() twice returned unequal sets",
     97         getMultiset().entrySet(), getMultiset().entrySet());
     98   }
     99 
    100   @CollectionSize.Require(ZERO)
    101   public void testEntrySet_hashCode_size0() {
    102     assertEquals("multiset.entrySet() has incorrect hash code",
    103         0, getMultiset().entrySet().hashCode());
    104   }
    105 
    106   @CollectionSize.Require(ONE)
    107   public void testEntrySet_hashCode_size1() {
    108     assertEquals("multiset.entrySet() has incorrect hash code",
    109         1 ^ samples.e0.hashCode(), getMultiset().entrySet().hashCode());
    110   }
    111 
    112   public void testEquals_yes() {
    113     assertTrue("multiset doesn't equal a multiset with the same elements",
    114         getMultiset().equals(HashMultiset.create(getSampleElements())));
    115   }
    116 
    117   public void testEquals_differentSize() {
    118     Multiset<E> other = HashMultiset.create(getSampleElements());
    119     other.add(samples.e0);
    120     assertFalse("multiset equals a multiset with a different size",
    121         getMultiset().equals(other));
    122   }
    123 
    124   @CollectionSize.Require(absent = ZERO)
    125   public void testEquals_differentElements() {
    126     Multiset<E> other = HashMultiset.create(getSampleElements());
    127     other.remove(samples.e0);
    128     other.add(samples.e3);
    129     assertFalse("multiset equals a multiset with different elements",
    130         getMultiset().equals(other));
    131   }
    132 
    133   @CollectionSize.Require(ZERO)
    134   public void testHashCode_size0() {
    135     assertEquals("multiset has incorrect hash code",
    136         0, getMultiset().hashCode());
    137   }
    138 
    139   @CollectionSize.Require(ONE)
    140   public void testHashCode_size1() {
    141     assertEquals("multiset has incorrect hash code",
    142         1 ^ samples.e0.hashCode(), getMultiset().hashCode());
    143   }
    144 
    145   /**
    146    * Returns {@link Method} instances for the read tests that assume multisets
    147    * support duplicates so that the test of {@code Multisets.forSet()} can
    148    * suppress them.
    149    */
    150   public static List<Method> getReadsDuplicateInitializingMethods() {
    151     return Arrays.asList(
    152         Platform.getMethod(MultisetReadsTester.class, "testCount_3"));
    153   }
    154 }
    155