ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Map/Get.java
Revision: 1.5
Committed: Sun Aug 18 01:05:34 2019 UTC (4 years, 9 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +2 -2 lines
Log Message:
fix more errorprone [UnnecessaryParentheses] warnings

File Contents

# Content
1 /*
2 * Copyright (c) 2005, 2012, 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 6306829
27 * @summary Verify assertions in get() javadocs
28 * @author Martin Buchholz
29 */
30
31 import java.util.HashMap;
32 import java.util.Hashtable;
33 import java.util.IdentityHashMap;
34 import java.util.LinkedHashMap;
35 import java.util.Map;
36 import java.util.Objects;
37 import java.util.SortedMap;
38 import java.util.TreeMap;
39 import java.util.WeakHashMap;
40 import java.util.concurrent.ConcurrentHashMap;
41 import java.util.concurrent.ConcurrentMap;
42 import java.util.concurrent.ConcurrentSkipListMap;
43
44 public class Get {
45
46 private static void realMain(String[] args) throws Throwable {
47 testMap(new Hashtable<Character,Boolean>());
48 testMap(new HashMap<Character,Boolean>());
49 testMap(new IdentityHashMap<Character,Boolean>());
50 testMap(new LinkedHashMap<Character,Boolean>());
51 testMap(new ConcurrentHashMap<Character,Boolean>());
52 testMap(new WeakHashMap<Character,Boolean>());
53 testMap(new TreeMap<Character,Boolean>());
54 testMap(new ConcurrentSkipListMap<Character,Boolean>());
55 }
56
57 private static void put(Map<Character,Boolean> m,
58 Character key, Boolean value,
59 Boolean oldValue) {
60 if (oldValue != null) {
61 check("containsValue(oldValue)", m.containsValue(oldValue));
62 check("values.contains(oldValue)", m.values().contains(oldValue));
63 }
64 equal(m.put(key, value), oldValue);
65 equal(m.get(key), value);
66 check("containsKey", m.containsKey(key));
67 check("keySet.contains", m.keySet().contains(key));
68 check("containsValue", m.containsValue(value));
69 check("values.contains", m.values().contains(value));
70 check("!isEmpty", ! m.isEmpty());
71 }
72
73 private static void testMap(Map<Character,Boolean> m) {
74 // We verify following assertions in get(Object) method javadocs
75 boolean permitsNullKeys = (! (m instanceof ConcurrentMap ||
76 m instanceof Hashtable ||
77 m instanceof SortedMap));
78 boolean permitsNullValues = (! (m instanceof ConcurrentMap ||
79 m instanceof Hashtable));
80 boolean usesIdentity = m instanceof IdentityHashMap;
81
82 System.err.println(m.getClass());
83 put(m, 'A', true, null);
84 put(m, 'A', false, true); // Guaranteed identical by JLS
85 put(m, 'B', true, null);
86 put(m, new Character('A'), false, usesIdentity ? null : false);
87 if (permitsNullKeys) {
88 try {
89 put(m, null, true, null);
90 put(m, null, false, true);
91 }
92 catch (Throwable t) { unexpected(m.getClass().getName(), t); }
93 } else {
94 try { m.get(null); fail(m.getClass().getName() + " did not reject null key"); }
95 catch (NullPointerException e) {}
96 catch (Throwable t) { unexpected(m.getClass().getName(), t); }
97
98 try { m.put(null, true); fail(m.getClass().getName() + " did not reject null key"); }
99 catch (NullPointerException e) {}
100 catch (Throwable t) { unexpected(m.getClass().getName(), t); }
101 }
102 if (permitsNullValues) {
103 try {
104 put(m, 'C', null, null);
105 put(m, 'C', true, null);
106 put(m, 'C', null, true);
107 }
108 catch (Throwable t) { unexpected(m.getClass().getName(), t); }
109 } else {
110 try { m.put('A', null); fail(m.getClass().getName() + " did not reject null key"); }
111 catch (NullPointerException e) {}
112 catch (Throwable t) { unexpected(m.getClass().getName(), t); }
113
114 try { m.put('C', null); fail(m.getClass().getName() + " did not reject null key"); }
115 catch (NullPointerException e) {}
116 catch (Throwable t) { unexpected(m.getClass().getName(), t); }
117 }
118 }
119
120 //--------------------- Infrastructure ---------------------------
121 static volatile int passed = 0, failed = 0;
122 static void pass() { passed++; }
123 static void fail() { failed++; new Error("Failure").printStackTrace(System.err); }
124 static void fail(String msg) { failed++; new Error("Failure: " + msg).printStackTrace(System.err); }
125 static void unexpected(String msg, Throwable t) { System.err.println("Unexpected: " + msg); unexpected(t); }
126 static void unexpected(Throwable t) { failed++; t.printStackTrace(System.err); }
127 static void check(boolean cond) { if (cond) pass(); else fail(); }
128 static void check(String desc, boolean cond) { if (cond) pass(); else fail(desc); }
129 static void equal(Object x, Object y) {
130 if (Objects.equals(x,y)) pass(); else fail(x + " not equal to " + y);
131 }
132
133 public static void main(String[] args) throws Throwable {
134 try { realMain(args); } catch (Throwable t) { unexpected(t); }
135
136 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
137 if (failed > 0) throw new Error("Some tests failed");
138 }
139 }