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