Vladimir Ivanov | 0898161 | 2014-09-10 19:19:47 +0400 | [diff] [blame] | 1 | /* |
Konstantin Shefov | b07bdeb | 2015-06-17 20:17:25 +0300 | [diff] [blame] | 2 | * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. |
Vladimir Ivanov | 0898161 | 2014-09-10 19:19:47 +0400 | [diff] [blame] | 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 | |
Vladimir Ivanov | 0898161 | 2014-09-10 19:19:47 +0400 | [diff] [blame] | 24 | |
Alan Bateman | db4d383 | 2016-03-17 19:04:16 +0000 | [diff] [blame] | 25 | import java.lang.invoke.MethodHandle; |
| 26 | import java.lang.invoke.MethodHandleHelper; |
| 27 | import java.lang.invoke.MethodType; |
Vladimir Ivanov | 0898161 | 2014-09-10 19:19:47 +0400 | [diff] [blame] | 28 | import sun.invoke.util.Wrapper; |
Vladimir Ivanov | 0898161 | 2014-09-10 19:19:47 +0400 | [diff] [blame] | 29 | import java.util.Arrays; |
| 30 | import java.util.Collections; |
Konstantin Shefov | b07bdeb | 2015-06-17 20:17:25 +0300 | [diff] [blame] | 31 | import com.oracle.testlibrary.jsr292.CodeCacheOverflowProcessor; |
Vladimir Ivanov | 0898161 | 2014-09-10 19:19:47 +0400 | [diff] [blame] | 32 | |
| 33 | /* @test |
| 34 | * @summary unit tests for varargs array methods: MethodHandleInfo.varargsArray(int), |
| 35 | * MethodHandleInfo.varargsArray(Class,int) & MethodHandleInfo.varargsList(int) |
Alan Bateman | db4d383 | 2016-03-17 19:04:16 +0000 | [diff] [blame] | 36 | * @modules java.base/sun.invoke.util |
Konstantin Shefov | b07bdeb | 2015-06-17 20:17:25 +0300 | [diff] [blame] | 37 | * @library /lib/testlibrary /lib/testlibrary/jsr292 |
Alan Bateman | db4d383 | 2016-03-17 19:04:16 +0000 | [diff] [blame] | 38 | * @compile/module=java.base java/lang/invoke/MethodHandleHelper.java |
| 39 | * @run main/bootclasspath VarargsArrayTest |
Alan Bateman | 36e0820 | 2016-05-03 09:09:57 +0100 | [diff] [blame] | 40 | * @run main/bootclasspath/othervm -DVarargsArrayTest.MAX_ARITY=255 -DVarargsArrayTest.START_ARITY=250 |
Alan Bateman | db4d383 | 2016-03-17 19:04:16 +0000 | [diff] [blame] | 41 | * VarargsArrayTest |
Vladimir Ivanov | 0898161 | 2014-09-10 19:19:47 +0400 | [diff] [blame] | 42 | */ |
| 43 | |
| 44 | /* This might take a while and burn lots of metadata: |
Alan Bateman | db4d383 | 2016-03-17 19:04:16 +0000 | [diff] [blame] | 45 | * @run main/bootclasspath -DVarargsArrayTest.MAX_ARITY=255 -DVarargsArrayTest.EXHAUSTIVE=true VarargsArrayTest |
Vladimir Ivanov | 0898161 | 2014-09-10 19:19:47 +0400 | [diff] [blame] | 46 | */ |
| 47 | public class VarargsArrayTest { |
| 48 | private static final Class<?> CLASS = VarargsArrayTest.class; |
| 49 | private static final int MAX_ARITY = Integer.getInteger(CLASS.getSimpleName()+".MAX_ARITY", 40); |
| 50 | private static final int START_ARITY = Integer.getInteger(CLASS.getSimpleName()+".START_ARITY", 0); |
| 51 | private static final boolean EXHAUSTIVE = Boolean.getBoolean(CLASS.getSimpleName()+".EXHAUSTIVE"); |
| 52 | |
| 53 | public static void main(String[] args) throws Throwable { |
Konstantin Shefov | b07bdeb | 2015-06-17 20:17:25 +0300 | [diff] [blame] | 54 | CodeCacheOverflowProcessor.runMHTest(VarargsArrayTest::test); |
| 55 | } |
| 56 | |
| 57 | public static void test() throws Throwable { |
Vladimir Ivanov | 0898161 | 2014-09-10 19:19:47 +0400 | [diff] [blame] | 58 | testVarargsArray(); |
| 59 | testVarargsReferenceArray(); |
| 60 | testVarargsPrimitiveArray(); |
| 61 | } |
| 62 | |
| 63 | public static void testVarargsArray() throws Throwable { |
| 64 | final int MIN = START_ARITY; |
| 65 | final int MAX = MAX_ARITY-2; // 253+1 would cause parameter overflow with 'this' added |
| 66 | for (int nargs = MIN; nargs <= MAX; nargs = nextArgCount(nargs, 17, MAX)) { |
Alan Bateman | db4d383 | 2016-03-17 19:04:16 +0000 | [diff] [blame] | 67 | MethodHandle target = MethodHandleHelper.varargsArray(nargs); |
Vladimir Ivanov | 0898161 | 2014-09-10 19:19:47 +0400 | [diff] [blame] | 68 | Object[] args = new Object[nargs]; |
| 69 | for (int i = 0; i < nargs; i++) |
| 70 | args[i] = "#"+i; |
| 71 | Object res = target.invokeWithArguments(args); |
| 72 | assertArrayEquals(args, (Object[])res); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public static void testVarargsReferenceArray() throws Throwable { |
| 77 | testTypedVarargsArray(Object[].class); |
| 78 | testTypedVarargsArray(String[].class); |
| 79 | testTypedVarargsArray(Number[].class); |
| 80 | } |
| 81 | |
| 82 | public static void testVarargsPrimitiveArray() throws Throwable { |
| 83 | testTypedVarargsArray(int[].class); |
| 84 | testTypedVarargsArray(long[].class); |
| 85 | testTypedVarargsArray(byte[].class); |
| 86 | testTypedVarargsArray(boolean[].class); |
| 87 | testTypedVarargsArray(short[].class); |
| 88 | testTypedVarargsArray(char[].class); |
| 89 | testTypedVarargsArray(float[].class); |
| 90 | testTypedVarargsArray(double[].class); |
| 91 | } |
| 92 | |
| 93 | private static int nextArgCount(int nargs, int density, int MAX) { |
| 94 | if (EXHAUSTIVE) return nargs + 1; |
| 95 | if (nargs >= MAX) return Integer.MAX_VALUE; |
| 96 | int BOT = 20, TOP = MAX-5; |
| 97 | if (density < 10) { BOT = 10; MAX = TOP-2; } |
| 98 | if (nargs <= BOT || nargs >= TOP) { |
| 99 | ++nargs; |
| 100 | } else { |
| 101 | int bump = Math.max(1, 100 / density); |
| 102 | nargs += bump; |
| 103 | if (nargs > TOP) nargs = TOP; |
| 104 | } |
| 105 | return nargs; |
| 106 | } |
| 107 | |
| 108 | private static void testTypedVarargsArray(Class<?> arrayType) throws Throwable { |
| 109 | Class<?> elemType = arrayType.getComponentType(); |
| 110 | int MIN = START_ARITY; |
| 111 | int MAX = MAX_ARITY-2; // 253+1 would cause parameter overflow with 'this' added |
| 112 | int density = 3; |
| 113 | if (elemType == int.class || elemType == long.class) density = 7; |
| 114 | if (elemType == long.class || elemType == double.class) { MAX /= 2; MIN /= 2; } |
| 115 | for (int nargs = MIN; nargs <= MAX; nargs = nextArgCount(nargs, density, MAX)) { |
| 116 | Object[] args = makeTestArray(elemType, nargs); |
Alan Bateman | db4d383 | 2016-03-17 19:04:16 +0000 | [diff] [blame] | 117 | MethodHandle varargsArray = MethodHandleHelper.varargsArray(arrayType, nargs); |
Vladimir Ivanov | 0898161 | 2014-09-10 19:19:47 +0400 | [diff] [blame] | 118 | MethodType vaType = varargsArray.type(); |
| 119 | assertEquals(arrayType, vaType.returnType()); |
| 120 | if (nargs != 0) { |
| 121 | assertEquals(elemType, vaType.parameterType(0)); |
| 122 | assertEquals(elemType, vaType.parameterType(vaType.parameterCount()-1)); |
| 123 | } |
| 124 | assertEquals(MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType)), |
| 125 | vaType); |
| 126 | Object res = varargsArray.invokeWithArguments(args); |
| 127 | assertEquals(res.getClass(), arrayType); |
| 128 | String resString = toArrayString(res); |
| 129 | assertEquals(Arrays.toString(args), resString); |
| 130 | |
| 131 | MethodHandle spreader = varargsArray.asSpreader(arrayType, nargs); |
| 132 | MethodType stype = spreader.type(); |
| 133 | assert(stype == MethodType.methodType(arrayType, arrayType)); |
| 134 | if (nargs <= 5) { |
| 135 | // invoke target as a spreader also: |
| 136 | @SuppressWarnings("cast") |
| 137 | Object res2 = spreader.invokeWithArguments((Object)res); |
| 138 | String res2String = toArrayString(res2); |
| 139 | assertEquals(Arrays.toString(args), res2String); |
| 140 | // invoke the spreader on a generic Object[] array; check for error |
| 141 | try { |
| 142 | Object res3 = spreader.invokeWithArguments((Object)args); |
| 143 | String res3String = toArrayString(res3); |
| 144 | assertTrue(arrayType.getName(), arrayType.isAssignableFrom(Object[].class)); |
| 145 | assertEquals(Arrays.toString(args), res3String); |
| 146 | } catch (ClassCastException ex) { |
| 147 | assertFalse(arrayType.getName(), arrayType.isAssignableFrom(Object[].class)); |
| 148 | } |
| 149 | } |
| 150 | if (nargs == 0) { |
| 151 | // invoke spreader on null arglist |
| 152 | Object res3 = spreader.invokeWithArguments((Object)null); |
| 153 | String res3String = toArrayString(res3); |
| 154 | assertEquals(Arrays.toString(args), res3String); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | private static Object[] makeTestArray(Class<?> elemType, int len) { |
| 160 | Wrapper elem = null; |
| 161 | if (elemType.isPrimitive()) |
| 162 | elem = Wrapper.forPrimitiveType(elemType); |
| 163 | else if (Wrapper.isWrapperType(elemType)) |
| 164 | elem = Wrapper.forWrapperType(elemType); |
| 165 | Object[] args = new Object[len]; |
| 166 | for (int i = 0; i < len; i++) { |
| 167 | Object arg = i * 100; |
| 168 | if (elem == null) { |
| 169 | if (elemType == String.class) |
| 170 | arg = "#"+arg; |
| 171 | arg = elemType.cast(arg); // just to make sure |
| 172 | } else { |
| 173 | switch (elem) { |
| 174 | case BOOLEAN: arg = (i % 3 == 0); break; |
| 175 | case CHAR: arg = 'a' + i; break; |
| 176 | case LONG: arg = (long)i * 1000_000_000; break; |
| 177 | case FLOAT: arg = (float)i / 100; break; |
| 178 | case DOUBLE: arg = (double)i / 1000_000; break; |
| 179 | } |
| 180 | arg = elem.cast(arg, elemType); |
| 181 | } |
| 182 | args[i] = arg; |
| 183 | } |
| 184 | return args; |
| 185 | } |
| 186 | |
| 187 | private static String toArrayString(Object a) { |
| 188 | if (a == null) return "null"; |
| 189 | Class<?> elemType = a.getClass().getComponentType(); |
| 190 | if (elemType == null) return a.toString(); |
| 191 | if (elemType.isPrimitive()) { |
| 192 | switch (Wrapper.forPrimitiveType(elemType)) { |
| 193 | case INT: return Arrays.toString((int[])a); |
| 194 | case BYTE: return Arrays.toString((byte[])a); |
| 195 | case BOOLEAN: return Arrays.toString((boolean[])a); |
| 196 | case SHORT: return Arrays.toString((short[])a); |
| 197 | case CHAR: return Arrays.toString((char[])a); |
| 198 | case FLOAT: return Arrays.toString((float[])a); |
| 199 | case LONG: return Arrays.toString((long[])a); |
| 200 | case DOUBLE: return Arrays.toString((double[])a); |
| 201 | } |
| 202 | } |
| 203 | return Arrays.toString((Object[])a); |
| 204 | } |
| 205 | |
| 206 | public static void assertArrayEquals(Object[] arr1, Object[] arr2) { |
| 207 | if (arr1 == null && arr2 == null) return; |
| 208 | if (arr1 != null && arr2 != null && arr1.length == arr2.length) { |
| 209 | for (int i = 0; i < arr1.length; i++) { |
| 210 | assertEquals(arr1[i], arr2[i]); |
| 211 | } |
| 212 | return; |
| 213 | } |
| 214 | throw new AssertionError(Arrays.deepToString(arr1) + " != " + Arrays.deepToString(arr2)); |
| 215 | } |
| 216 | |
| 217 | public static void assertEquals(Object o1, Object o2) { |
| 218 | if (o1 == null && o2 == null) return; |
| 219 | if (o1 != null && o1.equals(o2)) return; |
| 220 | throw new AssertionError(o1 + " != " + o2); |
| 221 | } |
| 222 | |
| 223 | public static void assertTrue(String msg, boolean b) { |
| 224 | if (!b) { |
| 225 | throw new AssertionError(msg); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | public static void assertFalse(String msg, boolean b) { |
| 230 | assertTrue(msg, !b); |
| 231 | } |
| 232 | } |