ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/HashMap/WhiteBoxResizeTest.java
Revision: 1.2
Committed: Wed Dec 19 14:48:32 2018 UTC (5 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.1: +30 -3 lines
Log Message:
add whitebox tests for initial capacity and LinkedHashMap, as with ConcurrentHashMap's whitebox tests

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.LinkedHashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.concurrent.ThreadLocalRandom;
34 import java.util.function.Supplier;
35 import java.util.stream.IntStream;
36
37 import static java.util.stream.Collectors.toMap;
38 import static org.testng.Assert.assertEquals;
39 import static org.testng.Assert.assertNull;
40
41 /*
42 * @test
43 * @bug 8210280
44 * @modules java.base/java.util:open
45 * @summary White box tests for HashMap internals around table resize
46 * @run testng WhiteBoxResizeTest
47 */
48 public class WhiteBoxResizeTest {
49 final ThreadLocalRandom rnd = ThreadLocalRandom.current();
50 final MethodHandle TABLE_SIZE_FOR;
51 final VarHandle THRESHOLD;
52 final VarHandle TABLE;
53
54 public WhiteBoxResizeTest() throws ReflectiveOperationException {
55 Class<?> mClass = HashMap.class;
56 String nodeClassName = mClass.getName() + "$Node";
57 Class<?> nodeArrayClass = Class.forName("[L" + nodeClassName + ";");
58 MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(mClass, MethodHandles.lookup());
59 TABLE = lookup.findVarHandle(mClass, "table", nodeArrayClass);
60 this.TABLE_SIZE_FOR = lookup.findStatic(
61 mClass, "tableSizeFor",
62 MethodType.methodType(int.class, int.class));
63 this.THRESHOLD = lookup.findVarHandle(mClass, "threshold", int.class);
64 }
65
66 int tableSizeFor(int n) {
67 try {
68 return (int) TABLE_SIZE_FOR.invoke(n);
69 } catch (Throwable t) { throw new AssertionError(t); }
70 }
71
72 Object[] table(HashMap map) {
73 try {
74 return (Object[]) TABLE.get(map);
75 } catch (Throwable t) { throw new AssertionError(t); }
76 }
77
78 int capacity(HashMap map) {
79 return table(map).length;
80 }
81
82 @Test
83 public void testTableSizeFor() {
84 assertEquals(tableSizeFor(0), 1);
85 assertEquals(tableSizeFor(1), 1);
86 assertEquals(tableSizeFor(2), 2);
87 assertEquals(tableSizeFor(3), 4);
88 assertEquals(tableSizeFor(15), 16);
89 assertEquals(tableSizeFor(16), 16);
90 assertEquals(tableSizeFor(17), 32);
91 int maxSize = 1 << 30;
92 assertEquals(tableSizeFor(maxSize - 1), maxSize);
93 assertEquals(tableSizeFor(maxSize), maxSize);
94 assertEquals(tableSizeFor(maxSize + 1), maxSize);
95 assertEquals(tableSizeFor(Integer.MAX_VALUE), maxSize);
96 }
97
98 @Test
99 public void capacityTestDefaultConstructor() {
100 capacityTestDefaultConstructor(new HashMap<>());
101 capacityTestDefaultConstructor(new LinkedHashMap<>());
102 }
103
104 void capacityTestDefaultConstructor(HashMap<Integer, Integer> map) {
105 assertNull(table(map));
106
107 map.put(1, 1);
108 assertEquals(capacity(map), 16); // default initial capacity
109
110 map.putAll(IntStream.range(0, 64).boxed().collect(toMap(i -> i, i -> i)));
111 assertEquals(capacity(map), 128);
112 }
113
114 @Test
115 public void capacityTestInitialCapacity() {
116 int initialCapacity = rnd.nextInt(1, 256);
117 List<Supplier<HashMap<Integer, Integer>>> suppliers = List.of(
118 () -> new HashMap<>(initialCapacity),
119 () -> new HashMap<>(initialCapacity, 0.75f),
120 () -> new LinkedHashMap<>(initialCapacity),
121 () -> new LinkedHashMap<>(initialCapacity, 0.75f));
122
123 for (Supplier<HashMap<Integer, Integer>> supplier : suppliers) {
124 HashMap<Integer, Integer> map = supplier.get();
125 assertNull(table(map));
126
127 map.put(1, 1);
128 assertEquals(capacity(map), tableSizeFor(initialCapacity));
129 }
130 }
131 }