2
|
1 |
/*
|
|
2 |
* Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 6215625
|
|
27 |
* @summary Check correct behavior when last element is removed.
|
|
28 |
* @author Martin Buchholz
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.util.*;
|
|
32 |
import java.util.concurrent.*;
|
|
33 |
|
|
34 |
public class LastElement {
|
3708
|
35 |
void test(String[] args) throws Throwable {
|
2
|
36 |
testQueue(new LinkedBlockingQueue<Integer>());
|
3708
|
37 |
testQueue(new LinkedBlockingDeque<Integer>());
|
|
38 |
testQueue(new ArrayBlockingQueue<Integer>(10, true));
|
|
39 |
testQueue(new ArrayBlockingQueue<Integer>(10, false));
|
4110
|
40 |
testQueue(new LinkedTransferQueue<Integer>());
|
2
|
41 |
|
|
42 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
|
43 |
if (failed > 0) throw new Exception("Some tests failed");
|
|
44 |
}
|
|
45 |
|
3708
|
46 |
void testQueue(BlockingQueue<Integer> q) throws Throwable {
|
2
|
47 |
Integer one = 1;
|
|
48 |
Integer two = 2;
|
|
49 |
Integer three = 3;
|
|
50 |
|
|
51 |
// remove(Object)
|
|
52 |
q.put(one);
|
|
53 |
q.put(two);
|
|
54 |
check(! q.isEmpty() && q.size() == 2);
|
|
55 |
check(q.remove(one));
|
|
56 |
check(q.remove(two));
|
|
57 |
check(q.isEmpty() && q.size() == 0);
|
|
58 |
q.put(three);
|
|
59 |
try {check(q.take() == three);}
|
|
60 |
catch (Throwable t) {unexpected(t);}
|
|
61 |
check(q.isEmpty() && q.size() == 0);
|
|
62 |
|
|
63 |
// iterator().remove()
|
|
64 |
q.clear();
|
|
65 |
q.put(one);
|
|
66 |
check(q.offer(two));
|
|
67 |
check(! q.isEmpty() && q.size() == 2);
|
|
68 |
Iterator<Integer> i = q.iterator();
|
|
69 |
check(i.next() == one);
|
|
70 |
i.remove();
|
|
71 |
check(i.next() == two);
|
|
72 |
i.remove();
|
|
73 |
check(q.isEmpty() && q.size() == 0);
|
|
74 |
q.put(three);
|
|
75 |
try {check(q.take() == three);}
|
|
76 |
catch (Throwable t) {unexpected(t);}
|
|
77 |
check(q.isEmpty() && q.size() == 0);
|
|
78 |
}
|
3708
|
79 |
|
|
80 |
//--------------------- Infrastructure ---------------------------
|
|
81 |
volatile int passed = 0, failed = 0;
|
|
82 |
void pass() {passed++;}
|
|
83 |
void fail() {failed++; Thread.dumpStack();}
|
|
84 |
void fail(String msg) {System.err.println(msg); fail();}
|
|
85 |
void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
|
86 |
void check(boolean cond) {if (cond) pass(); else fail();}
|
|
87 |
void equal(Object x, Object y) {
|
|
88 |
if (x == null ? y == null : x.equals(y)) pass();
|
|
89 |
else fail(x + " not equal to " + y);}
|
|
90 |
public static void main(String[] args) throws Throwable {
|
|
91 |
new LastElement().instanceMain(args);}
|
|
92 |
public void instanceMain(String[] args) throws Throwable {
|
|
93 |
try {test(args);} catch (Throwable t) {unexpected(t);}
|
|
94 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
|
95 |
if (failed > 0) throw new AssertionError("Some tests failed");}
|
2
|
96 |
}
|