49873
|
1 |
/*
|
|
2 |
* Copyright (c) 2017, 2017, 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
|
50609
|
7 |
* published by the Free Software Foundation.
|
49873
|
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 |
package jdk.internal.vm.compiler.collections.test;
|
|
24 |
|
|
25 |
import java.util.Arrays;
|
|
26 |
import java.util.Collection;
|
|
27 |
import java.util.Iterator;
|
|
28 |
import java.util.LinkedHashMap;
|
|
29 |
import java.util.Objects;
|
|
30 |
import java.util.Random;
|
|
31 |
|
|
32 |
import jdk.internal.vm.compiler.collections.EconomicMap;
|
|
33 |
import jdk.internal.vm.compiler.collections.Equivalence;
|
|
34 |
import jdk.internal.vm.compiler.collections.MapCursor;
|
|
35 |
import jdk.internal.vm.compiler.collections.UnmodifiableMapCursor;
|
|
36 |
import org.junit.Assert;
|
|
37 |
import org.junit.Test;
|
|
38 |
import org.junit.runner.RunWith;
|
|
39 |
import org.junit.runners.Parameterized;
|
|
40 |
import org.junit.runners.Parameterized.Parameter;
|
|
41 |
import org.junit.runners.Parameterized.Parameters;
|
|
42 |
|
|
43 |
@RunWith(Parameterized.class)
|
|
44 |
public class EconomicMapLargeTest {
|
|
45 |
|
|
46 |
@Parameter(value = 0) public EconomicMap<Object, Object> testMap;
|
|
47 |
@Parameter(value = 1) public EconomicMap<Object, Object> referenceMap;
|
|
48 |
@Parameter(value = 2) public String name;
|
|
49 |
|
|
50 |
@Parameters(name = "{2}")
|
|
51 |
public static Collection<Object[]> data() {
|
|
52 |
return Arrays.asList(new Object[]{EconomicMap.create(Equivalence.DEFAULT), EconomicMap.create(Equivalence.DEFAULT), "EconomicMap"},
|
|
53 |
new Object[]{EconomicMap.create(Equivalence.IDENTITY), EconomicMap.create(Equivalence.IDENTITY), "EconomicMap(IDENTITY)"},
|
|
54 |
new Object[]{EconomicMap.create(Equivalence.IDENTITY_WITH_SYSTEM_HASHCODE), EconomicMap.create(Equivalence.IDENTITY_WITH_SYSTEM_HASHCODE),
|
|
55 |
"EconomicMap(IDENTITY_WITH_SYSTEM_HASHCODE)"},
|
|
56 |
new Object[]{EconomicMap.create(Equivalence.DEFAULT), EconomicMap.wrapMap(new LinkedHashMap<>()), "EconomicMap<->wrapMap"},
|
|
57 |
new Object[]{EconomicMap.wrapMap(new LinkedHashMap<>()), EconomicMap.wrapMap(new LinkedHashMap<>()), "wrapMap"});
|
|
58 |
}
|
|
59 |
|
|
60 |
private static int[] createRandomRange(Random random, int count) {
|
|
61 |
int[] result = new int[count];
|
|
62 |
for (int i = 0; i < count; ++i) {
|
|
63 |
int range = random.nextInt(14);
|
|
64 |
if (range == 0 || range > 10) {
|
|
65 |
range = Integer.MAX_VALUE;
|
|
66 |
} else if (range == 10) {
|
|
67 |
range = 100;
|
|
68 |
}
|
|
69 |
result[i] = range;
|
|
70 |
}
|
|
71 |
return result;
|
|
72 |
}
|
|
73 |
|
|
74 |
private static final class BadHashClass {
|
|
75 |
private int value;
|
|
76 |
|
|
77 |
BadHashClass(int randomInt) {
|
|
78 |
this.value = randomInt;
|
|
79 |
}
|
|
80 |
|
|
81 |
@Override
|
|
82 |
public int hashCode() {
|
|
83 |
return 0;
|
|
84 |
}
|
|
85 |
|
|
86 |
@Override
|
|
87 |
public boolean equals(Object other) {
|
|
88 |
if (other instanceof BadHashClass) {
|
|
89 |
BadHashClass badHashClass = (BadHashClass) other;
|
|
90 |
return badHashClass.value == value;
|
|
91 |
}
|
|
92 |
return false;
|
|
93 |
}
|
|
94 |
}
|
|
95 |
|
|
96 |
interface MapAction {
|
|
97 |
Object perform(EconomicMap<Object, Object> map, int randomInt);
|
|
98 |
}
|
|
99 |
|
|
100 |
static final Object EXISTING_VALUE = new Object();
|
|
101 |
|
|
102 |
static final MapAction[] INCREASE_ACTIONS = new MapAction[]{
|
|
103 |
(map, randomInt) -> map.put(randomInt, "value"),
|
|
104 |
(map, randomInt) -> map.get(randomInt)
|
|
105 |
};
|
|
106 |
|
|
107 |
static final MapAction[] ACTIONS = new MapAction[]{
|
|
108 |
(map, randomInt) -> map.removeKey(randomInt),
|
|
109 |
(map, randomInt) -> map.put(randomInt, "value"),
|
|
110 |
(map, randomInt) -> map.put(randomInt, null),
|
|
111 |
(map, randomInt) -> map.put(EXISTING_VALUE, randomInt),
|
|
112 |
(map, randomInt) -> {
|
|
113 |
if (randomInt == 0) {
|
|
114 |
map.clear();
|
|
115 |
}
|
|
116 |
return map.isEmpty();
|
|
117 |
},
|
|
118 |
(map, randomInt) -> map.containsKey(randomInt),
|
|
119 |
(map, randomInt) -> map.get(randomInt),
|
|
120 |
(map, randomInt) -> map.put(new BadHashClass(randomInt), "unique"),
|
|
121 |
(map, randomInt) -> {
|
|
122 |
if (randomInt == 0) {
|
|
123 |
map.replaceAll((key, value) -> Objects.toString(value) + "!");
|
|
124 |
}
|
|
125 |
return map.isEmpty();
|
|
126 |
}
|
|
127 |
|
|
128 |
};
|
|
129 |
|
|
130 |
@Test
|
|
131 |
public void testVeryLarge() {
|
|
132 |
testMap.clear();
|
|
133 |
referenceMap.clear();
|
|
134 |
|
|
135 |
Random random = new Random(0);
|
|
136 |
for (int i = 0; i < 200000; ++i) {
|
|
137 |
for (int j = 0; j < INCREASE_ACTIONS.length; ++j) {
|
|
138 |
int nextInt = random.nextInt(10000000);
|
|
139 |
MapAction action = INCREASE_ACTIONS[j];
|
|
140 |
Object result = action.perform(testMap, nextInt);
|
|
141 |
Object referenceResult = action.perform(referenceMap, nextInt);
|
|
142 |
Assert.assertEquals(result, referenceResult);
|
|
143 |
}
|
|
144 |
}
|
|
145 |
}
|
|
146 |
|
|
147 |
/**
|
|
148 |
* Tests a sequence of random operations on the map.
|
|
149 |
*/
|
|
150 |
@Test
|
|
151 |
public void testAddRemove() {
|
|
152 |
testMap.clear();
|
|
153 |
referenceMap.clear();
|
|
154 |
|
|
155 |
for (int seed = 0; seed < 10; ++seed) {
|
|
156 |
Random random = new Random(seed);
|
|
157 |
int[] ranges = createRandomRange(random, ACTIONS.length);
|
|
158 |
int value = random.nextInt(10000);
|
|
159 |
for (int i = 0; i < value; ++i) {
|
|
160 |
for (int j = 0; j < ACTIONS.length; ++j) {
|
|
161 |
if (random.nextInt(ranges[j]) == 0) {
|
|
162 |
int nextInt = random.nextInt(100);
|
|
163 |
MapAction action = ACTIONS[j];
|
|
164 |
Object result = action.perform(testMap, nextInt);
|
|
165 |
Object referenceResult = action.perform(referenceMap, nextInt);
|
|
166 |
Assert.assertEquals(result, referenceResult);
|
|
167 |
if (j % 100 == 0) {
|
|
168 |
checkEquality(testMap, referenceMap);
|
|
169 |
}
|
|
170 |
}
|
|
171 |
}
|
|
172 |
|
|
173 |
if (random.nextInt(20) == 0) {
|
|
174 |
removeElement(random.nextInt(100), testMap, referenceMap);
|
|
175 |
}
|
|
176 |
}
|
|
177 |
}
|
|
178 |
}
|
|
179 |
|
|
180 |
private static void removeElement(int index, EconomicMap<?, ?> map, EconomicMap<?, ?> referenceMap) {
|
|
181 |
Assert.assertEquals(referenceMap.size(), map.size());
|
|
182 |
MapCursor<?, ?> cursor = map.getEntries();
|
|
183 |
MapCursor<?, ?> referenceCursor = referenceMap.getEntries();
|
|
184 |
int z = 0;
|
|
185 |
while (cursor.advance()) {
|
|
186 |
Assert.assertTrue(referenceCursor.advance());
|
|
187 |
Assert.assertEquals(referenceCursor.getKey(), cursor.getKey());
|
|
188 |
Assert.assertEquals(referenceCursor.getValue(), cursor.getValue());
|
|
189 |
if (index == z) {
|
|
190 |
cursor.remove();
|
|
191 |
referenceCursor.remove();
|
|
192 |
}
|
|
193 |
++z;
|
|
194 |
}
|
|
195 |
|
|
196 |
Assert.assertFalse(referenceCursor.advance());
|
|
197 |
}
|
|
198 |
|
|
199 |
private static void checkEquality(EconomicMap<?, ?> map, EconomicMap<?, ?> referenceMap) {
|
|
200 |
Assert.assertEquals(referenceMap.size(), map.size());
|
|
201 |
|
|
202 |
// Check entries.
|
|
203 |
UnmodifiableMapCursor<?, ?> cursor = map.getEntries();
|
|
204 |
UnmodifiableMapCursor<?, ?> referenceCursor = referenceMap.getEntries();
|
|
205 |
while (cursor.advance()) {
|
|
206 |
Assert.assertTrue(referenceCursor.advance());
|
|
207 |
Assert.assertEquals(referenceCursor.getKey(), cursor.getKey());
|
|
208 |
Assert.assertEquals(referenceCursor.getValue(), cursor.getValue());
|
|
209 |
}
|
|
210 |
|
|
211 |
// Check keys.
|
|
212 |
Iterator<?> iterator = map.getKeys().iterator();
|
|
213 |
Iterator<?> referenceIterator = referenceMap.getKeys().iterator();
|
|
214 |
while (iterator.hasNext()) {
|
|
215 |
Assert.assertTrue(referenceIterator.hasNext());
|
|
216 |
Assert.assertEquals(iterator.next(), referenceIterator.next());
|
|
217 |
}
|
|
218 |
|
|
219 |
// Check values.
|
|
220 |
iterator = map.getValues().iterator();
|
|
221 |
referenceIterator = referenceMap.getValues().iterator();
|
|
222 |
while (iterator.hasNext()) {
|
|
223 |
Assert.assertTrue(referenceIterator.hasNext());
|
|
224 |
Assert.assertEquals(iterator.next(), referenceIterator.next());
|
|
225 |
}
|
|
226 |
Assert.assertFalse(referenceIterator.hasNext());
|
|
227 |
}
|
|
228 |
|
|
229 |
}
|