changeset 13:083732b6300a

meth: dropArguments working, varargs handling improved
author jrose
date Thu, 28 May 2009 02:51:05 -0700
parents d6e69f64cd3e
children 1f748158f7ed
files netbeans/meth/test/jdk/java/dyn/MethodHandlesTest.java
diffstat 1 files changed, 48 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/netbeans/meth/test/jdk/java/dyn/MethodHandlesTest.java	Wed May 27 13:00:00 2009 -0700
+++ b/netbeans/meth/test/jdk/java/dyn/MethodHandlesTest.java	Thu May 28 02:51:05 2009 -0700
@@ -34,6 +34,7 @@
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -48,11 +49,21 @@
  */
 public class MethodHandlesTest {
 
+    @Test
+    public void testFirst() {
+        // left blank for debugging
+    }
+
     // current failures
     @Test @Ignore("failure in call to makeRawRetypeOnly in ToGeneric")
     public void testFail_1() {
         testSpreadArguments(int.class, 6);
     }
+    @Test @Ignore("failure in JVM when expanding the stack")
+    public void testFail_2() {
+        // if CONV_OP_IMPLEMENTED_MASK includes OP_SPREAD_ARGS, this crashes:
+        testSpreadArguments(Object.class, 2);
+    }
 
     public MethodHandlesTest() {
     }
@@ -649,22 +660,21 @@
         System.out.println("spreadArguments");
         for (Class<?> argType : new Class[]{Object.class, Integer.class, int.class}) {
             System.out.println("spreadArguments "+argType);
-            for (int i = 0; i < 10; i++) {
-                if (argType == int.class && i >= 6)  continue; // FIXME Fail_1
-                testSpreadArguments(argType, i);
+            for (int nargs = 0; nargs < 10; nargs++) {
+                if (argType == int.class && nargs >= 6)  continue; // FIXME Fail_1
+                testSpreadArguments(argType, nargs);
             }
         }
     }
-    public void testSpreadArguments(Class<?> argType, int i) {
-        Object[] args = new Object[i];
-        for (int j = 0; j < args.length; j++)
-            args[j] = randomArg(Integer.class);
-        MethodHandle target = ValueConversions.varargsArray(i);
+    public void testSpreadArguments(Class<?> argType, int nargs) {
+        MethodHandle target = ValueConversions.varargsArray(nargs);
+        MethodHandle target2 = changeArgTypes(target, argType);
+        Object[] args = randomArgs(target2.type().parameterArray());
         // make sure the target does what we think it does:
-        if (i < 5) {
+        if (nargs < 5) {
             Object[] check = (Object[]) MethodHandles.invoke(target, args);
             assertArrayEquals(args, check);
-            switch (i) {
+            switch (nargs) {
                 case 0:
                     check = target.<Object[]>invoke();
                     assertArrayEquals(args, check);
@@ -679,10 +689,9 @@
                     break;
             }
         }
-        MethodHandle target2 = changeArgTypes(target, argType);
         MethodType newType = MethodType.make(Object.class, Object[].class);
         MethodHandle result = MethodHandles.spreadArguments(target2, newType);
-        Object[] returnValue = (Object[]) result.invoke((Object)args);
+        Object[] returnValue = (Object[]) result.invoke(args);
         assertArrayEquals(args, returnValue);
     }
 
@@ -739,16 +748,35 @@
         fail("The test case is a prototype.");
     }
 
-    @Test @Ignore("unimplemented")
+    @Test
     public void testDropArguments() {
         System.out.println("dropArguments");
-        MethodHandle target = null;
-        int pos = 0;
-        Class<?>[] valueTypes = null;
-        MethodHandle expResult = null;
-        MethodHandle result = MethodHandles.dropArguments(target, pos, valueTypes);
-        assertEquals(expResult, result);
-        fail("The test case is a prototype.");
+        for (int nargs = 0; nargs <= 4; nargs++) {
+            for (int drop = 1; drop <= 4; drop++) {
+                for (int pos = 0; pos <= nargs; pos++) {
+                    testDropArguments(nargs, pos, drop);
+                }
+            }
+        }
+    }
+
+    void testDropArguments(int nargs, int pos, int drop) {
+        MethodHandle target = ValueConversions.varargsArray(nargs);
+        Object[] args = randomArgs(target.type().parameterArray());
+        Object res = MethodHandles.invoke(target, args);
+        assertArrayEquals(args, (Object[])res);
+        MethodHandle target2 = MethodHandles.dropArguments(target, pos,
+                Collections.nCopies(drop, Object.class).toArray(new Class[0]));
+        List<Object> resList = Arrays.asList(args);
+        List<Object> argsToDrop = new ArrayList<Object>(resList);
+        for (int i = drop; i > 0; i--) {
+            argsToDrop.add(pos, "blort#"+i);
+        }
+        Object res2 = MethodHandles.invoke(target2, argsToDrop.toArray());
+        Object res2List = Arrays.asList((Object[])res2);
+//        if (!resList.equals(res2List))
+//            System.out.println("*** fail at n/p/d = "+nargs+"/"+pos+"/"+drop+": "+argsToDrop+" => "+res2List);
+        assertEquals(resList, res2List);
     }
 
     private static final String MISSING_ARG = "missingArg";