• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.KNOWN_ORDER;
20 
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.annotations.GwtIncompatible;
23 import com.google.common.collect.Multiset.Entry;
24 import com.google.common.collect.Multisets;
25 import com.google.common.collect.testing.Helpers;
26 import com.google.common.collect.testing.features.CollectionFeature;
27 import java.lang.reflect.Method;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.List;
32 import org.junit.Ignore;
33 
34 /**
35  * Tests for {@code Multiset#forEachEntry}.
36  *
37  * @author Louis Wasserman
38  */
39 @GwtCompatible(emulated = true)
40 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
41 public class MultisetForEachEntryTester<E> extends AbstractMultisetTester<E> {
testForEachEntry()42   public void testForEachEntry() {
43     List<Entry<E>> expected = new ArrayList<>(getMultiset().entrySet());
44     List<Entry<E>> actual = new ArrayList<>();
45     getMultiset()
46         .forEachEntry((element, count) -> actual.add(Multisets.immutableEntry(element, count)));
47     Helpers.assertEqualIgnoringOrder(expected, actual);
48   }
49 
50   @CollectionFeature.Require(KNOWN_ORDER)
testForEachEntryOrdered()51   public void testForEachEntryOrdered() {
52     List<Entry<E>> expected = new ArrayList<>(getMultiset().entrySet());
53     List<Entry<E>> actual = new ArrayList<>();
54     getMultiset()
55         .forEachEntry((element, count) -> actual.add(Multisets.immutableEntry(element, count)));
56     assertEquals(expected, actual);
57   }
58 
testForEachEntryDuplicates()59   public void testForEachEntryDuplicates() {
60     initThreeCopies();
61     List<Entry<E>> expected = Collections.singletonList(Multisets.immutableEntry(e0(), 3));
62     List<Entry<E>> actual = new ArrayList<>();
63     getMultiset()
64         .forEachEntry((element, count) -> actual.add(Multisets.immutableEntry(element, count)));
65     assertEquals(expected, actual);
66   }
67 
68   /**
69    * Returns {@link Method} instances for the remove tests that assume multisets support duplicates
70    * so that the test of {@code Multisets.forSet()} can suppress them.
71    */
72   @GwtIncompatible // reflection
getForEachEntryDuplicateInitializingMethods()73   public static List<Method> getForEachEntryDuplicateInitializingMethods() {
74     return Arrays.asList(
75         Helpers.getMethod(MultisetForEachEntryTester.class, "testForEachEntryDuplicates"));
76   }
77 }
78