diff --git a/com.oracle.truffle.r.test.tck/src/com/oracle/truffle/r/test/tck/RTCKLanguageProvider.java b/com.oracle.truffle.r.test.tck/src/com/oracle/truffle/r/test/tck/RTCKLanguageProvider.java index 82d35b82752d20ce4c7a8912276db15eb2eb4f6c..f7827b40e3aa4de0234afd5faef883612d3edc40 100644 --- a/com.oracle.truffle.r.test.tck/src/com/oracle/truffle/r/test/tck/RTCKLanguageProvider.java +++ b/com.oracle.truffle.r.test.tck/src/com/oracle/truffle/r/test/tck/RTCKLanguageProvider.java @@ -32,10 +32,13 @@ import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.function.BiFunction; +import java.util.function.Predicate; import org.graalvm.polyglot.Context; import org.graalvm.polyglot.PolyglotException; import org.graalvm.polyglot.Source; +import org.graalvm.polyglot.SourceSection; import org.graalvm.polyglot.Value; +import org.graalvm.polyglot.tck.InlineSnippet; import org.graalvm.polyglot.tck.Snippet; import org.graalvm.polyglot.tck.TypeDescriptor; import org.junit.Assert; @@ -262,6 +265,30 @@ public final class RTCKLanguageProvider implements LanguageProvider { } } + @Override + public Collection<? extends InlineSnippet> createInlineScripts(Context context) { + List<InlineSnippet> res = new ArrayList<>(); + res.add(createInlineSnippet( + context, + "resources/mandel.R", + 5, + 6, + "resources/mandel_inline1.R")); + res.add(createInlineSnippet( + context, + "resources/mandel.R", + 18, + 19, + "resources/mandel_inline2.R")); + res.add(createInlineSnippet( + context, + "resources/quicksort.R", + 5, + 21, + "resources/quicksort_inline.R")); + return Collections.unmodifiableList(res); + } + private static Snippet createValueConstructor( Context context, String value, @@ -320,6 +347,33 @@ public final class RTCKLanguageProvider implements LanguageProvider { return opb.build(); } + private InlineSnippet createInlineSnippet(Context context, String sourceName, int l1, int l2, String snippetName) { + Snippet script = loadScript(context, sourceName, TypeDescriptor.ANY, null); + String simpleName = sourceName.substring(sourceName.lastIndexOf('/') + 1); + try { + InlineSnippet.Builder snippetBuilder = InlineSnippet.newBuilder(script, createSource(snippetName).getCharacters()); + if (l1 > 0) { + Predicate<SourceSection> locationPredicate = (SourceSection ss) -> { + return ss.getSource().getName().endsWith(simpleName) && l1 <= ss.getStartLine() && ss.getEndLine() <= l2; + }; + snippetBuilder.locationPredicate(locationPredicate); + } + snippetBuilder.resultVerifier((ResultVerifier.SnippetRun snippetRun) -> { + PolyglotException exception = snippetRun.getException(); + if (exception != null) { + throw exception; + } + Value result = snippetRun.getResult(); + if (!result.isNumber()) { + throw new AssertionError("Wrong value " + result.toString() + " from " + sourceName); + } + }); + return snippetBuilder.build(); + } catch (IOException ioe) { + throw new AssertionError("IOException while creating a test script.", ioe); + } + } + private static Snippet loadScript( Context context, String resourceName, diff --git a/com.oracle.truffle.r.test.tck/src/com/oracle/truffle/r/test/tck/resources/mandel_inline1.R b/com.oracle.truffle.r.test.tck/src/com/oracle/truffle/r/test/tck/resources/mandel_inline1.R new file mode 100644 index 0000000000000000000000000000000000000000..94a211db06a5b36faded5255ae6877f69e8d56f1 --- /dev/null +++ b/com.oracle.truffle.r.test.tck/src/com/oracle/truffle/r/test/tck/resources/mandel_inline1.R @@ -0,0 +1 @@ +(Mod(z) + Mod(c)) / n diff --git a/com.oracle.truffle.r.test.tck/src/com/oracle/truffle/r/test/tck/resources/mandel_inline2.R b/com.oracle.truffle.r.test.tck/src/com/oracle/truffle/r/test/tck/resources/mandel_inline2.R new file mode 100644 index 0000000000000000000000000000000000000000..2ee08576dfbb0bf21756bed432db2902b39ef612 --- /dev/null +++ b/com.oracle.truffle.r.test.tck/src/com/oracle/truffle/r/test/tck/resources/mandel_inline2.R @@ -0,0 +1 @@ +(sum(M) - r + i) / count diff --git a/com.oracle.truffle.r.test.tck/src/com/oracle/truffle/r/test/tck/resources/quicksort_inline.R b/com.oracle.truffle.r.test.tck/src/com/oracle/truffle/r/test/tck/resources/quicksort_inline.R new file mode 100644 index 0000000000000000000000000000000000000000..dbd5ac49e290f355fa9b4db123ce135a72e279b3 --- /dev/null +++ b/com.oracle.truffle.r.test.tck/src/com/oracle/truffle/r/test/tck/resources/quicksort_inline.R @@ -0,0 +1 @@ +a[i]*lo + a[ifelse(j >= 1, j, 1)]*hi