From 64016ba29cb39576b068e0007c824cd7fc219b35 Mon Sep 17 00:00:00 2001 From: Tomas Stupka <tomas.stupka@oracle.com> Date: Tue, 23 Jan 2018 20:11:37 +0100 Subject: [PATCH] more interop tests --- .../truffle/r/test/ExpectedTestOutput.test | 44 +++++++++ .../r/test/library/fastr/TestJavaInterop.java | 98 +++++++++++++++++++ 2 files changed, 142 insertions(+) diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test index 34a016ec0b..0b0edf26d2 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test @@ -143537,6 +143537,46 @@ character(0) Error in as.logical(test) : no method for coercing this external object to a vector +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testException# +#if (!any(R.version$engine == "FastR")) { cat('Error in new.external(Class, ...) :', '<<<NEWLINE>>>', ' Foreign function failed: java.io.IOException', '<<<NEWLINE>>>') } else { to <- new('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestExceptionsClass', 'java.io.IOException'); } +Error in new.external(Class, ...) : + Foreign function failed: java.io.IOException + +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testException# +#if (!any(R.version$engine == "FastR")) { cat('Error in new.external(Class, ...) :', '<<<NEWLINE>>>', ' Foreign function failed: java.io.IOException: msg', '<<<NEWLINE>>>') } else { to <- new('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestExceptionsClass', 'java.io.IOException', 'msg'); } +Error in new.external(Class, ...) : + Foreign function failed: java.io.IOException: msg + +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testException# +#if (!any(R.version$engine == "FastR")) { cat('Error in new.external(Class, ...) :', '<<<NEWLINE>>>', ' Foreign function failed: java.lang.RuntimeException', '<<<NEWLINE>>>') } else { to <- new('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestExceptionsClass', 'java.lang.RuntimeException'); } +Error in new.external(Class, ...) : + Foreign function failed: java.lang.RuntimeException + +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testException# +#if (!any(R.version$engine == "FastR")) { cat('Error in new.external(Class, ...) :', '<<<NEWLINE>>>', ' Foreign function failed: java.lang.RuntimeException: msg', '<<<NEWLINE>>>') } else { to <- new('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestExceptionsClass', 'java.lang.RuntimeException', 'msg'); } +Error in new.external(Class, ...) : + Foreign function failed: java.lang.RuntimeException: msg + +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testException# +#if (!any(R.version$engine == "FastR")) { cat('Error in to$exception("java.io.IOException") :', '<<<NEWLINE>>>', ' Foreign function failed: java.io.IOException', '<<<NEWLINE>>>') } else { to <- new('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestExceptionsClass');to$exception('java.io.IOException') } +Error in to$exception("java.io.IOException") : + Foreign function failed: java.io.IOException + +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testException# +#if (!any(R.version$engine == "FastR")) { cat('Error in to$exception("java.io.IOException", "msg") :', '<<<NEWLINE>>>', ' Foreign function failed: java.io.IOException: msg', '<<<NEWLINE>>>') } else { to <- new('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestExceptionsClass');to$exception('java.io.IOException', 'msg') } +Error in to$exception("java.io.IOException", "msg") : + Foreign function failed: java.io.IOException: msg + +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testException# +#if (!any(R.version$engine == "FastR")) { cat('Error in to$exception("java.lang.RuntimeException") :', '<<<NEWLINE>>>', ' Foreign function failed: java.lang.RuntimeException', '<<<NEWLINE>>>') } else { to <- new('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestExceptionsClass');to$exception('java.lang.RuntimeException') } +Error in to$exception("java.lang.RuntimeException") : + Foreign function failed: java.lang.RuntimeException + +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testException# +#if (!any(R.version$engine == "FastR")) { cat('Error in to$exception("java.lang.RuntimeException", "msg") :', '<<<NEWLINE>>>', ' Foreign function failed: java.lang.RuntimeException: msg', '<<<NEWLINE>>>') } else { to <- new('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestExceptionsClass');to$exception('java.lang.RuntimeException', 'msg') } +Error in to$exception("java.lang.RuntimeException", "msg") : + Foreign function failed: java.lang.RuntimeException: msg + ##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testFields# #if (!any(R.version$engine == "FastR")) { "a string" } else { to <- new.external(new.java.class('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestClass')); to$fieldStaticStringObject } [1] "a string" @@ -147008,6 +147048,10 @@ NULL #if (!any(R.version$engine == "FastR")) { NULL } else { to <- new.external(new.java.class('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestClass')); to$methodStaticReturnsNull() } NULL +##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testMethods# +#if (!any(R.version$engine == "FastR")) { NULL } else { to <- new.external(new.java.class('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestClass')); to$methodVoid() } +NULL + ##com.oracle.truffle.r.test.library.fastr.TestJavaInterop.testMethods# #if (!any(R.version$engine == "FastR")) { TRUE } else { to <- new.external(new.java.class('com.oracle.truffle.r.test.library.fastr.TestJavaInterop$TestClass')); to$methodBoolean() } [1] TRUE diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/fastr/TestJavaInterop.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/fastr/TestJavaInterop.java index 7d030e7bf3..953b97e87c 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/fastr/TestJavaInterop.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/fastr/TestJavaInterop.java @@ -39,6 +39,8 @@ import com.oracle.truffle.r.nodes.builtin.base.printer.DoubleVectorPrinter; import com.oracle.truffle.r.nodes.builtin.fastr.FastRInterop; import com.oracle.truffle.r.runtime.RType; import com.oracle.truffle.r.test.TestBase; +import com.oracle.truffle.r.test.library.fastr.TestJavaInterop.TestClass.TestPOJO; +import java.lang.reflect.Constructor; public class TestJavaInterop extends TestBase { @@ -1382,6 +1384,28 @@ public class TestJavaInterop extends TestBase { assertEvalFastR(CREATE_TRUFFLE_OBJECT + "range(to)", errorIn("range(to)", "invalid 'type' (external object) of argument")); } + private static final String CREATE_EXCEPTIONS_TO = "to <- new('" + TestExceptionsClass.class.getName() + "');"; + + @Test + public void testException() { + assertEvalFastR("to <- new('" + TestExceptionsClass.class.getName() + "', 'java.io.IOException');", + errorIn("new.external(Class, ...)", "Foreign function failed: java.io.IOException")); + assertEvalFastR("to <- new('" + TestExceptionsClass.class.getName() + "', 'java.io.IOException', 'msg');", + errorIn("new.external(Class, ...)", "Foreign function failed: java.io.IOException: msg")); + assertEvalFastR("to <- new('" + TestExceptionsClass.class.getName() + "', 'java.lang.RuntimeException');", + errorIn("new.external(Class, ...)", "Foreign function failed: java.lang.RuntimeException")); + assertEvalFastR("to <- new('" + TestExceptionsClass.class.getName() + "', 'java.lang.RuntimeException', 'msg');", + errorIn("new.external(Class, ...)", "Foreign function failed: java.lang.RuntimeException: msg")); + + assertEvalFastR(CREATE_EXCEPTIONS_TO + "to$exception('java.io.IOException')", errorIn("to$exception(\"java.io.IOException\")", "Foreign function failed: java.io.IOException")); + assertEvalFastR(CREATE_EXCEPTIONS_TO + "to$exception('java.io.IOException', 'msg')", + errorIn("to$exception(\"java.io.IOException\", \"msg\")", "Foreign function failed: java.io.IOException: msg")); + assertEvalFastR(CREATE_EXCEPTIONS_TO + "to$exception('java.lang.RuntimeException')", + errorIn("to$exception(\"java.lang.RuntimeException\")", "Foreign function failed: java.lang.RuntimeException")); + assertEvalFastR(CREATE_EXCEPTIONS_TO + "to$exception('java.lang.RuntimeException', 'msg')", + errorIn("to$exception(\"java.lang.RuntimeException\", \"msg\")", "Foreign function failed: java.lang.RuntimeException: msg")); + } + private String getRValue(Object value) { if (value == null) { return "NULL"; @@ -1420,6 +1444,11 @@ public class TestJavaInterop extends TestBase { sb.append("\\n')"); return sb.toString(); } + if (value instanceof TestPOJO) { + StringBuilder sb = new StringBuilder(); + sb.append("[external object]\n$data\n[1] \"").append(((TestPOJO) value).data).append("\"\n\n$toString\n[external object]\n\n$getData\n[external object]\n\n"); + return String.format("cat('%s')", sb.toString()); + } return value.toString(); } @@ -1829,6 +1858,7 @@ public class TestJavaInterop extends TestBase { public static Object fieldStaticNullObject = null; public Object fieldNullObject = null; + public TestPOJO pojo = new TestPOJO("POJO for field"); public List<Boolean> listBoolean = new ArrayList<>(Arrays.asList(true, false, true)); public List<Byte> listByte = new ArrayList<>(Arrays.asList((byte) 1, (byte) 2, (byte) 3)); @@ -1854,6 +1884,23 @@ public class TestJavaInterop extends TestBase { public List<Element> listObject = new ArrayList<>(Arrays.asList(new Element("a"), new Element("b"), new Element("c"), null)); public Element[] arrayObject = new Element[]{new Element("a"), new Element("b"), new Element("c"), null}; + public static class TestPOJO { + public final String data; + + public TestPOJO(String data) { + this.data = data; + } + + public String getData() { + return data; + } + + @Override + public String toString() { + return String.format("TetsPOJO[data='%s']", data); + } + } + public TestClass() { this(true, Byte.MAX_VALUE, 'a', Double.MAX_VALUE, 1.1f, Integer.MAX_VALUE, Long.MAX_VALUE, Short.MAX_VALUE, "a string"); } @@ -2005,6 +2052,10 @@ public class TestJavaInterop extends TestBase { return fieldStringObject; } + public TestPOJO returnsPOJO() { + return new TestPOJO("POJO for method"); + } + public String[] methodStringArray() { return fieldStringArray; } @@ -2089,6 +2140,9 @@ public class TestJavaInterop extends TestBase { Assert.assertNull(o); } + public void methodVoid() { + } + public String classAsArg(Class<?> c) { return c.getName(); } @@ -2445,4 +2499,48 @@ public class TestJavaInterop extends TestBase { throw new UnsupportedOperationException("Should not reach here."); } } + + public static class TestExceptionsClass { + + public TestExceptionsClass() { + + } + + public TestExceptionsClass(String className) throws Throwable { + if (className != null) { + throwEx(className); + } + } + + public TestExceptionsClass(String className, String msg) throws Throwable { + if (className != null) { + throwEx(className, msg); + } + } + + public static void exception(String className) throws Throwable { + throwEx(className); + } + + public static void exception(String className, String msg) throws Throwable { + throwEx(className, msg); + } + + private static void throwEx(String className) throws Throwable { + throwEx(className, null); + } + + private static void throwEx(String className, String msg) throws Throwable { + Class<?> clazz = Class.forName(className); + Object t; + if (msg == null) { + t = clazz.newInstance(); + } else { + Constructor<?> ctor = clazz.getDeclaredConstructor(String.class); + t = ctor.newInstance(msg); + } + assert t instanceof Throwable : "throwable instance expected: " + className; + throw (Throwable) t; + } + } } -- GitLab