CTS tests for single-source RS rsForEach

Bug: 23535985

Added CTS tests for the rsForEach API for Single-Source RenderScript.

Adjusted one existing CTS test (get_allocation.rs for GetAllocationTest)
to avoid calling the deprecated rsForEach API with a script argument.

Temporarily added the 0 target API level for all RenderScript tests, since
the new API rsForEach is not officially released yet. This would be
removed once the API is released, and assigned an official API level.

(cherry picked from commit 1b9f8e150fe79c0ef6e8d6c520aafe62dc234dad)

Change-Id: Ife5fd3e30de8c223dc7b5ea4d8c6c1a7a9e852f5
diff --git a/tests/tests/renderscript/src/android/renderscript/cts/SingleSourceForEachTest.java b/tests/tests/renderscript/src/android/renderscript/cts/SingleSourceForEachTest.java
new file mode 100644
index 0000000..e005013
--- /dev/null
+++ b/tests/tests/renderscript/src/android/renderscript/cts/SingleSourceForEachTest.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2015-2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.renderscript.cts;
+
+import android.renderscript.Allocation;
+import android.renderscript.Element;
+import android.renderscript.Script;
+import android.renderscript.Type;
+import android.renderscript.Type.Builder;
+
+public class SingleSourceForEachTest extends RSBaseCompute {
+
+    private static final int X = 1024;
+    private static final int Y = 768;
+
+    private Allocation testInputAlloc;
+    private Allocation testInputAlloc2;
+    private Allocation testOutputAlloc;
+    private Allocation baselineOutputAlloc;
+    private int testInputArray[];
+    private int testInputArray2[];
+    private int testOutputArray[];
+    private int baselineOutputArray[];
+    private ScriptC_single_source_script s;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        Type.Builder i32TypeBuilder = new Type.Builder(mRS, Element.I32(mRS));
+        i32TypeBuilder.setX(X).setY(Y);
+        testInputAlloc = Allocation.createTyped(mRS, i32TypeBuilder.create());
+        testInputAlloc2 = Allocation.createTyped(mRS, i32TypeBuilder.create());
+        testOutputAlloc = Allocation.createTyped(mRS, i32TypeBuilder.create());
+        baselineOutputAlloc = Allocation.createTyped(mRS, i32TypeBuilder.create());
+
+        testInputArray = new int[X * Y];
+        testInputArray2 = new int[X * Y];
+        testOutputArray = new int[X * Y];
+        baselineOutputArray = new int[X * Y];
+
+        s = new ScriptC_single_source_script(mRS);
+
+        RSUtils.genRandomInts(0x900d5eed, testInputArray, true, 32);
+        testInputAlloc.copyFrom(testInputArray);
+
+        for (int i = 0; i < testInputArray2.length; i++) {
+            testInputArray2[i] = i + 1;
+        }
+        testInputAlloc2.copyFrom(testInputArray2);
+    }
+
+    public void testSingleInputKernelLaunch() {
+        s.forEach_foo(testInputAlloc, baselineOutputAlloc);
+        s.invoke_testSingleInput(testInputAlloc, testOutputAlloc);
+        testOutputAlloc.copyTo(testOutputArray);
+        baselineOutputAlloc.copyTo(baselineOutputArray);
+        checkArray(baselineOutputArray, testOutputArray, Y, X, X);
+    }
+
+    public void testMultiInputKernelLaunch() {
+        s.forEach_goo(testInputAlloc, testInputAlloc2,
+                      baselineOutputAlloc);
+        s.invoke_testMultiInput(testInputAlloc, testInputAlloc2,
+                                testOutputAlloc);
+        testOutputAlloc.copyTo(testOutputArray);
+        baselineOutputAlloc.copyTo(baselineOutputArray);
+        checkArray(baselineOutputArray, testOutputArray, Y, X, X);
+    }
+
+    public void testKernelLaunchWithOptions() {
+        Script.LaunchOptions sc = new Script.LaunchOptions();
+        sc.setX(0, X);
+        sc.setY(0, Y / 2);
+        s.forEach_foo(testInputAlloc, baselineOutputAlloc, sc);
+        s.invoke_testLaunchOptions(testInputAlloc, testOutputAlloc, X, Y);
+        testOutputAlloc.copyTo(testOutputArray);
+        baselineOutputAlloc.copyTo(baselineOutputArray);
+        checkArray(baselineOutputArray, testOutputArray, Y, X, X);
+    }
+
+    public void testAllocationlessKernelLaunch() {
+        baselineOutputAlloc.copyFrom(testInputArray);
+        testOutputAlloc.copyFrom(testInputArray);
+
+        Script.LaunchOptions sc = new Script.LaunchOptions();
+        sc.setX(0, X);
+        sc.setY(0, Y);
+        s.set_gAllocOut(baselineOutputAlloc);
+        s.forEach_bar(sc);
+
+        s.invoke_testAllocationlessLaunch(testOutputAlloc, X, Y);
+
+        testOutputAlloc.copyTo(testOutputArray);
+        baselineOutputAlloc.copyTo(baselineOutputArray);
+        checkArray(baselineOutputArray, testOutputArray, Y, X, X);
+    }
+}
diff --git a/tests/tests/renderscript/src/android/renderscript/cts/get_allocation.rs b/tests/tests/renderscript/src/android/renderscript/cts/get_allocation.rs
index 0149e6c..01cb47c 100644
--- a/tests/tests/renderscript/src/android/renderscript/cts/get_allocation.rs
+++ b/tests/tests/renderscript/src/android/renderscript/cts/get_allocation.rs
@@ -6,11 +6,11 @@
 rs_allocation alloc_in;
 rs_allocation alloc_out;
 
