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.Map;
  31 import java.util.stream.IntStream;
  32 
  33 import static java.util.stream.Collectors.toMap;
  34 import static org.testng.Assert.assertEquals;
  35 
  36 /*
  37  * @test
  38  * @bug 8210280
  39  * @modules java.base/java.util:open
  40  * @summary White box tests for HashMap internals around table resize
  41  * @run testng WhiteBoxResizeTest
  42  */
  43 public class WhiteBoxResizeTest {
  44     final MethodHandle TABLE_SIZE_FOR;
  45     final VarHandle THRESHOLD;
  46     final VarHandle TABLE;
  47 
  48     public WhiteBoxResizeTest() throws ReflectiveOperationException {
  49         Class<?> mClass = HashMap.class;
  50         String nodeClassName = mClass.getName() + "$Node";
  51         Class<?> nodeArrayClass = Class.forName("[L" + nodeClassName + ";");
  52         MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(mClass, MethodHandles.lookup());
  53         TABLE = lookup.findVarHandle(mClass, "table", nodeArrayClass);
  54         this.TABLE_SIZE_FOR = lookup.findStatic(
  55                 mClass, "tableSizeFor",
  56                 MethodType.methodType(int.class, int.class));
  57         this.THRESHOLD = lookup.findVarHandle(mClass, "threshold", int.class);
  58     }
  59 
  60     int tableSizeFor(int n) {
  61         try {
  62             return (int) TABLE_SIZE_FOR.invoke(n);
  63         } catch (Throwable t) { throw new AssertionError(t); }
  64     }
  65 
  66     @Test
  67     public void testTableSizeFor() {
  68         assertEquals(tableSizeFor(0), 1);
  69         assertEquals(tableSizeFor(1), 1);
  70         assertEquals(tableSizeFor(2), 2);
  71         assertEquals(tableSizeFor(3), 4);
  72         assertEquals(tableSizeFor(15), 16);
  73         assertEquals(tableSizeFor(16), 16);
  74         assertEquals(tableSizeFor(17), 32);
  75         int maxSize = 1 << 30;
  76         assertEquals(tableSizeFor(maxSize - 1), maxSize);
  77         assertEquals(tableSizeFor(maxSize), maxSize);
  78         assertEquals(tableSizeFor(maxSize + 1), maxSize);
  79         assertEquals(tableSizeFor(Integer.MAX_VALUE), maxSize);
  80     }
  81 
  82     @Test
  83     public void putAllCapacityTest() {
  84         var map = new HashMap<Integer, Integer>();
  85         map.put(1, 1);
  86         map.putAll(IntStream.range(0, 64).boxed().collect(toMap(i -> i, i -> i)));
  87 
  88         int capacity = ((Object[]) TABLE.get(map)).length;
  89         assertEquals(capacity, 128);
  90     }
  91 }