ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collection/HotPotatoes.java
Revision: 1.5
Committed: Sun Jun 10 16:38:03 2018 UTC (6 years ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +2 -1 lines
Log Message:
fix errorprone [ClassNewInstance] warnings

File Contents

# User Rev Content
1 jsr166 1.1 /*
2 jsr166 1.2 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
3 jsr166 1.1 * 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 jsr166 1.2 * 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 jsr166 1.1 */
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 jsr166 1.4 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 jsr166 1.1
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 jsr166 1.5 final List<Integer> list = (List<Integer>)
74     argClazz.getDeclaredConstructor().newInstance();
75 jsr166 1.1 final Integer one = Integer.valueOf(1);
76     final List<Integer> oneElementList = Collections.singletonList(one);
77     final Constructor<? extends Collection> constr
78     = implClazz.getConstructor(Collection.class);
79     final Thread t = new CheckedThread() { public void realRun() {
80     for (int i = 0; i < iterations; i++) {
81     list.add(one);
82     list.remove(one);
83     }}};
84     t.setDaemon(true);
85     t.start();
86    
87     for (int i = 0; i < iterations; i++) {
88     Collection<?> coll = constr.newInstance(list);
89     Object[] elts = coll.toArray();
90     check(elts.length == 0 ||
91     (elts.length == 1 && elts[0] == one));
92     }
93     } catch (Throwable t) { unexpected(t); }
94     }
95    
96     //--------------------- Infrastructure ---------------------------
97     static volatile int passed = 0, failed = 0;
98     static void pass() {passed++;}
99     static void fail() {failed++; Thread.dumpStack();}
100     static void fail(String msg) {System.out.println(msg); fail();}
101     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
102     static void check(boolean cond) {if (cond) pass(); else fail();}
103     static void equal(Object x, Object y) {
104     if (x == null ? y == null : x.equals(y)) pass();
105     else fail(x + " not equal to " + y);}
106     public static void main(String[] args) throws Throwable {
107     try {realMain(args);} catch (Throwable t) {unexpected(t);}
108     System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
109     if (failed > 0) throw new AssertionError("Some tests failed");}
110 jsr166 1.3 private abstract static class CheckedThread extends Thread {
111 jsr166 1.1 public abstract void realRun() throws Throwable;
112     public void run() {
113     try { realRun(); } catch (Throwable t) { unexpected(t); }}}
114     }