35N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
35N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
35N/A *
35N/A * This code is free software; you can redistribute it and/or modify it
35N/A * under the terms of the GNU General Public License version 2 only, as
35N/A * published by the Free Software Foundation.
35N/A *
35N/A * This code is distributed in the hope that it will be useful, but WITHOUT
35N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
35N/A * version 2 for more details (a copy is included in the LICENSE file that
35N/A * accompanied this code).
35N/A *
35N/A * You should have received a copy of the GNU General Public License version
35N/A * 2 along with this work; if not, write to the Free Software Foundation,
35N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
35N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
35N/A */
35N/A
35N/A/*
35N/A * @test
35N/A * @bug 6612102
35N/A * @summary Test Map implementations for mutual compatibility
35N/A */
35N/A
35N/Aimport java.util.*;
35N/Aimport java.util.concurrent.*;
35N/A
35N/A/**
35N/A * Based on the strange scenario required to reproduce
35N/A * (coll) IdentityHashMap.iterator().remove() might decrement size twice
35N/A *
35N/A * It would be good to add more "Lockstep-style" tests to this file.
35N/A */
35N/Apublic class LockStep {
35N/A void mapsEqual(Map m1, Map m2) {
35N/A equal(m1, m2);
35N/A equal(m2, m1);
35N/A equal(m1.size(), m2.size());
35N/A equal(m1.isEmpty(), m2.isEmpty());
35N/A equal(m1.keySet(), m2.keySet());
35N/A equal(m2.keySet(), m1.keySet());
35N/A }
35N/A
35N/A void mapsEqual(List<Map> maps) {
35N/A Map first = maps.get(0);
35N/A for (Map map : maps)
35N/A mapsEqual(first, map);
35N/A }
35N/A
35N/A void put(List<Map> maps, Object key, Object val) {
35N/A for (Map map : maps)
35N/A map.put(key, val);
35N/A mapsEqual(maps);
35N/A }
35N/A
35N/A void removeLastTwo(List<Map> maps) {
35N/A Map first = maps.get(0);
35N/A int size = first.size();
35N/A Iterator fit = first.keySet().iterator();
35N/A for (int j = 0; j < size - 2; j++)
35N/A fit.next();
35N/A Object x1 = fit.next();
35N/A Object x2 = fit.next();
35N/A
35N/A for (Map map : maps) {
35N/A Iterator it = map.keySet().iterator();
35N/A while (it.hasNext()) {
35N/A Object x = it.next();
35N/A if (x == x1 || x == x2)
35N/A it.remove();
35N/A }
35N/A }
35N/A mapsEqual(maps);
35N/A }
35N/A
35N/A void remove(Map m, Iterator it) {
35N/A int size = m.size();
35N/A it.remove();
35N/A if (m.size() != size-1)
35N/A throw new Error(String.format("Incorrect size!%nmap=%s, size=%d%n",
35N/A m.toString(), m.size()));
35N/A }
35N/A
35N/A void test(String[] args) throws Throwable {
35N/A final int iterations = 100;
35N/A final Random r = new Random();
35N/A
35N/A for (int i = 0; i < iterations; i++) {
35N/A List<Map> maps = Arrays.asList(
35N/A new Map[] {
35N/A new IdentityHashMap(11),
35N/A new HashMap(16),
35N/A new LinkedHashMap(16),
35N/A new WeakHashMap(16),
35N/A new Hashtable(16),
35N/A new TreeMap(),
35N/A new ConcurrentHashMap(16),
35N/A new ConcurrentSkipListMap() });
35N/A
35N/A for (int j = 0; j < 10; j++)
35N/A put(maps, r.nextInt(100), r.nextInt(100));
35N/A removeLastTwo(maps);
35N/A }
35N/A }
35N/A
35N/A //--------------------- Infrastructure ---------------------------
35N/A volatile int passed = 0, failed = 0;
35N/A void pass() {passed++;}
35N/A void fail() {failed++; Thread.dumpStack();}
35N/A void fail(String msg) {System.err.println(msg); fail();}
35N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
35N/A void check(boolean cond) {if (cond) pass(); else fail();}
35N/A void equal(Object x, Object y) {
35N/A if (x == null ? y == null : x.equals(y)) pass();
35N/A else fail(x + " not equal to " + y);}
35N/A public static void main(String[] args) throws Throwable {
35N/A new LockStep().instanceMain(args);}
35N/A void instanceMain(String[] args) throws Throwable {
35N/A try {test(args);} catch (Throwable t) {unexpected(t);}
35N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
35N/A if (failed > 0) throw new AssertionError("Some tests failed");}
35N/A}