001/*
002 * Copyright (C) 2013 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.Helpers.assertEmpty;
020import static com.google.common.collect.testing.Helpers.copyToList;
021import static com.google.common.collect.testing.Helpers.getMethod;
022import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_QUERIES;
023import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
024import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
025import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
026import static com.google.common.collect.testing.features.CollectionSize.ZERO;
027import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;
028import static java.util.Arrays.asList;
029import static java.util.Collections.singleton;
030
031import com.google.common.annotations.GwtCompatible;
032import com.google.common.annotations.GwtIncompatible;
033import com.google.common.annotations.J2ktIncompatible;
034import com.google.common.collect.testing.WrongType;
035import com.google.common.collect.testing.features.CollectionFeature;
036import com.google.common.collect.testing.features.CollectionSize;
037import java.lang.reflect.Method;
038import java.util.List;
039import org.junit.Ignore;
040
041/**
042 * Tests for {@code Multiset#remove}, {@code Multiset.removeAll}, and {@code Multiset.retainAll} not
043 * already covered by the corresponding Collection testers.
044 *
045 * @author Jared Levy
046 */
047@GwtCompatible(emulated = true)
048@Ignore("test runners must not instantiate and run this directly, only via suites we build")
049// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
050@SuppressWarnings("JUnit4ClassUsedInJUnit3")
051public class MultisetRemoveTester<E> extends AbstractMultisetTester<E> {
052  @CollectionFeature.Require(SUPPORTS_REMOVE)
053  public void testRemoveNegative() {
054    assertThrows(IllegalArgumentException.class, () -> getMultiset().remove(e0(), -1));
055    expectUnchanged();
056  }
057
058  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
059  public void testRemoveUnsupported() {
060    assertThrows(UnsupportedOperationException.class, () -> getMultiset().remove(e0(), 2));
061  }
062
063  @CollectionFeature.Require(SUPPORTS_REMOVE)
064  public void testRemoveZeroNoOp() {
065    int originalCount = getMultiset().count(e0());
066    assertEquals("old count", originalCount, getMultiset().remove(e0(), 0));
067    expectUnchanged();
068  }
069
070  @CollectionSize.Require(absent = ZERO)
071  @CollectionFeature.Require(SUPPORTS_REMOVE)
072  public void testRemove_occurrences_present() {
073    assertEquals(
074        "multiset.remove(present, 2) didn't return the old count",
075        1,
076        getMultiset().remove(e0(), 2));
077    assertFalse(
078        "multiset contains present after multiset.remove(present, 2)",
079        getMultiset().contains(e0()));
080    assertEquals(0, getMultiset().count(e0()));
081  }
082
083  @CollectionSize.Require(SEVERAL)
084  @CollectionFeature.Require(SUPPORTS_REMOVE)
085  public void testRemove_some_occurrences_present() {
086    initThreeCopies();
087    assertEquals(
088        "multiset.remove(present, 2) didn't return the old count",
089        3,
090        getMultiset().remove(e0(), 2));
091    assertTrue(
092        "multiset contains present after multiset.remove(present, 2)",
093        getMultiset().contains(e0()));
094    assertEquals(1, getMultiset().count(e0()));
095  }
096
097  @CollectionFeature.Require(SUPPORTS_REMOVE)
098  public void testRemove_occurrences_absent() {
099    int distinct = getMultiset().elementSet().size();
100    assertEquals("multiset.remove(absent, 0) didn't return 0", 0, getMultiset().remove(e3(), 2));
101    assertEquals(distinct, getMultiset().elementSet().size());
102  }
103
104  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
105  public void testRemove_occurrences_unsupported_absent() {
106    // notice: we don't care whether it succeeds, or fails with UOE
107    try {
108      assertEquals(
109          "multiset.remove(absent, 2) didn't return 0 or throw an exception",
110          0,
111          getMultiset().remove(e3(), 2));
112    } catch (UnsupportedOperationException ok) {
113    }
114  }
115
116  @CollectionFeature.Require(SUPPORTS_REMOVE)
117  public void testRemove_occurrences_0() {
118    int oldCount = getMultiset().count(e0());
119    assertEquals(
120        "multiset.remove(E, 0) didn't return the old count",
121        oldCount,
122        getMultiset().remove(e0(), 0));
123  }
124
125  @CollectionFeature.Require(SUPPORTS_REMOVE)
126  public void testRemove_occurrences_negative() {
127    assertThrows(IllegalArgumentException.class, () -> getMultiset().remove(e0(), -1));
128  }
129
130  @CollectionFeature.Require(SUPPORTS_REMOVE)
131  public void testRemove_occurrences_wrongType() {
132    assertEquals(
133        "multiset.remove(wrongType, 1) didn't return 0",
134        0,
135        getMultiset().remove(WrongType.VALUE, 1));
136  }
137
138  @CollectionSize.Require(absent = ZERO)
139  @CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
140  public void testRemove_nullPresent() {
141    initCollectionWithNullElement();
142    assertEquals(1, getMultiset().remove(null, 2));
143    assertFalse(
144        "multiset contains present after multiset.remove(present, 2)",
145        getMultiset().contains(null));
146    assertEquals(0, getMultiset().count(null));
147  }
148
149  @CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_QUERIES})
150  public void testRemove_nullAbsent() {
151    assertEquals(0, getMultiset().remove(null, 2));
152  }
153
154  @CollectionFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_QUERIES)
155  public void testRemove_nullForbidden() {
156    assertThrows(NullPointerException.class, () -> getMultiset().remove(null, 2));
157  }
158
159  @CollectionSize.Require(SEVERAL)
160  @CollectionFeature.Require(SUPPORTS_REMOVE)
161  public void testRemoveAllIgnoresCount() {
162    initThreeCopies();
163    assertTrue(getMultiset().removeAll(singleton(e0())));
164    assertEmpty(getMultiset());
165  }
166
167  @CollectionSize.Require(SEVERAL)
168  @CollectionFeature.Require(SUPPORTS_REMOVE)
169  public void testRetainAllIgnoresCount() {
170    initThreeCopies();
171    List<E> contents = copyToList(getMultiset());
172    assertFalse(getMultiset().retainAll(singleton(e0())));
173    expectContents(contents);
174  }
175
176  /**
177   * Returns {@link Method} instances for the remove tests that assume multisets support duplicates
178   * so that the test of {@code Multisets.forSet()} can suppress them.
179   */
180  @J2ktIncompatible
181  @GwtIncompatible // reflection
182  public static List<Method> getRemoveDuplicateInitializingMethods() {
183    return asList(getMethod(MultisetRemoveTester.class, "testRemove_some_occurrences_present"));
184  }
185}