ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/TreeMap/NullPermissiveComparator.java
Revision: 1.2
Committed: Sun Sep 5 21:32:20 2010 UTC (13 years, 8 months ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.1: +4 -4 lines
Log Message:
Update legal notices to Oracle wording

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