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