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