Path: blob/master/test/jdk/java/util/HashMap/WhiteBoxResizeTest.java
41149 views
/*1* Copyright (c) 2018, Red Hat, Inc. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 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 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import org.testng.annotations.Test;2425import java.lang.invoke.MethodHandle;26import java.lang.invoke.MethodHandles;27import java.lang.invoke.MethodType;28import java.lang.invoke.VarHandle;29import java.util.HashMap;30import java.util.LinkedHashMap;31import java.util.List;32import java.util.Map;33import java.util.concurrent.ThreadLocalRandom;34import java.util.function.Supplier;35import java.util.stream.IntStream;3637import static java.util.stream.Collectors.toMap;38import static org.testng.Assert.assertEquals;39import static org.testng.Assert.assertNull;4041/*42* @test43* @bug 821028044* @modules java.base/java.util:open45* @summary White box tests for HashMap internals around table resize46* @run testng WhiteBoxResizeTest47* @key randomness48*/49public class WhiteBoxResizeTest {50final ThreadLocalRandom rnd = ThreadLocalRandom.current();51final MethodHandle TABLE_SIZE_FOR;52final VarHandle THRESHOLD;53final VarHandle TABLE;5455public WhiteBoxResizeTest() throws ReflectiveOperationException {56Class<?> mClass = HashMap.class;57String nodeClassName = mClass.getName() + "$Node";58Class<?> nodeArrayClass = Class.forName("[L" + nodeClassName + ";");59MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(mClass, MethodHandles.lookup());60TABLE = lookup.findVarHandle(mClass, "table", nodeArrayClass);61this.TABLE_SIZE_FOR = lookup.findStatic(62mClass, "tableSizeFor",63MethodType.methodType(int.class, int.class));64this.THRESHOLD = lookup.findVarHandle(mClass, "threshold", int.class);65}6667int tableSizeFor(int n) {68try {69return (int) TABLE_SIZE_FOR.invoke(n);70} catch (Throwable t) { throw new AssertionError(t); }71}7273Object[] table(HashMap map) {74try {75return (Object[]) TABLE.get(map);76} catch (Throwable t) { throw new AssertionError(t); }77}7879int capacity(HashMap map) {80return table(map).length;81}8283@Test84public void testTableSizeFor() {85assertEquals(tableSizeFor(0), 1);86assertEquals(tableSizeFor(1), 1);87assertEquals(tableSizeFor(2), 2);88assertEquals(tableSizeFor(3), 4);89assertEquals(tableSizeFor(15), 16);90assertEquals(tableSizeFor(16), 16);91assertEquals(tableSizeFor(17), 32);92int maxSize = 1 << 30;93assertEquals(tableSizeFor(maxSize - 1), maxSize);94assertEquals(tableSizeFor(maxSize), maxSize);95assertEquals(tableSizeFor(maxSize + 1), maxSize);96assertEquals(tableSizeFor(Integer.MAX_VALUE), maxSize);97}9899@Test100public void capacityTestDefaultConstructor() {101capacityTestDefaultConstructor(new HashMap<>());102capacityTestDefaultConstructor(new LinkedHashMap<>());103}104105void capacityTestDefaultConstructor(HashMap<Integer, Integer> map) {106assertNull(table(map));107108map.put(1, 1);109assertEquals(capacity(map), 16); // default initial capacity110111map.putAll(IntStream.range(0, 64).boxed().collect(toMap(i -> i, i -> i)));112assertEquals(capacity(map), 128);113}114115@Test116public void capacityTestInitialCapacity() {117int initialCapacity = rnd.nextInt(2, 128);118List<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));123124for (Supplier<HashMap<Integer, Integer>> supplier : suppliers) {125HashMap<Integer, Integer> map = supplier.get();126assertNull(table(map));127128map.put(1, 1);129assertEquals(capacity(map), tableSizeFor(initialCapacity));130}131}132}133134135