changeset 10:663ab03c3b69

meth: better unit tests
author jrose
date Tue, 26 May 2009 18:14:49 -0700
parents 99f16faedd40
children 16e8d2a2f373
files netbeans/meth/TEST.sh netbeans/meth/nbproject/project.properties netbeans/meth/test/jdk/java/dyn/MethodHandlesTest.java netbeans/meth/test/jdk/java/dyn/util/WrapperTest.java netbeans/meth/test/sun/dyn/util/WrapperTest.java
diffstat 5 files changed, 160 insertions(+), 101 deletions(-) [+]
line wrap: on
line diff
--- a/netbeans/meth/TEST.sh	Tue May 26 18:14:33 2009 -0700
+++ b/netbeans/meth/TEST.sh	Tue May 26 18:14:49 2009 -0700
@@ -15,3 +15,6 @@
 set -x
 $java7 -XX:+EnableMethodHandles -Xbootclasspath/p:"$cpath" jdk.java.dyn.MethodHandleDemo
 $java7 -XX:+EnableMethodHandles -Xbootclasspath/p:"$cpath" org.junit.runner.JUnitCore jdk.java.dyn.MethodHandlesTest
+
+# From ant:
+# ant -emacs -q -D{javac,test}.includes='**/MethodHandlesTest.java' test-single
--- a/netbeans/meth/nbproject/project.properties	Tue May 26 18:14:33 2009 -0700
+++ b/netbeans/meth/nbproject/project.properties	Tue May 26 18:14:49 2009 -0700
@@ -75,7 +75,7 @@
 # Space-separated list of JVM arguments used when running the project
 # (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value
 # or test-sys-prop.name=value to set system properties for unit tests):
-run.jvmargs=${config.run.jvmargs} -Xbootclasspath/p:"${run.classpath}:${libs.junit_4.classpath}"
+run.jvmargs=-ea -esa ${config.run.jvmargs} -Xbootclasspath/p:"${run.classpath}:${libs.junit_4.classpath}"
 #manual hack: override ${config.run.jvmargs} in ${config}.properties
 config.run.jvmargs=-XX:+EnableMethodHandles
 run.test.classpath=\
--- a/netbeans/meth/test/jdk/java/dyn/MethodHandlesTest.java	Tue May 26 18:14:33 2009 -0700
+++ b/netbeans/meth/test/jdk/java/dyn/MethodHandlesTest.java	Tue May 26 18:14:49 2009 -0700
@@ -704,16 +704,72 @@
         fail("The test case is a prototype.");
     }
 
-    @Test @Ignore("unimplemented")
+    private static final String MISSING_ARG = "missingArg";
+    static Object targetIfEquals() {
+        return called("targetIfEquals");
+    }
+    static Object fallbackIfNotEquals() {
+        return called("fallbackIfNotEquals");
+    }
+    static Object targetIfEquals(Object x) {
+        assertEquals(x, MISSING_ARG);
+        return called("targetIfEquals", x);
+    }
+    static Object fallbackIfNotEquals(Object x) {
+        assertFalse(x.toString(), x.equals(MISSING_ARG));
+        return called("fallbackIfNotEquals", x);
+    }
+    static Object targetIfEquals(Object x, Object y) {
+        assertEquals(x, y);
+        return called("targetIfEquals", x, y);
+    }
+    static Object fallbackIfNotEquals(Object x, Object y) {
+        assertFalse(x.toString(), x.equals(y));
+        return called("fallbackIfNotEquals", x, y);
+    }
+    static Object targetIfEquals(Object x, Object y, Object z) {
+        assertEquals(x, y);
+        return called("targetIfEquals", x, y, z);
+    }
+    static Object fallbackIfNotEquals(Object x, Object y, Object z) {
+        assertFalse(x.toString(), x.equals(y));
+        return called("fallbackIfNotEquals", x, y, z);
+    }
+
+    @Test
     public void testGuardWithTest() {
+        if (CAN_SKIP_WORKING)  return;
         System.out.println("guardWithTest");
-        MethodHandle test = null;
-        MethodHandle target = null;
-        MethodHandle fallback = null;
-        MethodHandle expResult = null;
-        MethodHandle result = MethodHandles.guardWithTest(test, target, fallback);
-        assertEquals(expResult, result);
-        fail("The test case is a prototype.");
+        for (int nargs = 0; nargs <= 3; nargs++) {
+            if (nargs != 2)  continue;  // FIXME: test more later
+            MethodHandle test = PRIVATE.findVirtual(Object.class, "equals", MethodType.make(boolean.class, Object.class));
+            MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "targetIfEquals", MethodType.makeGeneric(nargs));
+            MethodHandle fallback = PRIVATE.findStatic(MethodHandlesTest.class, "fallbackIfNotEquals", MethodType.makeGeneric(nargs));
+            while (test.type().parameterCount() < nargs)
+                test = MethodHandles.dropArguments(test, test.type().parameterCount()-1, Object.class);
+            while (test.type().parameterCount() > nargs)
+                test = MethodHandles.insertArgument(test, 0, MISSING_ARG);
+            MethodHandle mh = MethodHandles.guardWithTest(test, target, fallback);
+            Object[][] argLists = {
+                { },
+                { "foo" }, { MISSING_ARG },
+                { "foo", "foo" }, { "foo", "bar" },
+                { "foo", "foo", "baz" }, { "foo", "bar", "baz" }
+            };
+            for (Object[] argList : argLists) {
+                if (argList.length != nargs)  continue;
+                boolean equals;
+                switch (nargs) {
+                case 0:   equals = true; break;
+                case 1:   equals = MISSING_ARG.equals(argList[0]); break;
+                default:  equals = argList[0].equals(argList[1]); break;
+                }
+                String willCall = (equals ? "targetIfEquals" : "fallbackIfNotEquals");
+                System.out.println(logEntry(willCall, argList));
+                Object result = MethodHandles.invoke(mh, argList);
+                assertCalled(willCall, argList);
+            }
+        }
     }
 
     @Test @Ignore("unimplemented")