-void root(const int* in, int *out) {
-    *out = *in;
+int __attribute__((kernel)) copy(int in) {
+    return in;
 }
 
 void start() {
     alloc_in = rsGetAllocation(pointer);
-    rsForEach(script, alloc_in, alloc_out);
+    rsForEach(copy, alloc_in, alloc_out);
 }
diff --git a/tests/tests/renderscript/src/android/renderscript/cts/single_source_script.rs b/tests/tests/renderscript/src/android/renderscript/cts/single_source_script.rs
new file mode 100644
index 0000000..5e35aa3
--- /dev/null
+++ b/tests/tests/renderscript/src/android/renderscript/cts/single_source_script.rs
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2015-2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "shared.rsh"
+
+rs_allocation gAllocOut;
+
+int RS_KERNEL foo(int a) {
+    return a * 2;
+}
+
+int RS_KERNEL goo(int a, int b) {
+    return a + b;
+}
+
+void RS_KERNEL bar(int x, int y) {
+  int a = rsGetElementAt_int(gAllocOut, x, y);
+  a++;
+  rsSetElementAt_int(gAllocOut, a, x, y);
+}
+
+void testSingleInput(rs_allocation in, rs_allocation out) {
+    rsForEach(foo, in, out);
+}
+
+void testMultiInput(rs_allocation in1, rs_allocation in2, rs_allocation out) {
+    rsForEach(goo, in1, in2, out);
+}
+
+void testLaunchOptions(rs_allocation in, rs_allocation out, int dimX, int dimY) {
+    rs_script_call_t opts = {};
+    opts.xStart = 0;
+    opts.xEnd = dimX;
+    opts.yStart = 0;
+    opts.yEnd = dimY / 2;
+    rsForEachWithOptions(foo, &opts, in, out);
+}
+
+void testAllocationlessLaunch(rs_allocation inAndOut, int dimX, int dimY) {
+    gAllocOut = inAndOut;
+    rs_script_call_t opts = {};
+    opts.xStart = 0;
+    opts.xEnd = dimX;
+    opts.yStart = 0;
+    opts.yEnd = dimY;
+    rsForEachWithOptions(bar, &opts);
+}