1 /* 2 * Copyright (c) 2018, 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 /* @test 25 * @summary unit tests for java.lang.invoke.MethodHandles 26 * @library /lib/testlibrary /java/lang/invoke/common 27 * @compile MethodHandlesTest.java MethodHandlesPermuteArgumentsTest.java remote/RemoteExample.java 28 * @run junit/othervm/timeout=2500 -XX:+IgnoreUnrecognizedVMOptions 29 * -XX:-VerifyDependencies 30 * -esa 31 * test.java.lang.invoke.MethodHandlesPermuteArgumentsTest 32 */ 33 34 35 package test.java.lang.invoke; 36 37 import org.junit.*; 38 import test.java.lang.invoke.lib.CodeCacheOverflowProcessor; 39 40 import java.lang.invoke.MethodHandle; 41 import java.lang.invoke.MethodHandles; 42 import java.lang.invoke.MethodType; 43 import java.util.Arrays; 44 45 import static org.junit.Assert.*; 46 47 public class MethodHandlesPermuteArgumentsTest extends MethodHandlesTest { 48 49 @Test // SLOW testPermuteArguments()50 public void testPermuteArguments() throws Throwable { 51 CodeCacheOverflowProcessor.runMHTest(this::testPermuteArguments0); 52 } 53 54 // Android-added: @Test for test cases. 55 @Test testPermuteArguments0()56 public void testPermuteArguments0() throws Throwable { 57 if (CAN_SKIP_WORKING) return; 58 startTest("permuteArguments"); 59 testPermuteArguments(4, Integer.class, 2, long.class, 6); 60 if (CAN_TEST_LIGHTLY) return; 61 testPermuteArguments(4, Integer.class, 2, String.class, 0); 62 testPermuteArguments(6, Integer.class, 0, null, 30); 63 } 64 65 // Android-added: @Ignore as this is a test helper. 66 @Ignore testPermuteArguments(int max, Class<?> type1, int t2c, Class<?> type2, int dilution)67 public void testPermuteArguments(int max, Class<?> type1, int t2c, Class<?> type2, int dilution) throws Throwable { 68 if (verbosity >= 2) 69 System.out.println("permuteArguments "+max+"*"+type1.getName() 70 +(t2c==0?"":"/"+t2c+"*"+type2.getName()) 71 +(dilution > 0 ? " with dilution "+dilution : "")); 72 int t2pos = t2c == 0 ? 0 : 1; 73 for (int inargs = t2pos+1; inargs <= max; inargs++) { 74 Class<?>[] types = new Class<?>[inargs]; 75 Arrays.fill(types, type1); 76 if (t2c != 0) { 77 // Fill in a middle range with type2: 78 Arrays.fill(types, t2pos, Math.min(t2pos+t2c, inargs), type2); 79 } 80 Object[] args = randomArgs(types); 81 int numcases = 1; 82 for (int outargs = 0; outargs <= max; outargs++) { 83 if (outargs - inargs >= MAX_ARG_INCREASE) continue; 84 int casStep = dilution + 1; 85 // Avoid some common factors: 86 while ((casStep > 2 && casStep % 2 == 0 && inargs % 2 == 0) || 87 (casStep > 3 && casStep % 3 == 0 && inargs % 3 == 0)) 88 casStep++; 89 testPermuteArguments(args, types, outargs, numcases, casStep); 90 numcases *= inargs; 91 if (CAN_TEST_LIGHTLY && outargs < max-2) continue; 92 if (dilution > 10 && outargs >= 4) { 93 if (CAN_TEST_LIGHTLY) continue; 94 int[] reorder = new int[outargs]; 95 // Do some special patterns, which we probably missed. 96 // Replication of a single argument or argument pair. 97 for (int i = 0; i < inargs; i++) { 98 Arrays.fill(reorder, i); 99 testPermuteArguments(args, types, reorder); 100 for (int d = 1; d <= 2; d++) { 101 if (i + d >= inargs) continue; 102 for (int j = 1; j < outargs; j += 2) 103 reorder[j] += 1; 104 testPermuteArguments(args, types, reorder); 105 testPermuteArguments(args, types, reverse(reorder)); 106 } 107 } 108 // Repetition of a sequence of 3 or more arguments. 109 for (int i = 1; i < inargs; i++) { 110 for (int len = 3; len <= inargs; len++) { 111 for (int j = 0; j < outargs; j++) 112 reorder[j] = (i + (j % len)) % inargs; 113 testPermuteArguments(args, types, reorder); 114 testPermuteArguments(args, types, reverse(reorder)); 115 } 116 } 117 } 118 } 119 } 120 } 121 122 // Android-added: @Ignore for test helper. 123 @Ignore testPermuteArguments(Object[] args, Class<?>[] types, int outargs, int numcases, int casStep)124 public void testPermuteArguments(Object[] args, Class<?>[] types, 125 int outargs, int numcases, int casStep) throws Throwable { 126 int inargs = args.length; 127 int[] reorder = new int[outargs]; 128 for (int cas = 0; cas < numcases; cas += casStep) { 129 for (int i = 0, c = cas; i < outargs; i++) { 130 reorder[i] = c % inargs; 131 c /= inargs; 132 } 133 if (CAN_TEST_LIGHTLY && outargs >= 3 && 134 (reorder[0] == reorder[1] || reorder[1] == reorder[2])) 135 continue; 136 testPermuteArguments(args, types, reorder); 137 } 138 } 139 reverse(int[] reorder)140 static int[] reverse(int[] reorder) { 141 reorder = reorder.clone(); 142 for (int i = 0, imax = reorder.length / 2; i < imax; i++) { 143 int j = reorder.length - 1 - i; 144 int tem = reorder[i]; 145 reorder[i] = reorder[j]; 146 reorder[j] = tem; 147 } 148 return reorder; 149 } 150 151 // Android-added: @Ignore for test helper. 152 @Ignore testPermuteArguments(Object[] args, Class<?>[] types, int[] reorder)153 void testPermuteArguments(Object[] args, Class<?>[] types, int[] reorder) throws Throwable { 154 countTest(); 155 if (args == null && types == null) { 156 int max = 0; 157 for (int j : reorder) { 158 if (max < j) max = j; 159 } 160 args = randomArgs(max+1, Integer.class); 161 } 162 if (args == null) { 163 args = randomArgs(types); 164 } 165 if (types == null) { 166 types = new Class<?>[args.length]; 167 for (int i = 0; i < args.length; i++) 168 types[i] = args[i].getClass(); 169 } 170 int inargs = args.length, outargs = reorder.length; 171 assertTrue(inargs == types.length); 172 if (verbosity >= 3) 173 System.out.println("permuteArguments "+Arrays.toString(reorder)); 174 Object[] permArgs = new Object[outargs]; 175 Class<?>[] permTypes = new Class<?>[outargs]; 176 for (int i = 0; i < outargs; i++) { 177 permArgs[i] = args[reorder[i]]; 178 permTypes[i] = types[reorder[i]]; 179 } 180 if (verbosity >= 4) { 181 System.out.println("in args: "+Arrays.asList(args)); 182 System.out.println("out args: "+Arrays.asList(permArgs)); 183 System.out.println("in types: "+Arrays.asList(types)); 184 System.out.println("out types: "+Arrays.asList(permTypes)); 185 } 186 MethodType inType = MethodType.methodType(Object.class, types); 187 MethodType outType = MethodType.methodType(Object.class, permTypes); 188 MethodHandle target = varargsList(outargs).asType(outType); 189 MethodHandle newTarget = MethodHandles.permuteArguments(target, inType, reorder); 190 if (verbosity >= 5) System.out.println("newTarget = "+newTarget); 191 Object result = newTarget.invokeWithArguments(args); 192 Object expected = Arrays.asList(permArgs); 193 if (!expected.equals(result)) { 194 System.out.println("*** failed permuteArguments "+Arrays.toString(reorder)+ 195 " types="+Arrays.asList(types)); 196 System.out.println("in args: "+Arrays.asList(args)); 197 System.out.println("out args: "+expected); 198 System.out.println("bad args: "+result); 199 } 200 assertEquals(expected, result); 201 } 202 } 203