1 /* 2 * Copyright (C) 2007 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; 18 19 import com.google.common.annotations.GwtCompatible; 20 21 /** 22 * Unit tests for {@link HashMultimap}. 23 * 24 * @author Jared Levy 25 */ 26 @GwtCompatible 27 public class HashMultimapTest extends AbstractSetMultimapTest { 28 @Override protected Multimap<String, Integer> create() { 29 return HashMultimap.create(); 30 } 31 32 /* 33 * The behavior of toString() is tested by TreeMultimap, which shares a 34 * lot of code with HashMultimap and has deterministic iteration order. 35 */ 36 public void testCreate() { 37 HashMultimap<String, Integer> multimap = HashMultimap.create(); 38 multimap.put("foo", 1); 39 multimap.put("bar", 2); 40 multimap.put("foo", 3); 41 assertEquals(ImmutableSet.of(1, 3), multimap.get("foo")); 42 assertEquals(8, multimap.expectedValuesPerKey); 43 } 44 45 public void testCreateFromMultimap() { 46 Multimap<String, Integer> multimap = createSample(); 47 HashMultimap<String, Integer> copy = HashMultimap.create(multimap); 48 assertEquals(multimap, copy); 49 assertEquals(8, copy.expectedValuesPerKey); 50 } 51 52 public void testCreateFromSizes() { 53 HashMultimap<String, Integer> multimap = HashMultimap.create(20, 15); 54 multimap.put("foo", 1); 55 multimap.put("bar", 2); 56 multimap.put("foo", 3); 57 assertEquals(ImmutableSet.of(1, 3), multimap.get("foo")); 58 assertEquals(15, multimap.expectedValuesPerKey); 59 } 60 61 public void testCreateFromIllegalSizes() { 62 try { 63 HashMultimap.create(-20, 15); 64 fail(); 65 } catch (IllegalArgumentException expected) {} 66 67 try { 68 HashMultimap.create(20, -15); 69 fail(); 70 } catch (IllegalArgumentException expected) {} 71 } 72 73 public void testEmptyMultimapsEqual() { 74 Multimap<String, Integer> setMultimap = HashMultimap.create(); 75 Multimap<String, Integer> listMultimap = ArrayListMultimap.create(); 76 assertTrue(setMultimap.equals(listMultimap)); 77 assertTrue(listMultimap.equals(setMultimap)); 78 } 79 } 80