1 /* 2 * Copyright (C) 2013 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.SUPPORTS_ADD; 20 21 import com.google.common.annotations.GwtCompatible; 22 import com.google.common.collect.testing.features.CollectionFeature; 23 24 import java.util.Arrays; 25 import java.util.Collections; 26 27 /** 28 * Tests for {@code Multiset.add}. 29 * 30 * @author Jared Levy 31 */ 32 @GwtCompatible 33 public class MultisetAddTester<E> extends AbstractMultisetTester<E> { 34 @CollectionFeature.Require(absent = SUPPORTS_ADD) testAddUnsupported()35 public void testAddUnsupported() { 36 try { 37 getMultiset().add(samples.e0); 38 fail("Expected UnsupportedOperationException"); 39 } catch (UnsupportedOperationException expected) {} 40 } 41 42 @CollectionFeature.Require(SUPPORTS_ADD) testAddMeansAddOne()43 public void testAddMeansAddOne() { 44 int originalCount = getMultiset().count(samples.e0); 45 assertTrue(getMultiset().add(samples.e0)); 46 assertEquals(originalCount + 1, getMultiset().count(samples.e0)); 47 } 48 49 @CollectionFeature.Require(SUPPORTS_ADD) testAddOccurrencesZero()50 public void testAddOccurrencesZero() { 51 int originalCount = getMultiset().count(samples.e0); 52 assertEquals("old count", originalCount, getMultiset().add(samples.e0, 0)); 53 expectUnchanged(); 54 } 55 56 @CollectionFeature.Require(SUPPORTS_ADD) testAddOccurrences()57 public void testAddOccurrences() { 58 int originalCount = getMultiset().count(samples.e0); 59 assertEquals("old count", originalCount, getMultiset().add(samples.e0, 2)); 60 assertEquals("old count", originalCount + 2, getMultiset().count(samples.e0)); 61 } 62 63 @CollectionFeature.Require(SUPPORTS_ADD) testAddSeveralTimes()64 public void testAddSeveralTimes() { 65 int originalCount = getMultiset().count(samples.e0); 66 assertEquals(originalCount, getMultiset().add(samples.e0, 2)); 67 assertTrue(getMultiset().add(samples.e0)); 68 assertEquals(originalCount + 3, getMultiset().add(samples.e0, 1)); 69 assertEquals(originalCount + 4, getMultiset().count(samples.e0)); 70 } 71 72 @CollectionFeature.Require(absent = SUPPORTS_ADD) testAddOccurrences_unsupported()73 public void testAddOccurrences_unsupported() { 74 try { 75 getMultiset().add(samples.e0, 2); 76 fail("unsupported multiset.add(E, int) didn't throw exception"); 77 } catch (UnsupportedOperationException required) {} 78 } 79 80 @CollectionFeature.Require(SUPPORTS_ADD) testAddOccurrencesNegative()81 public void testAddOccurrencesNegative() { 82 try { 83 getMultiset().add(samples.e0, -1); 84 fail("multiset.add(E, -1) didn't throw an exception"); 85 } catch (IllegalArgumentException required) {} 86 } 87 88 @CollectionFeature.Require(SUPPORTS_ADD) testAddTooMany()89 public void testAddTooMany() { 90 getMultiset().add(samples.e3, Integer.MAX_VALUE); 91 try { 92 getMultiset().add(samples.e3); 93 fail(); 94 } catch (IllegalArgumentException expected) {} 95 assertEquals(Integer.MAX_VALUE, getMultiset().count(samples.e3)); 96 assertEquals(Integer.MAX_VALUE, getMultiset().size()); 97 } 98 99 @CollectionFeature.Require(SUPPORTS_ADD) testAddAll_emptySet()100 public void testAddAll_emptySet() { 101 assertFalse(getMultiset().addAll(Collections.<E>emptySet())); 102 expectUnchanged(); 103 } 104 105 @CollectionFeature.Require(SUPPORTS_ADD) testAddAll_emptyMultiset()106 public void testAddAll_emptyMultiset() { 107 assertFalse(getMultiset().addAll(getSubjectGenerator().create())); 108 expectUnchanged(); 109 } 110 111 @CollectionFeature.Require(SUPPORTS_ADD) testAddAll_nonEmptyList()112 public void testAddAll_nonEmptyList() { 113 assertTrue(getMultiset().addAll(Arrays.asList(samples.e3, samples.e4, samples.e3))); 114 expectAdded(samples.e3, samples.e4, samples.e3); 115 } 116 117 @CollectionFeature.Require(SUPPORTS_ADD) testAddAll_nonEmptyMultiset()118 public void testAddAll_nonEmptyMultiset() { 119 assertTrue(getMultiset().addAll( 120 getSubjectGenerator().create(samples.e3, samples.e4, samples.e3))); 121 expectAdded(samples.e3, samples.e4, samples.e3); 122 } 123 } 124