ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/HashMap/WhiteBoxResizeTest.java
Revision: 1.4
Committed: Wed Dec 19 21:11:53 2018 UTC (5 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.3: +1 -1 lines
Log Message:
capacityTestInitialCapacity avoid capacity of 1

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