• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.common.collect;
16 
17 import java.util.IdentityHashMap;
18 import java.util.Iterator;
19 import java.util.Map.Entry;
20 import junit.framework.TestCase;
21 
22 /**
23  * Tests for {@code AbstractBiMap}.
24  *
25  * @author Mike Bostock
26  */
27 public class AbstractBiMapTest extends TestCase {
28 
29   // The next two tests verify that map entries are not accessed after they're
30   // removed, since IdentityHashMap throws an exception when that occurs.
testIdentityKeySetIteratorRemove()31   public void testIdentityKeySetIteratorRemove() {
32     BiMap<Integer, String> bimap =
33         new AbstractBiMap<Integer, String>(
34             new IdentityHashMap<Integer, String>(), new IdentityHashMap<String, Integer>()) {};
35     bimap.put(1, "one");
36     bimap.put(2, "two");
37     bimap.put(3, "three");
38     Iterator<Integer> iterator = bimap.keySet().iterator();
39     iterator.next();
40     iterator.next();
41     iterator.remove();
42     iterator.next();
43     iterator.remove();
44     assertEquals(1, bimap.size());
45     assertEquals(1, bimap.inverse().size());
46   }
47 
testIdentityEntrySetIteratorRemove()48   public void testIdentityEntrySetIteratorRemove() {
49     BiMap<Integer, String> bimap =
50         new AbstractBiMap<Integer, String>(
51             new IdentityHashMap<Integer, String>(), new IdentityHashMap<String, Integer>()) {};
52     bimap.put(1, "one");
53     bimap.put(2, "two");
54     bimap.put(3, "three");
55     Iterator<Entry<Integer, String>> iterator = bimap.entrySet().iterator();
56     iterator.next();
57     iterator.next();
58     iterator.remove();
59     iterator.next();
60     iterator.remove();
61     assertEquals(1, bimap.size());
62     assertEquals(1, bimap.inverse().size());
63   }
64 }
65