Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/HashMap/ReplaceExisting.java
41152 views
1
/*
2
* Copyright (c) 2013, 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 8025173
27
* @summary Verify that replacing the value for an existing key does not
28
* corrupt active iterators, in particular due to a resize() occurring and
29
* not updating modCount.
30
* @run main ReplaceExisting
31
*/
32
33
import java.util.ConcurrentModificationException;
34
import java.util.HashMap;
35
import java.util.HashSet;
36
import java.util.Iterator;
37
38
public class ReplaceExisting {
39
/* Number of entries required to trigger a resize for cap=16, load=0.75*/
40
private static int ENTRIES = 13;
41
42
public static void main(String[] args) {
43
for (int i = 0; i <= ENTRIES; i++) {
44
HashMap<Integer,Integer> hm = prepHashMap();
45
testItr(hm, i);
46
}
47
}
48
49
/* Prepare a HashMap that will resize on next put() */
50
private static HashMap<Integer,Integer> prepHashMap() {
51
HashMap<Integer,Integer> hm = new HashMap<>(16, 0.75f);
52
// Add items to one more than the resize threshold
53
for (int i = 0; i < ENTRIES; i++) {
54
hm.put(i*10, i*10);
55
}
56
return hm;
57
}
58
59
/* Iterate hm for elemBeforePut elements, then call put() to replace value
60
* for existing key. With bug 8025173, this will also cause a resize, but
61
* not increase the modCount.
62
* Finish the iteration to check for a corrupt iterator.
63
*/
64
private static void testItr(HashMap<Integer,Integer> hm, int elemBeforePut) {
65
if (elemBeforePut > hm.size()) {
66
throw new IllegalArgumentException("Error in test: elemBeforePut must be <= HashMap size");
67
}
68
// Create a copy of the keys
69
HashSet<Integer> keys = new HashSet<>(hm.size());
70
keys.addAll(hm.keySet());
71
72
HashSet<Integer> collected = new HashSet<>(hm.size());
73
74
// Run itr for elemBeforePut items, collecting returned elems
75
Iterator<Integer> itr = hm.keySet().iterator();
76
for (int i = 0; i < elemBeforePut; i++) {
77
Integer retVal = itr.next();
78
if (!collected.add(retVal)) {
79
throw new RuntimeException("Corrupt iterator: key " + retVal + " already encountered");
80
}
81
}
82
83
// Do put() to replace entry (and resize table when bug present)
84
if (null == hm.put(0, 100)) {
85
throw new RuntimeException("Error in test: expected key 0 to be in the HashMap");
86
}
87
88
// Finish itr + collecting returned elems
89
while(itr.hasNext()) {
90
Integer retVal = itr.next();
91
if (!collected.add(retVal)) {
92
throw new RuntimeException("Corrupt iterator: key " + retVal + " already encountered");
93
}
94
}
95
96
// Compare returned elems to original copy of keys
97
if (!keys.equals(collected)) {
98
throw new RuntimeException("Collected keys do not match original set of keys");
99
}
100
}
101
}
102
103