1 /* 2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6612102 27 * @summary Test Map implementations for mutual compatibility 28 */ 29 30 import java.util.*; 31 import java.util.concurrent.*; 32 33 /** 34 * Based on the strange scenario required to reproduce 35 * (coll) IdentityHashMap.iterator().remove() might decrement size twice 36 * 37 * It would be good to add more "Lockstep-style" tests to this file. 38 */ 39 public class LockStep { 40 void mapsEqual(Map m1, Map m2) { 41 equal(m1, m2); 42 equal(m2, m1); 43 equal(m1.size(), m2.size()); 44 equal(m1.isEmpty(), m2.isEmpty()); 45 equal(m1.keySet(), m2.keySet()); 46 equal(m2.keySet(), m1.keySet()); 47 } 48 49 void mapsEqual(List<Map> maps) { 50 Map first = maps.get(0); 51 for (Map map : maps) 52 mapsEqual(first, map); 53 } 54 55 void put(List<Map> maps, Object key, Object val) { 56 for (Map map : maps) 57 map.put(key, val); 58 mapsEqual(maps); 59 } 60 61 void removeLastTwo(List<Map> maps) { 62 Map first = maps.get(0); 63 int size = first.size(); 64 Iterator fit = first.keySet().iterator(); 65 for (int j = 0; j < size - 2; j++) 66 fit.next(); 67 Object x1 = fit.next(); 68 Object x2 = fit.next(); 69 70 for (Map map : maps) { 71 Iterator it = map.keySet().iterator(); 72 while (it.hasNext()) { 73 Object x = it.next(); 74 if (x == x1 || x == x2) 75 it.remove(); 76 } 77 } 78 mapsEqual(maps); 79 } 80 81 void remove(Map m, Iterator it) { 82 int size = m.size(); 83 it.remove(); 84 if (m.size() != size-1) 85 throw new Error(String.format("Incorrect size!%nmap=%s, size=%d%n", 86 m.toString(), m.size())); 87 } 88 89 void test(String[] args) throws Throwable { 90 final int iterations = 100; 91 final Random r = new Random(); 92 93 for (int i = 0; i < iterations; i++) { 94 List<Map> maps = Arrays.asList( 95 new Map[] { 96 new IdentityHashMap(11), 97 new HashMap(16), 98 new LinkedHashMap(16), 99 new WeakHashMap(16), 100 new Hashtable(16), 101 new TreeMap(), 102 new ConcurrentHashMap(16), 103 new ConcurrentSkipListMap(), 104 Collections.checkedMap(new HashMap(16), Integer.class, Integer.class), 105 Collections.checkedSortedMap(new TreeMap(), Integer.class, Integer.class), 106 Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class), 107 Collections.synchronizedMap(new HashMap(16)), 108 Collections.synchronizedSortedMap(new TreeMap()), 109 Collections.synchronizedNavigableMap(new TreeMap()) 110 }); 111 112 for (int j = 0; j < 10; j++) 113 put(maps, r.nextInt(100), r.nextInt(100)); 114 removeLastTwo(maps); 115 } 116 } 117 118 //--------------------- Infrastructure --------------------------- 119 volatile int passed = 0, failed = 0; 120 void pass() {passed++;} 121 void fail() {failed++; Thread.dumpStack();} 122 void fail(String msg) {System.err.println(msg); fail();} 123 void unexpected(Throwable t) {failed++; t.printStackTrace();} 124 void check(boolean cond) {if (cond) pass(); else fail();} 125 void equal(Object x, Object y) { 126 if (x == null ? y == null : x.equals(y)) pass(); 127 else fail(x + " not equal to " + y);} 128 public static void main(String[] args) throws Throwable { 129 new LockStep().instanceMain(args);} 130 void instanceMain(String[] args) throws Throwable { 131 try {test(args);} catch (Throwable t) {unexpected(t);} 132 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 133 if (failed > 0) throw new AssertionError("Some tests failed");} 134 }