ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/PriorityQueue/AddNonComparable.java
Revision: 1.1
Committed: Thu Jun 2 01:15:46 2016 UTC (7 years, 11 months ago) by jsr166
Branch: MAIN
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 // TODO
103 // test(new ConcurrentSkipListSet<>(), NonComparable::new,
104 // (s, e) -> {
105 // assertEquals(s.size(), 0);
106 // assertTrue(e instanceof ClassCastException);
107 // });
108 test(new ConcurrentSkipListSet<>(), AComparable::new,
109 (s, e) -> {
110 assertEquals(s.size(), 1);
111 assertTrue(e == null);
112 });
113 }
114
115 static <K> void test(SortedMap<K,Boolean> map, Supplier<K> supplier,
116 BiConsumer<? super SortedMap<K,Boolean>, Throwable> checker) {
117 Throwable x = null;
118 try { map.put(supplier.get(), Boolean.TRUE); }
119 catch (Throwable e) { x = e; }
120 checker.accept(map, x);
121 }
122
123 @Test
124 public void maps() {
125 test(new TreeMap<>(), NonComparable::new,
126 (m, e) -> {
127 assertEquals(m.size(), 0);
128 assertTrue(e instanceof ClassCastException);
129 });
130 test(new TreeMap<>(), AComparable::new,
131 (m, e) -> {
132 assertEquals(m.size(), 1);
133 assertTrue(e == null);
134 });
135
136 // TODO
137 // test(new ConcurrentSkipListMap<>(), NonComparable::new,
138 // (s, e) -> {
139 // assertEquals(s.size(), 0);
140 // assertTrue(e instanceof ClassCastException);
141 // });
142 test(new ConcurrentSkipListMap<>(), AComparable::new,
143 (s, e) -> {
144 assertEquals(s.size(), 1);
145 assertTrue(e == null);
146 });
147 }
148
149 static class NonComparable { }
150
151 static class AComparable implements Comparable<AComparable> {
152 @Override public int compareTo(AComparable v) { return 0; }
153 }
154
155 }