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/HashMap/Bug8186171Test.java
38811 views
1
/*
2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3
*
4
* This code is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 2 only, as
6
* published by the Free Software Foundation.
7
*
8
* This code is distributed in the hope that it will be useful, but WITHOUT
9
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11
* version 2 for more details (a copy is included in the LICENSE file that
12
* accompanied this code).
13
*
14
* You should have received a copy of the GNU General Public License version
15
* 2 along with this work; if not, write to the Free Software Foundation,
16
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17
*
18
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19
* or visit www.oracle.com if you need additional information or have any
20
* questions.
21
*/
22
import java.util.List;
23
import java.util.Map;
24
import java.util.HashMap;
25
import java.util.Iterator;
26
import java.util.ArrayList;
27
import java.util.concurrent.ThreadLocalRandom;
28
29
import org.testng.annotations.Test;
30
import static org.testng.Assert.assertEquals;
31
import static org.testng.Assert.assertFalse;
32
import static org.testng.Assert.assertTrue;
33
34
/*
35
* @test
36
* @bug 8186171
37
* @run testng Bug8186171Test
38
* @summary Verify the fix for scenario reported in JDK-8186171
39
* @author [email protected]
40
*/
41
@Test
42
public class Bug8186171Test{
43
44
/**
45
* Tests and extends the scenario reported in
46
* https://bugs.openjdk.java.net/browse/JDK-8186171
47
* HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
48
* Runs 1000 times as it is based on randomization.
49
*/
50
@Test(invocationCount = 1000)
51
static void testBug8186171NonDeterministic()
52
{
53
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
54
55
final Object v1 = rnd.nextBoolean() ? null : 1;
56
final Object v2 = (rnd.nextBoolean() && v1 != null) ? null : 2;
57
58
/** If true, always lands in first bucket in hash tables. */
59
final boolean poorHash = rnd.nextBoolean();
60
61
class Key implements Comparable<Key> {
62
final int i;
63
Key(int i) { this.i = i; }
64
public int hashCode() { return poorHash ? 0 : super.hashCode(); }
65
public int compareTo(Key x) {
66
return Integer.compare(this.i, x.i);
67
}
68
}
69
70
// HashMap and ConcurrentHashMap have:
71
// TREEIFY_THRESHOLD = 8; UNTREEIFY_THRESHOLD = 6;
72
final int size = rnd.nextInt(1, 25);
73
74
List<Key> keys = new ArrayList<>();
75
for (int i = size; i-->0; ) keys.add(new Key(i));
76
Key keyToFrob = keys.get(rnd.nextInt(keys.size()));
77
78
Map<Key, Object> m = new HashMap<Key, Object>();
79
80
for (Key key : keys) m.put(key, v1);
81
82
for (Iterator<Map.Entry<Key, Object>> it = m.entrySet().iterator();
83
it.hasNext(); ) {
84
Map.Entry<Key, Object> entry = it.next();
85
if (entry.getKey() == keyToFrob)
86
entry.setValue(v2); // does this have the expected effect?
87
else
88
it.remove();
89
}
90
91
assertFalse(m.containsValue(v1));
92
assertTrue(m.containsValue(v2));
93
assertTrue(m.containsKey(keyToFrob));
94
assertEquals(1, m.size());
95
}
96
97
98
/**
99
* Tests and extends the scenario reported in
100
* https://bugs.openjdk.java.net/browse/JDK-8186171
101
* HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
102
* Runs single time by reproducing exact scenario for issue mentioned in 8186171
103
*/
104
@Test()
105
static void testBug8186171Deterministic(){
106
class Key implements Comparable<Key>
107
{
108
final int i;
109
Key(int i) { this.i = i; }
110
111
@Override
112
public int hashCode() { return 0; } //Returning same hashcode so that all keys landup to same bucket
113
114
@Override
115
public int compareTo(Key x){
116
if(this.i == x.i){
117
return 0;
118
}
119
else {
120
return Integer.compare(this.i, x.i);
121
}
122
}
123
@Override
124
public String toString()
125
{
126
return "Key_" + i;
127
}
128
}
129
130
// HashMap have TREEIFY_THRESHOLD = 8; UNTREEIFY_THRESHOLD = 6;
131
final int size = 11;
132
List<Key> keys = new ArrayList<>();
133
134
for (int i = 0; i < size; i++){
135
keys.add(new Key(i));
136
}
137
138
Key keyToFrob = keys.get(9);
139
Map<Key, Object> m = new HashMap<Key, Object>();
140
for (Key key : keys) m.put(key, null);
141
142
for (Iterator<Map.Entry<Key, Object>> it = m.entrySet().iterator(); it.hasNext(); ){
143
Map.Entry<Key, Object> entry = it.next();
144
if (entry.getKey() == keyToFrob){
145
entry.setValue(2);
146
}
147
else{
148
it.remove();
149
}
150
}
151
152
assertFalse(m.containsValue(null));
153
assertTrue(m.containsValue(2));
154
assertTrue(m.containsKey(keyToFrob));
155
assertEquals(1, m.size());
156
}
157
}
158
159