ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/TreeMap/NullAtEnd.java
Revision: 1.3
Committed: Wed Jan 4 04:46:19 2017 UTC (7 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.2: +3 -4 lines
Log Message:
convert to Diamond

File Contents

# Content
1 /*
2 * Copyright (c) 2005, 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 5018254
27 * @summary Test null-allowing Comparators
28 * @author Martin Buchholz
29 */
30
31 import java.util.*;
32 import java.util.concurrent.*;
33
34 public class NullAtEnd {
35 static volatile int passed = 0, failed = 0;
36
37 static void fail(String msg) {
38 failed++;
39 new AssertionError(msg).printStackTrace();
40 }
41
42 static void pass() {
43 passed++;
44 }
45
46 static void unexpected(Throwable t) {
47 failed++;
48 t.printStackTrace();
49 }
50
51 static void check(boolean condition, String msg) {
52 if (condition)
53 passed++;
54 else
55 fail(msg);
56 }
57
58 static void check(boolean condition) {
59 check(condition, "Assertion failure");
60 }
61
62 private static boolean eq(Object x, Object y) {
63 return x == null ? y == null : x.equals(y);
64 }
65
66 private static final Comparator<String> NULL_AT_END
67 = new Comparator<>() {
68 /**
69 * Allows for nulls. Null is greater than anything non-null.
70 */
71 public int compare(String x, String y) {
72 if (x == null && y == null) return 0;
73 if (x == null && y != null) return 1;
74 if (x != null && y == null) return -1;
75 return x.compareTo(y);
76 }
77 };
78
79 public static void main(String[] args) {
80 try {
81 SortedMap<String,String> m1 = new TreeMap<>(NULL_AT_END);
82 check(eq(m1.put("a", "a"), null));
83 check(eq(m1.put("b", "b"), null));
84 check(eq(m1.put("c", "c"), null));
85 check(eq(m1.put(null, "d"), null));
86
87 SortedMap<String,String> m2 = new TreeMap<>(m1);
88
89 check(eq(m1.lastKey(), null));
90 check(eq(m1.get(m1.lastKey()), "d"));
91 check(eq(m1.remove(m1.lastKey()), "d"));
92 check(eq(m1.lastKey(), "c"));
93
94 check(eq(m2.entrySet().toString(), "[a=a, b=b, c=c, null=d]"));
95
96 SortedMap<String,String> m3 = m2.tailMap("b");
97
98 check(eq(m3.lastKey(), null));
99 check(eq(m3.get(m3.lastKey()), "d"));
100 check(eq(m3.remove(m3.lastKey()), "d"));
101 check(eq(m3.lastKey(), "c"));
102
103 } catch (Throwable t) { unexpected(t); }
104
105 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
106 if (failed > 0) throw new Error("Some tests failed");
107 }
108 }