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