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