Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Map/Collisions.java
41149 views
1
/*
2
* Copyright (c) 2012, 2016, 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 7126277
27
* @run testng/othervm -Dtest.map.collisions.shortrun=true Collisions
28
* @summary Ensure Maps behave well with lots of hashCode() collisions.
29
*/
30
import java.util.BitSet;
31
import java.util.IdentityHashMap;
32
import java.util.Iterator;
33
import java.util.Map;
34
import java.util.function.Supplier;
35
36
import org.testng.annotations.Test;
37
import static org.testng.Assert.assertTrue;
38
import static org.testng.Assert.assertFalse;
39
import static org.testng.Assert.assertEquals;
40
import static org.testng.Assert.assertNotNull;
41
42
public class Collisions extends MapWithCollisionsProviders {
43
44
@Test(dataProvider = "mapsWithObjects")
45
void testIntegerIteration(String desc, Supplier<Map<IntKey, IntKey>> ms, IntKey val) {
46
Map<IntKey, IntKey> map = ms.get();
47
int mapSize = map.size();
48
49
BitSet all = new BitSet(mapSize);
50
for (Map.Entry<IntKey, IntKey> each : map.entrySet()) {
51
assertFalse(all.get(each.getKey().getValue()), "Iteration: key already seen");
52
all.set(each.getKey().getValue());
53
}
54
55
all.flip(0, mapSize);
56
assertTrue(all.isEmpty(), "Iteration: some keys not visited");
57
58
for (IntKey each : map.keySet()) {
59
assertFalse(all.get(each.getValue()), "Iteration: key already seen");
60
all.set(each.getValue());
61
}
62
63
all.flip(0, mapSize);
64
assertTrue(all.isEmpty(), "Iteration: some keys not visited");
65
66
int count = 0;
67
for (IntKey each : map.values()) {
68
count++;
69
}
70
71
assertEquals(map.size(), count,
72
String.format("Iteration: value count matches size m%d != c%d", map.size(), count));
73
}
74
75
@Test(dataProvider = "mapsWithStrings")
76
void testStringIteration(String desc, Supplier<Map<String, String>> ms, String val) {
77
Map<String, String> map = ms.get();
78
int mapSize = map.size();
79
80
BitSet all = new BitSet(mapSize);
81
for (Map.Entry<String, String> each : map.entrySet()) {
82
String key = each.getKey();
83
boolean longKey = key.length() > 5;
84
int index = key.hashCode() + (longKey ? mapSize / 2 : 0);
85
assertFalse(all.get(index), "key already seen");
86
all.set(index);
87
}
88
89
all.flip(0, mapSize);
90
assertTrue(all.isEmpty(), "some keys not visited");
91
92
for (String each : map.keySet()) {
93
boolean longKey = each.length() > 5;
94
int index = each.hashCode() + (longKey ? mapSize / 2 : 0);
95
assertFalse(all.get(index), "key already seen");
96
all.set(index);
97
}
98
99
all.flip(0, mapSize);
100
assertTrue(all.isEmpty(), "some keys not visited");
101
102
int count = 0;
103
for (String each : map.values()) {
104
count++;
105
}
106
107
assertEquals(map.size(), mapSize,
108
String.format("value count matches size m%d != k%d", map.size(), mapSize));
109
}
110
111
@Test(dataProvider = "mapsWithObjectsAndStrings")
112
void testRemove(String desc, Supplier<Map<Object, Object>> ms, Object val) {
113
Map<Object, Object> map = ms.get();
114
Object[] keys = map.keySet().toArray();
115
116
for (int i = 0; i < keys.length; i++) {
117
Object each = keys[i];
118
assertNotNull(map.remove(each),
119
String.format("remove: %s[%d]%s", desc, i, each));
120
}
121
122
assertTrue(map.size() == 0 && map.isEmpty(),
123
String.format("remove: map empty. size=%d", map.size()));
124
}
125
126
@Test(dataProvider = "mapsWithObjectsAndStrings")
127
void testKeysIteratorRemove(String desc, Supplier<Map<Object, Object>> ms, Object val) {
128
Map<Object, Object> map = ms.get();
129
130
Iterator<Object> each = map.keySet().iterator();
131
while (each.hasNext()) {
132
Object t = each.next();
133
each.remove();
134
assertFalse(map.containsKey(t), String.format("not removed: %s", each));
135
}
136
137
assertTrue(map.size() == 0 && map.isEmpty(),
138
String.format("remove: map empty. size=%d", map.size()));
139
}
140
141
@Test(dataProvider = "mapsWithObjectsAndStrings")
142
void testValuesIteratorRemove(String desc, Supplier<Map<Object, Object>> ms, Object val) {
143
Map<Object, Object> map = ms.get();
144
145
Iterator<Object> each = map.values().iterator();
146
while (each.hasNext()) {
147
Object t = each.next();
148
each.remove();
149
assertFalse(map.containsValue(t), String.format("not removed: %s", each));
150
}
151
152
assertTrue(map.size() == 0 && map.isEmpty(),
153
String.format("remove: map empty. size=%d", map.size()));
154
}
155
156
@Test(dataProvider = "mapsWithObjectsAndStrings")
157
void testEntriesIteratorRemove(String desc, Supplier<Map<Object, Object>> ms, Object val) {
158
Map<Object, Object> map = ms.get();
159
160
Iterator<Map.Entry<Object, Object>> each = map.entrySet().iterator();
161
while (each.hasNext()) {
162
Map.Entry<Object, Object> t = each.next();
163
Object key = t.getKey();
164
Object value = t.getValue();
165
each.remove();
166
assertTrue((map instanceof IdentityHashMap) || !map.entrySet().contains(t),
167
String.format("not removed: %s", each));
168
assertFalse(map.containsKey(key),
169
String.format("not removed: %s", each));
170
assertFalse(map.containsValue(value),
171
String.format("not removed: %s", each));
172
}
173
174
assertTrue(map.size() == 0 && map.isEmpty(),
175
String.format("remove: map empty. size=%d", map.size()));
176
}
177
178
}
179
180