jdk/test/java/util/List/LockStep.java
author sherman
Wed, 17 Aug 2011 14:18:30 -0700
changeset 10344 9a26a761ef07
parent 5506 202f599c92aa
child 30046 cf2c86e1819e
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, 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: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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     6359979
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Compare List implementations for identical behavior
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
import static java.util.Collections.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
@SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class LockStep {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    final int DEFAULT_SIZE = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    int size;           // Running time is O(size**2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    int intArg(String[] args, int i, int defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        return args.length > i ? Integer.parseInt(args[i]) : defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    boolean maybe(int n) { return rnd.nextInt(n) == 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    void test(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        size = intArg(args, 0, DEFAULT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        lockSteps(new ArrayList(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                  new LinkedList(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                  new Vector());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    void equalLists(List... lists) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        for (List list : lists)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            equalLists(list, lists[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    void equalLists(List x, List y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        equal(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        equal(y, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        equal(x.size(),     y.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        equal(x.isEmpty(),  y.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        equal(x.hashCode(), y.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        equal(x.toString(), y.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        equal(x.toArray(),  y.toArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    void lockSteps(List... lists) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        for (int i = 0; i < lists.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            if (maybe(4)) lists[i] = serialClone(lists[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        for (final List list : lists)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            testEmptyList(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            ListFrobber adder = randomAdder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            for (final List list : lists) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                adder.frob(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                equal(list.size(), i+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            equalLists(lists);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            final ListFrobber adder = randomAdder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            final ListFrobber remover = randomRemover();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            for (final List list : lists) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                THROWS(ConcurrentModificationException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                       new F(){void f(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                           Iterator it = list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                           adder.frob(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                           it.next();}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                       new F(){void f(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                           Iterator it = asSubList(list).iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                           remover.frob(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                           it.next();}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                       new F(){void f(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                           Iterator it = asSubList(asSubList(list)).iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                           adder.frob(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                           it.next();}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                       new F(){void f(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                           List subList = asSubList(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                           remover.frob(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                           subList.get(0);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                       new F(){void f(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                           List sl = asSubList(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                           List ssl = asSubList(sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                           adder.frob(sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                           ssl.get(0);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                       new F(){void f(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                           List sl = asSubList(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                           List ssl = asSubList(sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                           remover.frob(sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                           ssl.get(0);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        for (final List l : lists) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            final List sl = asSubList(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            final List ssl = asSubList(sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            ssl.add(0, 42);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            equal(ssl.get(0), 42);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            equal(sl.get(0), 42);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            equal(l.get(0), 42);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            final int s = l.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            final int rndIndex = rnd.nextInt(l.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            THROWS(IndexOutOfBoundsException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                   new F(){void f(){l.subList(rndIndex, rndIndex).get(0);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                   new F(){void f(){l.subList(s/2, s).get(s/2 + 1);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                   new F(){void f(){l.subList(s/2, s).get(-1);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                   );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            THROWS(IllegalArgumentException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                   new F(){void f(){  l.subList(1, 0);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                   new F(){void f(){ sl.subList(1, 0);}},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                   new F(){void f(){ssl.subList(1, 0);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        equalLists(lists);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        for (final List list : lists) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            equalLists(list, asSubList(list));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            equalLists(list, asSubList(asSubList(list)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        for (final List list : lists)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            System.out.println(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        for (int i = lists[0].size(); i > 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            ListFrobber remover = randomRemover();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            for (final List list : lists)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                remover.frob(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            equalLists(lists);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    <T> List<T> asSubList(List<T> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return list.subList(0, list.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    void testEmptyCollection(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        check(c.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        equal(c.size(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        equal(c.toString(),"[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        equal(c.toArray().length, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        equal(c.toArray(new Object[0]).length, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        Object[] a = new Object[1]; a[0] = Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        equal(c.toArray(a), a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        equal(a[0], null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    void testEmptyList(List list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        testEmptyCollection(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        equal(list.hashCode(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        equal(list, Collections.emptyList());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    final Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    abstract class ListFrobber { abstract void frob(List l); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    ListFrobber randomAdder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        final Integer e = rnd.nextInt(1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        final int subListCount = rnd.nextInt(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        final boolean atBeginning = rnd.nextBoolean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        final boolean useIterator = rnd.nextBoolean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        final boolean simpleIterator = rnd.nextBoolean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        return new ListFrobber() {void frob(List l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            final int s = l.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            List ll = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            for (int i = 0; i < subListCount; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                ll = asSubList(ll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (! useIterator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                if (atBeginning) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    switch (rnd.nextInt(3)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    case 0: ll.add(0, e); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    case 1: ll.subList(0, rnd.nextInt(s+1)).add(0, e); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    case 2: ll.subList(0, rnd.nextInt(s+1)).subList(0,0).add(0,e); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    default: throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    switch (rnd.nextInt(3)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    case 0: check(ll.add(e)); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    case 1: ll.subList(s/2, s).add(s - s/2, e); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    case 2: ll.subList(s, s).subList(0, 0).add(0, e); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    default: throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                if (atBeginning) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    ListIterator it = ll.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    equal(it.nextIndex(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    check(! it.hasPrevious());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    it.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    equal(it.previousIndex(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    equal(it.nextIndex(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    check(it.hasPrevious());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    final int siz = ll.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    ListIterator it = ll.listIterator(siz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    equal(it.previousIndex(), siz-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    it.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    equal(it.previousIndex(), siz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    equal(it.nextIndex(), siz+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    check(it.hasPrevious());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    ListFrobber randomRemover() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        final int position = rnd.nextInt(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        final int subListCount = rnd.nextInt(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return new ListFrobber() {void frob(List l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            final int s = l.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            List ll = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            for (int i = 0; i < subListCount; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                ll = asSubList(ll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            switch (position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            case 0: // beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                switch (rnd.nextInt(3)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                case 0: ll.remove(0); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                case 1: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    final Iterator it = ll.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    THROWS(IllegalStateException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                           new F(){void f(){it.remove();}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    THROWS(IllegalStateException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                           new F(){void f(){it.remove();}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    break;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                case 2: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    final ListIterator it = ll.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    THROWS(IllegalStateException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                           new F(){void f(){it.remove();}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    THROWS(IllegalStateException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                           new F(){void f(){it.remove();}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    break;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                default: throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            case 1: // midpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                switch (rnd.nextInt(3)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                case 0: ll.remove(s/2); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                case 1: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    final ListIterator it = ll.listIterator(s/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                case 2: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    final ListIterator it = ll.listIterator(s/2+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    it.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                default: throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            case 2: // end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                switch (rnd.nextInt(3)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                case 0: ll.remove(s-1); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                case 1: ll.subList(s-1, s).clear(); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    final ListIterator it = ll.listIterator(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    check(it.hasPrevious());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    THROWS(IllegalStateException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                           new F(){void f(){it.remove();}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    it.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    equal(it.nextIndex(), s-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    equal(it.nextIndex(), s-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    THROWS(IllegalStateException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                           new F(){void f(){it.remove();}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                default: throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            default: throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    void fail(String msg) {System.err.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    <T> void equal(T[] x, T[] y) {check(Arrays.equals(x,y));}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        new LockStep().instanceMain(args);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    void instanceMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        try {test(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    abstract class F {abstract void f() throws Throwable;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    void THROWS(Class<? extends Throwable> k, F... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        for (F f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            try {f.f(); fail("Expected " + k.getName() + " not thrown");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                else unexpected(t);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    static byte[] serializedForm(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            new ObjectOutputStream(baos).writeObject(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        } catch (IOException e) { throw new RuntimeException(e); }}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    static Object readObject(byte[] bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        InputStream is = new ByteArrayInputStream(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return new ObjectInputStream(is).readObject();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    static <T> T serialClone(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        try { return (T) readObject(serializedForm(obj)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        catch (Exception e) { throw new RuntimeException(e); }}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
}