001/* 002 * Copyright (C) 2013 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.assertEmpty; 020import static com.google.common.collect.testing.Helpers.getMethod; 021import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD; 022import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE; 023import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 024import static com.google.common.collect.testing.features.CollectionSize.ZERO; 025import static java.util.Arrays.asList; 026import static java.util.Collections.singleton; 027 028import com.google.common.annotations.GwtCompatible; 029import com.google.common.annotations.GwtIncompatible; 030import com.google.common.annotations.J2ktIncompatible; 031import com.google.common.collect.testing.features.CollectionFeature; 032import com.google.common.collect.testing.features.CollectionSize; 033import java.lang.reflect.Method; 034import java.util.List; 035import java.util.Set; 036 037/** 038 * Tests for {@code Multiset.elementSet()} not covered by the derived {@code SetTestSuiteBuilder}. 039 * 040 * @author Louis Wasserman 041 */ 042@GwtCompatible 043public class MultisetElementSetTester<E> extends AbstractMultisetTester<E> { 044 @CollectionFeature.Require(SUPPORTS_ADD) 045 public void testElementSetReflectsAddAbsent() { 046 Set<E> elementSet = getMultiset().elementSet(); 047 assertFalse(elementSet.contains(e3())); 048 getMultiset().add(e3(), 4); 049 assertTrue(elementSet.contains(e3())); 050 } 051 052 @CollectionSize.Require(absent = ZERO) 053 @CollectionFeature.Require(SUPPORTS_REMOVE) 054 public void testElementSetReflectsRemove() { 055 Set<E> elementSet = getMultiset().elementSet(); 056 assertTrue(elementSet.contains(e0())); 057 getMultiset().removeAll(singleton(e0())); 058 assertFalse(elementSet.contains(e0())); 059 } 060 061 @CollectionSize.Require(absent = ZERO) 062 @CollectionFeature.Require(SUPPORTS_REMOVE) 063 public void testElementSetRemovePropagatesToMultiset() { 064 Set<E> elementSet = getMultiset().elementSet(); 065 int size = getNumElements(); 066 int expectedSize = size - getMultiset().count(e0()); 067 assertTrue(elementSet.remove(e0())); 068 assertFalse(getMultiset().contains(e0())); 069 assertEquals(expectedSize, getMultiset().size()); 070 } 071 072 @CollectionSize.Require(SEVERAL) 073 @CollectionFeature.Require(SUPPORTS_REMOVE) 074 public void testElementSetRemoveDuplicatePropagatesToMultiset() { 075 initThreeCopies(); 076 int size = getNumElements(); 077 int expectedSize = size - getMultiset().count(e0()); 078 Set<E> elementSet = getMultiset().elementSet(); 079 assertTrue(elementSet.remove(e0())); 080 assertEmpty(getMultiset()); 081 assertEquals(expectedSize, getMultiset().size()); 082 } 083 084 @CollectionFeature.Require(SUPPORTS_REMOVE) 085 public void testElementSetRemoveAbsent() { 086 Set<E> elementSet = getMultiset().elementSet(); 087 assertFalse(elementSet.remove(e3())); 088 expectUnchanged(); 089 } 090 091 @CollectionFeature.Require(SUPPORTS_REMOVE) 092 public void testElementSetClear() { 093 getMultiset().elementSet().clear(); 094 assertEmpty(getMultiset()); 095 } 096 097 /** 098 * Returns {@link Method} instances for the read tests that assume multisets support duplicates so 099 * that the test of {@code Multisets.forSet()} can suppress them. 100 */ 101 @J2ktIncompatible 102 @GwtIncompatible // reflection 103 public static List<Method> getElementSetDuplicateInitializingMethods() { 104 return asList( 105 getMethod( 106 MultisetElementSetTester.class, "testElementSetRemoveDuplicatePropagatesToMultiset")); 107 } 108}