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

File Contents

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