ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/HashMap/WhiteBoxResizeTest.java
Revision: 1.5
Committed: Tue Jan 21 23:46:12 2020 UTC (4 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +1 -0 lines
Log Message:
8237589: Fix copyright header formatting

File Contents

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