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
17package com.google.common.collect;
18
19import com.google.common.annotations.GwtCompatible;
20
21import java.util.IdentityHashMap;
22import java.util.Iterator;
23import java.util.Map;
24import java.util.Map.Entry;
25import java.util.Set;
26
27/**
28 * Tests for {@link HashBiMap}.
29 *
30 * @author Mike Bostock
31 */
32@GwtCompatible
33public class HashBiMapTest extends AbstractBiMapTest {
34
35  @Override protected BiMap<Integer, String> create() {
36    return HashBiMap.create();
37  }
38
39  public void testCreate() {
40    BiMap<String, String> bimap = HashBiMap.create();
41    assertEquals(0, bimap.size());
42    bimap.put("canada", "dollar");
43    assertEquals("dollar", bimap.get("canada"));
44    assertEquals("canada", bimap.inverse().get("dollar"));
45  }
46
47  public void testMapConstructor() {
48    /* Test with non-empty Map. */
49    Map<String, String> map = ImmutableMap.of(
50        "canada", "dollar",
51        "chile", "peso",
52        "switzerland", "franc");
53    HashBiMap<String, String> bimap = HashBiMap.create(map);
54    assertEquals("dollar", bimap.get("canada"));
55    assertEquals("canada", bimap.inverse().get("dollar"));
56  }
57
58  private static final int N = 1000;
59
60  public void testBashIt() throws Exception {
61    BiMap<Integer, Integer> bimap = HashBiMap.create(N);
62    BiMap<Integer, Integer> inverse = bimap.inverse();
63
64    for (int i = 0; i < N; i++) {
65      assertNull(bimap.put(2 * i, 2 * i + 1));
66    }
67    for (int i = 0; i < N; i++) {
68      assertEquals(2 * i + 1, (int) bimap.get(2 * i));
69    }
70    for (int i = 0; i < N; i++) {
71      assertEquals(2 * i, (int) inverse.get(2 * i + 1));
72    }
73    for (int i = 0; i < N; i++) {
74      int oldValue = bimap.get(2 * i);
75      assertEquals(2 * i + 1, (int) bimap.put(2 * i, oldValue - 2));
76    }
77    for (int i = 0; i < N; i++) {
78      assertEquals(2 * i - 1, (int) bimap.get(2 * i));
79    }
80    for (int i = 0; i < N; i++) {
81      assertEquals(2 * i, (int) inverse.get(2 * i - 1));
82    }
83    Set<Entry<Integer, Integer>> entries = bimap.entrySet();
84    for (Entry<Integer, Integer> entry : entries) {
85      entry.setValue(entry.getValue() + 2 * N);
86    }
87    for (int i = 0; i < N; i++) {
88      assertEquals(2 * N + 2 * i - 1, (int) bimap.get(2 * i));
89    }
90  }
91
92  // The next two tests verify that map entries are not accessed after they're
93  // removed, since IdentityHashMap throws an exception when that occurs.
94  public void testIdentityKeySetIteratorRemove() {
95    bimap = new AbstractBiMap<Integer, String>(
96        new IdentityHashMap<Integer, String>(),
97        new IdentityHashMap<String, Integer>()) {};
98    putOneTwoThree();
99    Iterator<Integer> iterator = bimap.keySet().iterator();
100    iterator.next();
101    iterator.next();
102    iterator.remove();
103    iterator.next();
104    iterator.remove();
105    assertEquals(1, bimap.size());
106    assertEquals(1, bimap.inverse().size());
107  }
108
109  public void testIdentityEntrySetIteratorRemove() {
110    bimap = new AbstractBiMap<Integer, String>(
111        new IdentityHashMap<Integer, String>(),
112        new IdentityHashMap<String, Integer>()) {};
113    putOneTwoThree();
114    Iterator<Entry<Integer, String>> iterator = bimap.entrySet().iterator();
115    iterator.next();
116    iterator.next();
117    iterator.remove();
118    iterator.next();
119    iterator.remove();
120    assertEquals(1, bimap.size());
121    assertEquals(1, bimap.inverse().size());
122  }
123}
124