# HG changeset patch # User mli # Date 1543390483 -28800 # Node ID 70adb0f573a78c939783326dcac9d061a1ae30ce # Parent c76cfae195c8f05da723de1ec323452a06028af3 8211975: move testlibrary/jdk/testlibrary/OptimalCapacity.java to top-level library Reviewed-by: igerasim diff -r c76cfae195c8 -r 70adb0f573a7 test/jdk/java/lang/Character/UnicodeBlock/OptimalMapSize.java --- a/test/jdk/java/lang/Character/UnicodeBlock/OptimalMapSize.java Wed Nov 28 10:21:07 2018 +0530 +++ b/test/jdk/java/lang/Character/UnicodeBlock/OptimalMapSize.java Wed Nov 28 15:34:43 2018 +0800 @@ -25,14 +25,14 @@ * @test * @bug 8080535 8191410 * @summary Expected size of Character.UnicodeBlock.map is not optimal - * @library /lib/testlibrary + * @library /test/lib * @modules java.base/java.lang:open * java.base/java.util:open - * @build jdk.testlibrary.OptimalCapacity + * @build jdk.test.lib.util.OptimalCapacity * @run main OptimalMapSize */ -import jdk.testlibrary.OptimalCapacity; +import jdk.test.lib.util.OptimalCapacity; // What will be the number of the Unicode blocks in the future. // diff -r c76cfae195c8 -r 70adb0f573a7 test/jdk/java/lang/Enum/ConstantDirectoryOptimalCapacity.java --- a/test/jdk/java/lang/Enum/ConstantDirectoryOptimalCapacity.java Wed Nov 28 10:21:07 2018 +0530 +++ b/test/jdk/java/lang/Enum/ConstantDirectoryOptimalCapacity.java Wed Nov 28 15:34:43 2018 +0800 @@ -25,14 +25,14 @@ * @test * @bug 8200696 * @summary Initial capacity of Class.enumConstantDirectory is not optimal - * @library /lib/testlibrary + * @library /test/lib * @modules java.base/java.lang:open * java.base/java.util:open - * @build jdk.testlibrary.OptimalCapacity + * @build jdk.test.lib.util.OptimalCapacity * @run main ConstantDirectoryOptimalCapacity */ -import jdk.testlibrary.OptimalCapacity; +import jdk.test.lib.util.OptimalCapacity; public class ConstantDirectoryOptimalCapacity { diff -r c76cfae195c8 -r 70adb0f573a7 test/jdk/java/lang/invoke/VarHandle/AccessMode/OptimalMapSize.java --- a/test/jdk/java/lang/invoke/VarHandle/AccessMode/OptimalMapSize.java Wed Nov 28 10:21:07 2018 +0530 +++ b/test/jdk/java/lang/invoke/VarHandle/AccessMode/OptimalMapSize.java Wed Nov 28 15:34:43 2018 +0800 @@ -25,15 +25,15 @@ * @test * @bug 8200788 * @summary Optimal initial capacity of AccessMode.methodNameToAccessMode - * @library /lib/testlibrary + * @library /test/lib * @modules java.base/java.lang.invoke:open * java.base/java.util:open - * @build jdk.testlibrary.OptimalCapacity + * @build jdk.test.lib.util.OptimalCapacity * @run main OptimalMapSize */ import java.lang.invoke.VarHandle.AccessMode; -import jdk.testlibrary.OptimalCapacity; +import jdk.test.lib.util.OptimalCapacity; public class OptimalMapSize { public static void main(String[] args) throws Throwable { diff -r c76cfae195c8 -r 70adb0f573a7 test/jdk/lib/testlibrary/jdk/testlibrary/OptimalCapacity.java --- a/test/jdk/lib/testlibrary/jdk/testlibrary/OptimalCapacity.java Wed Nov 28 10:21:07 2018 +0530 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,240 +0,0 @@ -/* - * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * 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. - */ - -package jdk.testlibrary; - -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.IdentityHashMap; - -/** - * Utility functions to check that the static storages are pre-sized - * optimally. - */ -public final class OptimalCapacity { - - private OptimalCapacity() {} - - /** - * Checks adequacy of the initial capacity of a static field - * of type {@code ArrayList}. - * - * Having - *
-     * class XClass {
-     *     static ArrayList theList = new ArrayList(N);
-     * }
-     * 
- * - * you should call from the test - * - *
-     * OptimalCapacity.assertProperlySized(XClass.class, "theList", N);
-     * 
- */ - public static void ofArrayList(Class clazz, String fieldName, - int initialCapacity) - { - try { - Field field = clazz.getDeclaredField(fieldName); - field.setAccessible(true); - Object obj = field.get(null); - if (!ArrayList.class.equals(obj.getClass())) { - throw new RuntimeException("'" + field + - "' expected to be of type ArrayList"); - } - ArrayList list = (ArrayList)obj; - - // For ArrayList the optimal capacity is its final size - if (list.size() != initialCapacity) { - throw new RuntimeException("Size of '" + field + - "' is " + list.size() + - ", but expected to be " + initialCapacity); - } - if (internalArraySize(list) != initialCapacity) { - throw new RuntimeException("Capacity of '" + field + - "' is " + internalArraySize(list) + - ", but expected to be " + initialCapacity); - } - } catch (ReflectiveOperationException roe) { - throw new RuntimeException(roe); - } - } - - /** - * Checks adequacy of the initial capacity of a static field - * of type {@code HashMap}. - * - * Having - *
-     * class XClass {
-     *     static HashMap theMap = new HashMap(N);
-     * }
-     * 
- * - * you should call from the test - * - *
-     * OptimalCapacity.ofHashMap(XClass.class, "theMap", N);
-     * 
- */ - public static void ofHashMap(Class clazz, String fieldName, - int initialCapacity) - { - ofHashMap(clazz, null, fieldName, initialCapacity); - } - - /** - * Checks adequacy of the initial capacity of a non-static field - * of type {@code HashMap}. - * - * Having - *
-     * class XClass {
-     *     HashMap theMap = new HashMap(N);
-     * }
-     * XClass instance = ...
-     * 
- * - * you should call from the test - * - *
-     * OptimalCapacity.ofHashMap(XClass.class, instance, "theMap", N);
-     * 
- */ - public static void ofHashMap(Class clazz, Object instance, - String fieldName, int initialCapacity) - { - try { - Field field = clazz.getDeclaredField(fieldName); - field.setAccessible(true); - Object obj = field.get(instance); - if (!HashMap.class.equals(obj.getClass())) { - throw new RuntimeException(field + - " expected to be of type HashMap"); - } - HashMap map = (HashMap)obj; - - // Check that the map allocates only necessary amount of space - HashMap tmp = new HashMap<>(map); - if (internalArraySize(map) != internalArraySize(tmp)) { - throw new RuntimeException("Final capacity of '" + field + - "' is " + internalArraySize(map) + - ", which exceeds necessary minimum " + internalArraySize(tmp)); - } - - // Check that map is initially properly sized - tmp = new HashMap<>(initialCapacity); - tmp.put(new Object(), new Object()); // trigger storage init - if (internalArraySize(map) != internalArraySize(tmp)) { - throw new RuntimeException("Requested capacity of '" + field + - "' was " + initialCapacity + - ", which resulted in final capacity " + internalArraySize(tmp) + - ", which differs from necessary minimum " + internalArraySize(map)); - } - - } catch (ReflectiveOperationException roe) { - throw new RuntimeException(roe); - } - } - - /** - * Checks adequacy of the expected maximum size of a static field - * of type {@code IdentityHashMap}. - * - * Having - *
-     * class XClass {
-     *     static IdentityHashMap theMap = new IdentityHashMap(M);
-     * }
-     * 
- * - * you should call from the test - * - *
-     * OptimalCapacity.ofIdentityHashMap(XClass.class, "theMap", M);
-     * 
- */ - public static void ofIdentityHashMap(Class clazz, String fieldName, - int expectedMaxSize) - { - try { - Field field = clazz.getDeclaredField(fieldName); - field.setAccessible(true); - Object obj = field.get(null); - if (!IdentityHashMap.class.equals(obj.getClass())) { - throw new RuntimeException("'" + field + - "' expected to be of type IdentityHashMap"); - } - IdentityHashMap map = (IdentityHashMap)obj; - - // Check that size of map is what was expected - if (map.size() != expectedMaxSize) { - throw new RuntimeException("Size of '" + field + - "' is " + map.size() + - ", which differs from expected " + expectedMaxSize); - } - - // Check that the map allocated only necessary amount of memory - IdentityHashMap tmp = new IdentityHashMap<>(map); - if (internalArraySize(map) != internalArraySize(tmp)) { - throw new RuntimeException("Final capacity of '" + field + - "' is " + internalArraySize(map) + - ", which exceeds necessary minimum " + internalArraySize(tmp)); - } - - // Check that map was initially properly sized - tmp = new IdentityHashMap<>(expectedMaxSize); - tmp.put(new Object(), new Object()); // trigger storage init - if (internalArraySize(map) != internalArraySize(tmp)) { - throw new RuntimeException("Requested number of elements in '" + field + - "' was " + expectedMaxSize + - ", which resulted in final capacity " + internalArraySize(tmp) + - ", which differs from necessary minimum " + internalArraySize(map)); - } - } catch (ReflectiveOperationException roe) { - throw new RuntimeException(roe); - } - } - - /** - * Returns size of the internal storage. - */ - private static int internalArraySize(Object container) - throws ReflectiveOperationException { - Field field; - if (ArrayList.class.equals(container.getClass())) { - field = ArrayList.class.getDeclaredField("elementData"); - } else if (HashMap.class.equals(container.getClass())) { - field = HashMap.class.getDeclaredField("table"); - } else if (IdentityHashMap.class.equals(container.getClass())) { - field = IdentityHashMap.class.getDeclaredField("table"); - } else { - throw new RuntimeException("Unexpected class " + - container.getClass()); - } - field.setAccessible(true); - return ((Object[])field.get(container)).length; - } -} diff -r c76cfae195c8 -r 70adb0f573a7 test/lib/jdk/test/lib/util/OptimalCapacity.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/lib/jdk/test/lib/util/OptimalCapacity.java Wed Nov 28 15:34:43 2018 +0800 @@ -0,0 +1,240 @@ +/* + * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * 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. + */ + +package jdk.test.lib.util; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.IdentityHashMap; + +/** + * Utility functions to check that the static storages are pre-sized + * optimally. + */ +public final class OptimalCapacity { + + private OptimalCapacity() {} + + /** + * Checks adequacy of the initial capacity of a static field + * of type {@code ArrayList}. + * + * Having + *
+     * class XClass {
+     *     static ArrayList theList = new ArrayList(N);
+     * }
+     * 
+ * + * you should call from the test + * + *
+     * OptimalCapacity.assertProperlySized(XClass.class, "theList", N);
+     * 
+ */ + public static void ofArrayList(Class clazz, String fieldName, + int initialCapacity) + { + try { + Field field = clazz.getDeclaredField(fieldName); + field.setAccessible(true); + Object obj = field.get(null); + if (!ArrayList.class.equals(obj.getClass())) { + throw new RuntimeException("'" + field + + "' expected to be of type ArrayList"); + } + ArrayList list = (ArrayList)obj; + + // For ArrayList the optimal capacity is its final size + if (list.size() != initialCapacity) { + throw new RuntimeException("Size of '" + field + + "' is " + list.size() + + ", but expected to be " + initialCapacity); + } + if (internalArraySize(list) != initialCapacity) { + throw new RuntimeException("Capacity of '" + field + + "' is " + internalArraySize(list) + + ", but expected to be " + initialCapacity); + } + } catch (ReflectiveOperationException roe) { + throw new RuntimeException(roe); + } + } + + /** + * Checks adequacy of the initial capacity of a static field + * of type {@code HashMap}. + * + * Having + *
+     * class XClass {
+     *     static HashMap theMap = new HashMap(N);
+     * }
+     * 
+ * + * you should call from the test + * + *
+     * OptimalCapacity.ofHashMap(XClass.class, "theMap", N);
+     * 
+ */ + public static void ofHashMap(Class clazz, String fieldName, + int initialCapacity) + { + ofHashMap(clazz, null, fieldName, initialCapacity); + } + + /** + * Checks adequacy of the initial capacity of a non-static field + * of type {@code HashMap}. + * + * Having + *
+     * class XClass {
+     *     HashMap theMap = new HashMap(N);
+     * }
+     * XClass instance = ...
+     * 
+ * + * you should call from the test + * + *
+     * OptimalCapacity.ofHashMap(XClass.class, instance, "theMap", N);
+     * 
+ */ + public static void ofHashMap(Class clazz, Object instance, + String fieldName, int initialCapacity) + { + try { + Field field = clazz.getDeclaredField(fieldName); + field.setAccessible(true); + Object obj = field.get(instance); + if (!HashMap.class.equals(obj.getClass())) { + throw new RuntimeException(field + + " expected to be of type HashMap"); + } + HashMap map = (HashMap)obj; + + // Check that the map allocates only necessary amount of space + HashMap tmp = new HashMap<>(map); + if (internalArraySize(map) != internalArraySize(tmp)) { + throw new RuntimeException("Final capacity of '" + field + + "' is " + internalArraySize(map) + + ", which exceeds necessary minimum " + internalArraySize(tmp)); + } + + // Check that map is initially properly sized + tmp = new HashMap<>(initialCapacity); + tmp.put(new Object(), new Object()); // trigger storage init + if (internalArraySize(map) != internalArraySize(tmp)) { + throw new RuntimeException("Requested capacity of '" + field + + "' was " + initialCapacity + + ", which resulted in final capacity " + internalArraySize(tmp) + + ", which differs from necessary minimum " + internalArraySize(map)); + } + + } catch (ReflectiveOperationException roe) { + throw new RuntimeException(roe); + } + } + + /** + * Checks adequacy of the expected maximum size of a static field + * of type {@code IdentityHashMap}. + * + * Having + *
+     * class XClass {
+     *     static IdentityHashMap theMap = new IdentityHashMap(M);
+     * }
+     * 
+ * + * you should call from the test + * + *
+     * OptimalCapacity.ofIdentityHashMap(XClass.class, "theMap", M);
+     * 
+ */ + public static void ofIdentityHashMap(Class clazz, String fieldName, + int expectedMaxSize) + { + try { + Field field = clazz.getDeclaredField(fieldName); + field.setAccessible(true); + Object obj = field.get(null); + if (!IdentityHashMap.class.equals(obj.getClass())) { + throw new RuntimeException("'" + field + + "' expected to be of type IdentityHashMap"); + } + IdentityHashMap map = (IdentityHashMap)obj; + + // Check that size of map is what was expected + if (map.size() != expectedMaxSize) { + throw new RuntimeException("Size of '" + field + + "' is " + map.size() + + ", which differs from expected " + expectedMaxSize); + } + + // Check that the map allocated only necessary amount of memory + IdentityHashMap tmp = new IdentityHashMap<>(map); + if (internalArraySize(map) != internalArraySize(tmp)) { + throw new RuntimeException("Final capacity of '" + field + + "' is " + internalArraySize(map) + + ", which exceeds necessary minimum " + internalArraySize(tmp)); + } + + // Check that map was initially properly sized + tmp = new IdentityHashMap<>(expectedMaxSize); + tmp.put(new Object(), new Object()); // trigger storage init + if (internalArraySize(map) != internalArraySize(tmp)) { + throw new RuntimeException("Requested number of elements in '" + field + + "' was " + expectedMaxSize + + ", which resulted in final capacity " + internalArraySize(tmp) + + ", which differs from necessary minimum " + internalArraySize(map)); + } + } catch (ReflectiveOperationException roe) { + throw new RuntimeException(roe); + } + } + + /** + * Returns size of the internal storage. + */ + private static int internalArraySize(Object container) + throws ReflectiveOperationException { + Field field; + if (ArrayList.class.equals(container.getClass())) { + field = ArrayList.class.getDeclaredField("elementData"); + } else if (HashMap.class.equals(container.getClass())) { + field = HashMap.class.getDeclaredField("table"); + } else if (IdentityHashMap.class.equals(container.getClass())) { + field = IdentityHashMap.class.getDeclaredField("table"); + } else { + throw new RuntimeException("Unexpected class " + + container.getClass()); + } + field.setAccessible(true); + return ((Object[])field.get(container)).length; + } +}