• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.google.common.truth.Truth.assertThat;
20 
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.annotations.GwtIncompatible;
23 import com.google.common.collect.testing.features.CollectionFeature;
24 import com.google.common.collect.testing.features.CollectionSize;
25 import com.google.common.collect.testing.features.MapFeature;
26 import com.google.common.collect.testing.google.BiMapTestSuiteBuilder;
27 import com.google.common.collect.testing.google.TestStringBiMapGenerator;
28 import java.util.Iterator;
29 import java.util.Map;
30 import java.util.Map.Entry;
31 import java.util.Set;
32 import junit.framework.Test;
33 import junit.framework.TestCase;
34 import junit.framework.TestSuite;
35 
36 /**
37  * Tests for {@link HashBiMap}.
38  *
39  * @author Mike Bostock
40  */
41 @GwtCompatible(emulated = true)
42 public class HashBiMapTest extends TestCase {
43 
44   public static final class HashBiMapGenerator extends TestStringBiMapGenerator {
45     @Override
create(Entry<String, String>[] entries)46     protected BiMap<String, String> create(Entry<String, String>[] entries) {
47       BiMap<String, String> result = HashBiMap.create();
48       for (Entry<String, String> entry : entries) {
49         result.put(entry.getKey(), entry.getValue());
50       }
51       return result;
52     }
53   }
54 
55   @GwtIncompatible // suite
suite()56   public static Test suite() {
57     TestSuite suite = new TestSuite();
58     suite.addTest(
59         BiMapTestSuiteBuilder.using(new HashBiMapGenerator())
60             .named("HashBiMap")
61             .withFeatures(
62                 CollectionSize.ANY,
63                 CollectionFeature.SERIALIZABLE,
64                 CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
65                 CollectionFeature.KNOWN_ORDER,
66                 MapFeature.ALLOWS_NULL_KEYS,
67                 MapFeature.ALLOWS_NULL_VALUES,
68                 MapFeature.ALLOWS_ANY_NULL_QUERIES,
69                 MapFeature.GENERAL_PURPOSE)
70             .createTestSuite());
71     suite.addTestSuite(HashBiMapTest.class);
72     return suite;
73   }
74 
testMapConstructor()75   public void testMapConstructor() {
76     /* Test with non-empty Map. */
77     Map<String, String> map =
78         ImmutableMap.of(
79             "canada", "dollar",
80             "chile", "peso",
81             "switzerland", "franc");
82     HashBiMap<String, String> bimap = HashBiMap.create(map);
83     assertEquals("dollar", bimap.get("canada"));
84     assertEquals("canada", bimap.inverse().get("dollar"));
85   }
86 
87   private static final int N = 1000;
88 
testBashIt()89   public void testBashIt() throws Exception {
90     BiMap<Integer, Integer> bimap = HashBiMap.create(N);
91     BiMap<Integer, Integer> inverse = bimap.inverse();
92 
93     for (int i = 0; i < N; i++) {
94       assertNull(bimap.put(2 * i, 2 * i + 1));
95     }
96     for (int i = 0; i < N; i++) {
97       assertEquals(2 * i + 1, (int) bimap.get(2 * i));
98     }
99     for (int i = 0; i < N; i++) {
100       assertEquals(2 * i, (int) inverse.get(2 * i + 1));
101     }
102     for (int i = 0; i < N; i++) {
103       int oldValue = bimap.get(2 * i);
104       assertEquals(2 * i + 1, (int) bimap.put(2 * i, oldValue - 2));
105     }
106     for (int i = 0; i < N; i++) {
107       assertEquals(2 * i - 1, (int) bimap.get(2 * i));
108     }
109     for (int i = 0; i < N; i++) {
110       assertEquals(2 * i, (int) inverse.get(2 * i - 1));
111     }
112     Set<Entry<Integer, Integer>> entries = bimap.entrySet();
113     for (Entry<Integer, Integer> entry : entries) {
114       entry.setValue(entry.getValue() + 2 * N);
115     }
116     for (int i = 0; i < N; i++) {
117       assertEquals(2 * N + 2 * i - 1, (int) bimap.get(2 * i));
118     }
119   }
120 
testBiMapEntrySetIteratorRemove()121   public void testBiMapEntrySetIteratorRemove() {
122     BiMap<Integer, String> map = HashBiMap.create();
123     map.put(1, "one");
124     Set<Entry<Integer, String>> entries = map.entrySet();
125     Iterator<Entry<Integer, String>> iterator = entries.iterator();
126     Entry<Integer, String> entry = iterator.next();
127     entry.setValue("two"); // changes the iterator's current entry value
128     assertEquals("two", map.get(1));
129     assertEquals(Integer.valueOf(1), map.inverse().get("two"));
130     iterator.remove(); // removes the updated entry
131     assertTrue(map.isEmpty());
132   }
133 
testInsertionOrder()134   public void testInsertionOrder() {
135     BiMap<String, Integer> map = HashBiMap.create();
136     map.put("foo", 1);
137     map.put("bar", 2);
138     map.put("quux", 3);
139     assertThat(map.entrySet())
140         .containsExactly(
141             Maps.immutableEntry("foo", 1),
142             Maps.immutableEntry("bar", 2),
143             Maps.immutableEntry("quux", 3))
144         .inOrder();
145   }
146 
testInsertionOrderAfterRemoveFirst()147   public void testInsertionOrderAfterRemoveFirst() {
148     BiMap<String, Integer> map = HashBiMap.create();
149     map.put("foo", 1);
150     map.put("bar", 2);
151     map.put("quux", 3);
152 
153     map.remove("foo");
154     assertThat(map.entrySet())
155         .containsExactly(Maps.immutableEntry("bar", 2), Maps.immutableEntry("quux", 3))
156         .inOrder();
157   }
158 
testInsertionOrderAfterRemoveMiddle()159   public void testInsertionOrderAfterRemoveMiddle() {
160     BiMap<String, Integer> map = HashBiMap.create();
161     map.put("foo", 1);
162     map.put("bar", 2);
163     map.put("quux", 3);
164 
165     map.remove("bar");
166     assertThat(map.entrySet())
167         .containsExactly(Maps.immutableEntry("foo", 1), Maps.immutableEntry("quux", 3))
168         .inOrder();
169   }
170 
testInsertionOrderAfterRemoveLast()171   public void testInsertionOrderAfterRemoveLast() {
172     BiMap<String, Integer> map = HashBiMap.create();
173     map.put("foo", 1);
174     map.put("bar", 2);
175     map.put("quux", 3);
176 
177     map.remove("quux");
178     assertThat(map.entrySet())
179         .containsExactly(Maps.immutableEntry("foo", 1), Maps.immutableEntry("bar", 2))
180         .inOrder();
181   }
182 
testInsertionOrderAfterForcePut()183   public void testInsertionOrderAfterForcePut() {
184     BiMap<String, Integer> map = HashBiMap.create();
185     map.put("foo", 1);
186     map.put("bar", 2);
187     map.put("quux", 3);
188 
189     map.forcePut("quux", 1);
190     assertThat(map.entrySet())
191         .containsExactly(Maps.immutableEntry("bar", 2), Maps.immutableEntry("quux", 1))
192         .inOrder();
193   }
194 
testInsertionOrderAfterInverseForcePut()195   public void testInsertionOrderAfterInverseForcePut() {
196     BiMap<String, Integer> map = HashBiMap.create();
197     map.put("foo", 1);
198     map.put("bar", 2);
199     map.put("quux", 3);
200 
201     map.inverse().forcePut(1, "quux");
202     assertThat(map.entrySet())
203         .containsExactly(Maps.immutableEntry("bar", 2), Maps.immutableEntry("quux", 1))
204         .inOrder();
205   }
206 
testInverseInsertionOrderAfterInverse()207   public void testInverseInsertionOrderAfterInverse() {
208     BiMap<String, Integer> map = HashBiMap.create();
209     map.put("bar", 2);
210     map.put("quux", 1);
211 
212     assertThat(map.inverse().entrySet())
213         .containsExactly(Maps.immutableEntry(2, "bar"), Maps.immutableEntry(1, "quux"))
214         .inOrder();
215   }
216 
testInverseInsertionOrderAfterInverseForcePut()217   public void testInverseInsertionOrderAfterInverseForcePut() {
218     BiMap<String, Integer> map = HashBiMap.create();
219     map.put("foo", 1);
220     map.put("bar", 2);
221     map.put("quux", 3);
222 
223     map.inverse().forcePut(1, "quux");
224     assertThat(map.inverse().entrySet())
225         .containsExactly(Maps.immutableEntry(2, "bar"), Maps.immutableEntry(1, "quux"))
226         .inOrder();
227   }
228 
testInverseInsertionOrderAfterInverseForcePutPresentKey()229   public void testInverseInsertionOrderAfterInverseForcePutPresentKey() {
230     BiMap<String, Integer> map = HashBiMap.create();
231     map.put("foo", 1);
232     map.put("bar", 2);
233     map.put("quux", 3);
234     map.put("nab", 4);
235 
236     map.inverse().forcePut(4, "bar");
237     assertThat(map.entrySet())
238         .containsExactly(
239             Maps.immutableEntry("foo", 1),
240             Maps.immutableEntry("bar", 4),
241             Maps.immutableEntry("quux", 3))
242         .inOrder();
243   }
244 
testInverseEntrySetValueNewKey()245   public void testInverseEntrySetValueNewKey() {
246     BiMap<Integer, String> map = HashBiMap.create();
247     map.put(1, "a");
248     map.put(2, "b");
249     Iterator<Entry<String, Integer>> inverseEntryItr = map.inverse().entrySet().iterator();
250     Entry<String, Integer> entry = inverseEntryItr.next();
251     entry.setValue(3);
252     assertEquals(Maps.immutableEntry("b", 2), inverseEntryItr.next());
253     assertFalse(inverseEntryItr.hasNext());
254     assertThat(map.entrySet())
255         .containsExactly(Maps.immutableEntry(2, "b"), Maps.immutableEntry(3, "a"))
256         .inOrder();
257   }
258 }
259