• 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 junit.framework.TestCase;
18 
19 import java.util.IdentityHashMap;
20 import java.util.Iterator;
21 import java.util.Map.Entry;
22 
23 /**
24  * Tests for {@code AbstractBiMap}.
25  *
26  * @author Mike Bostock
27  */
28 public class AbstractBiMapTest extends TestCase {
29 
30   // The next two tests verify that map entries are not accessed after they're
31   // removed, since IdentityHashMap throws an exception when that occurs.
testIdentityKeySetIteratorRemove()32   public void testIdentityKeySetIteratorRemove() {
33     BiMap<Integer, String> bimap = new AbstractBiMap<Integer, String>(
34         new IdentityHashMap<Integer, String>(),
35         new IdentityHashMap<String, Integer>()) {};
36     bimap.put(1, "one");
37     bimap.put(2, "two");
38     bimap.put(3, "three");
39     Iterator<Integer> iterator = bimap.keySet().iterator();
40     iterator.next();
41     iterator.next();
42     iterator.remove();
43     iterator.next();
44     iterator.remove();
45     assertEquals(1, bimap.size());
46     assertEquals(1, bimap.inverse().size());
47   }
48 
testIdentityEntrySetIteratorRemove()49   public void testIdentityEntrySetIteratorRemove() {
50     BiMap<Integer, String> bimap = new AbstractBiMap<Integer, String>(
51         new IdentityHashMap<Integer, String>(),
52         new IdentityHashMap<String, Integer>()) {};
53     bimap.put(1, "one");
54     bimap.put(2, "two");
55     bimap.put(3, "three");
56     Iterator<Entry<Integer, String>> iterator = bimap.entrySet().iterator();
57     iterator.next();
58     iterator.next();
59     iterator.remove();
60     iterator.next();
61     iterator.remove();
62     assertEquals(1, bimap.size());
63     assertEquals(1, bimap.inverse().size());
64   }
65 }
66