4183N/A/*
4183N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4183N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4183N/A *
4183N/A * This code is free software; you can redistribute it and/or modify it
4183N/A * under the terms of the GNU General Public License version 2 only, as
4183N/A * published by the Free Software Foundation. Oracle designates this
4183N/A * particular file as subject to the "Classpath" exception as provided
4183N/A * by Oracle in the LICENSE file that accompanied this code.
4183N/A *
4183N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4183N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4183N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4183N/A * version 2 for more details (a copy is included in the LICENSE file that
4183N/A * accompanied this code).
4183N/A *
4183N/A * You should have received a copy of the GNU General Public License version
4183N/A * 2 along with this work; if not, write to the Free Software Foundation,
4183N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4183N/A *
4183N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4183N/A * or visit www.oracle.com if you need additional information or have any
4183N/A * questions.
4183N/A */
4183N/A
4183N/A/* @test
4183N/A * @summary unit tests for recursive method handles
4279N/A * @run junit/othervm -DRicochetTest.MAX_ARITY=50 test.java.lang.invoke.RicochetTest
4279N/A */
4279N/A/*
4279N/A * @ignore The following test creates an unreasonable number of adapters in -Xcomp mode (7049122)
4183N/A * @run junit/othervm -DRicochetTest.MAX_ARITY=255 test.java.lang.invoke.RicochetTest
4183N/A */
4183N/A
4183N/Apackage test.java.lang.invoke;
4183N/A
4183N/Aimport java.lang.invoke.*;
4183N/Aimport java.util.*;
4183N/Aimport org.junit.*;
4183N/Aimport static java.lang.invoke.MethodType.*;
4183N/Aimport static java.lang.invoke.MethodHandles.*;
4183N/Aimport static org.junit.Assert.*;
4183N/A
4183N/A
4183N/A/**
4183N/A *
4183N/A * @author jrose
4183N/A */
4183N/Apublic class RicochetTest {
5455N/A private static final Class<?> CLASS = RicochetTest.class;
4183N/A private static final int MAX_ARITY = Integer.getInteger(CLASS.getSimpleName()+".MAX_ARITY", 40);
4183N/A
4183N/A public static void main(String... av) throws Throwable {
4183N/A RicochetTest test = new RicochetTest();
4183N/A if (av.length > 0) test.testOnly = Arrays.asList(av).toString();
4183N/A if (REPEAT == 1 || test.testOnly != null) {
4183N/A test.testAll();
4183N/A if (test.testOnlyTests == null) throw new RuntimeException("no matching test: "+test.testOnly);
4183N/A } else if (REPEAT == 0) {
4183N/A org.junit.runner.JUnitCore.runClasses(RicochetTest.class);
4183N/A } else {
4183N/A verbose(1, "REPEAT="+REPEAT);
4183N/A for (int i = 0; i < REPEAT; i++) {
4183N/A test.testRepetition = (i+1);
4183N/A verbose(0, "[#"+test.testRepetition+"]");
4183N/A test.testAll();
4183N/A }
4183N/A }
4183N/A }
4183N/A int testRepetition;
4183N/A
4183N/A public void testAll() throws Throwable {
4183N/A testNull();
4183N/A testBoxInteger();
4183N/A testFilterReturnValue();
4183N/A testFilterObject();
4183N/A testBoxLong();
4183N/A testFilterInteger();
4183N/A testIntSpreads();
4183N/A testByteSpreads();
4183N/A testLongSpreads();
4183N/A testIntCollects();
4183N/A testReturns();
5452N/A testRecursion();
4183N/A }
4183N/A
4183N/A @Test
4183N/A public void testNull() throws Throwable {
4183N/A if (testRepetition > (1+REPEAT/100)) return; // trivial test
4183N/A if (!startTest("testNull")) return;
4183N/A assertEquals(opI(37), opI.invokeWithArguments(37));
4183N/A assertEqualFunction(opI, opI);
4183N/A }
4183N/A
4183N/A @Test
4183N/A public void testBoxInteger() throws Throwable {
4183N/A if (!startTest("testBoxInteger")) return;
4183N/A assertEqualFunction(opI, opI.asType(opL_I.type()).asType(opI.type()));
4183N/A }
4183N/A
4183N/A @Test
4183N/A public void testFilterReturnValue() throws Throwable {
4183N/A if (!startTest("testFilterReturnValue")) return;
4183N/A int[] ints = { 12, 23, 34, 45, 56, 67, 78, 89 };
4183N/A Object res = list8ints.invokeExact(ints[0], ints[1], ints[2], ints[3], ints[4], ints[5], ints[6], ints[7]);
4183N/A assertEquals(Arrays.toString(ints), res.toString());
4183N/A MethodHandle idreturn = filterReturnValue(list8ints, identity(Object.class));
4183N/A res = idreturn.invokeExact(ints[0], ints[1], ints[2], ints[3], ints[4], ints[5], ints[6], ints[7]);
4183N/A assertEquals(Arrays.toString(ints), res.toString());
4183N/A MethodHandle add0 = addL.bindTo(0);
4183N/A assertEqualFunction(filterReturnValue(opL2, add0), opL2);
4183N/A }
4183N/A
4183N/A @Test
4183N/A public void testFilterObject() throws Throwable {
4183N/A if (!startTest("testFilterObject")) return;
4183N/A MethodHandle add0 = addL.bindTo(0);
4183N/A assertEqualFunction(sequence(opL2, add0), opL2);
4183N/A int bump13 = -13; // value near 20 works as long as test values are near [-80..80]
4183N/A MethodHandle add13 = addL.bindTo(bump13);
4183N/A MethodHandle add13_0 = addL.bindTo(opI2(bump13, 0));
4183N/A MethodHandle add13_1 = addL.bindTo(opI2(0, bump13));
4183N/A assertEqualFunction(sequence(opL2, add13_0),
4183N/A filterArguments(opL2, 0, add13));
4183N/A assertEqualFunction(sequence(opL2, add13_1),
4183N/A filterArguments(opL2, 1, add13));
4183N/A System.out.println("[testFilterObject done]");
4183N/A }
4183N/A
4183N/A @Test
4183N/A public void testBoxLong() throws Throwable {
4183N/A if (!startTest("testBoxLong")) return;
4183N/A assertEqualFunction(opJ, opJ.asType(opL_J.type()).asType(opJ.type()));
4183N/A }
4183N/A
4183N/A @Test
4183N/A public void testFilterInteger() throws Throwable {
4183N/A if (!startTest("testFilterInteger")) return;
4183N/A assertEqualFunction(opI, sequence(convI_L, opL_I));
4183N/A }
4183N/A
4183N/A @Test
4183N/A public void testIntSpreads() throws Throwable {
4183N/A if (!startTest("testIntSpreads")) return;
4183N/A MethodHandle id = identity(int[].class);
4183N/A final int MAX = MAX_ARITY-2; // 253+1 would cause parameter overflow with 'this' added
4183N/A for (int nargs = 0; nargs <= MAX; nargs++) {
4183N/A if (nargs > 30 && nargs < MAX-20) nargs += 10;
4183N/A int[] args = new int[nargs];
5455N/A for (int j = 0; j < args.length; j++) args[j] = j + 11;
4183N/A //System.out.println("testIntSpreads "+Arrays.toString(args));
4183N/A int[] args1 = (int[]) id.invokeExact(args);
4183N/A assertArrayEquals(args, args1);
4183N/A MethodHandle coll = id.asCollector(int[].class, nargs);
4183N/A int[] args2 = args;
4183N/A switch (nargs) {
4183N/A case 0: args2 = (int[]) coll.invokeExact(); break;
4183N/A case 1: args2 = (int[]) coll.invokeExact(args[0]); break;
4183N/A case 2: args2 = (int[]) coll.invokeExact(args[0], args[1]); break;
4183N/A case 3: args2 = (int[]) coll.invokeExact(args[0], args[1], args[2]); break;
4183N/A case 4: args2 = (int[]) coll.invokeExact(args[0], args[1], args[2], args[3]); break;
4183N/A case 5: args2 = (int[]) coll.invokeExact(args[0], args[1], args[2], args[3], args[4]); break;
4183N/A }
4183N/A assertArrayEquals(args, args2);
4183N/A MethodHandle mh = coll.asSpreader(int[].class, nargs);
4183N/A int[] args3 = (int[]) mh.invokeExact(args);
4183N/A assertArrayEquals(args, args3);
4183N/A }
4183N/A }
4183N/A
4183N/A @Test
4183N/A public void testByteSpreads() throws Throwable {
4183N/A if (!startTest("testByteSpreads")) return;
4183N/A MethodHandle id = identity(byte[].class);
4183N/A final int MAX = MAX_ARITY-2; // 253+1 would cause parameter overflow with 'this' added
4183N/A for (int nargs = 0; nargs <= MAX; nargs++) {
4183N/A if (nargs > 30 && nargs < MAX-20) nargs += 10;
4183N/A byte[] args = new byte[nargs];
4183N/A for (int j = 0; j < args.length; j++) args[j] = (byte)(j + 11);
4183N/A //System.out.println("testByteSpreads "+Arrays.toString(args));
4183N/A byte[] args1 = (byte[]) id.invokeExact(args);
4183N/A assertArrayEquals(args, args1);
4183N/A MethodHandle coll = id.asCollector(byte[].class, nargs);
4183N/A byte[] args2 = args;
4183N/A switch (nargs) {
4183N/A case 0: args2 = (byte[]) coll.invokeExact(); break;
4183N/A case 1: args2 = (byte[]) coll.invokeExact(args[0]); break;
4183N/A case 2: args2 = (byte[]) coll.invokeExact(args[0], args[1]); break;
4183N/A case 3: args2 = (byte[]) coll.invokeExact(args[0], args[1], args[2]); break;
4183N/A case 4: args2 = (byte[]) coll.invokeExact(args[0], args[1], args[2], args[3]); break;
4183N/A case 5: args2 = (byte[]) coll.invokeExact(args[0], args[1], args[2], args[3], args[4]); break;
4183N/A }
4183N/A assertArrayEquals(args, args2);
4183N/A MethodHandle mh = coll.asSpreader(byte[].class, nargs);
4183N/A byte[] args3 = (byte[]) mh.invokeExact(args);
4183N/A assertArrayEquals(args, args3);
4183N/A }
4183N/A }
4183N/A
4183N/A @Test
4183N/A public void testLongSpreads() throws Throwable {
4183N/A if (!startTest("testLongSpreads")) return;
4183N/A MethodHandle id = identity(long[].class);
4183N/A final int MAX = (MAX_ARITY - 2) / 2; // 253/2+1 would cause parameter overflow with 'this' added
4183N/A for (int nargs = 0; nargs <= MAX; nargs++) {
4183N/A if (nargs > 30 && nargs < MAX-20) nargs += 10;
4183N/A long[] args = new long[nargs];
4183N/A for (int j = 0; j < args.length; j++) args[j] = (long)(j + 11);
4183N/A //System.out.println("testLongSpreads "+Arrays.toString(args));
4183N/A long[] args1 = (long[]) id.invokeExact(args);
4183N/A assertArrayEquals(args, args1);
4183N/A MethodHandle coll = id.asCollector(long[].class, nargs);
4183N/A long[] args2 = args;
4183N/A switch (nargs) {
4183N/A case 0: args2 = (long[]) coll.invokeExact(); break;
4183N/A case 1: args2 = (long[]) coll.invokeExact(args[0]); break;
4183N/A case 2: args2 = (long[]) coll.invokeExact(args[0], args[1]); break;
4183N/A case 3: args2 = (long[]) coll.invokeExact(args[0], args[1], args[2]); break;
4183N/A case 4: args2 = (long[]) coll.invokeExact(args[0], args[1], args[2], args[3]); break;
4183N/A case 5: args2 = (long[]) coll.invokeExact(args[0], args[1], args[2], args[3], args[4]); break;
4183N/A }
4183N/A assertArrayEquals(args, args2);
4183N/A MethodHandle mh = coll.asSpreader(long[].class, nargs);
4183N/A long[] args3 = (long[]) mh.invokeExact(args);
4183N/A assertArrayEquals(args, args3);
4183N/A }
4183N/A }
4183N/A
4183N/A @Test
4183N/A public void testIntCollects() throws Throwable {
4183N/A if (!startTest("testIntCollects")) return;
4183N/A for (MethodHandle lister : INT_LISTERS) {
4183N/A int outputs = lister.type().parameterCount();
4183N/A for (int collects = 0; collects <= Math.min(outputs, INT_COLLECTORS.length-1); collects++) {
4183N/A int inputs = outputs - 1 + collects;
4183N/A if (inputs < 0) continue;
4183N/A for (int pos = 0; pos + collects <= inputs; pos++) {
4183N/A MethodHandle collector = INT_COLLECTORS[collects];
4183N/A int[] args = new int[inputs];
4183N/A int ap = 0, arg = 31;
4183N/A for (int i = 0; i < pos; i++)
4183N/A args[ap++] = arg++ + 0;
4183N/A for (int i = 0; i < collects; i++)
4183N/A args[ap++] = arg++ + 10;
4183N/A while (ap < args.length)
4183N/A args[ap++] = arg++ + 20;
4183N/A // calculate piecemeal:
4183N/A //System.out.println("testIntCollects "+Arrays.asList(lister, pos, collector)+" on "+Arrays.toString(args));
4183N/A int[] collargs = Arrays.copyOfRange(args, pos, pos+collects);
4183N/A int coll = (int) collector.asSpreader(int[].class, collargs.length).invokeExact(collargs);
4183N/A int[] listargs = Arrays.copyOfRange(args, 0, outputs);
4183N/A System.arraycopy(args, pos+collects, listargs, pos+1, outputs - (pos+1));
4183N/A listargs[pos] = coll;
4183N/A //System.out.println(" coll="+coll+" listargs="+Arrays.toString(listargs));
4183N/A Object expect = lister.asSpreader(int[].class, listargs.length).invokeExact(listargs);
4183N/A //System.out.println(" expect="+expect);
4183N/A
4183N/A // now use the combined MH, and test the output:
5462N/A MethodHandle mh = collectArguments(lister, pos, int[].class, INT_COLLECTORS[collects]);
4183N/A if (mh == null) continue; // no infix collection, yet
4183N/A assert(mh.type().parameterCount() == inputs);
4183N/A Object observe = mh.asSpreader(int[].class, args.length).invokeExact(args);
4183N/A assertEquals(expect, observe);
4183N/A }
4183N/A }
4183N/A }
4183N/A }
4183N/A
5462N/A @Test
5462N/A public void testByteCollects() throws Throwable {
5462N/A if (!startTest("testByteCollects")) return;
5462N/A for (MethodHandle lister : BYTE_LISTERS) {
5462N/A int outputs = lister.type().parameterCount();
5462N/A for (int collects = 0; collects <= Math.min(outputs, BYTE_COLLECTORS.length-1); collects++) {
5462N/A int inputs = outputs - 1 + collects;
5462N/A if (inputs < 0) continue;
5462N/A for (int pos = 0; pos + collects <= inputs; pos++) {
5462N/A MethodHandle collector = BYTE_COLLECTORS[collects];
5462N/A byte[] args = new byte[inputs];
5462N/A int ap = 0, arg = 31;
5462N/A for (int i = 0; i < pos; i++)
5462N/A args[ap++] = (byte)(arg++ + 0);
5462N/A for (int i = 0; i < collects; i++)
5462N/A args[ap++] = (byte)(arg++ + 10);
5462N/A while (ap < args.length)
5462N/A args[ap++] = (byte)(arg++ + 20);
5462N/A // calculate piecemeal:
5462N/A //System.out.println("testIntCollects "+Arrays.asList(lister, pos, collector)+" on "+Arrays.toString(args));
5462N/A byte[] collargs = Arrays.copyOfRange(args, pos, pos+collects);
5462N/A byte coll = (byte) collector.asSpreader(byte[].class, collargs.length).invokeExact(collargs);
5462N/A byte[] listargs = Arrays.copyOfRange(args, 0, outputs);
5462N/A System.arraycopy(args, pos+collects, listargs, pos+1, outputs - (pos+1));
5462N/A listargs[pos] = coll;
5462N/A //System.out.println(" coll="+coll+" listargs="+Arrays.toString(listargs));
5462N/A Object expect = lister.asSpreader(byte[].class, listargs.length).invokeExact(listargs);
5462N/A //System.out.println(" expect="+expect);
5462N/A
5462N/A // now use the combined MH, and test the output:
5462N/A MethodHandle mh = collectArguments(lister, pos, byte[].class, BYTE_COLLECTORS[collects]);
5462N/A if (mh == null) continue; // no infix collection, yet
5462N/A assert(mh.type().parameterCount() == inputs);
5462N/A Object observe = mh.asSpreader(byte[].class, args.length).invokeExact(args);
5462N/A assertEquals(expect, observe);
5462N/A }
5462N/A }
5462N/A }
5462N/A }
5462N/A
5462N/A private static MethodHandle collectArguments(MethodHandle lister, int pos, Class<?> array, MethodHandle collector) {
4183N/A int collects = collector.type().parameterCount();
4183N/A int outputs = lister.type().parameterCount();
4183N/A if (pos == outputs - 1)
4183N/A return MethodHandles.filterArguments(lister, pos,
5462N/A collector.asSpreader(array, collects))
5462N/A .asCollector(array, collects);
4183N/A //return MethodHandles.collectArguments(lister, pos, collector); //no such animal
4183N/A return null;
4183N/A }
4183N/A
4183N/A private static final Class<?>[] RETURN_TYPES = {
4183N/A Object.class, String.class, Integer.class,
4183N/A int.class, long.class,
4183N/A boolean.class, byte.class, char.class, short.class,
4183N/A float.class, double.class,
4183N/A void.class,
4183N/A };
4183N/A
4183N/A @Test
4183N/A public void testReturns() throws Throwable {
4183N/A if (!startTest("testReturns")) return;
4183N/A // fault injection:
4183N/A int faultCount = 0; // total of 1296 tests
4183N/A faultCount = Integer.getInteger("testReturns.faultCount", 0);
4183N/A for (Class<?> ret : RETURN_TYPES) {
4183N/A // make a complicated identity function and pass something through it
4183N/A System.out.println(ret.getSimpleName());
4183N/A Class<?> vret = (ret == void.class) ? Void.class : ret;
4183N/A MethodHandle id = // (vret)->ret
4183N/A identity(vret).asType(methodType(ret, vret));
4183N/A final int LENGTH = 4;
4183N/A int[] index = {0};
4183N/A Object vals = java.lang.reflect.Array.newInstance(vret, LENGTH);
4183N/A MethodHandle indexGetter = //()->int
4183N/A insertArguments(arrayElementGetter(index.getClass()), 0, index, 0);
4183N/A MethodHandle valSelector = // (int)->vret
4183N/A arrayElementGetter(vals.getClass()).bindTo(vals);
4183N/A MethodHandle valGetter = // ()->vret
4183N/A foldArguments(valSelector, indexGetter);
4183N/A if (ret != void.class) {
4183N/A for (int i = 0; i < LENGTH; i++) {
4183N/A Object val = (i + 50);
4183N/A if (ret == boolean.class) val = (i % 3 == 0);
4183N/A if (ret == String.class) val = "#"+i;
4183N/A if (ret == char.class) val = (char)('a'+i);
4183N/A if (ret == byte.class) val = (byte)~i;
4183N/A if (ret == short.class) val = (short)(1<<i);
4183N/A java.lang.reflect.Array.set(vals, i, val);
4183N/A }
4183N/A }
4183N/A for (int i = 0; i < LENGTH; i++) {
4183N/A Object val = java.lang.reflect.Array.get(vals, i);
4183N/A System.out.println(i+" => "+val);
4183N/A index[0] = i;
4183N/A if (--faultCount == 0) index[0] ^= 1;
4183N/A Object x = valGetter.invokeWithArguments();
4183N/A assertEquals(val, x);
4183N/A // make a return-filter call: x = id(valGetter())
4183N/A if (--faultCount == 0) index[0] ^= 1;
4183N/A x = filterReturnValue(valGetter, id).invokeWithArguments();
4183N/A assertEquals(val, x);
4183N/A // make a filter call: x = id(*,valGetter(),*)
4183N/A for (int len = 1; len <= 4; len++) {
4183N/A for (int pos = 0; pos < len; pos++) {
4183N/A MethodHandle proj = id; // lambda(..., vret x,...){x}
4183N/A for (int j = 0; j < len; j++) {
4183N/A if (j == pos) continue;
4183N/A proj = dropArguments(proj, j, Object.class);
4183N/A }
4183N/A assert(proj.type().parameterCount() == len);
4183N/A // proj: (Object*, pos: vret, Object*)->ret
4183N/A assertEquals(vret, proj.type().parameterType(pos));
4183N/A MethodHandle vgFilter = dropArguments(valGetter, 0, Object.class);
4183N/A if (--faultCount == 0) index[0] ^= 1;
4183N/A x = filterArguments(proj, pos, vgFilter).invokeWithArguments(new Object[len]);
4183N/A assertEquals(val, x);
4183N/A }
4183N/A }
4183N/A // make a fold call:
4183N/A for (int len = 0; len <= 4; len++) {
4183N/A for (int fold = 0; fold <= len; fold++) {
4183N/A MethodHandle proj = id; // lambda(ret x, ...){x}
4183N/A if (ret == void.class) proj = constant(Object.class, null);
4183N/A int arg0 = (ret == void.class ? 0 : 1);
4183N/A for (int j = 0; j < len; j++) {
4183N/A proj = dropArguments(proj, arg0, Object.class);
4183N/A }
4183N/A assert(proj.type().parameterCount() == arg0 + len);
4183N/A // proj: (Object*, pos: vret, Object*)->ret
4183N/A if (arg0 != 0) assertEquals(vret, proj.type().parameterType(0));
4183N/A MethodHandle vgFilter = valGetter.asType(methodType(ret));
4183N/A for (int j = 0; j < fold; j++) {
4183N/A vgFilter = dropArguments(vgFilter, j, Object.class);
4183N/A }
4183N/A x = foldArguments(proj, vgFilter).invokeWithArguments(new Object[len]);
4183N/A if (--faultCount == 0) index[0] ^= 1;
4183N/A assertEquals(val, x);
4183N/A }
4183N/A }
4183N/A }
4183N/A }
4183N/A //System.out.println("faultCount="+faultCount);
4183N/A }
4183N/A
5452N/A @Test
5452N/A public void testRecursion() throws Throwable {
5452N/A if (!startTest("testRecursion")) return;
5452N/A final int LIMIT = 10;
5452N/A for (int i = 0; i < LIMIT; i++) {
5452N/A RFCB rfcb = new RFCB(i);
5452N/A Object x = "x", y = "y";
5452N/A Object result = rfcb.recursiveFunction(x, y);
5452N/A verbose(1, result);
5452N/A }
5452N/A }
5452N/A /** Recursive Function Control Block */
5452N/A private static class RFCB {
5452N/A java.util.Random random;
5452N/A final MethodHandle[] fns;
5452N/A int depth;
5455N/A @SuppressWarnings("LeakingThisInConstructor")
5452N/A RFCB(int seed) throws Throwable {
5452N/A this.random = new java.util.Random(seed);
5452N/A this.fns = new MethodHandle[Math.max(29, (1 << MAX_DEPTH-2)/3)];
5452N/A java.util.Arrays.fill(fns, lookup().bind(this, "recursiveFunction", genericMethodType(2)));
5452N/A for (int i = 5; i < fns.length; i++) {
5452N/A switch (i % 4) {
5452N/A case 0: fns[i] = filterArguments(fns[i - 5], 0, insertArguments(fns[i - 4], 1, ".")); break;
5452N/A case 1: fns[i] = filterArguments(fns[i - 5], 1, insertArguments(fns[i - 3], 1, ".")); break;
5452N/A case 2: fns[i] = filterReturnValue(fns[i - 5], insertArguments(fns[i - 2], 1, ".")); break;
5452N/A }
5452N/A }
5452N/A }
5452N/A Object recursiveFunction(Object x, Object y) throws Throwable {
5452N/A depth++;
5452N/A try {
5452N/A final int ACTION_COUNT = 11;
5452N/A switch (random.nextInt(ACTION_COUNT)) {
5452N/A case 1:
5452N/A Throwable ex = new RuntimeException();
5452N/A ex.fillInStackTrace();
5455N/A if (VERBOSITY >= 2) ex.printStackTrace(System.out);
5452N/A x = "ST; " + x;
5452N/A break;
5452N/A case 2:
5452N/A System.gc();
5452N/A x = "GC; " + x;
5452N/A break;
5452N/A }
5452N/A boolean isLeaf = (depth >= MAX_DEPTH);
5452N/A if (isLeaf) {
5452N/A return Arrays.asList(x, y).toString();
5452N/A }
5452N/A return fns[random.nextInt(fns.length)].invokeExact(x, y);
5452N/A } finally {
5452N/A depth--;
5452N/A }
5452N/A }
5452N/A }
5452N/A
4183N/A private static MethodHandle sequence(MethodHandle mh1, MethodHandle... mhs) {
4183N/A MethodHandle res = mh1;
4183N/A for (MethodHandle mh2 : mhs)
4183N/A res = filterReturnValue(res, mh2);
4183N/A return res;
4183N/A }
4183N/A private static void assertEqualFunction(MethodHandle x, MethodHandle y) throws Throwable {
4183N/A assertEquals(x.type(), y.type()); //??
4183N/A MethodType t = x.type();
4183N/A if (t.parameterCount() == 0) {
4183N/A assertEqualFunctionAt(null, x, y);
4183N/A return;
4183N/A }
4183N/A Class<?> ptype = t.parameterType(0);
4183N/A if (ptype == long.class || ptype == Long.class) {
4183N/A for (long i = -10; i <= 10; i++) {
4183N/A assertEqualFunctionAt(i, x, y);
4183N/A }
4183N/A } else {
4183N/A for (int i = -10; i <= 10; i++) {
4183N/A assertEqualFunctionAt(i, x, y);
4183N/A }
4183N/A }
4183N/A }
4183N/A private static void assertEqualFunctionAt(Object v, MethodHandle x, MethodHandle y) throws Throwable {
4183N/A Object[] args = new Object[x.type().parameterCount()];
4183N/A Arrays.fill(args, v);
4183N/A Object xval = invokeWithCatch(x, args);
4183N/A Object yval = invokeWithCatch(y, args);
4183N/A String msg = "ok";
4183N/A if (!Objects.equals(xval, yval)) {
4183N/A msg = ("applying "+x+" & "+y+" to "+v);
4183N/A }
4183N/A assertEquals(msg, xval, yval);
4183N/A }
4183N/A private static Object invokeWithCatch(MethodHandle mh, Object... args) throws Throwable {
4183N/A try {
4183N/A return mh.invokeWithArguments(args);
4183N/A } catch (Throwable ex) {
4183N/A System.out.println("threw: "+mh+Arrays.asList(args));
5455N/A ex.printStackTrace(System.out);
4183N/A return ex;
4183N/A }
4183N/A }
4183N/A
4183N/A private static final Lookup LOOKUP = lookup();
4183N/A private static MethodHandle findStatic(String name,
4183N/A Class<?> rtype,
4183N/A Class<?>... ptypes) {
4183N/A try {
4183N/A return LOOKUP.findStatic(LOOKUP.lookupClass(), name, methodType(rtype, ptypes));
4183N/A } catch (ReflectiveOperationException ex) {
4183N/A throw new RuntimeException(ex);
4183N/A }
4183N/A }
4183N/A private static MethodHandle findStatic(String name,
4183N/A Class<?> rtype,
4183N/A List<?> ptypes) {
4183N/A return findStatic(name, rtype, ptypes.toArray(new Class<?>[ptypes.size()]));
4183N/A }
4183N/A static int getProperty(String name, int dflt) {
4183N/A String qual = LOOKUP.lookupClass().getName();
4183N/A String prop = System.getProperty(qual+"."+name);
4183N/A if (prop == null) prop = System.getProperty(name);
4183N/A if (prop == null) return dflt;
4183N/A return Integer.parseInt(prop);
4183N/A }
4183N/A
4183N/A private static int opI(int... xs) {
4183N/A stress();
4183N/A int base = 100;
4183N/A int z = 0;
4183N/A for (int x : xs) {
4183N/A z = (z * base) + (x % base);
4183N/A }
4183N/A verbose("opI", xs.length, xs, z);
4183N/A return z;
4183N/A }
4183N/A private static int opI2(int x, int y) { return opI(x, y); } // x*100 + y%100
4183N/A private static int opI3(int x, int y, int z) { return opI(x, y, z); }
4183N/A private static int opI4(int w, int x, int y, int z) { return opI(w, x, y, z); }
4183N/A private static int opI(int x) { return opI2(x, 37); }
4183N/A private static Object opI_L(int x) { return (Object) opI(x); }
4183N/A private static long opJ3(long x, long y, long z) { return (long) opI3((int)x, (int)y, (int)z); }
4183N/A private static long opJ2(long x, long y) { return (long) opI2((int)x, (int)y); }
4183N/A private static long opJ(long x) { return (long) opI((int)x); }
4183N/A private static Object opL2(Object x, Object y) { return (Object) opI2((int)x, (int)y); }
4183N/A private static Object opL(Object x) { return (Object) opI((int)x); }
5455N/A private static int opL2_I(Object x, Object y) { return opI2((int)x, (int)y); }
5455N/A private static int opL_I(Object x) { return opI((int)x); }
4183N/A private static long opL_J(Object x) { return (long) opI((int)x); }
4183N/A private static final MethodHandle opI, opI2, opI3, opI4, opI_L, opJ, opJ2, opJ3, opL2, opL, opL2_I, opL_I, opL_J;
4183N/A static {
4183N/A opI4 = findStatic("opI4", int.class, int.class, int.class, int.class, int.class);
4183N/A opI3 = findStatic("opI3", int.class, int.class, int.class, int.class);
4183N/A opI2 = findStatic("opI2", int.class, int.class, int.class);
4183N/A opI = findStatic("opI", int.class, int.class);
4183N/A opI_L = findStatic("opI_L", Object.class, int.class);
4183N/A opJ = findStatic("opJ", long.class, long.class);
4183N/A opJ2 = findStatic("opJ2", long.class, long.class, long.class);
4183N/A opJ3 = findStatic("opJ3", long.class, long.class, long.class, long.class);
4183N/A opL2 = findStatic("opL2", Object.class, Object.class, Object.class);
4183N/A opL = findStatic("opL", Object.class, Object.class);
4183N/A opL2_I = findStatic("opL2_I", int.class, Object.class, Object.class);
4183N/A opL_I = findStatic("opL_I", int.class, Object.class);
4183N/A opL_J = findStatic("opL_J", long.class, Object.class);
4183N/A }
4183N/A private static final MethodHandle[] INT_COLLECTORS = {
4183N/A constant(int.class, 42), opI, opI2, opI3, opI4
4183N/A };
5462N/A private static final MethodHandle[] BYTE_COLLECTORS = {
5462N/A constant(byte.class, (byte)42), i2b(opI), i2b(opI2), i2b(opI3), i2b(opI4)
5462N/A };
4183N/A private static final MethodHandle[] LONG_COLLECTORS = {
4183N/A constant(long.class, 42), opJ, opJ2, opJ3
4183N/A };
4183N/A
4183N/A private static int addI(int x, int y) { stress(); return x+y; }
4183N/A private static Object addL(Object x, Object y) { return addI((int)x, (int)y); }
4183N/A private static final MethodHandle addI, addL;
4183N/A static {
4183N/A addI = findStatic("addI", int.class, int.class, int.class);
4183N/A addL = findStatic("addL", Object.class, Object.class, Object.class);
4183N/A }
4183N/A
4183N/A private static Object list8ints(int a, int b, int c, int d, int e, int f, int g, int h) {
4183N/A return Arrays.asList(a, b, c, d, e, f, g, h);
4183N/A }
4183N/A private static Object list8longs(long a, long b, long c, long d, long e, long f, long g, long h) {
4183N/A return Arrays.asList(a, b, c, d, e, f, g, h);
4183N/A }
4183N/A private static final MethodHandle list8ints = findStatic("list8ints", Object.class,
4183N/A Collections.nCopies(8, int.class));
4183N/A private static final MethodHandle list8longs = findStatic("list8longs", Object.class,
4183N/A Collections.nCopies(8, long.class));
5462N/A private static final MethodHandle[] INT_LISTERS, LONG_LISTERS, BYTE_LISTERS;
4183N/A static {
4183N/A int listerCount = list8ints.type().parameterCount() + 1;
4183N/A INT_LISTERS = new MethodHandle[listerCount];
4183N/A LONG_LISTERS = new MethodHandle[listerCount];
5462N/A BYTE_LISTERS = new MethodHandle[listerCount];
4183N/A MethodHandle lister = list8ints;
4183N/A MethodHandle llister = list8longs;
4183N/A for (int i = listerCount - 1; ; i--) {
4183N/A INT_LISTERS[i] = lister;
4183N/A LONG_LISTERS[i] = llister;
5462N/A BYTE_LISTERS[i] = i2b(lister);
4183N/A if (i == 0) break;
5455N/A lister = insertArguments(lister, i-1, 0);
5455N/A llister = insertArguments(llister, i-1, 0L);
4183N/A }
4183N/A }
5462N/A private static MethodHandle i2b(MethodHandle mh) {
5462N/A return MethodHandles.explicitCastArguments(mh, subst(mh.type(), int.class, byte.class));
5462N/A }
5462N/A private static MethodType subst(MethodType mt, Class<?> from, Class<?> to) {
5462N/A for (int i = 0; i < mt.parameterCount(); i++) {
5462N/A if (mt.parameterType(i) == from)
5462N/A mt = mt.changeParameterType(i, to);
5462N/A }
5462N/A if (mt.returnType() == from)
5462N/A mt = mt.changeReturnType(to);
5462N/A return mt;
5462N/A }
5462N/A
4183N/A
4183N/A private static Object convI_L(int x) { stress(); return (Object) x; }
4183N/A private static int convL_I(Object x) { stress(); return (int) x; }
4183N/A private static Object convJ_L(long x) { stress(); return (Object) x; }
4183N/A private static long convL_J(Object x) { stress(); return (long) x; }
4183N/A private static int convJ_I(long x) { stress(); return (int) x; }
4183N/A private static long convI_J(int x) { stress(); return (long) x; }
4183N/A private static final MethodHandle convI_L, convL_I, convJ_L, convL_J, convJ_I, convI_J;
4183N/A static {
4183N/A convI_L = findStatic("convI_L", Object.class, int.class);
4183N/A convL_I = findStatic("convL_I", int.class, Object.class);
4183N/A convJ_L = findStatic("convJ_L", Object.class, long.class);
4183N/A convL_J = findStatic("convL_J", long.class, Object.class);
4183N/A convJ_I = findStatic("convJ_I", int.class, long.class);
4183N/A convI_J = findStatic("convI_J", long.class, int.class);
4183N/A }
4183N/A
4183N/A // stress modes:
5452N/A private static final int MAX_DEPTH = getProperty("MAX_DEPTH", 5);
4183N/A private static final int REPEAT = getProperty("REPEAT", 0);
4183N/A private static final int STRESS = getProperty("STRESS", 0);
4183N/A private static /*v*/ int STRESS_COUNT;
4183N/A private static final Object[] SINK = new Object[4];
4183N/A private static void stress() {
4183N/A if (STRESS <= 0) return;
4183N/A int count = STRESS + (STRESS_COUNT++ & 0x1); // non-constant value
4183N/A for (int i = 0; i < count; i++) {
4183N/A SINK[i % SINK.length] = new Object[STRESS + i % (SINK.length + 1)];
4183N/A }
4183N/A }
4183N/A
4183N/A // verbosity:
4183N/A private static final int VERBOSITY = getProperty("VERBOSITY", 0) + (REPEAT == 0 ? 0 : -1);
4183N/A private static void verbose(Object a, Object b, Object c, Object d) {
4183N/A if (VERBOSITY <= 0) return;
4183N/A verbose(1, a, b, c, d);
4183N/A }
4183N/A private static void verbose(Object a, Object b, Object c) {
4183N/A if (VERBOSITY <= 0) return;
4183N/A verbose(1, a, b, c);
4183N/A }
4183N/A private static void verbose(int level, Object a, Object... bcd) {
4183N/A if (level > VERBOSITY) return;
4183N/A String m = a.toString();
4183N/A if (bcd != null && bcd.length > 0) {
4183N/A List<Object> l = new ArrayList<>(bcd.length);
4183N/A for (Object x : bcd) {
4183N/A if (x instanceof Object[]) x = Arrays.asList((Object[])x);
4183N/A if (x instanceof int[]) x = Arrays.toString((int[])x);
4183N/A if (x instanceof long[]) x = Arrays.toString((long[])x);
4183N/A l.add(x);
4183N/A }
4183N/A m = m+Arrays.asList(bcd);
4183N/A }
4183N/A System.out.println(m);
4183N/A }
4183N/A String testOnly;
4183N/A String testOnlyTests;
4183N/A private boolean startTest(String name) {
4183N/A if (testOnly != null && !testOnly.contains(name))
4183N/A return false;
4183N/A verbose(0, "["+name+"]");
4183N/A testOnlyTests = (testOnlyTests == null) ? name : testOnlyTests+" "+name;
4183N/A return true;
4183N/A }
4183N/A
4183N/A}