Add setElementAt tests to CTS.

Change-Id: I2e4226327c6fca0fa0c6ff8e5f6a385839ddc5af
diff --git a/tests/src/android/renderscript/cts/setelementat.rs b/tests/src/android/renderscript/cts/setelementat.rs
new file mode 100644
index 0000000..75e96f5
--- /dev/null
+++ b/tests/src/android/renderscript/cts/setelementat.rs
@@ -0,0 +1,51 @@
+#pragma version(1)
+#pragma rs java_package_name(android.renderscript.cts)
+
+#include "shared.rsh"
+
+int memset_toValue = 0;
+
+int compare_value = 0;
+int compare_failure = 2;
+int failure_value = 0;
+
+uint32_t dimX = 0;
+uint32_t dimY = 0;
+rs_allocation array;
+
+void memset(int *aout) {
+    *aout = memset_toValue;
+    return;
+}
+
+void compare(const int *ain) {
+    if (*ain != compare_value) {
+        rsAtomicCas(&compare_failure, 2, -1);
+        failure_value = *ain;
+    }
+    return;
+}
+
+void getCompareResult(int* aout) {
+    *aout = compare_failure;
+}
+
+void setLargeArray(const int *ain, uint32_t x) {
+    int source = 10;
+    if (x == 0) {
+        for (uint32_t i = 0; i < dimX; i++) {
+            rsSetElementAt(array, &source, i);
+        }
+    }
+}
+
+void setLargeArray2D(const int *ain, uint32_t x) {
+    int source = 10;
+    if (x == 0) {
+        for (uint32_t y = 0; y < dimY; y++) {
+            for (uint32_t xtemp = 0; xtemp < dimX; xtemp++) {
+                rsSetElementAt(array, &source, xtemp, y);
+            }
+        }
+    }
+}
diff --git a/tests/tests/renderscript/src/android/renderscript/cts/AllocationTest.java b/tests/tests/renderscript/src/android/renderscript/cts/AllocationTest.java
index 1c34087..4a1533c 100644
--- a/tests/tests/renderscript/src/android/renderscript/cts/AllocationTest.java
+++ b/tests/tests/renderscript/src/android/renderscript/cts/AllocationTest.java
@@ -28,6 +28,8 @@
 import android.renderscript.Type.Builder;
 import android.renderscript.Type.CubemapFace;
 
+import android.util.Log;
+
 public class AllocationTest extends RSBaseGraphics {
 
     // Test power of two and non power of two, equal and non-equal sizes
@@ -684,6 +686,101 @@
             }
         }
     }
+
+    public void testCopyFromAllocation() {
+        int nElemsX = 256;
+        int nElemsY = 32;
+        Type.Builder b = new Type.Builder(mRS, Element.I8(mRS));
+        Type.Builder b2 = new Type.Builder(mRS, Element.I32(mRS));
+        Allocation srcA = Allocation.createTyped(mRS, b.setX(nElemsX).setY(nElemsY).create());
+        Allocation dstA = Allocation.createTyped(mRS, b.setX(nElemsX).setY(nElemsY).create());
+
+        // wrong dimensionality
+        Allocation srcB_bad = Allocation.createTyped(mRS, b.setX(nElemsX).setY(1).create());
+        Allocation srcC_bad = Allocation.createTyped(mRS, b.setX(nElemsY).setY(nElemsX).create());
+        Allocation srcD_bad = Allocation.createTyped(mRS, b.setX(nElemsX*2).setY(nElemsY*2).create());
+
+        // wrong element type
+        Allocation srcE_bad = Allocation.createTyped(mRS, b2.setX(nElemsX).setY(nElemsY).create());
+
+        try {
+            dstA.copyFrom(srcB_bad);
+            fail("should throw RSIllegalArgumentException");
+        } catch (RSIllegalArgumentException e) {
+        }
+
+        try {
+            dstA.copyFrom(srcC_bad);
+            fail("should throw RSIllegalArgumentException");
+        } catch (RSIllegalArgumentException e) {
+        }
+
+        try {
+            dstA.copyFrom(srcD_bad);
+            fail("should throw RSIllegalArgumentException");
+        } catch (RSIllegalArgumentException e) {
+        }
+
+        try {
+            dstA.copyFrom(srcE_bad);
+            fail("should throw RSIllegalArgumentException");
+        } catch (RSIllegalArgumentException e) {
+        }
+
+        dstA.copyFrom(srcA);
+
+    }
+
+    public void testSetElementAt() {
+        Type.Builder b = new Type.Builder(mRS, Element.I32(mRS));
+        Allocation largeArray = Allocation.createTyped(mRS, b.setX(48).create());
+        Allocation singleElement = Allocation.createTyped(mRS, b.setX(1).create());
+
+        ScriptC_setelementat script = new ScriptC_setelementat(mRS, mRes, R.raw.setelementat);
+
+        script.set_memset_toValue(1);
+        script.forEach_memset(singleElement);
+
+        script.set_dimX(48);
+        script.set_array(largeArray);
+
+        script.forEach_setLargeArray(singleElement);
+
+        int[] result = new int[1];
+
+        script.set_compare_value(10);
+        script.forEach_compare(largeArray);
+        script.forEach_getCompareResult(singleElement);
+        singleElement.copyTo(result);
+        assertTrue(result[0] == 2);
+    }
+
+    public void testSetElementAt2D() {
+        Type.Builder b = new Type.Builder(mRS, Element.I32(mRS));
+
+        Allocation singleElement = Allocation.createTyped(mRS, b.setX(1).create());
+        Allocation largeArray = Allocation.createTyped(mRS, b.setX(48).setY(16).create());
+
+        ScriptC_setelementat script = new ScriptC_setelementat(mRS, mRes, R.raw.setelementat);
+
+        script.set_memset_toValue(1);
+        script.forEach_memset(singleElement);
+
+        script.set_dimX(48);
+        script.set_dimY(16);
+        script.set_array(largeArray);
+
+        script.forEach_setLargeArray2D(singleElement);
+
+        int[] result = new int[1];
+
+        script.set_compare_value(10);
+        script.forEach_compare(largeArray);
+        script.forEach_getCompareResult(singleElement);
+        singleElement.copyTo(result);
+        assertTrue(result[0] == 2);
+    }
+
 }