Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Map/LockStep.java
38812 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.*;31import java.util.concurrent.*;3233/**34* Based on the strange scenario required to reproduce35* (coll) IdentityHashMap.iterator().remove() might decrement size twice36*37* It would be good to add more "Lockstep-style" tests to this file.38*/39public class LockStep {40void mapsEqual(Map m1, Map m2) {41equal(m1, m2);42equal(m2, m1);43equal(m1.size(), m2.size());44equal(m1.isEmpty(), m2.isEmpty());45equal(m1.keySet(), m2.keySet());46equal(m2.keySet(), m1.keySet());47}4849void mapsEqual(List<Map> maps) {50Map first = maps.get(0);51for (Map map : maps)52mapsEqual(first, map);53}5455void put(List<Map> maps, Object key, Object val) {56for (Map map : maps)57map.put(key, val);58mapsEqual(maps);59}6061void removeLastTwo(List<Map> maps) {62Map first = maps.get(0);63int size = first.size();64Iterator fit = first.keySet().iterator();65for (int j = 0; j < size - 2; j++)66fit.next();67Object x1 = fit.next();68Object x2 = fit.next();6970for (Map map : maps) {71Iterator it = map.keySet().iterator();72while (it.hasNext()) {73Object x = it.next();74if (x == x1 || x == x2)75it.remove();76}77}78mapsEqual(maps);79}8081void remove(Map m, Iterator it) {82int size = m.size();83it.remove();84if (m.size() != size-1)85throw new Error(String.format("Incorrect size!%nmap=%s, size=%d%n",86m.toString(), m.size()));87}8889void test(String[] args) throws Throwable {90final int iterations = 100;91final Random r = new Random();9293for (int i = 0; i < iterations; i++) {94List<Map> maps = Arrays.asList(95new Map[] {96new IdentityHashMap(11),97new HashMap(16),98new LinkedHashMap(16),99new WeakHashMap(16),100new Hashtable(16),101new TreeMap(),102new ConcurrentHashMap(16),103new ConcurrentSkipListMap(),104Collections.checkedMap(new HashMap(16), Integer.class, Integer.class),105Collections.checkedSortedMap(new TreeMap(), Integer.class, Integer.class),106Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class),107Collections.synchronizedMap(new HashMap(16)),108Collections.synchronizedSortedMap(new TreeMap()),109Collections.synchronizedNavigableMap(new TreeMap())110});111112for (int j = 0; j < 10; j++)113put(maps, r.nextInt(100), r.nextInt(100));114removeLastTwo(maps);115}116}117118//--------------------- Infrastructure ---------------------------119volatile int passed = 0, failed = 0;120void pass() {passed++;}121void fail() {failed++; Thread.dumpStack();}122void fail(String msg) {System.err.println(msg); fail();}123void unexpected(Throwable t) {failed++; t.printStackTrace();}124void check(boolean cond) {if (cond) pass(); else fail();}125void equal(Object x, Object y) {126if (x == null ? y == null : x.equals(y)) pass();127else fail(x + " not equal to " + y);}128public static void main(String[] args) throws Throwable {129new LockStep().instanceMain(args);}130void instanceMain(String[] args) throws Throwable {131try {test(args);} catch (Throwable t) {unexpected(t);}132System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);133if (failed > 0) throw new AssertionError("Some tests failed");}134}135136137