ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Map/LockStep.java
Revision: 1.2
Committed: Sun Sep 5 21:32:20 2010 UTC (13 years, 8 months ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.1: +4 -4 lines
Log Message:
Update legal notices to Oracle wording

File Contents

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