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
17package com.google.common.collect.testing.google;
18
19import static com.google.common.collect.testing.Helpers.mapEntry;
20import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
21import static com.google.common.collect.testing.features.CollectionSize.ZERO;
22import static com.google.common.collect.testing.features.MapFeature.*;
23
24import com.google.common.annotations.GwtCompatible;
25import com.google.common.collect.Multimap;
26import com.google.common.collect.testing.features.CollectionSize;
27import com.google.common.collect.testing.features.MapFeature;
28
29import java.util.Collection;
30import 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
38public class MultimapSizeTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
39
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)
60  public void testIsEmptyYes() {
61    assertTrue(multimap().isEmpty());
62  }
63
64  @CollectionSize.Require(absent = ZERO)
65  public void testIsEmptyNo() {
66    assertFalse(multimap().isEmpty());
67  }
68
69  @CollectionSize.Require(absent = ZERO)
70  @MapFeature.Require(ALLOWS_NULL_KEYS)
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)
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})
87  public void testSizeNullKeyAndValue() {
88    initMultimapWithNullKeyAndValue();
89    assertEquals(getNumElements(), multimap().size());
90    assertFalse(multimap().isEmpty());
91  }
92
93  @CollectionSize.Require(SEVERAL)
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