jdk/test/java/util/Collections/RacingCollections.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6672 f01ef94a63e7
child 32649 2ee9017c7597
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6672
diff changeset
     2
 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6360946 6360948
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test various operations on concurrently mutating collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import static java.util.Collections.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class RacingCollections {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
     * How long to run each "race" (in milliseconds).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
     * Turn this up to some higher value like 1000 for stress testing:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     * java -Dmillis=1000 RacingCollections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    final static long defaultWorkTimeMillis = Long.getLong("millis", 10L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * Whether to print debug information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    final static boolean debug = Boolean.getBoolean("debug");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    final static Integer one = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    final static Integer two = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * A thread that mutates an object forever, alternating between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * being empty and containing singleton "two"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static class Frobber extends CheckedThread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        volatile boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        boolean keepGoing(int i) { return (i % 128 != 0) || ! done; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        final Object elLoco;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        Frobber(Object elLoco) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            this.elLoco = elLoco;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            this.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        @SuppressWarnings("unchecked") void clear(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            if (o instanceof Collection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                ((Collection<?>)o).clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                ((Map<?,?>)o).clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        @SuppressWarnings("unchecked") void realRun() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            // Mutate elLoco wildly forever, checking occasionally for "done"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            clear(elLoco);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if (elLoco instanceof List) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                List<Integer> l = (List<Integer>) elLoco;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                for (int i = 0; keepGoing(i); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    switch (i%2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    case 0: l.add(two);    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    case 1: l.add(0, two); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    switch (i%2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    case 0: l.remove(two); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    case 1: l.remove(0);   break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    }}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            else if (elLoco instanceof Deque) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                Deque<Integer> q = (Deque<Integer>) elLoco;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                for (int i = 0; keepGoing(i); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    switch (i%6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    case 0: q.add(two);        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    case 1: q.addFirst(two);   break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    case 2: q.addLast(two);    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    case 3: q.offer(two);      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    case 4: q.offerFirst(two); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    case 5: q.offerLast(two);  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    switch (i%6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    case 0: q.remove(two);     break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    case 1: q.removeFirst();   break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    case 2: q.removeLast();    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    case 3: q.poll();          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    case 4: q.pollFirst();     break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    case 5: q.pollLast();      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    }}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            else if (elLoco instanceof Queue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                Queue<Integer> q = (Queue<Integer>) elLoco;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                for (int i = 0; keepGoing(i); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    switch (i%2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    case 0: q.add(two);    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    case 1: q.offer(two);  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    switch (i%2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    case 0: q.remove(two); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    case 1: q.poll();      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    }}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            else if (elLoco instanceof Map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                Map<Integer, Boolean> m = (Map<Integer, Boolean>) elLoco;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                for (int i = 0; keepGoing(i); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    m.put(two, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    m.remove(two);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                }}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            else if (elLoco instanceof Collection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                Collection<Integer> c = (Collection<Integer>) elLoco;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                for (int i = 0; keepGoing(i); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    c.add(two);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    c.remove(two);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                }}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            else { throw new Error("Huh? " + elLoco); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        void enoughAlready() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            try { join(); } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private static void checkEqualSanity(Object theRock, Object elLoco) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        //equal(theRock, theRock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        equal(elLoco, elLoco);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // It would be nice someday to have theRock and elLoco never "equal",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // although the meaning of "equal" for mutating collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // is a bit fuzzy.  Uncomment when/if we fix:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // 6374942: Improve thread safety of collection .equals() methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        //notEqual(theRock, elLoco);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        //notEqual(elLoco, theRock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        notEqual(theRock.toString(), elLoco.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    static class Looper {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        final long quittingTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        Looper() { this(defaultWorkTimeMillis); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        Looper(long workTimeMillis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            quittingTime = System.nanoTime() + workTimeMillis * 1024 * 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        boolean keepGoing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return (i++ % 128 != 0) || (System.nanoTime() < quittingTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private static void frob(Object theRock, Object elLoco) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        Frobber frobber = new Frobber(elLoco);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (theRock instanceof Collection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                Collection<Integer> c = (Collection<Integer>) theRock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                if (! c.contains(one))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    c.add(one);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                Map<Integer, Boolean> m = (Map<Integer, Boolean>) theRock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                if (! m.containsKey(one))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    m.put(one, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            for (Looper looper = new Looper(); looper.keepGoing(); )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                checkEqualSanity(theRock, elLoco);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        finally { frobber.enoughAlready(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private static List<Map<Integer, Boolean>> newConcurrentMaps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        List<Map<Integer, Boolean>> list =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            new ArrayList<Map<Integer, Boolean>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        list.add(new ConcurrentHashMap<Integer, Boolean>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        list.add(new ConcurrentSkipListMap<Integer, Boolean>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private static List<Map<Integer, Boolean>> maps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        List<Map<Integer, Boolean>> list = newConcurrentMaps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        list.add(new Hashtable<Integer, Boolean>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        list.add(new HashMap<Integer, Boolean>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        list.add(new TreeMap<Integer, Boolean>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        Comparator<Integer> cmp = new Comparator<Integer>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            public int compare(Integer x, Integer y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                return x - y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        list.add(new TreeMap<Integer, Boolean>(Collections.reverseOrder(cmp)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    private static List<Set<Integer>> newConcurrentSets() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        List<Set<Integer>> list = new ArrayList<Set<Integer>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        list.add(new ConcurrentSkipListSet<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        list.add(new CopyOnWriteArraySet<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private static List<Set<Integer>> newSets() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        List<Set<Integer>> list = newConcurrentSets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        list.add(new HashSet<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        list.add(new TreeSet<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        list.add(new TreeSet<Integer>(Collections.reverseOrder()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private static List<List<Integer>> newConcurrentLists() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        List<List<Integer>> list = new ArrayList<List<Integer>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        list.add(new CopyOnWriteArrayList<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    private static List<List<Integer>> newLists() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        List<List<Integer>> list = newConcurrentLists();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        list.add(new Vector<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        list.add(new ArrayList<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    private static List<Queue<Integer>> newConcurrentQueues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        List<Queue<Integer>> list =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            new ArrayList<Queue<Integer>>(newConcurrentDeques());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        list.add(new LinkedBlockingQueue<Integer>(10));
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   237
        list.add(new LinkedTransferQueue<Integer>());
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   238
        list.add(new ConcurrentLinkedQueue<Integer>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private static List<Queue<Integer>> newQueues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        List<Queue<Integer>> list =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            new ArrayList<Queue<Integer>>(newDeques());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        list.add(new LinkedBlockingQueue<Integer>(10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    private static List<Deque<Integer>> newConcurrentDeques() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        List<Deque<Integer>> list = new ArrayList<Deque<Integer>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        list.add(new LinkedBlockingDeque<Integer>(10));
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   252
        list.add(new ConcurrentLinkedDeque<Integer>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private static List<Deque<Integer>> newDeques() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        List<Deque<Integer>> list = newConcurrentDeques();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        list.add(new ArrayDeque<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        list.add(new LinkedList<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    private static void describe(Class<?> k, Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            System.out.printf("%s: %s, %s%n", k.getSimpleName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                              x.getClass().getSimpleName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                              y.getClass().getSimpleName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private static void realMain(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        for (Map<Integer, Boolean> x : maps())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            for (Map<Integer, Boolean> y : newConcurrentMaps()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                describe(Map.class, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                x.put(one, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                frob(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                frob(unmodifiableMap(x), y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                frob(synchronizedMap(x), y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                frob(x, synchronizedMap(y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                frob(checkedMap(x, Integer.class, Boolean.class), y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                frob(x, checkedMap(y, Integer.class, Boolean.class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                x.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                frob(newSetFromMap(x), newSetFromMap(y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                frob(x.keySet(), newSetFromMap(y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        for (Set<Integer> x : newSets())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            for (Set<Integer> y : newConcurrentSets()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                describe(Set.class, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                frob(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                frob(unmodifiableSet(x), y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                frob(synchronizedSet(x), y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                frob(x, synchronizedSet(y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                frob(checkedSet(x, Integer.class), y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                frob(x, checkedSet(y, Integer.class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        for (List<Integer> x : newLists())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            for (List<Integer> y : newConcurrentLists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                describe(List.class, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                frob(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                frob(unmodifiableList(x), y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                frob(synchronizedList(x), y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                frob(x, synchronizedList(y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                frob(checkedList(x, Integer.class), y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                frob(x, checkedList(y, Integer.class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        for (Queue<Integer> x : newQueues())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            for (Queue<Integer> y : newConcurrentQueues()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                describe(Queue.class, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                frob(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        for (Deque<Integer> x : newDeques())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            for (Deque<Integer> y : newConcurrentDeques()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                describe(Deque.class, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                frob(asLifoQueue(x), y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                frob(x, asLifoQueue(y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    static String toString(Object x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return ((x instanceof Collection) || (x instanceof Map)) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            x.getClass().getName() : x.toString();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        else fail(toString(x) + " not equal to " + toString(y));}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    static void notEqual(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (x == null ? y == null : x.equals(y))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            fail(toString(x) + " equal to " + toString(y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        else pass();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    private static abstract class CheckedThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        abstract void realRun() throws Throwable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            try { realRun(); } catch (Throwable t) { unexpected(t); }}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
}