/* * Copyright (c) 2018, Red Hat, Inc. All rights reserved. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import org.testng.annotations.Test; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; import java.lang.invoke.VarHandle; import java.util.HashMap; import java.util.stream.IntStream; import static java.util.stream.Collectors.toMap; import static org.testng.Assert.assertEquals; /* * @test * @bug 8210280 * @modules java.base/java.util:open * @summary White box tests for HashMap internals around table resize * @run testng WhiteBoxResizeTest */ public class WhiteBoxResizeTest { final MethodHandle TABLE_SIZE_FOR; final MethodHandle RESIZE; final VarHandle THRESHOLD; final VarHandle TABLE; public WhiteBoxResizeTest() throws ReflectiveOperationException { Class mClass = HashMap.class; String nodeClassName = mClass.getName() + "$Node"; Class nodeArrayClass = Class.forName("[L" + nodeClassName + ";"); MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(mClass, MethodHandles.lookup()); TABLE = lookup.findVarHandle(mClass, "table", nodeArrayClass); this.TABLE_SIZE_FOR = lookup.findStatic( mClass, "tableSizeFor", MethodType.methodType(int.class, int.class)); this.RESIZE = lookup.findVirtual(mClass, "resize", MethodType.methodType(nodeArrayClass, int.class)); this.THRESHOLD = lookup.findVarHandle(mClass, "threshold", int.class); } int tableSizeFor(int n) { try { return (int) TABLE_SIZE_FOR.invoke(n); } catch (Throwable t) { throw new AssertionError(t); } } @Test public void testTableSizeFor() { assertEquals(tableSizeFor(0), 1); assertEquals(tableSizeFor(1), 1); assertEquals(tableSizeFor(2), 2); assertEquals(tableSizeFor(3), 4); assertEquals(tableSizeFor(15), 16); assertEquals(tableSizeFor(16), 16); assertEquals(tableSizeFor(17), 32); int maxSize = 1 << 30; assertEquals(tableSizeFor(maxSize - 1), maxSize); assertEquals(tableSizeFor(maxSize), maxSize); assertEquals(tableSizeFor(maxSize + 1), maxSize); assertEquals(tableSizeFor(Integer.MAX_VALUE), maxSize); } @Test public void putAllCapacityTest() { var map = new HashMap(); map.putAll(IntStream.range(0, 10).boxed().collect(toMap(i -> i, i -> i))); int capacity = ((Object[]) TABLE.get(map)).length; assertEquals(capacity, 16); } @Test public void callResizeWithSizeTest() throws Throwable { var map = new HashMap(); RESIZE.invoke(map, 0); int capacity = ((Object[]) TABLE.get(map)).length; assertEquals(capacity, 1); RESIZE.invoke(map, 15); capacity = ((Object[]) TABLE.get(map)).length; assertEquals(capacity, 16); RESIZE.invoke(map, 256); capacity = ((Object[]) TABLE.get(map)).length; assertEquals(capacity, 256); RESIZE.invoke(map, 257); capacity = ((Object[]) TABLE.get(map)).length; assertEquals(capacity, 512); } }