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.Helpers.assertEqualIgnoringOrder;
020import static com.google.common.collect.testing.Helpers.getMethod;
021import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
022import static java.util.Arrays.asList;
023import static java.util.Collections.singletonList;
024
025import com.google.common.annotations.GwtCompatible;
026import com.google.common.annotations.GwtIncompatible;
027import com.google.common.annotations.J2ktIncompatible;
028import com.google.common.collect.Multiset.Entry;
029import com.google.common.collect.Multisets;
030import com.google.common.collect.testing.features.CollectionFeature;
031import java.lang.reflect.Method;
032import java.util.ArrayList;
033import java.util.List;
034
035/**
036 * Tests for {@code Multiset#forEachEntry}.
037 *
038 * @author Louis Wasserman
039 */
040@GwtCompatible
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    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 = 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  @J2ktIncompatible
073  @GwtIncompatible // reflection
074  public static List<Method> getForEachEntryDuplicateInitializingMethods() {
075    return asList(getMethod(MultisetForEachEntryTester.class, "testForEachEntryDuplicates"));
076  }
077}