1 /* 2 * Copyright (C) 2013 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.ALLOWS_NULL_QUERIES; 20 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES; 21 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE; 22 import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 23 import static com.google.common.collect.testing.features.CollectionSize.ZERO; 24 import static com.google.common.truth.Truth.assertThat; 25 26 import com.google.common.annotations.GwtCompatible; 27 import com.google.common.annotations.GwtIncompatible; 28 import com.google.common.collect.testing.Helpers; 29 import com.google.common.collect.testing.WrongType; 30 import com.google.common.collect.testing.features.CollectionFeature; 31 import com.google.common.collect.testing.features.CollectionSize; 32 33 import java.lang.reflect.Method; 34 import java.util.Arrays; 35 import java.util.Collections; 36 import java.util.List; 37 38 /** 39 * Tests for {@code Multiset#remove}, {@code Multiset.removeAll}, and {@code Multiset.retainAll} 40 * not already covered by the corresponding Collection testers. 41 * 42 * @author Jared Levy 43 */ 44 @GwtCompatible(emulated = true) 45 public class MultisetRemoveTester<E> extends AbstractMultisetTester<E> { 46 @CollectionFeature.Require(SUPPORTS_REMOVE) testRemoveNegative()47 public void testRemoveNegative() { 48 try { 49 getMultiset().remove(samples.e0, -1); 50 fail("Expected IllegalArgumentException"); 51 } catch (IllegalArgumentException expected) {} 52 expectUnchanged(); 53 } 54 55 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) testRemoveUnsupported()56 public void testRemoveUnsupported() { 57 try { 58 getMultiset().remove(samples.e0, 2); 59 fail("Expected UnsupportedOperationException"); 60 } catch (UnsupportedOperationException expected) {} 61 } 62 63 @CollectionFeature.Require(SUPPORTS_REMOVE) testRemoveZeroNoOp()64 public void testRemoveZeroNoOp() { 65 int originalCount = getMultiset().count(samples.e0); 66 assertEquals("old count", originalCount, getMultiset().remove(samples.e0, 0)); 67 expectUnchanged(); 68 } 69 70 @CollectionSize.Require(absent = ZERO) 71 @CollectionFeature.Require(SUPPORTS_REMOVE) testRemove_occurrences_present()72 public void testRemove_occurrences_present() { 73 assertEquals("multiset.remove(present, 2) didn't return the old count", 74 1, getMultiset().remove(samples.e0, 2)); 75 assertFalse("multiset contains present after multiset.remove(present, 2)", 76 getMultiset().contains(samples.e0)); 77 assertEquals(0, getMultiset().count(samples.e0)); 78 } 79 80 @CollectionSize.Require(SEVERAL) 81 @CollectionFeature.Require(SUPPORTS_REMOVE) testRemove_some_occurrences_present()82 public void testRemove_some_occurrences_present() { 83 initThreeCopies(); 84 assertEquals("multiset.remove(present, 2) didn't return the old count", 85 3, getMultiset().remove(samples.e0, 2)); 86 assertTrue("multiset contains present after multiset.remove(present, 2)", 87 getMultiset().contains(samples.e0)); 88 assertEquals(1, getMultiset().count(samples.e0)); 89 } 90 91 @CollectionFeature.Require(SUPPORTS_REMOVE) testRemove_occurrences_absent()92 public void testRemove_occurrences_absent() { 93 assertEquals("multiset.remove(absent, 0) didn't return 0", 94 0, getMultiset().remove(samples.e3, 2)); 95 } 96 97 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) testRemove_occurrences_unsupported_absent()98 public void testRemove_occurrences_unsupported_absent() { 99 // notice: we don't care whether it succeeds, or fails with UOE 100 try { 101 assertEquals( 102 "multiset.remove(absent, 2) didn't return 0 or throw an exception", 103 0, getMultiset().remove(samples.e3, 2)); 104 } catch (UnsupportedOperationException ok) {} 105 } 106 107 @CollectionFeature.Require(SUPPORTS_REMOVE) testRemove_occurrences_0()108 public void testRemove_occurrences_0() { 109 int oldCount = getMultiset().count(samples.e0); 110 assertEquals("multiset.remove(E, 0) didn't return the old count", 111 oldCount, getMultiset().remove(samples.e0, 0)); 112 } 113 114 @CollectionFeature.Require(SUPPORTS_REMOVE) testRemove_occurrences_negative()115 public void testRemove_occurrences_negative() { 116 try { 117 getMultiset().remove(samples.e0, -1); 118 fail("multiset.remove(E, -1) didn't throw an exception"); 119 } catch (IllegalArgumentException required) {} 120 } 121 122 @CollectionFeature.Require(SUPPORTS_REMOVE) testRemove_occurrences_wrongType()123 public void testRemove_occurrences_wrongType() { 124 assertEquals("multiset.remove(wrongType, 1) didn't return 0", 125 0, getMultiset().remove(WrongType.VALUE, 1)); 126 } 127 128 @CollectionSize.Require(absent = ZERO) 129 @CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUES}) testRemove_nullPresent()130 public void testRemove_nullPresent() { 131 initCollectionWithNullElement(); 132 assertEquals(1, getMultiset().remove(null, 2)); 133 assertFalse("multiset contains present after multiset.remove(present, 2)", 134 getMultiset().contains(null)); 135 assertEquals(0, getMultiset().count(null)); 136 } 137 138 @CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_QUERIES}) testRemove_nullAbsent()139 public void testRemove_nullAbsent() { 140 assertEquals(0, getMultiset().remove(null, 2)); 141 } 142 143 @CollectionFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_QUERIES) testRemove_nullForbidden()144 public void testRemove_nullForbidden() { 145 try { 146 getMultiset().remove(null, 2); 147 fail("Expected NullPointerException"); 148 } catch (NullPointerException expected) {} 149 } 150 151 @CollectionSize.Require(SEVERAL) 152 @CollectionFeature.Require(SUPPORTS_REMOVE) testRemoveAllIgnoresCount()153 public void testRemoveAllIgnoresCount() { 154 initThreeCopies(); 155 assertTrue(getMultiset().removeAll(Collections.singleton(samples.e0))); 156 assertThat(getMultiset()).isEmpty(); 157 } 158 159 @CollectionSize.Require(SEVERAL) 160 @CollectionFeature.Require(SUPPORTS_REMOVE) testRetainAllIgnoresCount()161 public void testRetainAllIgnoresCount() { 162 initThreeCopies(); 163 List<E> contents = Helpers.copyToList(getMultiset()); 164 assertFalse(getMultiset().retainAll(Collections.singleton(samples.e0))); 165 expectContents(contents); 166 } 167 168 /** 169 * Returns {@link Method} instances for the remove tests that assume multisets 170 * support duplicates so that the test of {@code Multisets.forSet()} can 171 * suppress them. 172 */ 173 @GwtIncompatible("reflection") getRemoveDuplicateInitializingMethods()174 public static List<Method> getRemoveDuplicateInitializingMethods() { 175 return Arrays.asList( 176 Helpers.getMethod(MultisetRemoveTester.class, "testRemove_some_occurrences_present")); 177 } 178 } 179