jdk/test/java/util/Deque/ChorusLine.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 7668 d4a77089c587
child 41131 87edc8451f8a
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6672
diff changeset
     2
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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 6324846
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Deque implementations must behave isomorphically
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.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class ChorusLine {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private interface Tweaker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        void run(Deque<Integer> deq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 7668
diff changeset
    39
    private static final Tweaker[] tweakers = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        new Tweaker() { public void run(Deque<Integer> deq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            for (int i = 0; i < 7; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                deq.addLast(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            deq.removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            deq.removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            deq.addLast(7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            deq.addLast(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            Iterator<Integer> it = deq.descendingIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            equal(it.next(), 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            try {it.remove();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            catch (IllegalStateException e) {pass();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            deq.addLast(9);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            it = deq.descendingIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            equal(it.next(), 9);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            equal(it.next(), 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            try {it.remove();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            catch (IllegalStateException e) {pass();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            equal(it.next(), 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            System.out.println(deq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        new Tweaker() { public void run(Deque<Integer> deq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            deq.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            check(deq.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            check(deq.size() == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            check(! deq.iterator().hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            check(! deq.descendingIterator().hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            try {deq.iterator().next(); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            catch (NoSuchElementException e) {pass();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            try {deq.descendingIterator().next(); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            catch (NoSuchElementException e) {pass();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        new Tweaker() { public void run(Deque<Integer> deq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            for (int i = 0; i < 11; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                deq.add(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            Iterator<Integer> it = deq.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            equal(it.next(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            equal(it.next(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            deq.addFirst(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            deq.addFirst(-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            it = deq.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            equal(it.next(), -2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            equal(it.next(), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            equal(it.next(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            it = deq.descendingIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            try {it.remove(); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            catch (IllegalStateException e) {pass();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            equal(it.next(), 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            try {it.remove(); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            catch (IllegalStateException e) {pass();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            equal(it.next(), 9);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            equal(it.next(), 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            System.out.println(deq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        new Tweaker() { public void run(Deque<Integer> deq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            while (deq.size() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                Iterator<Integer> it = deq.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                it.next(); it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                it = deq.descendingIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                it.next(); it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            System.out.println(deq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Collection<Deque<Integer>> deqs = new ArrayDeque<Deque<Integer>>(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        deqs.add(new ArrayDeque<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        deqs.add(new LinkedList<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        deqs.add(new LinkedBlockingDeque<Integer>());
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   132
        deqs.add(new ConcurrentLinkedDeque<Integer>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        equal(deqs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        for (Tweaker tweaker : tweakers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            for (Deque<Integer> deq : deqs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                tweaker.run(deq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            equal(deqs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private static void equal(Iterable<Deque<Integer>> deqs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        Deque<Integer> prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        for (Deque<Integer> deq : deqs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (prev != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                equal(prev.isEmpty(), deq.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                equal(prev.size(), deq.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                equal(prev.toString(), deq.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            prev = deq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        Deque<Iterator<Integer>> its = new ArrayDeque<Iterator<Integer>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        for (Deque<Integer> deq : deqs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            its.addLast(deq.iterator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        equal(its);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        Deque<Iterator<Integer>> dits = new ArrayDeque<Iterator<Integer>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        for (Deque<Integer> deq : deqs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            dits.addLast(deq.descendingIterator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        equal(dits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private static void equal(Deque<Iterator<Integer>> its) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        Iterator<Integer> it0 = its.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        while (it0.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            Integer i = it0.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            for (Iterator<Integer> it : its)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                equal(it.next(), i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        for (Iterator<Integer> it : its) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            try {it.next(); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            catch (NoSuchElementException e) {pass();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
}