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 36 public void forEach(int testId, Allocation mOut) throws RSRuntimeException { 37 switch (testId) { 38 case TEST_ID_X: 39 script_x.forEach_root(mOut); 40 break; 41 case TEST_ID_XY: 42 script_xy.forEach_root(mOut); 43 break; 44 } 45 } 46 47 private void setupArrays(int size) { 48 Random random = new Random(0x12345678); 49 in = new int[size]; 50 for (int i = 0; i < size; ++i) { 51 in[i] = random.nextInt(100); 52 } 53 out = new int[size]; 54 } 55 56 public void testX () { 57 setupArrays(INPUTSIZE); 58 script_x = new ScriptC_get_element_at_x(mRS); 59 gIn = Allocation.createSized(mRS, Element.U32(mRS), INPUTSIZE); 60 gIn.copyFrom(in); 61 mOut = Allocation.createTyped(mRS, gIn.getType()); 62 script_x.set_gIn(gIn); 63 try { 64 forEach(TEST_ID_X, mOut); 65 } catch (RSRuntimeException e) { 66 } 67 mOut.copyTo(out); 68 for (int k = 0; k < INPUTSIZE; ++k) { 69 assertEquals("idx = " + k, in[k], out[k]); 70 } 71 } 72 73 public void testXY () { 74 setupArrays(INPUTSIZE*INPUTSIZE); 75 script_xy = new ScriptC_get_element_at_x_y(mRS); 76 Type.Builder builder = new Type.Builder(mRS, Element.U32(mRS)); 77 builder.setX(INPUTSIZE); 78 builder.setY(INPUTSIZE); 79 Type type = builder.create(); 80 gIn = Allocation.createTyped(mRS, type); 81 gIn.copyFrom(in); 82 mOut = Allocation.createTyped(mRS, gIn.getType()); 83 script_xy.set_gIn(gIn); 84 try { 85 forEach(TEST_ID_XY, mOut); 86 } catch (RSRuntimeException e) { 87 } 88 mOut.copyTo(out); 89 for (int k = 0; k < INPUTSIZE*INPUTSIZE; ++k) { 90 assertEquals("idx = " + k, in[k], out[k]); 91 } 92 } 93 } 94