author | igerasim |
Mon, 02 Jun 2014 19:49:57 +0400 | |
changeset 24692 | 268fbc344d53 |
parent 5506 | 202f599c92aa |
child 34714 | 2637c9a952c2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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); |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
5506
diff
changeset
|
59 |
THROWS(IllegalStateException.class, () -> q.add("d")); |
2 | 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"); |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
5506
diff
changeset
|
68 |
THROWS(NoSuchElementException.class, () -> q.remove()); |
2 | 69 |
equal(q.poll(), null); |
70 |
check(q.isEmpty()); |
|
71 |
equal(q.size(), 0); |
|
72 |
} catch (Throwable t) { unexpected(t); } |
|
73 |
} |
|
74 |
||
75 |
//--------------------- Infrastructure --------------------------- |
|
76 |
static volatile int passed = 0, failed = 0; |
|
77 |
static void pass() {passed++;} |
|
78 |
static void fail() {failed++; Thread.dumpStack();} |
|
79 |
static void fail(String msg) {System.out.println(msg); fail();} |
|
80 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
81 |
static void check(boolean cond) {if (cond) pass(); else fail();} |
|
82 |
static void equal(Object x, Object y) { |
|
83 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
84 |
else fail(x + " not equal to " + y);} |
|
85 |
public static void main(String[] args) throws Throwable { |
|
86 |
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
|
87 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
88 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
5506
diff
changeset
|
89 |
interface Fun {void f() throws Throwable;} |
2 | 90 |
private static void THROWS(Class<? extends Throwable> k, Fun... fs) { |
91 |
for (Fun f : fs) |
|
92 |
try { f.f(); fail("Expected " + k.getName() + " not thrown"); } |
|
93 |
catch (Throwable t) { |
|
94 |
if (k.isAssignableFrom(t.getClass())) pass(); |
|
95 |
else unexpected(t);}} |
|
96 |
} |