ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Map/LockStep.java
Revision: 1.4
Committed: Mon Jan 8 03:12:03 2018 UTC (6 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.3: +14 -2 lines
Log Message:
organize imports

File Contents

# Content
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 = Arrays.asList(
108 new Map[] {
109 new IdentityHashMap(11),
110 new HashMap(16),
111 new LinkedHashMap(16),
112 new WeakHashMap(16),
113 new Hashtable(16),
114 new TreeMap(),
115 new ConcurrentHashMap(16),
116 new ConcurrentSkipListMap(),
117 Collections.checkedMap(new HashMap(16), Integer.class, Integer.class),
118 Collections.checkedSortedMap(new TreeMap(), Integer.class, Integer.class),
119 Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class),
120 Collections.synchronizedMap(new HashMap(16)),
121 Collections.synchronizedSortedMap(new TreeMap()),
122 Collections.synchronizedNavigableMap(new TreeMap())
123 });
124
125 for (int j = 0; j < 10; j++)
126 put(maps, r.nextInt(100), r.nextInt(100));
127 removeLastTwo(maps);
128 }
129 }
130
131 //--------------------- Infrastructure ---------------------------
132 volatile int passed = 0, failed = 0;
133 void pass() {passed++;}
134 void fail() {failed++; Thread.dumpStack();}
135 void fail(String msg) {System.err.println(msg); fail();}
136 void unexpected(Throwable t) {failed++; t.printStackTrace();}
137 void check(boolean cond) {if (cond) pass(); else fail();}
138 void equal(Object x, Object y) {
139 if (x == null ? y == null : x.equals(y)) pass();
140 else fail(x + " not equal to " + y);}
141 public static void main(String[] args) throws Throwable {
142 new LockStep().instanceMain(args);}
143 void instanceMain(String[] args) throws Throwable {
144 try {test(args);} catch (Throwable t) {unexpected(t);}
145 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
146 if (failed > 0) throw new AssertionError("Some tests failed");}
147 }