From 757ba3878c48db46174dc8fe3b09543ddb750ce9 Mon Sep 17 00:00:00 2001
From: Martin Entlicher <martin.entlicher@oracle.com>
Date: Wed, 21 Mar 2018 16:05:21 +0100
Subject: [PATCH] [GR-7855] TCK test of inline parse implemented.

---
 .../r/test/tck/RTCKLanguageProvider.java      | 54 +++++++++++++++++++
 .../r/test/tck/resources/mandel_inline1.R     |  1 +
 .../r/test/tck/resources/mandel_inline2.R     |  1 +
 .../r/test/tck/resources/quicksort_inline.R   |  1 +
 4 files changed, 57 insertions(+)
 create mode 100644 com.oracle.truffle.r.test.tck/src/com/oracle/truffle/r/test/tck/resources/mandel_inline1.R
 create mode 100644 com.oracle.truffle.r.test.tck/src/com/oracle/truffle/r/test/tck/resources/mandel_inline2.R
 create mode 100644 com.oracle.truffle.r.test.tck/src/com/oracle/truffle/r/test/tck/resources/quicksort_inline.R

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 82d35b8275..f7827b40e3 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 0000000000..94a211db06
--- /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 0000000000..2ee08576df
--- /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 0000000000..dbd5ac49e2
--- /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
-- 
GitLab