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