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