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