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.google;
018
019import static com.google.common.collect.testing.features.CollectionSize.ONE;
020import static com.google.common.collect.testing.features.CollectionSize.ZERO;
021
022import com.google.common.annotations.GwtCompatible;
023import com.google.common.collect.HashMultiset;
024import com.google.common.collect.Multiset;
025import com.google.common.collect.Multisets;
026import com.google.common.collect.testing.features.CollectionSize;
027
028/**
029 * A generic JUnit test which tests multiset-specific read operations. Can't be invoked directly;
030 * please see {@link com.google.common.collect.testing.SetTestSuiteBuilder}.
031 *
032 * @author Jared Levy
033 */
034@GwtCompatible
035public class MultisetReadsTester<E> extends AbstractMultisetTester<E> {
036
037  @CollectionSize.Require(absent = ZERO)
038  public void testElementSet_contains() {
039    assertTrue(
040        "multiset.elementSet().contains(present) returned false",
041        getMultiset().elementSet().contains(e0()));
042  }
043
044  @CollectionSize.Require(absent = ZERO)
045  public void testEntrySet_contains() {
046    assertTrue(
047        "multiset.entrySet() didn't contain [present, 1]",
048        getMultiset().entrySet().contains(Multisets.immutableEntry(e0(), 1)));
049  }
050
051  public void testEntrySet_contains_count0() {
052    assertFalse(
053        "multiset.entrySet() contains [missing, 0]",
054        getMultiset().entrySet().contains(Multisets.immutableEntry(e3(), 0)));
055  }
056
057  public void testEntrySet_contains_nonentry() {
058    assertFalse(
059        "multiset.entrySet() contains a non-entry", getMultiset().entrySet().contains(e0()));
060  }
061
062  public void testEntrySet_twice() {
063    assertEquals(
064        "calling multiset.entrySet() twice returned unequal sets",
065        getMultiset().entrySet(),
066        getMultiset().entrySet());
067  }
068
069  @CollectionSize.Require(ZERO)
070  public void testEntrySet_hashCode_size0() {
071    assertEquals(
072        "multiset.entrySet() has incorrect hash code", 0, getMultiset().entrySet().hashCode());
073  }
074
075  @CollectionSize.Require(ONE)
076  public void testEntrySet_hashCode_size1() {
077    assertEquals(
078        "multiset.entrySet() has incorrect hash code",
079        1 ^ e0().hashCode(),
080        getMultiset().entrySet().hashCode());
081  }
082
083  public void testEquals_yes() {
084    assertTrue(
085        "multiset doesn't equal a multiset with the same elements",
086        getMultiset().equals(HashMultiset.create(getSampleElements())));
087  }
088
089  public void testEquals_differentSize() {
090    Multiset<E> other = HashMultiset.create(getSampleElements());
091    other.add(e0());
092    assertFalse("multiset equals a multiset with a different size", getMultiset().equals(other));
093  }
094
095  @CollectionSize.Require(absent = ZERO)
096  public void testEquals_differentElements() {
097    Multiset<E> other = HashMultiset.create(getSampleElements());
098    other.remove(e0());
099    other.add(e3());
100    assertFalse("multiset equals a multiset with different elements", getMultiset().equals(other));
101  }
102
103  @CollectionSize.Require(ZERO)
104  public void testHashCode_size0() {
105    assertEquals("multiset has incorrect hash code", 0, getMultiset().hashCode());
106  }
107
108  @CollectionSize.Require(ONE)
109  public void testHashCode_size1() {
110    assertEquals("multiset has incorrect hash code", 1 ^ e0().hashCode(), getMultiset().hashCode());
111  }
112}