J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 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 { |
| 35 | static volatile int passed = 0, failed = 0; |
| 36 | |
| 37 | static void fail(String msg) { |
| 38 | failed++; |
| 39 | new Exception(msg).printStackTrace(); |
| 40 | } |
| 41 | |
| 42 | static void pass() { |
| 43 | passed++; |
| 44 | } |
| 45 | |
| 46 | static void unexpected(Throwable t) { |
| 47 | failed++; |
| 48 | t.printStackTrace(); |
| 49 | } |
| 50 | |
| 51 | static void check(boolean condition, String msg) { |
| 52 | if (condition) |
| 53 | passed++; |
| 54 | else |
| 55 | fail(msg); |
| 56 | } |
| 57 | |
| 58 | static void check(boolean condition) { |
| 59 | check(condition, "Assertion failure"); |
| 60 | } |
| 61 | |
| 62 | public static void main(String[] args) throws Throwable { |
| 63 | testQueue(new LinkedBlockingQueue<Integer>()); |
| 64 | // Uncomment when LinkedBlockingDeque is integrated |
| 65 | //testQueue(new LinkedBlockingDeque<Integer>()); |
| 66 | testQueue(new ArrayBlockingQueue<Integer>(10)); |
| 67 | |
| 68 | System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
| 69 | if (failed > 0) throw new Exception("Some tests failed"); |
| 70 | } |
| 71 | |
| 72 | private static void testQueue(BlockingQueue<Integer> q) throws Throwable { |
| 73 | Integer one = 1; |
| 74 | Integer two = 2; |
| 75 | Integer three = 3; |
| 76 | |
| 77 | // remove(Object) |
| 78 | q.put(one); |
| 79 | q.put(two); |
| 80 | check(! q.isEmpty() && q.size() == 2); |
| 81 | check(q.remove(one)); |
| 82 | check(q.remove(two)); |
| 83 | check(q.isEmpty() && q.size() == 0); |
| 84 | q.put(three); |
| 85 | try {check(q.take() == three);} |
| 86 | catch (Throwable t) {unexpected(t);} |
| 87 | check(q.isEmpty() && q.size() == 0); |
| 88 | |
| 89 | // iterator().remove() |
| 90 | q.clear(); |
| 91 | q.put(one); |
| 92 | check(q.offer(two)); |
| 93 | check(! q.isEmpty() && q.size() == 2); |
| 94 | Iterator<Integer> i = q.iterator(); |
| 95 | check(i.next() == one); |
| 96 | i.remove(); |
| 97 | check(i.next() == two); |
| 98 | i.remove(); |
| 99 | check(q.isEmpty() && q.size() == 0); |
| 100 | q.put(three); |
| 101 | try {check(q.take() == three);} |
| 102 | catch (Throwable t) {unexpected(t);} |
| 103 | check(q.isEmpty() && q.size() == 0); |
| 104 | } |
| 105 | } |