jdk/test/java/util/Deque/ChorusLine.java
author dl
Fri, 23 Sep 2016 13:24:33 -0700
changeset 41131 87edc8451f8a
parent 32649 2ee9017c7597
child 43522 f9c6f543c4db
permissions -rw-r--r--
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21 Reviewed-by: martin, chegar, shade
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
41131
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 32649
diff changeset
    31
import java.util.ArrayDeque;
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 32649
diff changeset
    32
import java.util.Collection;
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 32649
diff changeset
    33
import java.util.Deque;
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 32649
diff changeset
    34
import java.util.Iterator;
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 32649
diff changeset
    35
import java.util.LinkedList;
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 32649
diff changeset
    36
import java.util.NoSuchElementException;
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 32649
diff changeset
    37
import java.util.concurrent.ConcurrentLinkedDeque;
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 32649
diff changeset
    38
import java.util.concurrent.LinkedBlockingDeque;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class ChorusLine {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private interface Tweaker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        void run(Deque<Integer> deq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 7668
diff changeset
    45
    private static final Tweaker[] tweakers = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        new Tweaker() { public void run(Deque<Integer> deq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            for (int i = 0; i < 7; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                deq.addLast(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            deq.removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            deq.removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            deq.addLast(7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            deq.addLast(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            Iterator<Integer> it = deq.descendingIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            equal(it.next(), 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            try {it.remove();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            catch (IllegalStateException e) {pass();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            deq.addLast(9);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            it = deq.descendingIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            equal(it.next(), 9);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            equal(it.next(), 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            try {it.remove();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            catch (IllegalStateException e) {pass();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            equal(it.next(), 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            System.out.println(deq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        new Tweaker() { public void run(Deque<Integer> deq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            deq.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            check(deq.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            check(deq.size() == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            check(! deq.iterator().hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            check(! deq.descendingIterator().hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            try {deq.iterator().next(); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            catch (NoSuchElementException e) {pass();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            try {deq.descendingIterator().next(); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            catch (NoSuchElementException e) {pass();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        new Tweaker() { public void run(Deque<Integer> deq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            for (int i = 0; i < 11; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                deq.add(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            Iterator<Integer> it = deq.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            equal(it.next(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            equal(it.next(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            deq.addFirst(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            deq.addFirst(-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            it = deq.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            equal(it.next(), -2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            equal(it.next(), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            equal(it.next(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            it = deq.descendingIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            try {it.remove(); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            catch (IllegalStateException e) {pass();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            equal(it.next(), 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            try {it.remove(); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            catch (IllegalStateException e) {pass();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            equal(it.next(), 9);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            equal(it.next(), 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            System.out.println(deq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        new Tweaker() { public void run(Deque<Integer> deq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            while (deq.size() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                Iterator<Integer> it = deq.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                it.next(); it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                it = deq.descendingIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                it.next(); it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            System.out.println(deq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Collection<Deque<Integer>> deqs = new ArrayDeque<Deque<Integer>>(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        deqs.add(new ArrayDeque<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        deqs.add(new LinkedList<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        deqs.add(new LinkedBlockingDeque<Integer>());
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
   138
        deqs.add(new ConcurrentLinkedDeque<Integer>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        equal(deqs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        for (Tweaker tweaker : tweakers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            for (Deque<Integer> deq : deqs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                tweaker.run(deq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            equal(deqs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private static void equal(Iterable<Deque<Integer>> deqs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        Deque<Integer> prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        for (Deque<Integer> deq : deqs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (prev != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                equal(prev.isEmpty(), deq.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                equal(prev.size(), deq.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                equal(prev.toString(), deq.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            prev = deq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        Deque<Iterator<Integer>> its = new ArrayDeque<Iterator<Integer>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        for (Deque<Integer> deq : deqs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            its.addLast(deq.iterator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        equal(its);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        Deque<Iterator<Integer>> dits = new ArrayDeque<Iterator<Integer>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        for (Deque<Integer> deq : deqs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            dits.addLast(deq.descendingIterator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        equal(dits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private static void equal(Deque<Iterator<Integer>> its) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        Iterator<Integer> it0 = its.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        while (it0.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            Integer i = it0.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            for (Iterator<Integer> it : its)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                equal(it.next(), i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        for (Iterator<Integer> it : its) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            try {it.next(); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            catch (NoSuchElementException e) {pass();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
}