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.jspecify.annotations.NullMarked;
032import org.jspecify.annotations.Nullable;
033
034/**
035 * Tester for the {@code size} methods of {@code Multimap} and its views.
036 *
037 * @author Louis Wasserman
038 */
039@GwtCompatible
040@NullMarked
041public class MultimapSizeTester<K extends @Nullable Object, V extends @Nullable Object>
042    extends AbstractMultimapTester<K, V, Multimap<K, V>> {
043
044  public void testSize() {
045    int expectedSize = getNumElements();
046    Multimap<K, V> multimap = multimap();
047    assertEquals(expectedSize, multimap.size());
048
049    int size = 0;
050    for (Entry<K, V> entry : multimap.entries()) {
051      assertTrue(multimap.containsEntry(entry.getKey(), entry.getValue()));
052      size++;
053    }
054    assertEquals(expectedSize, size);
055
056    int size2 = 0;
057    for (Entry<K, Collection<V>> entry2 : multimap.asMap().entrySet()) {
058      size2 += entry2.getValue().size();
059    }
060    assertEquals(expectedSize, size2);
061  }
062
063  @CollectionSize.Require(ZERO)
064  public void testIsEmptyYes() {
065    assertTrue(multimap().isEmpty());
066  }
067
068  @CollectionSize.Require(absent = ZERO)
069  public void testIsEmptyNo() {
070    assertFalse(multimap().isEmpty());
071  }
072
073  @CollectionSize.Require(absent = ZERO)
074  @MapFeature.Require(ALLOWS_NULL_KEYS)
075  public void testSizeNullKey() {
076    initMultimapWithNullKey();
077    assertEquals(getNumElements(), multimap().size());
078    assertFalse(multimap().isEmpty());
079  }
080
081  @CollectionSize.Require(absent = ZERO)
082  @MapFeature.Require(ALLOWS_NULL_VALUES)
083  public void testSizeNullValue() {
084    initMultimapWithNullValue();
085    assertEquals(getNumElements(), multimap().size());
086    assertFalse(multimap().isEmpty());
087  }
088
089  @CollectionSize.Require(absent = ZERO)
090  @MapFeature.Require({ALLOWS_NULL_KEYS, ALLOWS_NULL_VALUES})
091  public void testSizeNullKeyAndValue() {
092    initMultimapWithNullKeyAndValue();
093    assertEquals(getNumElements(), multimap().size());
094    assertFalse(multimap().isEmpty());
095  }
096
097  @CollectionSize.Require(SEVERAL)
098  public void testSizeMultipleValues() {
099    resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k0(), v2()));
100
101    assertEquals(3, multimap().size());
102    assertEquals(3, multimap().entries().size());
103    assertEquals(3, multimap().keys().size());
104
105    assertEquals(1, multimap().keySet().size());
106    assertEquals(1, multimap().asMap().size());
107  }
108}