xref: /aosp_15_r20/cts/tests/tests/renderscript/src/android/renderscript/cts/GetElementAt.java (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.renderscript.cts;
18 
19 import android.renderscript.Allocation;
20 import android.renderscript.Element;
21 import android.renderscript.Type;
22 import android.renderscript.RSRuntimeException;
23 import java.util.Random;
24 
25 public class GetElementAt extends RSBaseCompute {
26     static final int TEST_ID_X = 0;
27     static final int TEST_ID_XY = 1;
28     private Allocation mOut;
29     private Allocation gIn;
30     private int[] in;
31     private int[] out;
32     ScriptC_get_element_at_x script_x;
33     ScriptC_get_element_at_x_y script_xy;
34 
35     @Override
tearDown()36     protected void tearDown() throws Exception {
37         if (mOut != null) {
38             mOut.destroy();
39         }
40         if (gIn != null) {
41             gIn.destroy();
42         }
43         if (script_x != null) {
44             script_x.destroy();
45         }
46         if (script_xy != null) {
47             script_xy.destroy();
48         }
49         super.tearDown();
50     }
51 
52     @Override
forEach(int testId, Allocation mOut)53     public void forEach(int testId, Allocation mOut) throws RSRuntimeException {
54         switch (testId) {
55         case TEST_ID_X:
56             script_x.forEach_root(mOut);
57             break;
58         case TEST_ID_XY:
59             script_xy.forEach_root(mOut);
60             break;
61         }
62     }
63 
setupArrays(int size)64     private void setupArrays(int size) {
65         Random random = new Random(0x12345678);
66         in = new int[size];
67         for (int i = 0; i < size; ++i) {
68             in[i] = random.nextInt(100);
69         }
70         out = new int[size];
71     }
72 
testX()73     public void testX () {
74         setupArrays(INPUTSIZE);
75         script_x = new ScriptC_get_element_at_x(mRS);
76         gIn = Allocation.createSized(mRS, Element.U32(mRS), INPUTSIZE);
77         gIn.copyFrom(in);
78         mOut = Allocation.createTyped(mRS, gIn.getType());
79         script_x.set_gIn(gIn);
80         try {
81             forEach(TEST_ID_X, mOut);
82         } catch (RSRuntimeException e) {
83         }
84         mOut.copyTo(out);
85         for (int k = 0; k < INPUTSIZE; ++k) {
86             assertEquals("idx = " + k, in[k], out[k]);
87         }
88     }
89 
testXY()90     public void testXY () {
91         setupArrays(INPUTSIZE*INPUTSIZE);
92         script_xy = new ScriptC_get_element_at_x_y(mRS);
93         Type.Builder builder = new Type.Builder(mRS, Element.U32(mRS));
94         builder.setX(INPUTSIZE);
95         builder.setY(INPUTSIZE);
96         Type type = builder.create();
97         gIn = Allocation.createTyped(mRS, type);
98         gIn.copyFrom(in);
99         mOut = Allocation.createTyped(mRS, gIn.getType());
100         script_xy.set_gIn(gIn);
101         try {
102             forEach(TEST_ID_XY, mOut);
103         } catch (RSRuntimeException e) {
104         }
105         mOut.copyTo(out);
106         for (int k = 0; k < INPUTSIZE*INPUTSIZE; ++k) {
107             assertEquals("idx = " + k, in[k], out[k]);
108         }
109     }
110 }
111