1 /* 2 * Copyright (C) 2012 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.Helpers.mapEntry; 20 import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 21 import static com.google.common.collect.testing.features.CollectionSize.ZERO; 22 import static com.google.common.collect.testing.features.MapFeature.*; 23 24 import com.google.common.annotations.GwtCompatible; 25 import com.google.common.collect.Multimap; 26 import com.google.common.collect.testing.features.CollectionSize; 27 import com.google.common.collect.testing.features.MapFeature; 28 29 import java.util.Collection; 30 import java.util.Map.Entry; 31 32 /** 33 * Tester for the {@code size} methods of {@code Multimap} and its views. 34 * 35 * @author Louis Wasserman 36 */ 37 @GwtCompatible 38 public class MultimapSizeTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 39 testSize()40 public void testSize() { 41 int expectedSize = getNumElements(); 42 Multimap<K, V> multimap = multimap(); 43 assertEquals(expectedSize, multimap.size()); 44 45 int size = 0; 46 for (Entry<K, V> entry : multimap.entries()) { 47 assertTrue(multimap.containsEntry(entry.getKey(), entry.getValue())); 48 size++; 49 } 50 assertEquals(expectedSize, size); 51 52 int size2 = 0; 53 for (Entry<K, Collection<V>> entry2 : multimap.asMap().entrySet()) { 54 size2 += entry2.getValue().size(); 55 } 56 assertEquals(expectedSize, size2); 57 } 58 59 @CollectionSize.Require(ZERO) testIsEmptyYes()60 public void testIsEmptyYes() { 61 assertTrue(multimap().isEmpty()); 62 } 63 64 @CollectionSize.Require(absent = ZERO) testIsEmptyNo()65 public void testIsEmptyNo() { 66 assertFalse(multimap().isEmpty()); 67 } 68 69 @CollectionSize.Require(absent = ZERO) 70 @MapFeature.Require(ALLOWS_NULL_KEYS) testSizeNullKey()71 public void testSizeNullKey() { 72 initMultimapWithNullKey(); 73 assertEquals(getNumElements(), multimap().size()); 74 assertFalse(multimap().isEmpty()); 75 } 76 77 @CollectionSize.Require(absent = ZERO) 78 @MapFeature.Require(ALLOWS_NULL_VALUES) testSizeNullValue()79 public void testSizeNullValue() { 80 initMultimapWithNullValue(); 81 assertEquals(getNumElements(), multimap().size()); 82 assertFalse(multimap().isEmpty()); 83 } 84 85 @CollectionSize.Require(absent = ZERO) 86 @MapFeature.Require({ALLOWS_NULL_KEYS, ALLOWS_NULL_VALUES}) testSizeNullKeyAndValue()87 public void testSizeNullKeyAndValue() { 88 initMultimapWithNullKeyAndValue(); 89 assertEquals(getNumElements(), multimap().size()); 90 assertFalse(multimap().isEmpty()); 91 } 92 93 @CollectionSize.Require(SEVERAL) testSizeMultipleValues()94 public void testSizeMultipleValues() { 95 resetContainer( 96 mapEntry(sampleKeys().e0, sampleValues().e0), 97 mapEntry(sampleKeys().e0, sampleValues().e1), 98 mapEntry(sampleKeys().e0, sampleValues().e2)); 99 100 assertEquals(3, multimap().size()); 101 assertEquals(3, multimap().entries().size()); 102 assertEquals(3, multimap().keys().size()); 103 104 assertEquals(1, multimap().keySet().size()); 105 assertEquals(1, multimap().asMap().size()); 106 } 107 } 108