--- a/netbeans/meth/test/jdk/java/dyn/util/WrapperTest.java	Tue May 26 18:14:33 2009 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,92 +0,0 @@
-/*
- * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- * 
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Sun designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Sun in the LICENSE file that accompanied this code.
- * 
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- * 
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- * 
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
- */
-
-package jdk.java.dyn.util;
-
-import org.junit.Test;
-import sun.dyn.util.Wrapper;
-import static sun.dyn.util.Wrapper.*;
-
-/**
- *
- * @author jrose
- */
-public class WrapperTest {
-    static void assertLocal(boolean z) {
-        if (!z)  throw new InternalError();
-    }
-
-    @Test public void test() { test(true); }
-
-    static boolean test(boolean verbose) {
-        Class<Integer> PTYPE = int.class, WTYPE = Integer.class;
-        assertLocal(PTYPE != WTYPE);
-        assertLocal(forPrimitiveType(PTYPE) == INT);
-        assertLocal(forWrapperType(WTYPE) == INT);
-        assertLocal(asPrimitiveType(PTYPE) == PTYPE);
-        assertLocal(asPrimitiveType(WTYPE) == PTYPE);
-        assertLocal(  asWrapperType(PTYPE) == WTYPE);
-        assertLocal(  asWrapperType(WTYPE) == WTYPE);
-
-        assertLocal(forWrapperType(Object.class) == OBJECT);
-        assertLocal(forWrapperType(Iterable.class) == OBJECT);
-        assertLocal(forWrapperType(Void.class) == VOID);
-        assertLocal(forWrapperType(Integer.class) == INT);
-        assertLocal(forPrimitiveType(void.class) == VOID);
-        assertLocal(forPrimitiveType(int.class).bitWidth() == 32);
-        assertLocal(forPrimitiveType(char.class) == CHAR);
-        assertLocal(forPrimitiveType(char.class).bitWidth() == 16);
-        assertLocal(forPrimitiveType(short.class).bitWidth() == 16);
-        assertLocal(forPrimitiveType(byte.class).bitWidth() == 8);
-        assertLocal(forPrimitiveType(boolean.class).bitWidth() == 1);
-        assertLocal(forPrimitiveType(double.class).bitWidth() == 64);
-        assertLocal(forPrimitiveType(float.class).bitWidth() == 32);
-
-        Wrapper[] wrappers = { BYTE, SHORT, INT, LONG, BOOLEAN, CHAR, FLOAT, DOUBLE, VOID, OBJECT };
-        assertLocal(values().length == wrappers.length);
-        String chars = "BSIJ"          + "ZCFD"          + "VL";
-        String bitws = "\10\20\40\100" + "\01\20\40\100" + "\0\0";
-        String kinds = "\1\1\1\1"      + "\2\2\4\4"      + "\0\0";
-        String slots = "\1\1\1\2"      + "\1\1\1\2"      + "\0\1";
-        for (int i = 0; i < wrappers.length; i++) {
-            Wrapper w = wrappers[i];
-            if (verbose)  System.out.println("testing "+w);
-            assertLocal(forPrimitiveType(w.primitiveType()) == w);
-            assertLocal(forWrapperType(w.wrapperType()) == w);
-            assertLocal(w.basicTypeChar() == chars.charAt(i));
-            assertLocal(w.bitWidth() == bitws.charAt(i));
-            int wkind = (w.isSigned() ? 1 : 0) | (w.isUnsigned() ? 2 : 0) | (w.isFloating() ? 4 : 0);
-            assertLocal(wkind == kinds.charAt(i));
-            assertLocal(w.stackSlots() == slots.charAt(i));
-            assertLocal(w.isNumeric()  == ((wkind & 7) != 0));
-            assertLocal(w.isIntegral() == ((wkind & 3) != 0));
-            assertLocal(w.isSubwordOrInt() == (w.isIntegral() && w.bitWidth() <= 32));
-        }
-        return true;
-    }
-    public static void main(String... av) {
-        test(true);
-    }
-}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/netbeans/meth/test/sun/dyn/util/WrapperTest.java	Tue May 26 18:14:49 2009 -0700
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package sun.dyn.util;
+
+import org.junit.Test;
+import sun.dyn.util.Wrapper;
+import static sun.dyn.util.Wrapper.*;
+
+/**
+ *
+ * @author jrose
+ */
+public class WrapperTest {
+    static void assertLocal(boolean z) {
+        if (!z)  throw new InternalError();
+    }
+
+    @Test public void test() { test(true); }
+
+    static boolean test(boolean verbose) {
+        Class<Integer> PTYPE = int.class, WTYPE = Integer.class;
+        assertLocal(PTYPE != WTYPE);
+        assertLocal(forPrimitiveType(PTYPE) == INT);
+        assertLocal(forWrapperType(WTYPE) == INT);
+        assertLocal(asPrimitiveType(PTYPE) == PTYPE);
+        assertLocal(asPrimitiveType(WTYPE) == PTYPE);
+        assertLocal(  asWrapperType(PTYPE) == WTYPE);
+        assertLocal(  asWrapperType(WTYPE) == WTYPE);
+
+        assertLocal(forWrapperType(Object.class) == OBJECT);
+        assertLocal(forWrapperType(Iterable.class) == OBJECT);
+        assertLocal(forWrapperType(Void.class) == VOID);
+        assertLocal(forWrapperType(Integer.class) == INT);
+        assertLocal(forPrimitiveType(void.class) == VOID);
+        assertLocal(forPrimitiveType(int.class).bitWidth() == 32);
+        assertLocal(forPrimitiveType(char.class) == CHAR);
+        assertLocal(forPrimitiveType(char.class).bitWidth() == 16);
+        assertLocal(forPrimitiveType(short.class).bitWidth() == 16);
+        assertLocal(forPrimitiveType(byte.class).bitWidth() == 8);
+        assertLocal(forPrimitiveType(boolean.class).bitWidth() == 1);
+        assertLocal(forPrimitiveType(double.class).bitWidth() == 64);
+        assertLocal(forPrimitiveType(float.class).bitWidth() == 32);
+
+        Wrapper[] wrappers = { BYTE, SHORT, INT, LONG, BOOLEAN, CHAR, FLOAT, DOUBLE, VOID, OBJECT };
+        assertLocal(values().length == wrappers.length);
+        String chars = "BSIJ"          + "ZCFD"          + "VL";
+        String bitws = "\10\20\40\100" + "\01\20\40\100" + "\0\0";
+        String kinds = "\1\1\1\1"      + "\2\2\4\4"      + "\0\0";
+        String slots = "\1\1\1\2"      + "\1\1\1\2"      + "\0\1";
+        for (int i = 0; i < wrappers.length; i++) {
+            Wrapper w = wrappers[i];
+            if (verbose)  System.out.println("testing "+w);
+            assertLocal(forPrimitiveType(w.primitiveType()) == w);
+            assertLocal(forWrapperType(w.wrapperType()) == w);
+            assertLocal(w.basicTypeChar() == chars.charAt(i));
+            assertLocal(w.bitWidth() == bitws.charAt(i));
+            int wkind = (w.isSigned() ? 1 : 0) | (w.isUnsigned() ? 2 : 0) | (w.isFloating() ? 4 : 0);
+            assertLocal(wkind == kinds.charAt(i));
+            assertLocal(w.stackSlots() == slots.charAt(i));
+            assertLocal(w.isNumeric()  == ((wkind & 7) != 0));
+            assertLocal(w.isIntegral() == ((wkind & 3) != 0));
+            assertLocal(w.isSubwordOrInt() == (w.isIntegral() && w.bitWidth() <= 32));
+        }
+        return true;
+    }
+    public static void main(String... av) {
+        test(true);
+    }
+}
\ No newline at end of file