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