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 17 package com.google.common.collect.testing.google; 18 19 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD; 20 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE; 21 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE; 22 import static com.google.common.collect.testing.features.CollectionSize.ONE; 23 import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 24 import static com.google.common.collect.testing.features.CollectionSize.ZERO; 25 import static com.google.common.collect.testing.google.MultisetFeature.ENTRIES_ARE_VIEWS; 26 27 import com.google.common.annotations.GwtCompatible; 28 import com.google.common.collect.Iterables; 29 import com.google.common.collect.Multiset; 30 import com.google.common.collect.Multisets; 31 import com.google.common.collect.testing.features.CollectionFeature; 32 import com.google.common.collect.testing.features.CollectionSize; 33 34 import java.util.Collections; 35 import java.util.Iterator; 36 37 /** 38 * Tests for {@code Multiset.entrySet}. 39 * 40 * @author Jared Levy 41 */ 42 @GwtCompatible 43 public class MultisetEntrySetTester<E> extends AbstractMultisetTester<E> { 44 45 @CollectionFeature.Require(SUPPORTS_REMOVE) testEntrySet_clear()46 public void testEntrySet_clear() { 47 getMultiset().entrySet().clear(); 48 assertTrue("multiset not empty after entrySet().clear()", 49 getMultiset().isEmpty()); 50 } 51 52 @CollectionSize.Require(ONE) 53 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) testEntrySet_iteratorRemovePropagates()54 public void testEntrySet_iteratorRemovePropagates() { 55 Iterator<Multiset.Entry<E>> iterator = getMultiset().entrySet().iterator(); 56 assertTrue( 57 "non-empty multiset.entrySet() iterator.hasNext() returned false", 58 iterator.hasNext()); 59 assertEquals("multiset.entrySet() iterator.next() returned incorrect entry", 60 Multisets.immutableEntry(samples.e0, 1), iterator.next()); 61 assertFalse( 62 "size 1 multiset.entrySet() iterator.hasNext() returned true " 63 + "after next()", 64 iterator.hasNext()); 65 iterator.remove(); 66 assertTrue( 67 "multiset isn't empty after multiset.entrySet() iterator.remove()", 68 getMultiset().isEmpty()); 69 } 70 71 @CollectionSize.Require(absent = ZERO) 72 @CollectionFeature.Require(SUPPORTS_REMOVE) testEntrySet_removePresent()73 public void testEntrySet_removePresent() { 74 assertTrue( 75 "multiset.entrySet.remove(presentEntry) returned false", 76 getMultiset().entrySet().remove( 77 Multisets.immutableEntry(samples.e0, 1))); 78 assertFalse( 79 "multiset contains element after removing its entry", 80 getMultiset().contains(samples.e0)); 81 } 82 83 @CollectionSize.Require(absent = ZERO) 84 @CollectionFeature.Require(SUPPORTS_REMOVE) testEntrySet_removeAbsent()85 public void testEntrySet_removeAbsent() { 86 assertFalse( 87 "multiset.entrySet.remove(missingEntry) returned true", 88 getMultiset().entrySet().remove( 89 Multisets.immutableEntry(samples.e0, 2))); 90 assertTrue( 91 "multiset didn't contain element after removing a missing entry", 92 getMultiset().contains(samples.e0)); 93 } 94 95 @CollectionSize.Require(absent = ZERO) 96 @CollectionFeature.Require(SUPPORTS_REMOVE) testEntrySet_removeAllPresent()97 public void testEntrySet_removeAllPresent() { 98 assertTrue( 99 "multiset.entrySet.removeAll(presentEntry) returned false", 100 getMultiset().entrySet().removeAll( 101 Collections.singleton(Multisets.immutableEntry(samples.e0, 1)))); 102 assertFalse( 103 "multiset contains element after removing its entry", 104 getMultiset().contains(samples.e0)); 105 } 106 107 @CollectionSize.Require(absent = ZERO) 108 @CollectionFeature.Require(SUPPORTS_REMOVE) testEntrySet_removeAllAbsent()109 public void testEntrySet_removeAllAbsent() { 110 assertFalse( 111 "multiset.entrySet.remove(missingEntry) returned true", 112 getMultiset().entrySet().removeAll( 113 Collections.singleton(Multisets.immutableEntry(samples.e0, 2)))); 114 assertTrue( 115 "multiset didn't contain element after removing a missing entry", 116 getMultiset().contains(samples.e0)); 117 } 118 119 @CollectionSize.Require(ONE) 120 @CollectionFeature.Require(SUPPORTS_REMOVE) testEntrySet_retainAllPresent()121 public void testEntrySet_retainAllPresent() { 122 assertFalse( 123 "multiset.entrySet.retainAll(presentEntry) returned false", 124 getMultiset().entrySet().retainAll( 125 Collections.singleton(Multisets.immutableEntry(samples.e0, 1)))); 126 assertTrue( 127 "multiset doesn't contains element after retaining its entry", 128 getMultiset().contains(samples.e0)); 129 } 130 131 @CollectionSize.Require(ONE) 132 @CollectionFeature.Require(SUPPORTS_REMOVE) testEntrySet_retainAllAbsent()133 public void testEntrySet_retainAllAbsent() { 134 assertTrue( 135 "multiset.entrySet.retainAll(missingEntry) returned true", 136 getMultiset().entrySet().retainAll( 137 Collections.singleton(Multisets.immutableEntry(samples.e0, 2)))); 138 assertFalse( 139 "multiset contains element after retaining a different entry", 140 getMultiset().contains(samples.e0)); 141 } 142 143 @CollectionSize.Require(SEVERAL) 144 @CollectionFeature.Require(SUPPORTS_REMOVE) 145 @MultisetFeature.Require(ENTRIES_ARE_VIEWS) testEntryViewReflectsRemove()146 public void testEntryViewReflectsRemove() { 147 initThreeCopies(); 148 assertEquals(3, getMultiset().count(samples.e0)); 149 Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet()); 150 assertEquals(3, entry.getCount()); 151 assertTrue(getMultiset().remove(samples.e0)); 152 assertEquals(2, entry.getCount()); 153 assertTrue(getMultiset().elementSet().remove(samples.e0)); 154 assertEquals(0, entry.getCount()); 155 } 156 157 @CollectionSize.Require(SEVERAL) 158 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 159 @MultisetFeature.Require(ENTRIES_ARE_VIEWS) testEntryReflectsIteratorRemove()160 public void testEntryReflectsIteratorRemove() { 161 initThreeCopies(); 162 assertEquals(3, getMultiset().count(samples.e0)); 163 Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet()); 164 assertEquals(3, entry.getCount()); 165 Iterator<E> itr = getMultiset().iterator(); 166 itr.next(); 167 itr.remove(); 168 assertEquals(2, entry.getCount()); 169 itr.next(); 170 itr.remove(); 171 itr.next(); 172 itr.remove(); 173 assertEquals(0, entry.getCount()); 174 } 175 176 @CollectionSize.Require(SEVERAL) 177 @CollectionFeature.Require(SUPPORTS_REMOVE) 178 @MultisetFeature.Require(ENTRIES_ARE_VIEWS) testEntryReflectsClear()179 public void testEntryReflectsClear() { 180 initThreeCopies(); 181 assertEquals(3, getMultiset().count(samples.e0)); 182 Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet()); 183 assertEquals(3, entry.getCount()); 184 getMultiset().clear(); 185 assertEquals(0, entry.getCount()); 186 } 187 188 @CollectionSize.Require(SEVERAL) 189 @CollectionFeature.Require(SUPPORTS_REMOVE) 190 @MultisetFeature.Require(ENTRIES_ARE_VIEWS) testEntryReflectsEntrySetClear()191 public void testEntryReflectsEntrySetClear() { 192 initThreeCopies(); 193 assertEquals(3, getMultiset().count(samples.e0)); 194 Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet()); 195 assertEquals(3, entry.getCount()); 196 getMultiset().entrySet().clear(); 197 assertEquals(0, entry.getCount()); 198 } 199 200 @CollectionSize.Require(SEVERAL) 201 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 202 @MultisetFeature.Require(ENTRIES_ARE_VIEWS) testEntryReflectsEntrySetIteratorRemove()203 public void testEntryReflectsEntrySetIteratorRemove() { 204 initThreeCopies(); 205 assertEquals(3, getMultiset().count(samples.e0)); 206 Iterator<Multiset.Entry<E>> entryItr = getMultiset().entrySet().iterator(); 207 Multiset.Entry<E> entry = entryItr.next(); 208 entryItr.remove(); 209 assertEquals(0, entry.getCount()); 210 } 211 212 @CollectionSize.Require(SEVERAL) 213 @CollectionFeature.Require(SUPPORTS_REMOVE) 214 @MultisetFeature.Require(ENTRIES_ARE_VIEWS) testEntryReflectsElementSetClear()215 public void testEntryReflectsElementSetClear() { 216 initThreeCopies(); 217 assertEquals(3, getMultiset().count(samples.e0)); 218 Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet()); 219 assertEquals(3, entry.getCount()); 220 getMultiset().elementSet().clear(); 221 assertEquals(0, entry.getCount()); 222 } 223 224 @CollectionSize.Require(SEVERAL) 225 @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) 226 @MultisetFeature.Require(ENTRIES_ARE_VIEWS) testEntryReflectsElementSetIteratorRemove()227 public void testEntryReflectsElementSetIteratorRemove() { 228 initThreeCopies(); 229 assertEquals(3, getMultiset().count(samples.e0)); 230 Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet()); 231 assertEquals(3, entry.getCount()); 232 Iterator<E> elementItr = getMultiset().elementSet().iterator(); 233 elementItr.next(); 234 elementItr.remove(); 235 assertEquals(0, entry.getCount()); 236 } 237 238 @CollectionSize.Require(SEVERAL) 239 @CollectionFeature.Require({SUPPORTS_REMOVE, SUPPORTS_ADD}) 240 @MultisetFeature.Require(ENTRIES_ARE_VIEWS) testEntryReflectsRemoveThenAdd()241 public void testEntryReflectsRemoveThenAdd() { 242 initThreeCopies(); 243 assertEquals(3, getMultiset().count(samples.e0)); 244 Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet()); 245 assertEquals(3, entry.getCount()); 246 assertTrue(getMultiset().remove(samples.e0)); 247 assertEquals(2, entry.getCount()); 248 assertTrue(getMultiset().elementSet().remove(samples.e0)); 249 assertEquals(0, entry.getCount()); 250 getMultiset().add(samples.e0, 2); 251 assertEquals(2, entry.getCount()); 252 } 253 testToString()254 public void testToString() { 255 assertEquals(getMultiset().entrySet().toString(), getMultiset().toString()); 256 } 257 } 258