changeset 19786:2ddc2ddd89ac

added ArgSupplier mechanism for supporting tests that modify their arguments
author Doug Simon <doug.simon@oracle.com>
date Tue, 10 Mar 2015 21:39:31 +0100
parents b99cbe1b125c
children 7117697d11e1
files graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/GraalCompilerTest.java
diffstat 1 files changed, 36 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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<Object> {
+    }
+
+    /**
+     * 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.<DeoptimizationReason> emptySet(), receiver, args);
     }
@@ -736,7 +770,7 @@
         if (!method.isAccessible()) {
             method.setAccessible(true);
         }
-        return method.invoke(receiver, args);
+        return method.invoke(receiver, applyArgSuppliers(args));
     }
 
     /**