jdk/test/java/util/Collection/BiggernYours.java
author igerasim
Mon, 02 Jun 2014 19:49:57 +0400
changeset 24692 268fbc344d53
parent 14342 8435a30053c1
child 32649 2ee9017c7597
permissions -rw-r--r--
8037866: Replace the Fun class in tests with lambdas Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 14342
diff changeset
     2
 * Copyright (c) 2006, 2014, 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 6415641 6377302
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Concurrent collections are permitted to lie about their size
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 java.io.*;
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
@SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class BiggernYours {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 7668
diff changeset
    37
    static final Random rnd = new Random(18675309);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static void compareCollections(Collection c1, Collection c2) {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 7668
diff changeset
    40
        Object[] c1Array = c1.toArray();
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 7668
diff changeset
    41
        Object[] c2Array = c2.toArray();
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 7668
diff changeset
    42
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 7668
diff changeset
    43
        check(c1Array.length == c2Array.length);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 7668
diff changeset
    44
        for(Object aC1 : c1Array) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 7668
diff changeset
    45
            boolean found = false;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 7668
diff changeset
    46
            for(Object aC2 : c2Array) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 7668
diff changeset
    47
                if(Objects.equals(aC1, aC2)) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 7668
diff changeset
    48
                    found = true;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 7668
diff changeset
    49
                    break;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 7668
diff changeset
    50
                }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 7668
diff changeset
    51
            }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 7668
diff changeset
    52
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 7668
diff changeset
    53
            if(!found)
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 7668
diff changeset
    54
                fail(aC1 + " not found in " + Arrays.toString(c2Array));
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 7668
diff changeset
    55
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static void compareMaps(Map m1, Map m2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        compareCollections(m1.keySet(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                           m2.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        compareCollections(m1.values(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                           m2.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        compareCollections(m1.entrySet(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                           m2.entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static void compareNavigableMaps(NavigableMap m1, NavigableMap m2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        compareMaps(m1, m2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        compareMaps(m1.descendingMap(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    m2.descendingMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        compareMaps(m1.tailMap(Integer.MIN_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    m2.tailMap(Integer.MIN_VALUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        compareMaps(m1.headMap(Integer.MAX_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    m2.headMap(Integer.MAX_VALUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static void compareNavigableSets(NavigableSet s1, NavigableSet s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        compareCollections(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        compareCollections(s1.descendingSet(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                           s2.descendingSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        compareCollections(s1.tailSet(Integer.MIN_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                           s2.tailSet(Integer.MIN_VALUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    static abstract class MapFrobber { abstract void frob(Map m); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static abstract class SetFrobber { abstract void frob(Set s); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static abstract class ColFrobber { abstract void frob(Collection c); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static ColFrobber adder(final int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return new ColFrobber() {void frob(Collection c) { c.add(i); }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    static final ColFrobber[] adders =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    { adder(1), adder(3), adder(2) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static MapFrobber putter(final int k, final int v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return new MapFrobber() {void frob(Map m) { m.put(k,v); }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    static final MapFrobber[] putters =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    { putter(1, -2), putter(3, -6), putter(2, -4) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    static void unexpected(Throwable t, Object suspect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        System.out.println(suspect.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        unexpected(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    static void testCollections(Collection c1, Collection c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            compareCollections(c1, c2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            for (ColFrobber adder : adders) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                for (Collection c : new Collection[]{c1, c2})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    adder.frob(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                compareCollections(c1, c2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } catch (Throwable t) { unexpected(t, c1); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    static void testNavigableSets(NavigableSet s1, NavigableSet s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            compareNavigableSets(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            for (ColFrobber adder : adders) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                for (Set s : new Set[]{s1, s2})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    adder.frob(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                compareNavigableSets(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        } catch (Throwable t) { unexpected(t, s1); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    static void testMaps(Map m1, Map m2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            compareMaps(m1, m2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            for (MapFrobber putter : putters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                for (Map m : new Map[]{m1, m2})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    putter.frob(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                compareMaps(m1, m2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } catch (Throwable t) { unexpected(t, m1); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    static void testNavigableMaps(NavigableMap m1, NavigableMap m2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            compareNavigableMaps(m1, m2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            for (MapFrobber putter : putters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                for (Map m : new Map[]{m1, m2})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    putter.frob(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                compareNavigableMaps(m1, m2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } catch (Throwable t) { unexpected(t, m1); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    static int randomize(int size) { return rnd.nextInt(size + 2); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    @SuppressWarnings("serial")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        testNavigableMaps(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            new ConcurrentSkipListMap(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            new ConcurrentSkipListMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                public int size() {return randomize(super.size());}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        testNavigableSets(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            new ConcurrentSkipListSet(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            new ConcurrentSkipListSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                public int size() {return randomize(super.size());}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        testCollections(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            new CopyOnWriteArraySet(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            new CopyOnWriteArraySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                public int size() {return randomize(super.size());}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        testCollections(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            new CopyOnWriteArrayList(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            new CopyOnWriteArrayList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                public int size() {return randomize(super.size());}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        testCollections(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            new TreeSet(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            new TreeSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                public int size() {return randomize(super.size());}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        testMaps(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            new ConcurrentHashMap(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            new ConcurrentHashMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                public int size() {return randomize(super.size());}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        testCollections(
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   187
            new ConcurrentLinkedDeque(),
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   188
            new ConcurrentLinkedDeque() {
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   189
                public int size() {return randomize(super.size());}});
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   190
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   191
        testCollections(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            new ConcurrentLinkedQueue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            new ConcurrentLinkedQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                public int size() {return randomize(super.size());}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   196
        testCollections(
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   197
            new LinkedTransferQueue(),
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   198
            new LinkedTransferQueue() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
   199
                public int size() {return randomize(super.size());}});
3708
f838f712922e 6868712: Improve concurrent queue tests
dl
parents: 2
diff changeset
   200
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        testCollections(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            new LinkedBlockingQueue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            new LinkedBlockingQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                public int size() {return randomize(super.size());}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        testCollections(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            new LinkedBlockingDeque(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            new LinkedBlockingDeque() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                public int size() {return randomize(super.size());}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        testCollections(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            new ArrayBlockingQueue(5),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            new ArrayBlockingQueue(5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                public int size() {return randomize(super.size());}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        testCollections(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            new PriorityBlockingQueue(5),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            new PriorityBlockingQueue(5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                public int size() {return randomize(super.size());}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    static void arrayEqual(Object[] x, Object[] y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (x == null ? y == null : Arrays.equals(x, y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        else fail(Arrays.toString(x) + " not equal to " + Arrays.toString(y));}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private static abstract class CheckedThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        abstract void realRun() throws Throwable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            try {realRun();} catch (Throwable t) {unexpected(t);}}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
}