Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/PermuteArgsTest.java
47216 views
/*1* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 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 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @summary unit tests for method handles which permute their arguments25* @library /lib/testlibrary/jsr292 /lib/testlibrary26* @run testng/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies -ea -esa -DPermuteArgsTest.MAX_ARITY=8 test.java.lang.invoke.PermuteArgsTest27*/28/* Examples of manual runs:29* java -DPermuteArgsTest.{DRY_RUN=true,MAX_ARITY=253} test.java.lang.invoke.PermuteArgsTest30* java -DPermuteArgsTest.{VERBOSE=true,MAX_ARITY=5} test.java.lang.invoke.PermuteArgsTest31* java test.java.lang.invoke.PermuteArgsTest list3I[2,0,1] listJLJ[2,0,1]32*/3334package test.java.lang.invoke;3536import org.testng.*;37import org.testng.annotations.*;3839import com.oracle.testlibrary.jsr292.CodeCacheOverflowProcessor;4041import java.util.*;42import java.lang.reflect.*;4344import java.lang.invoke.*;45import static java.lang.invoke.MethodHandles.*;46import static java.lang.invoke.MethodType.*;4748public class PermuteArgsTest {49private static final Class<?> CLASS = PermuteArgsTest.class;50private static final int MAX_ARITY = Integer.getInteger(CLASS.getSimpleName()+".MAX_ARITY", 8);51private static final boolean DRY_RUN = Boolean.getBoolean(CLASS.getSimpleName()+".DRY_RUN");52private static final boolean VERBOSE = Boolean.getBoolean(CLASS.getSimpleName()+".VERBOSE") || DRY_RUN;5354static Object list2I(int x, int y) {55return Arrays.asList(x, y);56}57static Object list3I(int x, int y, int z) {58return Arrays.asList(x, y, z);59}60static Object list4I(int w, int x, int y, int z) {61return Arrays.asList(w, x, y, z);62}63static Object list2J(long x, long y) {64return Arrays.asList(x, y);65}66static Object list3J(long x, long y, long z) {67return Arrays.asList(x, y, z);68}69static Object list4J(long w, long x, long y, long z) {70return Arrays.asList(w, x, y, z);71}72static Object list2I2J(int w, int x, long y, long z) {73return Arrays.asList(w, x, y, z);74}75static Object list2J2I(long w, long x, int y, int z) {76return Arrays.asList(w, x, y, z);77}78static Object listLJJ(Object x, long y, long z) {79return Arrays.asList(x, y, z);80}81static Object listJLJ(long x, Object y, long z) {82return Arrays.asList(x, y, z);83}84static Object listJJL(long x, long y, Object z) {85return Arrays.asList(x, y, z);86}87static Object listJLL(long x, Object y, Object z) {88return Arrays.asList(x, y, z);89}90static Object listLJL(Object x, long y, Object z) {91return Arrays.asList(x, y, z);92}93static Object listLLJ(Object x, Object y, long z) {94return Arrays.asList(x, y, z);95}96static Object listJLLJ(long w, Object x, Object y, long z) {97return Arrays.asList(w, x, y, z);98}99static Object listLJJL(Object w, long x, long y, Object z) {100return Arrays.asList(w, x, y, z);101}102static Object listI_etc(int... va) {103ArrayList<Object> res = new ArrayList<>();104for (int x : va) res.add(x);105return res;106}107static Object listIJL_etc(int x, long y, Object z, Object... va) {108ArrayList<Object> res = new ArrayList<>();109res.addAll(Arrays.asList(x, y, z));110res.addAll(Arrays.asList(va));111return res;112}113114public static void main(String argv[]) throws Throwable {115if (argv.length > 0) {116for (String arg : argv) {117// arg ::= name[n,...]118int k = arg.indexOf('[');119String mhName = arg.substring(0, k).trim();120String permString = arg.substring(k);121testOnePermutation(mhName, permString);122}123return;124}125new PermuteArgsTest().test();126}127128static int testCases;129130@Test131public void test() throws Throwable {132CodeCacheOverflowProcessor.runMHTest(this::test0);133}134135public void test0() throws Throwable {136testCases = 0;137Lookup lookup = lookup();138for (Method m : lookup.lookupClass().getDeclaredMethods()) {139if (m.getName().startsWith("list") &&140Modifier.isStatic(m.getModifiers())) {141test(m.getName(), lookup.unreflect(m));142}143}144System.out.println("ran a total of "+testCases+" test cases");145}146147static int jump(int i, int min, int max) {148if (i >= min && i <= max-1) {149// jump faster150int len = max-min;151if (i < min + len/2)152i = min + len/2;153else154i = max-1;155}156return i;157}158159static void test(String name, MethodHandle mh) throws Throwable {160if (VERBOSE)161System.out.println("mh = "+name+" : "+mh+" { "162+Arrays.toString(junkArgs(mh.type().parameterArray())));163int testCases0 = testCases;164if (!mh.isVarargsCollector()) {165// normal case166testPermutations(mh);167} else {168// varargs case; add params up to MAX_ARITY169MethodType mt = mh.type();170int posArgs = mt.parameterCount() - 1;171int arity0 = Math.max(3, posArgs);172for (int arity = arity0; arity <= MAX_ARITY; arity++) {173MethodHandle mh1;174try {175mh1 = adjustArity(mh, arity);176} catch (IllegalArgumentException ex) {177System.out.println("*** mh = "+name+" : "+mh+"; arity = "+arity+" => "+ex);178ex.printStackTrace(System.out);179break; // cannot get this arity for this type180}181test("("+arity+")"+name, mh1);182arity = jump(arity, arity0*2, MAX_ARITY);183}184}185if (VERBOSE)186System.out.println("ran "+(testCases - testCases0)+" test cases for "+name+" }");187}188189static MethodHandle adjustArity(MethodHandle mh, int arity) {190MethodType mt = mh.type();191int posArgs = mt.parameterCount() - 1;192Class<?> reptype = mt.parameterType(posArgs).getComponentType();193MethodType mt1 = mt.dropParameterTypes(posArgs, posArgs+1);194while (mt1.parameterCount() < arity) {195Class<?> pt = reptype;196if (pt == Object.class && posArgs > 0)197// repeat types cyclically if possible:198pt = mt1.parameterType(mt1.parameterCount() - posArgs);199mt1 = mt1.appendParameterTypes(pt);200}201try {202return mh.asType(mt1);203} catch (WrongMethodTypeException | IllegalArgumentException ex) {204throw new IllegalArgumentException("cannot convert to type "+mt1+" from "+mh, ex);205}206}207static MethodHandle findTestMH(String name, int[] perm) throws ReflectiveOperationException {208int arity = perm.length;209Lookup lookup = lookup();210for (Method m : lookup.lookupClass().getDeclaredMethods()) {211if (m.getName().equals(name) &&212Modifier.isStatic(m.getModifiers())) {213MethodHandle mh = lookup.unreflect(m);214int mhArity = mh.type().parameterCount();215if (mh.isVarargsCollector()) {216if (mhArity-1 <= arity)217return adjustArity(mh, arity);218} else if (mhArity == arity) {219return mh;220}221}222}223throw new RuntimeException("no such method for arity "+arity+": "+name);224}225226static void testPermutations(MethodHandle mh) throws Throwable {227HashSet<String> done = new HashSet<>();228MethodType mt = mh.type();229int[] perm = nullPerm(mt.parameterCount());230final int MARGIN = (perm.length <= 10 ? 2 : 0);231int testCases0 = testCases;232for (int j = 0; j <= 1; j++) {233int maxStart = perm.length-1;234if (j != 0) maxStart /= 2;235for (int start = 0; start <= maxStart; start++) {236int maxOmit = (maxStart - start) / 2;237if (start != 0) maxOmit = 2;238if (j != 0) maxOmit = 1;239for (int omit = 0; omit <= maxOmit; omit++) {240int end = perm.length - omit;241if (end - start >= 2) {242//System.out.println("testPermutations"+Arrays.asList(start, end)+(j == 0 ? "" : " (reverse)"));243testPermutations(mh, perm, start, end, done);244}245omit = jump(omit, (start == 0 && j == 0 ? MARGIN : 0), maxOmit);246}247start = jump(start, (j == 0 ? MARGIN : 0), maxStart);248}249// do everything in reverse:250reverse(perm, 0, perm.length);251}252switch (perm.length) {253case 2: assert(testCases - testCases0 == 2); break;254case 3: assert(testCases - testCases0 == 6); break;255case 4: assert(testCases - testCases0 == 24); break;256case 5: assert(testCases - testCases0 == 120); break;257case 6: assert(testCases - testCases0 > 720/3); break;258}259}260261static void testPermutations(MethodHandle mh, int[] perm, int start, int end, Set<String> done) throws Throwable {262if (end - start <= 1) return;263for (int j = 0; j <= 1; j++) {264testRotations(mh, perm, start, end, done);265if (end - start <= 2) return;266reverse(perm, start, end);267}268if (end - start <= 3) return;269int excess4 = (end - start) - 4;270// composed rotations:271int start2 = start + 1 + excess4/3;272int end2 = end - excess4/3;273end2 = start2 + Math.min(start == 0 ? 4 : 3, end2 - start2);274int skips = (perm.length+3)/5;275for (int i = start; i < end; i++) {276rotate(perm, start, end);277if (skips > 1 && ((i-start) + (i-start)/7) % skips != 0) continue;278for (int j = 0; j <= 1; j++) {279testPermutations(mh, perm, start2, end2, done);280reverse(perm, start, end);281}282}283}284285static void testRotations(MethodHandle mh, int[] perm, int start, int end, Set<String> done) throws Throwable {286Object[] args = junkArgs(mh.type().parameterArray());287for (int i = start; i < end; i++) {288if (done.add(Arrays.toString(perm)))289testOnePermutation(mh, perm, args);290rotate(perm, start, end);291}292}293294static void testOnePermutation(MethodHandle mh, int[] perm, Object[] args) throws Throwable {295MethodType mt = mh.type();296MethodType pmt = methodType(mt.returnType(), unpermuteArgs(perm, mt.parameterArray(), Class[].class));297if (VERBOSE)298System.out.println(Arrays.toString(perm));299testCases += 1;300if (DRY_RUN)301return;302Object res = permuteArguments(mh, pmt, perm).invokeWithArguments(unpermuteArgs(perm, args));303String str = String.valueOf(res);304if (!Arrays.toString(args).equals(str)) {305System.out.println(Arrays.toString(perm)+" "+str+" *** WRONG ***");306}307}308309// For reproducing failures:310static void testOnePermutation(String mhName, String permString) throws Throwable {311String s = permString;312s = s.replace('[', ' ').replace(']', ' ').replace(',', ' '); // easier to trim spaces313s = s.trim();314int[] perm = new int[s.length()];315int arity = 0;316while (!s.isEmpty()) {317int k = s.indexOf(' ');318if (k < 0) k = s.length();319perm[arity++] = Integer.parseInt(s.substring(0, k));320s = s.substring(k).trim();321}322perm = Arrays.copyOf(perm, arity);323testOnePermutation(mhName, perm);324}325static void testOnePermutation(String mhName, int[] perm) throws Throwable {326MethodHandle mh = findTestMH(mhName, perm);327System.out.println("mh = "+mhName+" : "+mh+" { "328+Arrays.toString(junkArgs(mh.type().parameterArray())));329Object[] args = junkArgs(mh.type().parameterArray());330testOnePermutation(mh, perm, args);331System.out.println("}");332}333334static Object[] junkArgs(Class<?>[] ptypes) {335Object[] args = new Object[ptypes.length];336for (int i = 0; i < ptypes.length; i++) {337Class<?> pt = ptypes[i];338Object arg;339if (pt == Void.class) arg = null;340else if (pt == int.class) arg = i + 101;341else if (pt == long.class) arg = i + 10_000_000_001L;342else arg = "#" + (i + 1);343args[i] = arg;344}345return args;346}347348static int[] nullPerm(int len) {349int[] perm = new int[len];350for (int i = 0; i < len; i++)351perm[i] = i;352return perm;353}354static void rotate(int[] perm) {355rotate(perm, 0, perm.length);356}357static void rotate(int[] perm, int start, int end) {358int x = perm[end-1];359for (int j = start; j < end; j++) {360int y = perm[j]; perm[j] = x; x = y;361}362}363static void reverse(int[] perm) {364reverse(perm, 0, perm.length);365}366static void reverse(int[] perm, int start, int end) {367int mid = start + (end - start)/2;368for (int j = start; j < mid; j++) {369int k = (end-1) - j;370int x = perm[j]; perm[j] = perm[k]; perm[k] = x;371}372}373// Permute the args according to the inverse of perm.374static Object[] unpermuteArgs(int[] perm, Object[] args) {375return unpermuteArgs(perm, args, Object[].class);376}377static <T> T[] unpermuteArgs(int[] perm, T[] args, Class<T[]> Tclass) {378T[] res = Arrays.copyOf(new Object[0], perm.length, Tclass);379for (int i = 0; i < perm.length; i++)380res[perm[i]] = args[i];381return res;382}383}384385386