ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collection/HotPotatoes.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: +10 -3 lines
Log Message:
organize imports

File Contents

# Content
1 /*
2 * Copyright (c) 2005, 2006, 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 6355660 6347106 6394004
27 * @summary methods taking concurrently mutating collection should work
28 * @author Martin Buchholz
29 */
30
31 import java.lang.reflect.Constructor;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.List;
37 import java.util.PriorityQueue;
38 import java.util.Vector;
39 import java.util.concurrent.CopyOnWriteArrayList;
40 import java.util.concurrent.PriorityBlockingQueue;
41
42 @SuppressWarnings("unchecked")
43 public class HotPotatoes {
44 private static void realMain(String[] args) throws Throwable {
45 testImplementation(Vector.class);
46 testImplementation(ArrayList.class);
47 testImplementation(PriorityQueue.class);
48 testImplementation(PriorityBlockingQueue.class);
49 }
50
51 private static void testImplementation(Class<? extends Collection> implClazz)
52 throws Throwable
53 {
54 testPotato(implClazz, Vector.class);
55 testPotato(implClazz, CopyOnWriteArrayList.class);
56
57 final Constructor<? extends Collection> constr
58 = implClazz.getConstructor(Collection.class);
59 final Collection<Object> coll
60 = constr.newInstance(Arrays.asList(new String[] {}));
61 coll.add(1);
62 equal(coll.toString(), "[1]");
63 }
64
65 private static void testPotato(Class<? extends Collection> implClazz,
66 Class<? extends List> argClazz)
67 throws Throwable
68 {
69 try {
70 System.out.printf("implClazz=%s, argClazz=%s\n",
71 implClazz.getName(), argClazz.getName());
72 final int iterations = 100000;
73 final List<Integer> list = (List<Integer>) argClazz.newInstance();
74 final Integer one = Integer.valueOf(1);
75 final List<Integer> oneElementList = Collections.singletonList(one);
76 final Constructor<? extends Collection> constr
77 = implClazz.getConstructor(Collection.class);
78 final Thread t = new CheckedThread() { public void realRun() {
79 for (int i = 0; i < iterations; i++) {
80 list.add(one);
81 list.remove(one);
82 }}};
83 t.setDaemon(true);
84 t.start();
85
86 for (int i = 0; i < iterations; i++) {
87 Collection<?> coll = constr.newInstance(list);
88 Object[] elts = coll.toArray();
89 check(elts.length == 0 ||
90 (elts.length == 1 && elts[0] == one));
91 }
92 } catch (Throwable t) { unexpected(t); }
93 }
94
95 //--------------------- Infrastructure ---------------------------
96 static volatile int passed = 0, failed = 0;
97 static void pass() {passed++;}
98 static void fail() {failed++; Thread.dumpStack();}
99 static void fail(String msg) {System.out.println(msg); fail();}
100 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
101 static void check(boolean cond) {if (cond) pass(); else fail();}
102 static void equal(Object x, Object y) {
103 if (x == null ? y == null : x.equals(y)) pass();
104 else fail(x + " not equal to " + y);}
105 public static void main(String[] args) throws Throwable {
106 try {realMain(args);} catch (Throwable t) {unexpected(t);}
107 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
108 if (failed > 0) throw new AssertionError("Some tests failed");}
109 private abstract static class CheckedThread extends Thread {
110 public abstract void realRun() throws Throwable;
111 public void run() {
112 try { realRun(); } catch (Throwable t) { unexpected(t); }}}
113 }