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 =
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"); }
|
1 /*
2 * Copyright (c) 2005, 2026, 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 * @library /test/lib
30 */
31
32 import jdk.test.lib.valueclass.VClass;
33 import java.util.*;
34 import java.util.concurrent.*;
35
36 public class AsLifoQueue {
37
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 =
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 try {
79 Deque<VClass> deq = new ArrayDeque<>();
80 Queue<VClass> q = Collections.asLifoQueue(deq);
81 check(q.add(new VClass(1, new int[] { 1 })));
82 check(q.add(new VClass(2, new int[] { 2 })));
83 equal(q.peek(), new VClass(2, new int[] { 2 }));
84 equal(q.remove(), new VClass(2, new int[] { 2 }));
85 equal(q.poll(), new VClass(1, new int[] { 1 }));
86 check(q.isEmpty());
87 } catch (Throwable t) { unexpected(t); }
88 }
89
90 //--------------------- Infrastructure ---------------------------
91 static volatile int passed = 0, failed = 0;
92 static void pass() {passed++;}
93 static void fail() {failed++; Thread.dumpStack();}
94 static void fail(String msg) {System.out.println(msg); fail();}
95 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
96 static void check(boolean cond) {if (cond) pass(); else fail();}
97 static void equal(Object x, Object y) {
98 if (x == null ? y == null : x.equals(y)) pass();
99 else fail(x + " not equal to " + y);}
100 public static void main(String[] args) throws Throwable {
101 try {realMain(args);} catch (Throwable t) {unexpected(t);}
102 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
103 if (failed > 0) throw new AssertionError("Some tests failed");}
104 interface Fun {void f() throws Throwable;}
105 private static void THROWS(Class<? extends Throwable> k, Fun... fs) {
106 for (Fun f : fs)
107 try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
|