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