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