001/*
002 * Copyright (C) 2016 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.features.CollectionFeature.KNOWN_ORDER;
020
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.annotations.GwtIncompatible;
023import com.google.common.collect.Multiset.Entry;
024import com.google.common.collect.Multisets;
025import com.google.common.collect.testing.Helpers;
026import com.google.common.collect.testing.features.CollectionFeature;
027import java.lang.reflect.Method;
028import java.util.ArrayList;
029import java.util.Arrays;
030import java.util.Collections;
031import java.util.List;
032import org.junit.Ignore;
033
034/**
035 * Tests for {@code Multiset#forEachEntry}.
036 *
037 * @author Louis Wasserman
038 */
039@GwtCompatible(emulated = true)
040@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
041public class MultisetForEachEntryTester<E> extends AbstractMultisetTester<E> {
042  public void testForEachEntry() {
043    List<Entry<E>> expected = new ArrayList<>(getMultiset().entrySet());
044    List<Entry<E>> actual = new ArrayList<>();
045    getMultiset()
046        .forEachEntry((element, count) -> actual.add(Multisets.immutableEntry(element, count)));
047    Helpers.assertEqualIgnoringOrder(expected, actual);
048  }
049
050  @CollectionFeature.Require(KNOWN_ORDER)
051  public void testForEachEntryOrdered() {
052    List<Entry<E>> expected = new ArrayList<>(getMultiset().entrySet());
053    List<Entry<E>> actual = new ArrayList<>();
054    getMultiset()
055        .forEachEntry((element, count) -> actual.add(Multisets.immutableEntry(element, count)));
056    assertEquals(expected, actual);
057  }
058
059  public void testForEachEntryDuplicates() {
060    initThreeCopies();
061    List<Entry<E>> expected = Collections.singletonList(Multisets.immutableEntry(e0(), 3));
062    List<Entry<E>> actual = new ArrayList<>();
063    getMultiset()
064        .forEachEntry((element, count) -> actual.add(Multisets.immutableEntry(element, count)));
065    assertEquals(expected, actual);
066  }
067
068  /**
069   * Returns {@link Method} instances for the remove tests that assume multisets support duplicates
070   * so that the test of {@code Multisets.forSet()} can suppress them.
071   */
072  @GwtIncompatible // reflection
073  public static List<Method> getForEachEntryDuplicateInitializingMethods() {
074    return Arrays.asList(
075        Helpers.getMethod(MultisetForEachEntryTester.class, "testForEachEntryDuplicates"));
076  }
077}