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 |
import java.util.*; |
32 |
import java.util.concurrent.*; |
33 |
|
34 |
public class AsLifoQueue { |
35 |
|
36 |
private static void realMain(String[] args) throws Throwable { |
37 |
try { |
38 |
Deque<String> deq = new ArrayDeque<String>(); |
39 |
check(deq.addAll(Arrays.asList("b", "a", "c"))); |
40 |
equal(deq.toString(), "[b, a, c]"); |
41 |
check(deq.add("d")); |
42 |
equal(deq.toString(), "[b, a, c, d]"); |
43 |
Queue<String> q = Collections.asLifoQueue(deq); |
44 |
check(q.add("e")); |
45 |
equal(deq.toString(),"[e, b, a, c, d]"); |
46 |
} catch (Throwable t) { unexpected(t); } |
47 |
|
48 |
// Inspired by an excellent bug report by Jason Mehrens |
49 |
try { |
50 |
final Queue<String> q = |
51 |
Collections.asLifoQueue(new LinkedBlockingDeque<String>(3)); |
52 |
check(q.isEmpty()); equal(q.size(), 0); |
53 |
check(q.add("a")); check(! q.isEmpty()); equal(q.size(), 1); |
54 |
check(q.offer("b")); |
55 |
check(q.add("c")); |
56 |
equal(q.size(), 3); |
57 |
check(! q.offer("d")); |
58 |
equal(q.size(), 3); |
59 |
THROWS(IllegalStateException.class, () -> q.add("d")); |
60 |
equal(q.size(), 3); |
61 |
equal(q.toString(), "[c, b, a]"); |
62 |
equal(q.peek(), "c"); |
63 |
equal(q.element(), "c"); |
64 |
equal(q.remove(), "c"); |
65 |
equal(q.poll(), "b"); |
66 |
equal(q.peek(), "a"); |
67 |
equal(q.remove(), "a"); |
68 |
THROWS(NoSuchElementException.class, () -> q.remove()); |
69 |
equal(q.poll(), null); |
70 |
check(q.isEmpty()); |
71 |
equal(q.size(), 0); |
72 |
} catch (Throwable t) { unexpected(t); } |
73 |
|
74 |
THROWS(NullPointerException.class, () -> Collections.asLifoQueue(null)); |
75 |
} |
76 |
|
77 |
//--------------------- Infrastructure --------------------------- |
78 |
static volatile int passed = 0, failed = 0; |
79 |
static void pass() {passed++;} |
80 |
static void fail() {failed++; Thread.dumpStack();} |
81 |
static void fail(String msg) {System.out.println(msg); fail();} |
82 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
83 |
static void check(boolean cond) {if (cond) pass(); else fail();} |
84 |
static void equal(Object x, Object y) { |
85 |
if (x == null ? y == null : x.equals(y)) pass(); |
86 |
else fail(x + " not equal to " + y);} |
87 |
public static void main(String[] args) throws Throwable { |
88 |
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
89 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
90 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
91 |
interface Fun {void f() throws Throwable;} |
92 |
private static void THROWS(Class<? extends Throwable> k, Fun... fs) { |
93 |
for (Fun f : fs) |
94 |
try { f.f(); fail("Expected " + k.getName() + " not thrown"); } |
95 |
catch (Throwable t) { |
96 |
if (k.isAssignableFrom(t.getClass())) pass(); |
97 |
else unexpected(t);}} |
98 |
} |