• 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 com.google.common.annotations.GwtCompatible;
20 import com.google.common.annotations.GwtIncompatible;
21 import com.google.common.collect.testing.features.CollectionFeature;
22 import com.google.common.collect.testing.features.CollectionSize;
23 import com.google.common.collect.testing.features.MapFeature;
24 import com.google.common.collect.testing.google.BiMapTestSuiteBuilder;
25 import com.google.common.collect.testing.google.TestStringBiMapGenerator;
26 
27 import junit.framework.Test;
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30 
31 import java.util.Iterator;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 import java.util.Set;
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(BiMapTestSuiteBuilder.using(new HashBiMapGenerator())
59       .named("HashBiMap")
60       .withFeatures(CollectionSize.ANY,
61           CollectionFeature.SERIALIZABLE,
62           CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
63           MapFeature.ALLOWS_NULL_KEYS,
64           MapFeature.ALLOWS_NULL_VALUES,
65           MapFeature.ALLOWS_ANY_NULL_QUERIES,
66           MapFeature.GENERAL_PURPOSE)
67       .createTestSuite());
68     suite.addTestSuite(HashBiMapTest.class);
69     return suite;
70   }
71 
testMapConstructor()72   public void testMapConstructor() {
73     /* Test with non-empty Map. */
74     Map<String, String> map = ImmutableMap.of(
75         "canada", "dollar",
76         "chile", "peso",
77         "switzerland", "franc");
78     HashBiMap<String, String> bimap = HashBiMap.create(map);
79     assertEquals("dollar", bimap.get("canada"));
80     assertEquals("canada", bimap.inverse().get("dollar"));
81   }
82 
83   private static final int N = 1000;
84 
testBashIt()85   public void testBashIt() throws Exception {
86     BiMap<Integer, Integer> bimap = HashBiMap.create(N);
87     BiMap<Integer, Integer> inverse = bimap.inverse();
88 
89     for (int i = 0; i < N; i++) {
90       assertNull(bimap.put(2 * i, 2 * i + 1));
91     }
92     for (int i = 0; i < N; i++) {
93       assertEquals(2 * i + 1, (int) bimap.get(2 * i));
94     }
95     for (int i = 0; i < N; i++) {
96       assertEquals(2 * i, (int) inverse.get(2 * i + 1));
97     }
98     for (int i = 0; i < N; i++) {
99       int oldValue = bimap.get(2 * i);
100       assertEquals(2 * i + 1, (int) bimap.put(2 * i, oldValue - 2));
101     }
102     for (int i = 0; i < N; i++) {
103       assertEquals(2 * i - 1, (int) bimap.get(2 * i));
104     }
105     for (int i = 0; i < N; i++) {
106       assertEquals(2 * i, (int) inverse.get(2 * i - 1));
107     }
108     Set<Entry<Integer, Integer>> entries = bimap.entrySet();
109     for (Entry<Integer, Integer> entry : entries) {
110       entry.setValue(entry.getValue() + 2 * N);
111     }
112     for (int i = 0; i < N; i++) {
113       assertEquals(2 * N + 2 * i - 1, (int) bimap.get(2 * i));
114     }
115   }
116 
testBiMapEntrySetIteratorRemove()117   public void testBiMapEntrySetIteratorRemove() {
118     BiMap<Integer, String> map = HashBiMap.create();
119     map.put(1, "one");
120     Set<Map.Entry<Integer, String>> entries = map.entrySet();
121     Iterator<Map.Entry<Integer, String>> iterator = entries.iterator();
122     Map.Entry<Integer, String> entry = iterator.next();
123     entry.setValue("two"); // changes the iterator's current entry value
124     assertEquals("two", map.get(1));
125     assertEquals(Integer.valueOf(1), map.inverse().get("two"));
126     iterator.remove(); // removes the updated entry
127     assertTrue(map.isEmpty());
128   }
129 }
130