1 /*
2  * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug     6301085 6192552 6365601
27  * @summary Basic tests for asLifoQueue
28  * @author  Martin Buchholz
29  */
30 
31 package test.java.util.Collections;
32 
33 import java.util.*;
34 import java.util.concurrent.*;
35 
36 public class AsLifoQueue {
37 
realMain(String[] args)38     private static void realMain(String[] args) throws Throwable {
39         try {
40             Deque<String> deq = new ArrayDeque<String>();
41             check(deq.addAll(Arrays.asList("b", "a", "c")));
42             equal(deq.toString(), "[b, a, c]");
43             check(deq.add("d"));
44             equal(deq.toString(), "[b, a, c, d]");
45             Queue<String> q = Collections.asLifoQueue(deq);
46             check(q.add("e"));
47             equal(deq.toString(),"[e, b, a, c, d]");
48         } catch (Throwable t) { unexpected(t); }
49 
50         // Inspired by an excellent bug report by Jason Mehrens
51         try {
52             final Queue<String> q =
53                 Collections.asLifoQueue(new LinkedBlockingDeque<String>(3));
54             check(q.isEmpty()); equal(q.size(), 0);
55             check(q.add("a")); check(! q.isEmpty()); equal(q.size(), 1);
56             check(q.offer("b"));
57             check(q.add("c"));
58             equal(q.size(), 3);
59             check(! q.offer("d"));
60             equal(q.size(), 3);
61             THROWS(IllegalStateException.class, () -> q.add("d"));
62             equal(q.size(), 3);
63             equal(q.toString(), "[c, b, a]");
64             equal(q.peek(), "c");
65             equal(q.element(), "c");
66             equal(q.remove(), "c");
67             equal(q.poll(), "b");
68             equal(q.peek(), "a");
69             equal(q.remove(), "a");
70             THROWS(NoSuchElementException.class, () -> q.remove());
71             equal(q.poll(), null);
72             check(q.isEmpty());
73             equal(q.size(), 0);
74         } catch (Throwable t) { unexpected(t); }
75 
76         THROWS(NullPointerException.class, () -> Collections.asLifoQueue(null));
77     }
78 
79     //--------------------- Infrastructure ---------------------------
80     static volatile int passed = 0, failed = 0;
pass()81     static void pass() {passed++;}
fail()82     static void fail() {failed++; Thread.dumpStack();}
fail(String msg)83     static void fail(String msg) {System.out.println(msg); fail();}
unexpected(Throwable t)84     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
check(boolean cond)85     static void check(boolean cond) {if (cond) pass(); else fail();}
equal(Object x, Object y)86     static void equal(Object x, Object y) {
87         if (x == null ? y == null : x.equals(y)) pass();
88         else fail(x + " not equal to " + y);}
main(String[] args)89     public static void main(String[] args) throws Throwable {
90         try {realMain(args);} catch (Throwable t) {unexpected(t);}
91         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
92         if (failed > 0) throw new AssertionError("Some tests failed");}
f()93     interface Fun {void f() throws Throwable;}
THROWS(Class<? extends Throwable> k, Fun... fs)94     private static void THROWS(Class<? extends Throwable> k, Fun... fs) {
95         for (Fun f : fs)
96             try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
97             catch (Throwable t) {
98                 if (k.isAssignableFrom(t.getClass())) pass();
99                 else unexpected(t);}}
100 }
101