# HG changeset patch # User Doug Simon # Date 1426019971 -3600 # Node ID 2ddc2ddd89aca862fc61bfaf9d868d1b18d2d19c # Parent b99cbe1b125c4035230581c0867091d466834c2e added ArgSupplier mechanism for supporting tests that modify their arguments diff -r b99cbe1b125c -r 2ddc2ddd89ac graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/GraalCompilerTest.java --- a/graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/GraalCompilerTest.java Tue Mar 10 21:29:12 2015 +0100 +++ b/graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/GraalCompilerTest.java Tue Mar 10 21:39:31 2015 +0100 @@ -30,6 +30,7 @@ import java.lang.reflect.*; import java.util.*; import java.util.concurrent.atomic.*; +import java.util.function.*; import org.junit.*; import org.junit.internal.*; @@ -528,7 +529,7 @@ executeArgs[i + 1] = args[i]; } } - return executeArgs; + return applyArgSuppliers(executeArgs); } protected void test(String name, Object... args) { @@ -542,6 +543,22 @@ } } + /** + * Type denoting a lambda that supplies a fresh value each time it is called. This is useful + * when supplying an argument to {@link GraalCompilerTest#test(String, Object...)} where the + * test modifies the state of the argument (e.g., updates a field). + */ + @FunctionalInterface + public interface ArgSupplier extends Supplier { + } + + /** + * Convenience method for using an {@link ArgSupplier} lambda in a varargs list. + */ + public static Object supply(ArgSupplier supplier) { + return supplier; + } + protected void test(ResolvedJavaMethod method, Object receiver, Object... args) { Result expect = executeExpected(method, receiver, args); if (getCodeCache() == null) { @@ -550,6 +567,23 @@ testAgainstExpected(method, expect, receiver, args); } + /** + * Process a given set of arguments, converting any {@link ArgSupplier} argument to the argument + * it supplies. + */ + protected Object[] applyArgSuppliers(Object... args) { + Object[] res = args; + for (int i = 0; i < args.length; i++) { + if (args[i] instanceof ArgSupplier) { + if (res == args) { + res = args.clone(); + } + res[i] = ((ArgSupplier) args[i]).get(); + } + } + return res; + } + protected void testAgainstExpected(ResolvedJavaMethod method, Result expect, Object receiver, Object... args) { testAgainstExpected(method, expect, Collections. emptySet(), receiver, args); } @@ -736,7 +770,7 @@ if (!method.isAccessible()) { method.setAccessible(true); } - return method.invoke(receiver, args); + return method.invoke(receiver, applyArgSuppliers(args)); } /**