ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/TreeMap/NullPermissiveComparator.java
Revision: 1.3
Committed: Mon Jan 8 03:12:03 2018 UTC (6 years, 4 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +3 -4 lines
Log Message:
organize imports

File Contents

# Content
1 /*
2 * Copyright (c) 2006, 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 6467933
27 * @summary Test proper handling of comparators permitting nulls
28 * @author Martin Buchholz
29 */
30
31 import java.util.Comparator;
32 import java.util.Map;
33 import java.util.TreeMap;
34
35 @SuppressWarnings("unchecked")
36 public class NullPermissiveComparator {
37
38 static void equal(Map m, String s) {
39 equal(m.toString(), s);
40 }
41
42 static void realMain(String[] args) throws Throwable {
43 final Comparator nullLow = new Comparator() {
44 public int compare(Object x, Object y) {
45 return x == y ? 0 :
46 x == null ? -1 :
47 y == null ? 1 :
48 ((Comparable)x).compareTo(y); }};
49
50 final Comparator nullHigh = new Comparator() {
51 public int compare(Object x, Object y) {
52 return x == y ? 0 :
53 x == null ? 1 :
54 y == null ? -1 :
55 ((Comparable)x).compareTo(y); }};
56
57 TreeMap m = new TreeMap(nullLow);
58
59 m.put("a", "A");
60 m.put("b", "B");
61 m.put("c", "C");
62
63 equal(m, "{a=A, b=B, c=C}");
64 equal(m.headMap("b"), "{a=A}");
65 equal(m.tailMap("b"), "{b=B, c=C}");
66 equal(m.headMap(null), "{}");
67 equal(m.tailMap(null), "{a=A, b=B, c=C}");
68
69 m.put(null, "NULL");
70
71 equal(m, "{null=NULL, a=A, b=B, c=C}");
72 equal(m.headMap("b"), "{null=NULL, a=A}");
73 equal(m.tailMap("b"), "{b=B, c=C}");
74 equal(m.headMap(null), "{}");
75 equal(m.tailMap(null), "{null=NULL, a=A, b=B, c=C}");
76
77 m = new TreeMap(nullHigh);
78
79 m.put("a", "A");
80 m.put("b", "B");
81 m.put("c", "C");
82
83 equal(m, "{a=A, b=B, c=C}");
84 equal(m.headMap("b"), "{a=A}");
85 equal(m.tailMap("b"), "{b=B, c=C}");
86 equal(m.headMap(null), "{a=A, b=B, c=C}");
87 equal(m.tailMap(null), "{}");
88
89 m.put(null, "NULL");
90
91 equal(m, "{a=A, b=B, c=C, null=NULL}");
92 equal(m.headMap("b"), "{a=A}");
93 equal(m.tailMap("b"), "{b=B, c=C, null=NULL}");
94 equal(m.headMap(null), "{a=A, b=B, c=C}");
95 equal(m.tailMap(null), "{null=NULL}");
96 }
97
98 //--------------------- Infrastructure ---------------------------
99 static volatile int passed = 0, failed = 0;
100 static void pass() {passed++;}
101 static void fail() {failed++; Thread.dumpStack();}
102 static void fail(String msg) {System.out.println(msg); fail();}
103 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
104 static void check(boolean cond) {if (cond) pass(); else fail();}
105 static void equal(Object x, Object y) {
106 if (x == null ? y == null : x.equals(y)) pass();
107 else fail(x + " not equal to " + y);}
108 public static void main(String[] args) throws Throwable {
109 try {realMain(args);} catch (Throwable t) {unexpected(t);}
110 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
111 if (failed > 0) throw new AssertionError("Some tests failed");}
112 }