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.
037@SuppressWarnings("JUnit4ClassUsedInJUnit3")
038public class MultisetReadsTester<E> extends AbstractMultisetTester<E> {
039
040  @CollectionSize.Require(absent = ZERO)
041  public void testElementSet_contains() {
042    assertTrue(
043        "multiset.elementSet().contains(present) returned false",
044        getMultiset().elementSet().contains(e0()));
045  }
046
047  @CollectionSize.Require(absent = ZERO)
048  public void testEntrySet_contains() {
049    assertTrue(
050        "multiset.entrySet() didn't contain [present, 1]",
051        getMultiset().entrySet().contains(Multisets.immutableEntry(e0(), 1)));
052  }
053
054  public void testEntrySet_contains_count0() {
055    assertFalse(
056        "multiset.entrySet() contains [missing, 0]",
057        getMultiset().entrySet().contains(Multisets.immutableEntry(e3(), 0)));
058  }
059
060  public void testEntrySet_contains_nonentry() {
061    assertFalse(
062        "multiset.entrySet() contains a non-entry", getMultiset().entrySet().contains(e0()));
063  }
064
065  public void testEntrySet_twice() {
066    assertEquals(
067        "calling multiset.entrySet() twice returned unequal sets",
068        getMultiset().entrySet(),
069        getMultiset().entrySet());
070  }
071
072  @CollectionSize.Require(ZERO)
073  public void testEntrySet_hashCode_size0() {
074    assertEquals(
075        "multiset.entrySet() has incorrect hash code", 0, getMultiset().entrySet().hashCode());
076  }
077
078  @CollectionSize.Require(ONE)
079  public void testEntrySet_hashCode_size1() {
080    assertEquals(
081        "multiset.entrySet() has incorrect hash code",
082        1 ^ e0().hashCode(),
083        getMultiset().entrySet().hashCode());
084  }
085
086  public void testEquals_yes() {
087    assertTrue(
088        "multiset doesn't equal a multiset with the same elements",
089        getMultiset().equals(HashMultiset.create(getSampleElements())));
090  }
091
092  public void testEquals_differentSize() {
093    Multiset<E> other = HashMultiset.create(getSampleElements());
094    other.add(e0());
095    assertFalse("multiset equals a multiset with a different size", getMultiset().equals(other));
096  }
097
098  @CollectionSize.Require(absent = ZERO)
099  public void testEquals_differentElements() {
100    Multiset<E> other = HashMultiset.create(getSampleElements());
101    other.remove(e0());
102    other.add(e3());
103    assertFalse("multiset equals a multiset with different elements", getMultiset().equals(other));
104  }
105
106  @CollectionSize.Require(ZERO)
107  public void testHashCode_size0() {
108    assertEquals("multiset has incorrect hash code", 0, getMultiset().hashCode());
109  }
110
111  @CollectionSize.Require(ONE)
112  public void testHashCode_size1() {
113    assertEquals("multiset has incorrect hash code", 1 ^ e0().hashCode(), getMultiset().hashCode());
114  }
115}