jdk/test/java/util/concurrent/tck/LinkedBlockingDequeTest.java
author dl
Mon, 10 Apr 2017 13:46:19 -0700
changeset 44589 64d9270bd24c
parent 42319 0193886267c3
child 45937 646816090183
permissions -rw-r--r--
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04 Reviewed-by: martin, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     1
/*
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     3
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     6
 * published by the Free Software Foundation.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     7
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    12
 * accompanied this code).
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    13
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    17
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    20
 * questions.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    21
 */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    22
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    23
/*
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    27
 * file:
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    28
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    29
 * Written by Doug Lea with assistance from members of JCP JSR-166
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    30
 * Expert Group and released to the public domain, as explained at
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    31
 * http://creativecommons.org/publicdomain/zero/1.0/
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    32
 */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    33
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    34
import static java.util.concurrent.TimeUnit.MILLISECONDS;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    35
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    36
import java.util.ArrayList;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    37
import java.util.Arrays;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    38
import java.util.Collection;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    39
import java.util.Deque;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    40
import java.util.Iterator;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    41
import java.util.NoSuchElementException;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    42
import java.util.Queue;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    43
import java.util.concurrent.BlockingDeque;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    44
import java.util.concurrent.BlockingQueue;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    45
import java.util.concurrent.CountDownLatch;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    46
import java.util.concurrent.Executors;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    47
import java.util.concurrent.ExecutorService;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    48
import java.util.concurrent.LinkedBlockingDeque;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    49
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    50
import junit.framework.Test;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    51
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    52
public class LinkedBlockingDequeTest extends JSR166TestCase {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    53
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    54
    public static class Unbounded extends BlockingQueueTest {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    55
        protected BlockingQueue emptyCollection() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    56
            return new LinkedBlockingDeque();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    57
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    58
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    59
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    60
    public static class Bounded extends BlockingQueueTest {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    61
        protected BlockingQueue emptyCollection() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    62
            return new LinkedBlockingDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    63
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    64
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    65
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    66
    public static void main(String[] args) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    67
        main(suite(), args);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    68
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    69
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    70
    public static Test suite() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40280
diff changeset
    71
        class Implementation implements CollectionImplementation {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40280
diff changeset
    72
            public Class<?> klazz() { return LinkedBlockingDeque.class; }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40280
diff changeset
    73
            public Collection emptyCollection() { return new LinkedBlockingDeque(); }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40280
diff changeset
    74
            public Object makeElement(int i) { return i; }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40280
diff changeset
    75
            public boolean isConcurrent() { return true; }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40280
diff changeset
    76
            public boolean permitsNulls() { return false; }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40280
diff changeset
    77
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    78
        return newTestSuite(LinkedBlockingDequeTest.class,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    79
                            new Unbounded().testSuite(),
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40280
diff changeset
    80
                            new Bounded().testSuite(),
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40280
diff changeset
    81
                            CollectionTest.testSuite(new Implementation()));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    82
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    83
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    84
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    85
     * Returns a new deque of given size containing consecutive
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40280
diff changeset
    86
     * Integers 0 ... n - 1.
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    87
     */
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 42319
diff changeset
    88
    private static LinkedBlockingDeque<Integer> populatedDeque(int n) {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    89
        LinkedBlockingDeque<Integer> q =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    90
            new LinkedBlockingDeque<Integer>(n);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    91
        assertTrue(q.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    92
        for (int i = 0; i < n; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    93
            assertTrue(q.offer(new Integer(i)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    94
        assertFalse(q.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    95
        assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    96
        assertEquals(n, q.size());
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40280
diff changeset
    97
        assertEquals((Integer) 0, q.peekFirst());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40280
diff changeset
    98
        assertEquals((Integer) (n - 1), q.peekLast());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    99
        return q;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   100
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   101
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   102
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   103
     * isEmpty is true before add, false after
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   104
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   105
    public void testEmpty() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   106
        LinkedBlockingDeque q = new LinkedBlockingDeque();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   107
        assertTrue(q.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   108
        q.add(new Integer(1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   109
        assertFalse(q.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   110
        q.add(new Integer(2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   111
        q.removeFirst();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   112
        q.removeFirst();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   113
        assertTrue(q.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   114
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   115
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   116
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   117
     * size changes when elements added and removed
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   118
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   119
    public void testSize() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   120
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   121
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   122
            assertEquals(SIZE - i, q.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   123
            q.removeFirst();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   124
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   125
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   126
            assertEquals(i, q.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   127
            q.add(new Integer(i));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   128
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   129
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   130
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   131
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   132
     * offerFirst(null) throws NullPointerException
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   133
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   134
    public void testOfferFirstNull() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   135
        LinkedBlockingDeque q = new LinkedBlockingDeque();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   136
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   137
            q.offerFirst(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   138
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   139
        } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   140
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   141
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   142
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   143
     * offerLast(null) throws NullPointerException
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   144
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   145
    public void testOfferLastNull() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   146
        LinkedBlockingDeque q = new LinkedBlockingDeque();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   147
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   148
            q.offerLast(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   149
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   150
        } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   151
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   152
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   153
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   154
     * OfferFirst succeeds
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   155
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   156
    public void testOfferFirst() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   157
        LinkedBlockingDeque q = new LinkedBlockingDeque();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   158
        assertTrue(q.offerFirst(new Integer(0)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   159
        assertTrue(q.offerFirst(new Integer(1)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   160
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   161
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   162
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   163
     * OfferLast succeeds
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   164
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   165
    public void testOfferLast() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   166
        LinkedBlockingDeque q = new LinkedBlockingDeque();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   167
        assertTrue(q.offerLast(new Integer(0)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   168
        assertTrue(q.offerLast(new Integer(1)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   169
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   170
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   171
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   172
     * pollFirst succeeds unless empty
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   173
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   174
    public void testPollFirst() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   175
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   176
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   177
            assertEquals(i, q.pollFirst());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   178
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   179
        assertNull(q.pollFirst());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   180
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   181
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   182
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   183
     * pollLast succeeds unless empty
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   184
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   185
    public void testPollLast() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   186
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   187
        for (int i = SIZE - 1; i >= 0; --i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   188
            assertEquals(i, q.pollLast());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   189
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   190
        assertNull(q.pollLast());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   191
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   192
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   193
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   194
     * peekFirst returns next element, or null if empty
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   195
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   196
    public void testPeekFirst() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   197
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   198
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   199
            assertEquals(i, q.peekFirst());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   200
            assertEquals(i, q.pollFirst());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   201
            assertTrue(q.peekFirst() == null ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   202
                       !q.peekFirst().equals(i));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   203
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   204
        assertNull(q.peekFirst());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   205
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   206
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   207
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   208
     * peek returns next element, or null if empty
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   209
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   210
    public void testPeek() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   211
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   212
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   213
            assertEquals(i, q.peek());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   214
            assertEquals(i, q.pollFirst());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   215
            assertTrue(q.peek() == null ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   216
                       !q.peek().equals(i));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   217
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   218
        assertNull(q.peek());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   219
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   220
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   221
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   222
     * peekLast returns next element, or null if empty
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   223
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   224
    public void testPeekLast() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   225
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   226
        for (int i = SIZE - 1; i >= 0; --i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   227
            assertEquals(i, q.peekLast());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   228
            assertEquals(i, q.pollLast());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   229
            assertTrue(q.peekLast() == null ||
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   230
                       !q.peekLast().equals(i));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   231
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   232
        assertNull(q.peekLast());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   233
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   234
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   235
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   236
     * getFirst() returns first element, or throws NSEE if empty
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   237
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   238
    public void testFirstElement() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   239
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   240
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   241
            assertEquals(i, q.getFirst());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   242
            assertEquals(i, q.pollFirst());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   243
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   244
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   245
            q.getFirst();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   246
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   247
        } catch (NoSuchElementException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   248
        assertNull(q.peekFirst());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   249
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   250
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   251
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   252
     * getLast() returns last element, or throws NSEE if empty
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   253
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   254
    public void testLastElement() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   255
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   256
        for (int i = SIZE - 1; i >= 0; --i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   257
            assertEquals(i, q.getLast());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   258
            assertEquals(i, q.pollLast());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   259
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   260
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   261
            q.getLast();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   262
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   263
        } catch (NoSuchElementException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   264
        assertNull(q.peekLast());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   265
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   266
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   267
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   268
     * removeFirst() removes first element, or throws NSEE if empty
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   269
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   270
    public void testRemoveFirst() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   271
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   272
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   273
            assertEquals(i, q.removeFirst());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   274
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   275
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   276
            q.removeFirst();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   277
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   278
        } catch (NoSuchElementException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   279
        assertNull(q.peekFirst());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   280
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   281
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   282
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   283
     * removeLast() removes last element, or throws NSEE if empty
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   284
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   285
    public void testRemoveLast() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   286
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   287
        for (int i = SIZE - 1; i >= 0; --i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   288
            assertEquals(i, q.removeLast());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   289
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   290
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   291
            q.removeLast();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   292
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   293
        } catch (NoSuchElementException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   294
        assertNull(q.peekLast());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   295
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   296
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   297
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   298
     * remove removes next element, or throws NSEE if empty
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   299
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   300
    public void testRemove() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   301
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   302
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   303
            assertEquals(i, q.remove());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   304
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   305
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   306
            q.remove();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   307
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   308
        } catch (NoSuchElementException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   309
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   310
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   311
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   312
     * removeFirstOccurrence(x) removes x and returns true if present
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   313
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   314
    public void testRemoveFirstOccurrence() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   315
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   316
        for (int i = 1; i < SIZE; i += 2) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   317
            assertTrue(q.removeFirstOccurrence(new Integer(i)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   318
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   319
        for (int i = 0; i < SIZE; i += 2) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   320
            assertTrue(q.removeFirstOccurrence(new Integer(i)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   321
            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   322
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   323
        assertTrue(q.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   324
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   325
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   326
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   327
     * removeLastOccurrence(x) removes x and returns true if present
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   328
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   329
    public void testRemoveLastOccurrence() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   330
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   331
        for (int i = 1; i < SIZE; i += 2) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   332
            assertTrue(q.removeLastOccurrence(new Integer(i)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   333
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   334
        for (int i = 0; i < SIZE; i += 2) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   335
            assertTrue(q.removeLastOccurrence(new Integer(i)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   336
            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   337
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   338
        assertTrue(q.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   339
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   340
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   341
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   342
     * peekFirst returns element inserted with addFirst
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   343
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   344
    public void testAddFirst() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   345
        LinkedBlockingDeque q = populatedDeque(3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   346
        q.pollLast();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   347
        q.addFirst(four);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   348
        assertSame(four, q.peekFirst());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   349
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   350
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   351
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   352
     * peekLast returns element inserted with addLast
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   353
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   354
    public void testAddLast() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   355
        LinkedBlockingDeque q = populatedDeque(3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   356
        q.pollLast();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   357
        q.addLast(four);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   358
        assertSame(four, q.peekLast());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   359
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   360
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   361
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   362
     * A new deque has the indicated capacity, or Integer.MAX_VALUE if
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   363
     * none given
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   364
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   365
    public void testConstructor1() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   366
        assertEquals(SIZE, new LinkedBlockingDeque(SIZE).remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   367
        assertEquals(Integer.MAX_VALUE, new LinkedBlockingDeque().remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   368
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   369
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   370
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   371
     * Constructor throws IllegalArgumentException if capacity argument nonpositive
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   372
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   373
    public void testConstructor2() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   374
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   375
            new LinkedBlockingDeque(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   376
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   377
        } catch (IllegalArgumentException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   378
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   379
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   380
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   381
     * Initializing from null Collection throws NullPointerException
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   382
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   383
    public void testConstructor3() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   384
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   385
            new LinkedBlockingDeque(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   386
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   387
        } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   388
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   389
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   390
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   391
     * Initializing from Collection of null elements throws NullPointerException
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   392
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   393
    public void testConstructor4() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   394
        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   395
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   396
            new LinkedBlockingDeque(elements);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   397
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   398
        } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   399
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   400
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   401
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   402
     * Initializing from Collection with some null elements throws
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   403
     * NullPointerException
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   404
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   405
    public void testConstructor5() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   406
        Integer[] ints = new Integer[SIZE];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   407
        for (int i = 0; i < SIZE - 1; ++i)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   408
            ints[i] = i;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   409
        Collection<Integer> elements = Arrays.asList(ints);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   410
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   411
            new LinkedBlockingDeque(elements);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   412
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   413
        } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   414
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   415
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   416
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   417
     * Deque contains all elements of collection used to initialize
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   418
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   419
    public void testConstructor6() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   420
        Integer[] ints = new Integer[SIZE];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   421
        for (int i = 0; i < SIZE; ++i)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   422
            ints[i] = i;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   423
        LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   424
        for (int i = 0; i < SIZE; ++i)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   425
            assertEquals(ints[i], q.poll());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   426
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   427
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   428
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   429
     * Deque transitions from empty to full when elements added
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   430
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   431
    public void testEmptyFull() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   432
        LinkedBlockingDeque q = new LinkedBlockingDeque(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   433
        assertTrue(q.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   434
        assertEquals("should have room for 2", 2, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   435
        q.add(one);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   436
        assertFalse(q.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   437
        q.add(two);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   438
        assertFalse(q.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   439
        assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   440
        assertFalse(q.offer(three));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   441
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   442
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   443
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   444
     * remainingCapacity decreases on add, increases on remove
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   445
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   446
    public void testRemainingCapacity() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   447
        BlockingQueue q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   448
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   449
            assertEquals(i, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   450
            assertEquals(SIZE, q.size() + q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   451
            assertEquals(i, q.remove());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   452
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   453
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   454
            assertEquals(SIZE - i, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   455
            assertEquals(SIZE, q.size() + q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   456
            assertTrue(q.add(i));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   457
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   458
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   459
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   460
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   461
     * push(null) throws NPE
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   462
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   463
    public void testPushNull() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   464
        LinkedBlockingDeque q = new LinkedBlockingDeque(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   465
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   466
            q.push(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   467
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   468
        } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   469
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   470
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   471
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   472
     * push succeeds if not full; throws ISE if full
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   473
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   474
    public void testPush() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   475
        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   476
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   477
            Integer x = new Integer(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   478
            q.push(x);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   479
            assertEquals(x, q.peek());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   480
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   481
        assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   482
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   483
            q.push(new Integer(SIZE));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   484
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   485
        } catch (IllegalStateException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   486
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   487
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   488
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   489
     * peekFirst returns element inserted with push
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   490
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   491
    public void testPushWithPeek() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   492
        LinkedBlockingDeque q = populatedDeque(3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   493
        q.pollLast();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   494
        q.push(four);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   495
        assertSame(four, q.peekFirst());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   496
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   497
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   498
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   499
     * pop removes next element, or throws NSEE if empty
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   500
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   501
    public void testPop() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   502
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   503
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   504
            assertEquals(i, q.pop());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   505
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   506
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   507
            q.pop();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   508
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   509
        } catch (NoSuchElementException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   510
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   511
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   512
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   513
     * Offer succeeds if not full; fails if full
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   514
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   515
    public void testOffer() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   516
        LinkedBlockingDeque q = new LinkedBlockingDeque(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   517
        assertTrue(q.offer(zero));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   518
        assertFalse(q.offer(one));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   519
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   520
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   521
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   522
     * add succeeds if not full; throws ISE if full
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   523
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   524
    public void testAdd() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   525
        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   526
        for (int i = 0; i < SIZE; ++i)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   527
            assertTrue(q.add(new Integer(i)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   528
        assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   529
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   530
            q.add(new Integer(SIZE));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   531
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   532
        } catch (IllegalStateException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   533
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   534
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   535
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   536
     * addAll(this) throws IAE
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   537
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   538
    public void testAddAllSelf() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   539
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   540
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   541
            q.addAll(q);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   542
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   543
        } catch (IllegalArgumentException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   544
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   545
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   546
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   547
     * addAll of a collection with any null elements throws NPE after
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   548
     * possibly adding some elements
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   549
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   550
    public void testAddAll3() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   551
        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   552
        Integer[] ints = new Integer[SIZE];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   553
        for (int i = 0; i < SIZE - 1; ++i)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   554
            ints[i] = new Integer(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   555
        Collection<Integer> elements = Arrays.asList(ints);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   556
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   557
            q.addAll(elements);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   558
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   559
        } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   560
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   561
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   562
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   563
     * addAll throws IllegalStateException if not enough room
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   564
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   565
    public void testAddAll4() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   566
        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE - 1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   567
        Integer[] ints = new Integer[SIZE];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   568
        for (int i = 0; i < SIZE; ++i)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   569
            ints[i] = new Integer(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   570
        Collection<Integer> elements = Arrays.asList(ints);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   571
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   572
            q.addAll(elements);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   573
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   574
        } catch (IllegalStateException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   575
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   576
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   577
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   578
     * Deque contains all elements, in traversal order, of successful addAll
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   579
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   580
    public void testAddAll5() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   581
        Integer[] empty = new Integer[0];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   582
        Integer[] ints = new Integer[SIZE];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   583
        for (int i = 0; i < SIZE; ++i)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   584
            ints[i] = new Integer(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   585
        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   586
        assertFalse(q.addAll(Arrays.asList(empty)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   587
        assertTrue(q.addAll(Arrays.asList(ints)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   588
        for (int i = 0; i < SIZE; ++i)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   589
            assertEquals(ints[i], q.poll());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   590
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   591
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   592
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   593
     * all elements successfully put are contained
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   594
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   595
    public void testPut() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   596
        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   597
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   598
            Integer x = new Integer(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   599
            q.put(x);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   600
            assertTrue(q.contains(x));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   601
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   602
        assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   603
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   604
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   605
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   606
     * put blocks interruptibly if full
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   607
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   608
    public void testBlockingPut() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   609
        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   610
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   611
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   612
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   613
                for (int i = 0; i < SIZE; ++i)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   614
                    q.put(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   615
                assertEquals(SIZE, q.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   616
                assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   617
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   618
                Thread.currentThread().interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   619
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   620
                    q.put(99);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   621
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   622
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   623
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   624
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   625
                pleaseInterrupt.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   626
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   627
                    q.put(99);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   628
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   629
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   630
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   631
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   632
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   633
        await(pleaseInterrupt);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   634
        assertThreadStaysAlive(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   635
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   636
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   637
        assertEquals(SIZE, q.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   638
        assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   639
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   640
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   641
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   642
     * put blocks interruptibly waiting for take when full
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   643
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   644
    public void testPutWithTake() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   645
        final int capacity = 2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   646
        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   647
        final CountDownLatch pleaseTake = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   648
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   649
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   650
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   651
                for (int i = 0; i < capacity; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   652
                    q.put(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   653
                pleaseTake.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   654
                q.put(86);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   655
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   656
                pleaseInterrupt.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   657
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   658
                    q.put(99);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   659
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   660
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   661
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   662
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   663
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   664
        await(pleaseTake);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   665
        assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   666
        assertEquals(0, q.take());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   667
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   668
        await(pleaseInterrupt);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   669
        assertThreadStaysAlive(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   670
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   671
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   672
        assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   673
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   674
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   675
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   676
     * timed offer times out if full and elements not taken
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   677
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   678
    public void testTimedOffer() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   679
        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   680
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   681
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   682
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   683
                q.put(new Object());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   684
                q.put(new Object());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   685
                long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   686
                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   687
                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   688
                pleaseInterrupt.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   689
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   690
                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   691
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   692
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   693
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   694
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   695
        await(pleaseInterrupt);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   696
        assertThreadStaysAlive(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   697
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   698
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   699
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   700
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   701
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   702
     * take retrieves elements in FIFO order
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   703
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   704
    public void testTake() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   705
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   706
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   707
            assertEquals(i, q.take());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   708
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   709
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   710
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   711
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   712
     * take removes existing elements until empty, then blocks interruptibly
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   713
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   714
    public void testBlockingTake() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   715
        final LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   716
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   717
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   718
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   719
                for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   720
                    assertEquals(i, q.take());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   721
                }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   722
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   723
                Thread.currentThread().interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   724
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   725
                    q.take();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   726
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   727
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   728
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   729
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   730
                pleaseInterrupt.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   731
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   732
                    q.take();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   733
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   734
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   735
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   736
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   737
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   738
        await(pleaseInterrupt);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   739
        assertThreadStaysAlive(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   740
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   741
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   742
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   743
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   744
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   745
     * poll succeeds unless empty
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   746
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   747
    public void testPoll() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   748
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   749
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   750
            assertEquals(i, q.poll());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   751
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   752
        assertNull(q.poll());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   753
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   754
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   755
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   756
     * timed poll with zero timeout succeeds when non-empty, else times out
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   757
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   758
    public void testTimedPoll0() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   759
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   760
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   761
            assertEquals(i, q.poll(0, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   762
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   763
        assertNull(q.poll(0, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   764
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   765
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   766
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   767
     * timed poll with nonzero timeout succeeds when non-empty, else times out
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   768
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   769
    public void testTimedPoll() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   770
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   771
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   772
            long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   773
            assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   774
            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   775
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   776
        long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   777
        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   778
        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   779
        checkEmpty(q);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   780
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   781
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   782
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   783
     * Interrupted timed poll throws InterruptedException instead of
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   784
     * returning timeout status
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   785
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   786
    public void testInterruptedTimedPoll() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   787
        final BlockingQueue<Integer> q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   788
        final CountDownLatch aboutToWait = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   789
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   790
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   791
                long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   792
                for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   793
                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   794
                }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   795
                aboutToWait.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   796
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   797
                    q.poll(LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   798
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   799
                } catch (InterruptedException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   800
                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   801
                }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   802
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   803
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 42319
diff changeset
   804
        await(aboutToWait);
40280
5542e4584972 8162627: Miscellaneous changes imported from jsr166 CVS 2016-08
dl
parents: 35394
diff changeset
   805
        waitForThreadToEnterWaitState(t);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   806
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   807
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   808
        checkEmpty(q);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   809
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   810
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   811
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   812
     * putFirst(null) throws NPE
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   813
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   814
    public void testPutFirstNull() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   815
        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   816
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   817
            q.putFirst(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   818
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   819
        } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   820
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   821
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   822
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   823
     * all elements successfully putFirst are contained
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   824
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   825
    public void testPutFirst() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   826
        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   827
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   828
            Integer x = new Integer(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   829
            q.putFirst(x);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   830
            assertTrue(q.contains(x));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   831
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   832
        assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   833
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   834
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   835
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   836
     * putFirst blocks interruptibly if full
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   837
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   838
    public void testBlockingPutFirst() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   839
        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   840
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   841
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   842
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   843
                for (int i = 0; i < SIZE; ++i)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   844
                    q.putFirst(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   845
                assertEquals(SIZE, q.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   846
                assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   847
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   848
                Thread.currentThread().interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   849
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   850
                    q.putFirst(99);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   851
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   852
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   853
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   854
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   855
                pleaseInterrupt.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   856
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   857
                    q.putFirst(99);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   858
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   859
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   860
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   861
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   862
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   863
        await(pleaseInterrupt);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   864
        assertThreadStaysAlive(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   865
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   866
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   867
        assertEquals(SIZE, q.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   868
        assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   869
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   870
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   871
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   872
     * putFirst blocks interruptibly waiting for take when full
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   873
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   874
    public void testPutFirstWithTake() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   875
        final int capacity = 2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   876
        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   877
        final CountDownLatch pleaseTake = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   878
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   879
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   880
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   881
                for (int i = 0; i < capacity; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   882
                    q.putFirst(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   883
                pleaseTake.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   884
                q.putFirst(86);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   885
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   886
                pleaseInterrupt.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   887
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   888
                    q.putFirst(99);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   889
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   890
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   891
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   892
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   893
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   894
        await(pleaseTake);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   895
        assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   896
        assertEquals(capacity - 1, q.take());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   897
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   898
        await(pleaseInterrupt);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   899
        assertThreadStaysAlive(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   900
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   901
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   902
        assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   903
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   904
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   905
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   906
     * timed offerFirst times out if full and elements not taken
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   907
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   908
    public void testTimedOfferFirst() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   909
        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   910
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   911
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   912
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   913
                q.putFirst(new Object());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   914
                q.putFirst(new Object());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   915
                long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   916
                assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   917
                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   918
                pleaseInterrupt.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   919
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   920
                    q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   921
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   922
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   923
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   924
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   925
        await(pleaseInterrupt);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   926
        assertThreadStaysAlive(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   927
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   928
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   929
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   930
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   931
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   932
     * take retrieves elements in FIFO order
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   933
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   934
    public void testTakeFirst() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   935
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   936
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   937
            assertEquals(i, q.takeFirst());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   938
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   939
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   940
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   941
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   942
     * takeFirst() blocks interruptibly when empty
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   943
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   944
    public void testTakeFirstFromEmptyBlocksInterruptibly() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   945
        final BlockingDeque q = new LinkedBlockingDeque();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   946
        final CountDownLatch threadStarted = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   947
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   948
            public void realRun() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   949
                threadStarted.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   950
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   951
                    q.takeFirst();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   952
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   953
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   954
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   955
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   956
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   957
        await(threadStarted);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   958
        assertThreadStaysAlive(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   959
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   960
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   961
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   962
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   963
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   964
     * takeFirst() throws InterruptedException immediately if interrupted
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   965
     * before waiting
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   966
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   967
    public void testTakeFirstFromEmptyAfterInterrupt() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   968
        final BlockingDeque q = new LinkedBlockingDeque();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   969
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   970
            public void realRun() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   971
                Thread.currentThread().interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   972
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   973
                    q.takeFirst();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   974
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   975
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   976
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   977
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   978
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   979
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   980
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   981
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   982
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   983
     * takeLast() blocks interruptibly when empty
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   984
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   985
    public void testTakeLastFromEmptyBlocksInterruptibly() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   986
        final BlockingDeque q = new LinkedBlockingDeque();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   987
        final CountDownLatch threadStarted = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   988
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   989
            public void realRun() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   990
                threadStarted.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   991
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   992
                    q.takeLast();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   993
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   994
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   995
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   996
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   997
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   998
        await(threadStarted);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   999
        assertThreadStaysAlive(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1000
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1001
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1002
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1003
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1004
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1005
     * takeLast() throws InterruptedException immediately if interrupted
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1006
     * before waiting
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1007
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1008
    public void testTakeLastFromEmptyAfterInterrupt() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1009
        final BlockingDeque q = new LinkedBlockingDeque();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1010
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1011
            public void realRun() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1012
                Thread.currentThread().interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1013
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1014
                    q.takeLast();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1015
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1016
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1017
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1018
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1019
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1020
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1021
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1022
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1023
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1024
     * takeFirst removes existing elements until empty, then blocks interruptibly
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1025
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1026
    public void testBlockingTakeFirst() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1027
        final LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1028
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1029
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1030
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1031
                for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1032
                    assertEquals(i, q.takeFirst());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1033
                }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1034
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1035
                Thread.currentThread().interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1036
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1037
                    q.takeFirst();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1038
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1039
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1040
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1041
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1042
                pleaseInterrupt.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1043
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1044
                    q.takeFirst();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1045
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1046
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1047
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1048
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1049
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1050
        await(pleaseInterrupt);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1051
        assertThreadStaysAlive(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1052
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1053
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1054
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1055
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1056
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1057
     * timed pollFirst with zero timeout succeeds when non-empty, else times out
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1058
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1059
    public void testTimedPollFirst0() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1060
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1061
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1062
            assertEquals(i, q.pollFirst(0, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1063
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1064
        assertNull(q.pollFirst(0, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1065
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1066
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1067
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1068
     * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1069
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1070
    public void testTimedPollFirst() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1071
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1072
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1073
            long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1074
            assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1075
            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1076
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1077
        long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1078
        assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1079
        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1080
        checkEmpty(q);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1081
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1082
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1083
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1084
     * Interrupted timed pollFirst throws InterruptedException instead of
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1085
     * returning timeout status
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1086
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1087
    public void testInterruptedTimedPollFirst() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1088
        final LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1089
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1090
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1091
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1092
                long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1093
                for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1094
                    assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1095
                }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1096
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1097
                Thread.currentThread().interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1098
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1099
                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1100
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1101
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1102
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1103
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1104
                pleaseInterrupt.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1105
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1106
                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1107
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1108
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1109
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1110
                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1111
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1112
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1113
        await(pleaseInterrupt);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1114
        assertThreadStaysAlive(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1115
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1116
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1117
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1118
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1119
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1120
     * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1121
     * on interruption throws
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1122
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1123
    public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1124
        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1125
        final CheckedBarrier barrier = new CheckedBarrier(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1126
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1127
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1128
                long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1129
                assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1130
                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1131
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1132
                barrier.await();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1133
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1134
                assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1135
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1136
                Thread.currentThread().interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1137
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1138
                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1139
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1140
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1141
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1142
                barrier.await();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1143
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1144
                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1145
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1146
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1147
                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1148
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1149
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1150
        barrier.await();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1151
        long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1152
        assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1153
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1154
        barrier.await();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1155
        assertThreadStaysAlive(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1156
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1157
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1158
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1159
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1160
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1161
     * putLast(null) throws NPE
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1162
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1163
    public void testPutLastNull() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1164
        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1165
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1166
            q.putLast(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1167
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1168
        } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1169
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1170
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1171
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1172
     * all elements successfully putLast are contained
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1173
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1174
    public void testPutLast() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1175
        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1176
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1177
            Integer x = new Integer(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1178
            q.putLast(x);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1179
            assertTrue(q.contains(x));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1180
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1181
        assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1182
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1183
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1184
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1185
     * putLast blocks interruptibly if full
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1186
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1187
    public void testBlockingPutLast() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1188
        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1189
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1190
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1191
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1192
                for (int i = 0; i < SIZE; ++i)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1193
                    q.putLast(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1194
                assertEquals(SIZE, q.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1195
                assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1196
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1197
                Thread.currentThread().interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1198
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1199
                    q.putLast(99);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1200
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1201
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1202
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1203
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1204
                pleaseInterrupt.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1205
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1206
                    q.putLast(99);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1207
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1208
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1209
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1210
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1211
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1212
        await(pleaseInterrupt);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1213
        assertThreadStaysAlive(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1214
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1215
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1216
        assertEquals(SIZE, q.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1217
        assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1218
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1219
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1220
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1221
     * putLast blocks interruptibly waiting for take when full
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1222
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1223
    public void testPutLastWithTake() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1224
        final int capacity = 2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1225
        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1226
        final CountDownLatch pleaseTake = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1227
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1228
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1229
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1230
                for (int i = 0; i < capacity; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1231
                    q.putLast(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1232
                pleaseTake.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1233
                q.putLast(86);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1234
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1235
                pleaseInterrupt.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1236
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1237
                    q.putLast(99);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1238
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1239
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1240
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1241
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1242
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1243
        await(pleaseTake);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1244
        assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1245
        assertEquals(0, q.take());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1246
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1247
        await(pleaseInterrupt);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1248
        assertThreadStaysAlive(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1249
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1250
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1251
        assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1252
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1253
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1254
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1255
     * timed offerLast times out if full and elements not taken
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1256
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1257
    public void testTimedOfferLast() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1258
        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1259
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1260
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1261
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1262
                q.putLast(new Object());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1263
                q.putLast(new Object());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1264
                long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1265
                assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1266
                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1267
                pleaseInterrupt.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1268
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1269
                    q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1270
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1271
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1272
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1273
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1274
        await(pleaseInterrupt);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1275
        assertThreadStaysAlive(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1276
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1277
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1278
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1279
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1280
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1281
     * takeLast retrieves elements in FIFO order
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1282
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1283
    public void testTakeLast() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1284
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1285
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1286
            assertEquals(SIZE - i - 1, q.takeLast());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1287
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1288
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1289
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1290
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1291
     * takeLast removes existing elements until empty, then blocks interruptibly
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1292
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1293
    public void testBlockingTakeLast() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1294
        final LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1295
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1296
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1297
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1298
                for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1299
                    assertEquals(SIZE - i - 1, q.takeLast());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1300
                }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1301
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1302
                Thread.currentThread().interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1303
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1304
                    q.takeLast();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1305
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1306
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1307
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1308
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1309
                pleaseInterrupt.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1310
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1311
                    q.takeLast();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1312
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1313
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1314
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1315
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1316
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1317
        await(pleaseInterrupt);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1318
        assertThreadStaysAlive(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1319
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1320
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1321
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1322
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1323
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1324
     * timed pollLast with zero timeout succeeds when non-empty, else times out
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1325
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1326
    public void testTimedPollLast0() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1327
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1328
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1329
            assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1330
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1331
        assertNull(q.pollLast(0, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1332
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1333
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1334
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1335
     * timed pollLast with nonzero timeout succeeds when non-empty, else times out
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1336
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1337
    public void testTimedPollLast() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1338
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1339
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1340
            long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1341
            assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1342
            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1343
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1344
        long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1345
        assertNull(q.pollLast(timeoutMillis(), MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1346
        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1347
        checkEmpty(q);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1348
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1349
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1350
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1351
     * Interrupted timed pollLast throws InterruptedException instead of
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1352
     * returning timeout status
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1353
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1354
    public void testInterruptedTimedPollLast() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1355
        final LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1356
        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1357
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1358
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1359
                long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1360
                for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1361
                    assertEquals(SIZE - i - 1,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1362
                                 q.pollLast(LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1363
                }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1364
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1365
                Thread.currentThread().interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1366
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1367
                    q.pollLast(LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1368
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1369
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1370
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1371
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1372
                pleaseInterrupt.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1373
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1374
                    q.pollLast(LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1375
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1376
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1377
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1378
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1379
                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1380
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1381
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1382
        await(pleaseInterrupt);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1383
        assertThreadStaysAlive(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1384
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1385
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1386
        checkEmpty(q);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1387
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1388
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1389
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1390
     * timed poll before a delayed offerLast fails; after offerLast succeeds;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1391
     * on interruption throws
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1392
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1393
    public void testTimedPollWithOfferLast() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1394
        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1395
        final CheckedBarrier barrier = new CheckedBarrier(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1396
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1397
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1398
                long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1399
                assertNull(q.poll(timeoutMillis(), MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1400
                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1401
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1402
                barrier.await();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1403
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1404
                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1405
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1406
                Thread.currentThread().interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1407
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1408
                    q.poll(LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1409
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1410
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1411
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1412
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1413
                barrier.await();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1414
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1415
                    q.poll(LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1416
                    shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1417
                } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1418
                assertFalse(Thread.interrupted());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1419
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1420
                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1421
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1422
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1423
        barrier.await();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1424
        long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1425
        assertTrue(q.offerLast(zero, LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1426
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1427
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1428
        barrier.await();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1429
        assertThreadStaysAlive(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1430
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1431
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1432
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1433
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1434
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1435
     * element returns next element, or throws NSEE if empty
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1436
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1437
    public void testElement() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1438
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1439
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1440
            assertEquals(i, q.element());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1441
            q.poll();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1442
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1443
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1444
            q.element();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1445
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1446
        } catch (NoSuchElementException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1447
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1448
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1449
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1450
     * contains(x) reports true when elements added but not yet removed
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1451
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1452
    public void testContains() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1453
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1454
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1455
            assertTrue(q.contains(new Integer(i)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1456
            q.poll();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1457
            assertFalse(q.contains(new Integer(i)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1458
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1459
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1460
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1461
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1462
     * clear removes all elements
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1463
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1464
    public void testClear() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1465
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1466
        q.clear();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1467
        assertTrue(q.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1468
        assertEquals(0, q.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1469
        assertEquals(SIZE, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1470
        q.add(one);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1471
        assertFalse(q.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1472
        assertTrue(q.contains(one));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1473
        q.clear();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1474
        assertTrue(q.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1475
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1476
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1477
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1478
     * containsAll(c) is true when c contains a subset of elements
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1479
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1480
    public void testContainsAll() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1481
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1482
        LinkedBlockingDeque p = new LinkedBlockingDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1483
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1484
            assertTrue(q.containsAll(p));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1485
            assertFalse(p.containsAll(q));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1486
            p.add(new Integer(i));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1487
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1488
        assertTrue(p.containsAll(q));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1489
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1490
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1491
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1492
     * retainAll(c) retains only those elements of c and reports true if changed
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1493
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1494
    public void testRetainAll() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1495
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1496
        LinkedBlockingDeque p = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1497
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1498
            boolean changed = q.retainAll(p);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1499
            if (i == 0)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1500
                assertFalse(changed);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1501
            else
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1502
                assertTrue(changed);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1503
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1504
            assertTrue(q.containsAll(p));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1505
            assertEquals(SIZE - i, q.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1506
            p.remove();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1507
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1508
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1509
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1510
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1511
     * removeAll(c) removes only those elements of c and reports true if changed
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1512
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1513
    public void testRemoveAll() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1514
        for (int i = 1; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1515
            LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1516
            LinkedBlockingDeque p = populatedDeque(i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1517
            assertTrue(q.removeAll(p));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1518
            assertEquals(SIZE - i, q.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1519
            for (int j = 0; j < i; ++j) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1520
                Integer x = (Integer)(p.remove());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1521
                assertFalse(q.contains(x));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1522
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1523
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1524
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1525
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1526
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1527
     * toArray contains all elements in FIFO order
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1528
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1529
    public void testToArray() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1530
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1531
        Object[] o = q.toArray();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1532
        for (int i = 0; i < o.length; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1533
            assertSame(o[i], q.poll());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1534
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1535
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1536
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1537
     * toArray(a) contains all elements in FIFO order
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1538
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1539
    public void testToArray2() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1540
        LinkedBlockingDeque<Integer> q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1541
        Integer[] ints = new Integer[SIZE];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1542
        Integer[] array = q.toArray(ints);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1543
        assertSame(ints, array);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1544
        for (int i = 0; i < ints.length; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1545
            assertSame(ints[i], q.remove());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1546
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1547
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1548
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1549
     * toArray(incompatible array type) throws ArrayStoreException
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1550
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1551
    public void testToArray1_BadArg() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1552
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1553
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1554
            q.toArray(new String[10]);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1555
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1556
        } catch (ArrayStoreException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1557
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1558
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1559
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1560
     * iterator iterates through all elements
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1561
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1562
    public void testIterator() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1563
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1564
        Iterator it = q.iterator();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1565
        int i;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1566
        for (i = 0; it.hasNext(); i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1567
            assertTrue(q.contains(it.next()));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1568
        assertEquals(i, SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1569
        assertIteratorExhausted(it);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1570
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1571
        it = q.iterator();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1572
        for (i = 0; it.hasNext(); i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1573
            assertEquals(it.next(), q.take());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1574
        assertEquals(i, SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1575
        assertIteratorExhausted(it);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1576
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1577
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1578
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1579
     * iterator of empty collection has no elements
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1580
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1581
    public void testEmptyIterator() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1582
        Deque c = new LinkedBlockingDeque();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1583
        assertIteratorExhausted(c.iterator());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1584
        assertIteratorExhausted(c.descendingIterator());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1585
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1586
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1587
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1588
     * iterator.remove removes current element
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1589
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1590
    public void testIteratorRemove() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1591
        final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1592
        q.add(two);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1593
        q.add(one);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1594
        q.add(three);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1595
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1596
        Iterator it = q.iterator();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1597
        it.next();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1598
        it.remove();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1599
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1600
        it = q.iterator();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1601
        assertSame(it.next(), one);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1602
        assertSame(it.next(), three);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1603
        assertFalse(it.hasNext());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1604
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1605
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1606
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1607
     * iterator ordering is FIFO
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1608
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1609
    public void testIteratorOrdering() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1610
        final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1611
        q.add(one);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1612
        q.add(two);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1613
        q.add(three);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1614
        assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1615
        int k = 0;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1616
        for (Iterator it = q.iterator(); it.hasNext();) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1617
            assertEquals(++k, it.next());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1618
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1619
        assertEquals(3, k);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1620
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1621
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1622
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1623
     * Modifications do not cause iterators to fail
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1624
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1625
    public void testWeaklyConsistentIteration() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1626
        final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1627
        q.add(one);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1628
        q.add(two);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1629
        q.add(three);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1630
        for (Iterator it = q.iterator(); it.hasNext();) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1631
            q.remove();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1632
            it.next();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1633
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1634
        assertEquals(0, q.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1635
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1636
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1637
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1638
     * Descending iterator iterates through all elements
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1639
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1640
    public void testDescendingIterator() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1641
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1642
        int i = 0;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1643
        Iterator it = q.descendingIterator();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1644
        while (it.hasNext()) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1645
            assertTrue(q.contains(it.next()));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1646
            ++i;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1647
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1648
        assertEquals(i, SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1649
        assertFalse(it.hasNext());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1650
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1651
            it.next();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1652
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1653
        } catch (NoSuchElementException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1654
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1655
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1656
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1657
     * Descending iterator ordering is reverse FIFO
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1658
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1659
    public void testDescendingIteratorOrdering() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1660
        final LinkedBlockingDeque q = new LinkedBlockingDeque();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1661
        for (int iters = 0; iters < 100; ++iters) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1662
            q.add(new Integer(3));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1663
            q.add(new Integer(2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1664
            q.add(new Integer(1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1665
            int k = 0;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1666
            for (Iterator it = q.descendingIterator(); it.hasNext();) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1667
                assertEquals(++k, it.next());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1668
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1669
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1670
            assertEquals(3, k);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1671
            q.remove();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1672
            q.remove();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1673
            q.remove();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1674
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1675
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1676
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1677
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1678
     * descendingIterator.remove removes current element
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1679
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1680
    public void testDescendingIteratorRemove() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1681
        final LinkedBlockingDeque q = new LinkedBlockingDeque();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1682
        for (int iters = 0; iters < 100; ++iters) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1683
            q.add(new Integer(3));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1684
            q.add(new Integer(2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1685
            q.add(new Integer(1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1686
            Iterator it = q.descendingIterator();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1687
            assertEquals(it.next(), new Integer(1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1688
            it.remove();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1689
            assertEquals(it.next(), new Integer(2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1690
            it = q.descendingIterator();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1691
            assertEquals(it.next(), new Integer(2));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1692
            assertEquals(it.next(), new Integer(3));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1693
            it.remove();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1694
            assertFalse(it.hasNext());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1695
            q.remove();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1696
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1697
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1698
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1699
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1700
     * toString contains toStrings of elements
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1701
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1702
    public void testToString() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1703
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1704
        String s = q.toString();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1705
        for (int i = 0; i < SIZE; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1706
            assertTrue(s.contains(String.valueOf(i)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1707
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1708
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1709
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1710
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1711
     * offer transfers elements across Executor tasks
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1712
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1713
    public void testOfferInExecutor() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1714
        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1715
        q.add(one);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1716
        q.add(two);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1717
        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1718
        final ExecutorService executor = Executors.newFixedThreadPool(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1719
        try (PoolCleaner cleaner = cleaner(executor)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1720
            executor.execute(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1721
                public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1722
                    assertFalse(q.offer(three));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1723
                    threadsStarted.await();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1724
                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1725
                    assertEquals(0, q.remainingCapacity());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1726
                }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1727
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1728
            executor.execute(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1729
                public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1730
                    threadsStarted.await();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1731
                    assertSame(one, q.take());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1732
                }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1733
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1734
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1735
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1736
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1737
     * timed poll retrieves elements across Executor threads
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1738
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1739
    public void testPollInExecutor() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1740
        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1741
        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1742
        final ExecutorService executor = Executors.newFixedThreadPool(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1743
        try (PoolCleaner cleaner = cleaner(executor)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1744
            executor.execute(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1745
                public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1746
                    assertNull(q.poll());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1747
                    threadsStarted.await();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1748
                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1749
                    checkEmpty(q);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1750
                }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1751
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1752
            executor.execute(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1753
                public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1754
                    threadsStarted.await();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1755
                    q.put(one);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1756
                }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1757
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1758
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1759
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1760
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1761
     * A deserialized serialized deque has same elements in same order
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1762
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1763
    public void testSerialization() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1764
        Queue x = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1765
        Queue y = serialClone(x);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1766
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1767
        assertNotSame(y, x);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1768
        assertEquals(x.size(), y.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1769
        assertEquals(x.toString(), y.toString());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1770
        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1771
        while (!x.isEmpty()) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1772
            assertFalse(y.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1773
            assertEquals(x.remove(), y.remove());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1774
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1775
        assertTrue(y.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1776
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1777
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1778
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1779
     * drainTo(c) empties deque into another collection c
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1780
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1781
    public void testDrainTo() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1782
        LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1783
        ArrayList l = new ArrayList();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1784
        q.drainTo(l);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1785
        assertEquals(0, q.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1786
        assertEquals(SIZE, l.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1787
        for (int i = 0; i < SIZE; ++i)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1788
            assertEquals(l.get(i), new Integer(i));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1789
        q.add(zero);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1790
        q.add(one);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1791
        assertFalse(q.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1792
        assertTrue(q.contains(zero));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1793
        assertTrue(q.contains(one));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1794
        l.clear();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1795
        q.drainTo(l);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1796
        assertEquals(0, q.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1797
        assertEquals(2, l.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1798
        for (int i = 0; i < 2; ++i)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1799
            assertEquals(l.get(i), new Integer(i));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1800
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1801
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1802
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1803
     * drainTo empties full deque, unblocking a waiting put.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1804
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1805
    public void testDrainToWithActivePut() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1806
        final LinkedBlockingDeque q = populatedDeque(SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1807
        Thread t = new Thread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1808
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1809
                q.put(new Integer(SIZE + 1));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1810
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1811
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1812
        t.start();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1813
        ArrayList l = new ArrayList();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1814
        q.drainTo(l);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1815
        assertTrue(l.size() >= SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1816
        for (int i = 0; i < SIZE; ++i)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1817
            assertEquals(l.get(i), new Integer(i));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1818
        t.join();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1819
        assertTrue(q.size() + l.size() >= SIZE);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1820
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1821
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1822
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1823
     * drainTo(c, n) empties first min(n, size) elements of queue into c
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1824
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1825
    public void testDrainToN() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1826
        LinkedBlockingDeque q = new LinkedBlockingDeque();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1827
        for (int i = 0; i < SIZE + 2; ++i) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1828
            for (int j = 0; j < SIZE; j++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1829
                assertTrue(q.offer(new Integer(j)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1830
            ArrayList l = new ArrayList();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1831
            q.drainTo(l, i);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1832
            int k = (i < SIZE) ? i : SIZE;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1833
            assertEquals(k, l.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1834
            assertEquals(SIZE - k, q.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1835
            for (int j = 0; j < k; ++j)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1836
                assertEquals(l.get(j), new Integer(j));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1837
            do {} while (q.poll() != null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1838
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1839
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1840
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1841
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1842
     * remove(null), contains(null) always return false
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1843
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1844
    public void testNeverContainsNull() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1845
        Deque<?>[] qs = {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1846
            new LinkedBlockingDeque<Object>(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1847
            populatedDeque(2),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1848
        };
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1849
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1850
        for (Deque<?> q : qs) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1851
            assertFalse(q.contains(null));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1852
            assertFalse(q.remove(null));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1853
            assertFalse(q.removeFirstOccurrence(null));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1854
            assertFalse(q.removeLastOccurrence(null));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1855
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1856
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1857
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1858
}