author | mduigou |
Wed, 30 May 2012 22:18:37 -0700 | |
changeset 12859 | c44b88bb9b5e |
parent 5506 | 202f599c92aa |
child 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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) { |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
53 |
check("containsValue(oldValue)", m.containsValue(oldValue)); |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
54 |
check("values.contains(oldValue)", m.values().contains(oldValue)); |
2 | 55 |
} |
56 |
equal(m.put(key, value), oldValue); |
|
57 |
equal(m.get(key), value); |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
58 |
check("containsKey", m.containsKey(key)); |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
59 |
check("keySet.contains", m.keySet().contains(key)); |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
60 |
check("containsValue", m.containsValue(value)); |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
61 |
check("values.contains", m.values().contains(value)); |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
62 |
check("!isEmpty", ! m.isEmpty()); |
2 | 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 |
||
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
74 |
System.err.println(m.getClass()); |
2 | 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 |
} |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
84 |
catch (Throwable t) { unexpected(m.getClass().getName(), t); } |
2 | 85 |
} else { |
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
86 |
try { m.get(null); fail(m.getClass().getName() + " did not reject null key"); } |
2 | 87 |
catch (NullPointerException e) {} |
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
88 |
catch (Throwable t) { unexpected(m.getClass().getName(), t); } |
2 | 89 |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
90 |
try { m.put(null, true); fail(m.getClass().getName() + " did not reject null key"); } |
2 | 91 |
catch (NullPointerException e) {} |
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
92 |
catch (Throwable t) { unexpected(m.getClass().getName(), t); } |
2 | 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 |
} |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
100 |
catch (Throwable t) { unexpected(m.getClass().getName(), t); } |
2 | 101 |
} else { |
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
102 |
try { m.put('A', null); fail(m.getClass().getName() + " did not reject null key"); } |
2 | 103 |
catch (NullPointerException e) {} |
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
104 |
catch (Throwable t) { unexpected(m.getClass().getName(), t); } |
2 | 105 |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
106 |
try { m.put('C', null); fail(m.getClass().getName() + " did not reject null key"); } |
2 | 107 |
catch (NullPointerException e) {} |
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
108 |
catch (Throwable t) { unexpected(m.getClass().getName(), t); } |
2 | 109 |
} |
110 |
} |
|
111 |
||
112 |
//--------------------- Infrastructure --------------------------- |
|
113 |
static volatile int passed = 0, failed = 0; |
|
114 |
static void pass() { passed++; } |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
115 |
static void fail() { failed++; (new Error("Failure")).printStackTrace(System.err); } |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
116 |
static void fail(String msg) { failed++; (new Error("Failure: " + msg)).printStackTrace(System.err); } |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
117 |
static void unexpected(String msg, Throwable t) { System.err.println("Unexpected: " + msg); unexpected(t); } |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
118 |
static void unexpected(Throwable t) { failed++; t.printStackTrace(System.err); } |
2 | 119 |
static void check(boolean cond) { if (cond) pass(); else fail(); } |
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
120 |
static void check(String desc, boolean cond) { if (cond) pass(); else fail(desc); } |
2 | 121 |
static void equal(Object x, Object y) { |
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
122 |
if(Objects.equals(x,y)) pass(); else fail(x + " not equal to " + y); |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
123 |
} |
2 | 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); |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
5506
diff
changeset
|
129 |
if (failed > 0) throw new Error("Some tests failed"); |
2 | 130 |
} |
131 |
} |