ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/PriorityQueue/AddNonComparable.java
Revision: 1.2
Committed: Wed Jun 15 20:16:10 2016 UTC (7 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.1: +10 -12 lines
Log Message:
8066070: PriorityQueue corrupted when adding non-Comparable

File Contents

# Content
1 /*
2 * Copyright (c) 2015, 2016, 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 8066070
27 * @run testng AddNonComparable
28 */
29
30 import java.util.PriorityQueue;
31 import java.util.Queue;
32 import java.util.SortedMap;
33 import java.util.SortedSet;
34 import java.util.TreeMap;
35 import java.util.TreeSet;
36 import java.util.concurrent.ConcurrentSkipListMap;
37 import java.util.concurrent.ConcurrentSkipListSet;
38 import java.util.concurrent.PriorityBlockingQueue;
39 import java.util.function.BiConsumer;
40 import java.util.function.Supplier;
41
42 import static org.testng.Assert.*;
43 import org.testng.annotations.Test;
44
45 public class AddNonComparable {
46
47 static <E> void test(Queue<E> queue, Supplier<E> supplier,
48 BiConsumer<? super Queue<E>, Throwable> checker) {
49 Throwable x = null;
50 try { queue.add(supplier.get()); }
51 catch (Throwable e) { x = e; }
52 checker.accept(queue, x);
53 }
54
55 @Test
56 public void queues() {
57 test(new PriorityQueue<>(), NonComparable::new,
58 (q, e) -> {
59 assertEquals(q.size(), 0);
60 assertTrue(e instanceof ClassCastException);
61 });
62 test(new PriorityQueue<>(), AComparable::new,
63 (q, e) -> {
64 assertEquals(q.size(), 1);
65 assertTrue(e == null);
66 });
67
68 test(new PriorityBlockingQueue<>(), NonComparable::new,
69 (q, e) -> {
70 assertEquals(q.size(), 0);
71 assertTrue(e instanceof ClassCastException);
72 });
73 test(new PriorityBlockingQueue<>(), AComparable::new,
74 (q, e) -> {
75 assertEquals(q.size(), 1);
76 assertTrue(e == null);
77 });
78 }
79
80 static <E> void test(SortedSet<E> set, Supplier<E> supplier,
81 BiConsumer<? super SortedSet<E>, Throwable> checker) {
82 Throwable x = null;
83 try { set.add(supplier.get()); }
84 catch (Throwable e) { x = e; }
85 checker.accept(set, x);
86 }
87
88
89 @Test
90 public void sets() {
91 test(new TreeSet<>(), NonComparable::new,
92 (s, e) -> {
93 assertEquals(s.size(), 0);
94 assertTrue(e instanceof ClassCastException);
95 });
96 test(new TreeSet<>(), AComparable::new,
97 (s, e) -> {
98 assertEquals(s.size(), 1);
99 assertTrue(e == null);
100 });
101
102 test(new ConcurrentSkipListSet<>(), NonComparable::new,
103 (s, e) -> {
104 assertEquals(s.size(), 0);
105 assertTrue(e instanceof ClassCastException);
106 });
107 test(new ConcurrentSkipListSet<>(), AComparable::new,
108 (s, e) -> {
109 assertEquals(s.size(), 1);
110 assertTrue(e == null);
111 });
112 }
113
114 static <K> void test(SortedMap<K,Boolean> map, Supplier<K> supplier,
115 BiConsumer<? super SortedMap<K,Boolean>, Throwable> checker) {
116 Throwable x = null;
117 try { map.put(supplier.get(), Boolean.TRUE); }
118 catch (Throwable e) { x = e; }
119 checker.accept(map, x);
120 }
121
122 @Test
123 public void maps() {
124 test(new TreeMap<>(), NonComparable::new,
125 (m, e) -> {
126 assertEquals(m.size(), 0);
127 assertTrue(e instanceof ClassCastException);
128 });
129 test(new TreeMap<>(), AComparable::new,
130 (m, e) -> {
131 assertEquals(m.size(), 1);
132 assertTrue(e == null);
133 });
134
135 test(new ConcurrentSkipListMap<>(), NonComparable::new,
136 (s, e) -> {
137 assertEquals(s.size(), 0);
138 assertTrue(e instanceof ClassCastException);
139 });
140 test(new ConcurrentSkipListMap<>(), AComparable::new,
141 (s, e) -> {
142 assertEquals(s.size(), 1);
143 assertTrue(e == null);
144 });
145 }
146
147 static class NonComparable { }
148
149 static class AComparable implements Comparable<AComparable> {
150 @Override public int compareTo(AComparable v) { return 0; }
151 }
152
153 }