ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/HashMap/WhiteBoxResizeTest.java
Revision: 1.1
Committed: Tue Dec 18 20:21:24 2018 UTC (5 years, 5 months ago) by jsr166
Branch: MAIN
Log Message:
8210280: Unnecessary reallocation when invoking HashMap.putAll()

File Contents

# Content
1 /*
2 * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
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
23 import org.testng.annotations.Test;
24
25 import java.lang.invoke.MethodHandle;
26 import java.lang.invoke.MethodHandles;
27 import java.lang.invoke.MethodType;
28 import java.lang.invoke.VarHandle;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.stream.IntStream;
32
33 import static java.util.stream.Collectors.toMap;
34 import static org.testng.Assert.assertEquals;
35 import static org.testng.Assert.assertNull;
36
37 /*
38 * @test
39 * @bug 8210280
40 * @modules java.base/java.util:open
41 * @summary White box tests for HashMap internals around table resize
42 * @run testng WhiteBoxResizeTest
43 */
44 public class WhiteBoxResizeTest {
45 final MethodHandle TABLE_SIZE_FOR;
46 final VarHandle THRESHOLD;
47 final VarHandle TABLE;
48
49 public WhiteBoxResizeTest() throws ReflectiveOperationException {
50 Class<?> mClass = HashMap.class;
51 String nodeClassName = mClass.getName() + "$Node";
52 Class<?> nodeArrayClass = Class.forName("[L" + nodeClassName + ";");
53 MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(mClass, MethodHandles.lookup());
54 TABLE = lookup.findVarHandle(mClass, "table", nodeArrayClass);
55 this.TABLE_SIZE_FOR = lookup.findStatic(
56 mClass, "tableSizeFor",
57 MethodType.methodType(int.class, int.class));
58 this.THRESHOLD = lookup.findVarHandle(mClass, "threshold", int.class);
59 }
60
61 int tableSizeFor(int n) {
62 try {
63 return (int) TABLE_SIZE_FOR.invoke(n);
64 } catch (Throwable t) { throw new AssertionError(t); }
65 }
66
67 Object[] table(HashMap map) {
68 try {
69 return (Object[]) TABLE.get(map);
70 } catch (Throwable t) { throw new AssertionError(t); }
71 }
72
73 int capacity(HashMap map) {
74 return table(map).length;
75 }
76
77 @Test
78 public void testTableSizeFor() {
79 assertEquals(tableSizeFor(0), 1);
80 assertEquals(tableSizeFor(1), 1);
81 assertEquals(tableSizeFor(2), 2);
82 assertEquals(tableSizeFor(3), 4);
83 assertEquals(tableSizeFor(15), 16);
84 assertEquals(tableSizeFor(16), 16);
85 assertEquals(tableSizeFor(17), 32);
86 int maxSize = 1 << 30;
87 assertEquals(tableSizeFor(maxSize - 1), maxSize);
88 assertEquals(tableSizeFor(maxSize), maxSize);
89 assertEquals(tableSizeFor(maxSize + 1), maxSize);
90 assertEquals(tableSizeFor(Integer.MAX_VALUE), maxSize);
91 }
92
93 @Test
94 public void capacityTest() {
95 HashMap<Integer, Integer> map = new HashMap<>();
96 assertNull(table(map));
97
98 map.put(1, 1);
99 assertEquals(capacity(map), 16);
100
101 map.putAll(IntStream.range(0, 64).boxed().collect(toMap(i -> i, i -> i)));
102 assertEquals(capacity(map), 128);
103 }
104 }