001/*
002 * Copyright (C) 2012 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.mapEntry;
020import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
021import static com.google.common.collect.testing.features.CollectionSize.ZERO;
022import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
023import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
024
025import com.google.common.annotations.GwtCompatible;
026import com.google.common.collect.Multimap;
027import com.google.common.collect.testing.features.CollectionSize;
028import com.google.common.collect.testing.features.MapFeature;
029import java.util.Collection;
030import java.util.Map.Entry;
031import org.checkerframework.checker.nullness.qual.Nullable;
032import org.junit.Ignore;
033
034/**
035 * Tester for the {@code size} methods of {@code Multimap} and its views.
036 *
037 * @author Louis Wasserman
038 */
039@GwtCompatible
040@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
041@ElementTypesAreNonnullByDefault
042public class MultimapSizeTester<K extends @Nullable Object, V extends @Nullable Object>
043    extends AbstractMultimapTester<K, V, Multimap<K, V>> {
044
045  public void testSize() {
046    int expectedSize = getNumElements();
047    Multimap<K, V> multimap = multimap();
048    assertEquals(expectedSize, multimap.size());
049
050    int size = 0;
051    for (Entry<K, V> entry : multimap.entries()) {
052      assertTrue(multimap.containsEntry(entry.getKey(), entry.getValue()));
053      size++;
054    }
055    assertEquals(expectedSize, size);
056
057    int size2 = 0;
058    for (Entry<K, Collection<V>> entry2 : multimap.asMap().entrySet()) {
059      size2 += entry2.getValue().size();
060    }
061    assertEquals(expectedSize, size2);
062  }
063
064  @CollectionSize.Require(ZERO)
065  public void testIsEmptyYes() {
066    assertTrue(multimap().isEmpty());
067  }
068
069  @CollectionSize.Require(absent = ZERO)
070  public void testIsEmptyNo() {
071    assertFalse(multimap().isEmpty());
072  }
073
074  @CollectionSize.Require(absent = ZERO)
075  @MapFeature.Require(ALLOWS_NULL_KEYS)
076  public void testSizeNullKey() {
077    initMultimapWithNullKey();
078    assertEquals(getNumElements(), multimap().size());
079    assertFalse(multimap().isEmpty());
080  }
081
082  @CollectionSize.Require(absent = ZERO)
083  @MapFeature.Require(ALLOWS_NULL_VALUES)
084  public void testSizeNullValue() {
085    initMultimapWithNullValue();
086    assertEquals(getNumElements(), multimap().size());
087    assertFalse(multimap().isEmpty());
088  }
089
090  @CollectionSize.Require(absent = ZERO)
091  @MapFeature.Require({ALLOWS_NULL_KEYS, ALLOWS_NULL_VALUES})
092  public void testSizeNullKeyAndValue() {
093    initMultimapWithNullKeyAndValue();
094    assertEquals(getNumElements(), multimap().size());
095    assertFalse(multimap().isEmpty());
096  }
097
098  @CollectionSize.Require(SEVERAL)
099  public void testSizeMultipleValues() {
100    resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k0(), v2()));
101
102    assertEquals(3, multimap().size());
103    assertEquals(3, multimap().entries().size());
104    assertEquals(3, multimap().keys().size());
105
106    assertEquals(1, multimap().keySet().size());
107    assertEquals(1, multimap().asMap().size());
108  }
109}