1 /*
2  * Copyright (c) 2005, 2013, 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     6207984 6268068
27  * @summary Test contains/remove equator compatibility
28  * @author  Martin Buchholz
29  */
30 
31 package test.java.util.PriorityQueue;
32 
33 import java.util.ArrayDeque;
34 import java.util.Arrays;
35 import java.util.Comparator;
36 import java.util.List;
37 import java.util.PriorityQueue;
38 import java.util.Queue;
39 import java.util.concurrent.ArrayBlockingQueue;
40 import java.util.concurrent.LinkedBlockingDeque;
41 import java.util.concurrent.LinkedBlockingQueue;
42 import java.util.concurrent.LinkedTransferQueue;
43 import java.util.concurrent.PriorityBlockingQueue;
44 
45 public class RemoveContains {
46     static volatile int passed = 0, failed = 0;
47 
fail(String msg)48     static void fail(String msg) {
49         failed++;
50         new AssertionError(msg).printStackTrace();
51     }
52 
pass()53     static void pass() {
54         passed++;
55     }
56 
unexpected(Throwable t)57     static void unexpected(Throwable t) {
58         failed++;
59         t.printStackTrace();
60     }
61 
check(boolean condition, String msg)62     static void check(boolean condition, String msg) {
63         if (condition)
64             passed++;
65         else
66             fail(msg);
67     }
68 
check(boolean condition)69     static void check(boolean condition) {
70         check(condition, "Assertion failure");
71     }
72 
main(String[] args)73     public static void main(String[] args) {
74         final Comparator<String> firstChar = new Comparator<>() {
75             public int compare(String x, String y) {
76                 return x.charAt(0) - y.charAt(0); }};
77 
78         test(new PriorityQueue<String>(firstChar));
79         test(new PriorityQueue<String>(10, firstChar));
80         test(new PriorityBlockingQueue<String>(10, firstChar));
81         test(new ArrayBlockingQueue<String>(10));
82         test(new LinkedBlockingQueue<String>(10));
83         test(new LinkedBlockingDeque<String>(10));
84         test(new LinkedTransferQueue<String>());
85         test(new ArrayDeque<String>(10));
86 
87         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
88         if (failed > 0) throw new Error("Some tests failed");
89     }
90 
test(Queue<String> q)91     private static void test(Queue<String> q) {
92         try {
93             List<String> words =
94                 Arrays.asList("foo", "fee", "fi", "fo", "fum",
95                               "Englishman");
96             q.addAll(words);
97             for (String word : words)
98                 check(q.contains(word));
99             check(! q.contains("flurble"));
100 
101             check(q.remove("fi"));
102             for (String word : words)
103                 check(q.contains(word) ^ word.equals("fi"));
104 
105             check(! q.remove("fi"));
106             check(! q.remove("flurble"));
107 
108         } catch (Throwable t) { unexpected(t); }
109     }
110 }
111