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