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