MultisetReadsTester.java revision 3c77433663281544363151bf284b0240dfd22a42
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 17package com.google.common.collect.testing.google; 18 19import static com.google.common.collect.testing.features.CollectionSize.ONE; 20import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 21import static com.google.common.collect.testing.features.CollectionSize.ZERO; 22 23import com.google.common.annotations.GwtCompatible; 24import com.google.common.annotations.GwtIncompatible; 25import com.google.common.collect.HashMultiset; 26import com.google.common.collect.Multiset; 27import com.google.common.collect.Multisets; 28import com.google.common.collect.testing.Helpers; 29import com.google.common.collect.testing.WrongType; 30import com.google.common.collect.testing.features.CollectionSize; 31 32import java.lang.reflect.Method; 33import java.util.Arrays; 34import java.util.List; 35 36/** 37 * A generic JUnit test which tests multiset-specific read operations. 38 * Can't be invoked directly; please see 39 * {@link com.google.common.collect.testing.SetTestSuiteBuilder}. 40 * 41 * @author Jared Levy 42 */ 43@GwtCompatible(emulated = true) 44public class MultisetReadsTester<E> extends AbstractMultisetTester<E> { 45 public void testCount_0() { 46 assertEquals("multiset.count(missing) didn't return 0", 47 0, getMultiset().count(samples.e3)); 48 } 49 50 @CollectionSize.Require(absent = ZERO) 51 public void testCount_1() { 52 assertEquals("multiset.count(present) didn't return 1", 53 1, getMultiset().count(samples.e0)); 54 } 55 56 @CollectionSize.Require(SEVERAL) 57 public void testCount_3() { 58 initThreeCopies(); 59 assertEquals("multiset.count(thriceContained) didn't return 3", 60 3, getMultiset().count(samples.e0)); 61 } 62 63 public void testCount_null() { 64 assertEquals("multiset.count(null) didn't return 0", 65 0, getMultiset().count(null)); 66 } 67 68 public void testCount_wrongType() { 69 assertEquals("multiset.count(wrongType) didn't return 0", 70 0, getMultiset().count(WrongType.VALUE)); 71 } 72 73 @CollectionSize.Require(absent = ZERO) 74 public void testElementSet_contains() { 75 assertTrue("multiset.elementSet().contains(present) returned false", 76 getMultiset().elementSet().contains(samples.e0)); 77 } 78 79 @CollectionSize.Require(absent = ZERO) 80 public void testEntrySet_contains() { 81 assertTrue("multiset.entrySet() didn't contain [present, 1]", 82 getMultiset().entrySet().contains( 83 Multisets.immutableEntry(samples.e0, 1))); 84 } 85 86 public void testEntrySet_contains_count0() { 87 assertFalse("multiset.entrySet() contains [missing, 0]", 88 getMultiset().entrySet().contains( 89 Multisets.immutableEntry(samples.e3, 0))); 90 } 91 92 public void testEntrySet_contains_nonentry() { 93 assertFalse("multiset.entrySet() contains a non-entry", 94 getMultiset().entrySet().contains(samples.e0)); 95 } 96 97 public void testEntrySet_twice() { 98 assertEquals("calling multiset.entrySet() twice returned unequal sets", 99 getMultiset().entrySet(), getMultiset().entrySet()); 100 } 101 102 @CollectionSize.Require(ZERO) 103 public void testEntrySet_hashCode_size0() { 104 assertEquals("multiset.entrySet() has incorrect hash code", 105 0, getMultiset().entrySet().hashCode()); 106 } 107 108 @CollectionSize.Require(ONE) 109 public void testEntrySet_hashCode_size1() { 110 assertEquals("multiset.entrySet() has incorrect hash code", 111 1 ^ samples.e0.hashCode(), getMultiset().entrySet().hashCode()); 112 } 113 114 public void testEquals_yes() { 115 assertTrue("multiset doesn't equal a multiset with the same elements", 116 getMultiset().equals(HashMultiset.create(getSampleElements()))); 117 } 118 119 public void testEquals_differentSize() { 120 Multiset<E> other = HashMultiset.create(getSampleElements()); 121 other.add(samples.e0); 122 assertFalse("multiset equals a multiset with a different size", 123 getMultiset().equals(other)); 124 } 125 126 @CollectionSize.Require(absent = ZERO) 127 public void testEquals_differentElements() { 128 Multiset<E> other = HashMultiset.create(getSampleElements()); 129 other.remove(samples.e0); 130 other.add(samples.e3); 131 assertFalse("multiset equals a multiset with different elements", 132 getMultiset().equals(other)); 133 } 134 135 @CollectionSize.Require(ZERO) 136 public void testHashCode_size0() { 137 assertEquals("multiset has incorrect hash code", 138 0, getMultiset().hashCode()); 139 } 140 141 @CollectionSize.Require(ONE) 142 public void testHashCode_size1() { 143 assertEquals("multiset has incorrect hash code", 144 1 ^ samples.e0.hashCode(), getMultiset().hashCode()); 145 } 146 147 /** 148 * Returns {@link Method} instances for the read tests that assume multisets 149 * support duplicates so that the test of {@code Multisets.forSet()} can 150 * suppress them. 151 */ 152 @GwtIncompatible("reflection") 153 public static List<Method> getReadsDuplicateInitializingMethods() { 154 return Arrays.asList( 155 Helpers.getMethod(MultisetReadsTester.class, "testCount_3")); 156 } 157} 158