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