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