View Javadoc
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.ZERO;
21  
22  import com.google.common.annotations.GwtCompatible;
23  import com.google.common.collect.HashMultiset;
24  import com.google.common.collect.Multiset;
25  import com.google.common.collect.Multisets;
26  import com.google.common.collect.testing.features.CollectionSize;
27  
28  /**
29   * A generic JUnit test which tests multiset-specific read operations.
30   * Can't be invoked directly; please see
31   * {@link com.google.common.collect.testing.SetTestSuiteBuilder}.
32   *
33   * @author Jared Levy
34   */
35  @GwtCompatible
36  public class MultisetReadsTester<E> extends AbstractMultisetTester<E> {
37  
38    @CollectionSize.Require(absent = ZERO)
39    public void testElementSet_contains() {
40      assertTrue(
41          "multiset.elementSet().contains(present) returned false",
42          getMultiset().elementSet().contains(e0()));
43    }
44  
45    @CollectionSize.Require(absent = ZERO)
46    public void testEntrySet_contains() {
47      assertTrue(
48          "multiset.entrySet() didn't contain [present, 1]",
49          getMultiset().entrySet().contains(Multisets.immutableEntry(e0(), 1)));
50    }
51  
52    public void testEntrySet_contains_count0() {
53      assertFalse(
54          "multiset.entrySet() contains [missing, 0]",
55          getMultiset().entrySet().contains(Multisets.immutableEntry(e3(), 0)));
56    }
57  
58    public void testEntrySet_contains_nonentry() {
59      assertFalse(
60          "multiset.entrySet() contains a non-entry", getMultiset().entrySet().contains(e0()));
61    }
62  
63    public void testEntrySet_twice() {
64      assertEquals(
65          "calling multiset.entrySet() twice returned unequal sets",
66          getMultiset().entrySet(),
67          getMultiset().entrySet());
68    }
69  
70    @CollectionSize.Require(ZERO)
71    public void testEntrySet_hashCode_size0() {
72      assertEquals(
73          "multiset.entrySet() has incorrect hash code", 0, getMultiset().entrySet().hashCode());
74    }
75  
76    @CollectionSize.Require(ONE)
77    public void testEntrySet_hashCode_size1() {
78      assertEquals(
79          "multiset.entrySet() has incorrect hash code",
80          1 ^ e0().hashCode(),
81          getMultiset().entrySet().hashCode());
82    }
83  
84    public void testEquals_yes() {
85      assertTrue(
86          "multiset doesn't equal a multiset with the same elements",
87          getMultiset().equals(HashMultiset.create(getSampleElements())));
88    }
89  
90    public void testEquals_differentSize() {
91      Multiset<E> other = HashMultiset.create(getSampleElements());
92      other.add(e0());
93      assertFalse("multiset equals a multiset with a different size", getMultiset().equals(other));
94    }
95  
96    @CollectionSize.Require(absent = ZERO)
97    public void testEquals_differentElements() {
98      Multiset<E> other = HashMultiset.create(getSampleElements());
99      other.remove(e0());
100     other.add(e3());
101     assertFalse("multiset equals a multiset with different elements", getMultiset().equals(other));
102   }
103 
104   @CollectionSize.Require(ZERO)
105   public void testHashCode_size0() {
106     assertEquals("multiset has incorrect hash code", 0, getMultiset().hashCode());
107   }
108 
109   @CollectionSize.Require(ONE)
110   public void testHashCode_size1() {
111     assertEquals("multiset has incorrect hash code", 1 ^ e0().hashCode(), getMultiset().hashCode());
112   }
113 }