View Javadoc
1   /*
2    * Copyright (C) 2008 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.SUPPORTS_ADD;
20  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
21  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
22  import static com.google.common.collect.testing.features.CollectionSize.ONE;
23  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
24  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
25  import static com.google.common.collect.testing.google.MultisetFeature.ENTRIES_ARE_VIEWS;
26  
27  import com.google.common.annotations.GwtCompatible;
28  import com.google.common.collect.Iterables;
29  import com.google.common.collect.Multiset;
30  import com.google.common.collect.Multisets;
31  import com.google.common.collect.testing.features.CollectionFeature;
32  import com.google.common.collect.testing.features.CollectionSize;
33  import java.util.Collections;
34  import java.util.Iterator;
35  
36  /**
37   * Tests for {@code Multiset.entrySet}.
38   *
39   * @author Jared Levy
40   */
41  @GwtCompatible
42  public class MultisetEntrySetTester<E> extends AbstractMultisetTester<E> {
43  
44    @CollectionFeature.Require(SUPPORTS_REMOVE)
45    public void testEntrySet_clear() {
46      getMultiset().entrySet().clear();
47      assertTrue("multiset not empty after entrySet().clear()", getMultiset().isEmpty());
48    }
49  
50    @CollectionSize.Require(ONE)
51    @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
52    public void testEntrySet_iteratorRemovePropagates() {
53      Iterator<Multiset.Entry<E>> iterator = getMultiset().entrySet().iterator();
54      assertTrue(
55          "non-empty multiset.entrySet() iterator.hasNext() returned false", iterator.hasNext());
56      assertEquals(
57          "multiset.entrySet() iterator.next() returned incorrect entry",
58          Multisets.immutableEntry(e0(), 1),
59          iterator.next());
60      assertFalse(
61          "size 1 multiset.entrySet() iterator.hasNext() returned true after next()",
62          iterator.hasNext());
63      iterator.remove();
64      assertTrue(
65          "multiset isn't empty after multiset.entrySet() iterator.remove()",
66          getMultiset().isEmpty());
67    }
68  
69    @CollectionSize.Require(absent = ZERO)
70    @CollectionFeature.Require(SUPPORTS_REMOVE)
71    public void testEntrySet_removePresent() {
72      assertTrue(
73          "multiset.entrySet.remove(presentEntry) returned false",
74          getMultiset().entrySet().remove(Multisets.immutableEntry(e0(), 1)));
75      assertFalse("multiset contains element after removing its entry", getMultiset().contains(e0()));
76    }
77  
78    @CollectionSize.Require(absent = ZERO)
79    @CollectionFeature.Require(SUPPORTS_REMOVE)
80    public void testEntrySet_removeAbsent() {
81      assertFalse(
82          "multiset.entrySet.remove(missingEntry) returned true",
83          getMultiset().entrySet().remove(Multisets.immutableEntry(e0(), 2)));
84      assertTrue(
85          "multiset didn't contain element after removing a missing entry",
86          getMultiset().contains(e0()));
87    }
88  
89    @CollectionSize.Require(absent = ZERO)
90    @CollectionFeature.Require(SUPPORTS_REMOVE)
91    public void testEntrySet_removeAllPresent() {
92      assertTrue(
93          "multiset.entrySet.removeAll(presentEntry) returned false",
94          getMultiset()
95              .entrySet()
96              .removeAll(Collections.singleton(Multisets.immutableEntry(e0(), 1))));
97      assertFalse("multiset contains element after removing its entry", getMultiset().contains(e0()));
98    }
99  
100   @CollectionSize.Require(absent = ZERO)
101   @CollectionFeature.Require(SUPPORTS_REMOVE)
102   public void testEntrySet_removeAllAbsent() {
103     assertFalse(
104         "multiset.entrySet.remove(missingEntry) returned true",
105         getMultiset()
106             .entrySet()
107             .removeAll(Collections.singleton(Multisets.immutableEntry(e0(), 2))));
108     assertTrue(
109         "multiset didn't contain element after removing a missing entry",
110         getMultiset().contains(e0()));
111   }
112 
113   @CollectionSize.Require(ONE)
114   @CollectionFeature.Require(SUPPORTS_REMOVE)
115   public void testEntrySet_retainAllPresent() {
116     assertFalse(
117         "multiset.entrySet.retainAll(presentEntry) returned false",
118         getMultiset()
119             .entrySet()
120             .retainAll(Collections.singleton(Multisets.immutableEntry(e0(), 1))));
121     assertTrue(
122         "multiset doesn't contains element after retaining its entry",
123         getMultiset().contains(e0()));
124   }
125 
126   @CollectionSize.Require(ONE)
127   @CollectionFeature.Require(SUPPORTS_REMOVE)
128   public void testEntrySet_retainAllAbsent() {
129     assertTrue(
130         "multiset.entrySet.retainAll(missingEntry) returned true",
131         getMultiset()
132             .entrySet()
133             .retainAll(Collections.singleton(Multisets.immutableEntry(e0(), 2))));
134     assertFalse(
135         "multiset contains element after retaining a different entry",
136         getMultiset().contains(e0()));
137   }
138 
139   @CollectionSize.Require(SEVERAL)
140   @CollectionFeature.Require(SUPPORTS_REMOVE)
141   @MultisetFeature.Require(ENTRIES_ARE_VIEWS)
142   public void testEntryViewReflectsRemove() {
143     initThreeCopies();
144     assertEquals(3, getMultiset().count(e0()));
145     Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet());
146     assertEquals(3, entry.getCount());
147     assertTrue(getMultiset().remove(e0()));
148     assertEquals(2, entry.getCount());
149     assertTrue(getMultiset().elementSet().remove(e0()));
150     assertEquals(0, entry.getCount());
151   }
152 
153   @CollectionSize.Require(SEVERAL)
154   @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
155   @MultisetFeature.Require(ENTRIES_ARE_VIEWS)
156   public void testEntryReflectsIteratorRemove() {
157     initThreeCopies();
158     assertEquals(3, getMultiset().count(e0()));
159     Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet());
160     assertEquals(3, entry.getCount());
161     Iterator<E> itr = getMultiset().iterator();
162     itr.next();
163     itr.remove();
164     assertEquals(2, entry.getCount());
165     itr.next();
166     itr.remove();
167     itr.next();
168     itr.remove();
169     assertEquals(0, entry.getCount());
170   }
171 
172   @CollectionSize.Require(SEVERAL)
173   @CollectionFeature.Require(SUPPORTS_REMOVE)
174   @MultisetFeature.Require(ENTRIES_ARE_VIEWS)
175   public void testEntryReflectsClear() {
176     initThreeCopies();
177     assertEquals(3, getMultiset().count(e0()));
178     Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet());
179     assertEquals(3, entry.getCount());
180     getMultiset().clear();
181     assertEquals(0, entry.getCount());
182   }
183 
184   @CollectionSize.Require(SEVERAL)
185   @CollectionFeature.Require(SUPPORTS_REMOVE)
186   @MultisetFeature.Require(ENTRIES_ARE_VIEWS)
187   public void testEntryReflectsEntrySetClear() {
188     initThreeCopies();
189     assertEquals(3, getMultiset().count(e0()));
190     Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet());
191     assertEquals(3, entry.getCount());
192     getMultiset().entrySet().clear();
193     assertEquals(0, entry.getCount());
194   }
195 
196   @CollectionSize.Require(SEVERAL)
197   @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
198   @MultisetFeature.Require(ENTRIES_ARE_VIEWS)
199   public void testEntryReflectsEntrySetIteratorRemove() {
200     initThreeCopies();
201     assertEquals(3, getMultiset().count(e0()));
202     Iterator<Multiset.Entry<E>> entryItr = getMultiset().entrySet().iterator();
203     Multiset.Entry<E> entry = entryItr.next();
204     entryItr.remove();
205     assertEquals(0, entry.getCount());
206   }
207 
208   @CollectionSize.Require(SEVERAL)
209   @CollectionFeature.Require(SUPPORTS_REMOVE)
210   @MultisetFeature.Require(ENTRIES_ARE_VIEWS)
211   public void testEntryReflectsElementSetClear() {
212     initThreeCopies();
213     assertEquals(3, getMultiset().count(e0()));
214     Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet());
215     assertEquals(3, entry.getCount());
216     getMultiset().elementSet().clear();
217     assertEquals(0, entry.getCount());
218   }
219 
220   @CollectionSize.Require(SEVERAL)
221   @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
222   @MultisetFeature.Require(ENTRIES_ARE_VIEWS)
223   public void testEntryReflectsElementSetIteratorRemove() {
224     initThreeCopies();
225     assertEquals(3, getMultiset().count(e0()));
226     Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet());
227     assertEquals(3, entry.getCount());
228     Iterator<E> elementItr = getMultiset().elementSet().iterator();
229     elementItr.next();
230     elementItr.remove();
231     assertEquals(0, entry.getCount());
232   }
233 
234   @CollectionSize.Require(SEVERAL)
235   @CollectionFeature.Require({SUPPORTS_REMOVE, SUPPORTS_ADD})
236   @MultisetFeature.Require(ENTRIES_ARE_VIEWS)
237   public void testEntryReflectsRemoveThenAdd() {
238     initThreeCopies();
239     assertEquals(3, getMultiset().count(e0()));
240     Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet());
241     assertEquals(3, entry.getCount());
242     assertTrue(getMultiset().remove(e0()));
243     assertEquals(2, entry.getCount());
244     assertTrue(getMultiset().elementSet().remove(e0()));
245     assertEquals(0, entry.getCount());
246     getMultiset().add(e0(), 2);
247     assertEquals(2, entry.getCount());
248   }
249 
250   public void testToString() {
251     assertEquals(getMultiset().entrySet().toString(), getMultiset().toString());
252   }
253 }