From 460f837f9a855278aa48116e14e66289adf1daa0 Mon Sep 17 00:00:00 2001
From: Florian Angerer <florian.angerer@oracle.com>
Date: Fri, 18 Aug 2017 12:04:52 +0200
Subject: [PATCH] Fixing RNG tests.

---
 .../r/nodes/builtin/base/RNGFunctions.java        |  8 +-------
 .../com/oracle/truffle/r/runtime/rng/RRNG.java    | 15 ++++++++++++---
 .../com/oracle/truffle/r/test/rng/TestRRNG.java   |  4 ++--
 3 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/RNGFunctions.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/RNGFunctions.java
index 1078617dec..0d8a7a3ed9 100644
--- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/RNGFunctions.java
+++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/RNGFunctions.java
@@ -109,13 +109,6 @@ public class RNGFunctions {
             Casts.noCasts(FastRSetSeed.class);
         }
 
-        @Specialization
-        @TruffleBoundary
-        protected RNull setSeed(int[] data) {
-            RContext.getInstance().stateRNG.currentSeeds = data;
-            return RNull.instance;
-        }
-
         @Specialization
         @TruffleBoundary
         protected RNull setSeed(RAbstractIntVector data) {
@@ -146,6 +139,7 @@ public class RNGFunctions {
                 int[] seedsArr = (int[]) seeds;
                 return RDataFactory.createIntVector(seedsArr, RDataFactory.INCOMPLETE_VECTOR);
             }
+            assert seeds != null;
             return seeds;
         }
     }
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/rng/RRNG.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/rng/RRNG.java
index e4388adb82..954b440821 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/rng/RRNG.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/rng/RRNG.java
@@ -104,7 +104,13 @@ public class RRNG {
         private RandomNumberGenerator currentGenerator;
         private final RandomNumberGenerator[] allGenerators;
         private NormKind currentNormKind;
-        public Object currentSeeds;
+
+        /**
+         * Stores the current RNG seed. The type is Object because the user may assign an arbitrary
+         * value to variable {@value RRNG#RANDOM_SEED}. Allowed types are therefore any R value or
+         * an {@code int[]}.
+         */
+        public Object currentSeeds = RMissing.instance;
 
         private ContextStateImpl() {
             this.currentNormKind = DEFAULT_NORM_KIND;
@@ -325,13 +331,16 @@ public class RRNG {
             int[] seedsArr = (int[]) seeds;
             tmp = seedsArr.length == 0 ? RRuntime.INT_NA : seedsArr[0];
         } else {
-            assert seeds != RMissing.instance;
             assert seeds instanceof RTypedValue;
-            RError.warning(RError.NO_CALLER, RError.Message.SEED_TYPE, ((RTypedValue) seeds).getRType().getName());
+            // allow RMissing to indicate that ".Random.seed" has not been initialized yet
+            if (seeds != RMissing.instance) {
+                RError.warning(RError.NO_CALLER, RError.Message.SEED_TYPE, ((RTypedValue) seeds).getRType().getName());
+            }
             handleInvalidSeed();
             return;
         }
         if (tmp == RRuntime.INT_NA || tmp < 0 || tmp > 1000) {
+            assert seeds != RMissing.instance;
             String type = seeds instanceof RTypedValue ? ((RTypedValue) seeds).getRType().getName() : "unknown";
             RError.warning(RError.NO_CALLER, RError.Message.SEED_TYPE, type);
             handleInvalidSeed();
diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/rng/TestRRNG.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/rng/TestRRNG.java
index b578868551..0a0b86d08c 100644
--- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/rng/TestRRNG.java
+++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/rng/TestRRNG.java
@@ -32,9 +32,9 @@ public class TestRRNG extends TestBase {
         // changes generator to MarsagliaMulticarry and sets its 2 seeds
         assertEval(".Random.seed <- c(401L, 1L, 2L); runif(3)");
         // wrong values: not integer
-        assertEval(Output.IgnoreWarningContext, ".Random.seed <- c(401, 1, 2); invisible(runif(3))");
+        assertEval(Output.IgnoreWarningContext, Output.MayIgnoreWarningContext, ".Random.seed <- c(401, 1, 2); invisible(runif(3))");
         // wrong values: wrong generator number
-        assertEval(Output.IgnoreWarningContext, ".Random.seed <- c(999, 1, 2); invisible(runif(3))");
+        assertEval(Output.IgnoreWarningContext, Output.MayIgnoreWarningContext, ".Random.seed <- c(999, 1, 2); invisible(runif(3))");
     }
 
     @Test
-- 
GitLab