1 /*
2  * Copyright (C) 2016 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 // Don't edit this file!  It is auto-generated by frameworks/rs/api/generate.sh.
18 
19 package android.renderscript.cts;
20 
21 import android.renderscript.Allocation;
22 import android.renderscript.RSRuntimeException;
23 import android.renderscript.Element;
24 import android.renderscript.cts.Target;
25 
26 import java.util.Arrays;
27 
28 public class TestRootn extends RSBaseCompute {
29 
30     private ScriptC_TestRootn script;
31     private ScriptC_TestRootnRelaxed scriptRelaxed;
32 
33     @Override
setUp()34     protected void setUp() throws Exception {
35         super.setUp();
36         script = new ScriptC_TestRootn(mRS);
37         scriptRelaxed = new ScriptC_TestRootnRelaxed(mRS);
38     }
39 
40     public class ArgumentsFloatIntFloat {
41         public float inV;
42         public int inN;
43         public Target.Floaty out;
44     }
45 
checkRootnFloatIntFloat()46     private void checkRootnFloatIntFloat() {
47         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0x4daae0ccl, false);
48         Allocation inN = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 1, 0x4daae0c4l, false);
49         try {
50             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
51             script.set_gAllocInN(inN);
52             script.forEach_testRootnFloatIntFloat(inV, out);
53             verifyResultsRootnFloatIntFloat(inV, inN, out, false);
54         } catch (Exception e) {
55             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testRootnFloatIntFloat: " + e.toString());
56         }
57         try {
58             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
59             scriptRelaxed.set_gAllocInN(inN);
60             scriptRelaxed.forEach_testRootnFloatIntFloat(inV, out);
61             verifyResultsRootnFloatIntFloat(inV, inN, out, true);
62         } catch (Exception e) {
63             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testRootnFloatIntFloat: " + e.toString());
64         }
65     }
66 
verifyResultsRootnFloatIntFloat(Allocation inV, Allocation inN, Allocation out, boolean relaxed)67     private void verifyResultsRootnFloatIntFloat(Allocation inV, Allocation inN, Allocation out, boolean relaxed) {
68         float[] arrayInV = new float[INPUTSIZE * 1];
69         Arrays.fill(arrayInV, (float) 42);
70         inV.copyTo(arrayInV);
71         int[] arrayInN = new int[INPUTSIZE * 1];
72         Arrays.fill(arrayInN, (int) 42);
73         inN.copyTo(arrayInN);
74         float[] arrayOut = new float[INPUTSIZE * 1];
75         Arrays.fill(arrayOut, (float) 42);
76         out.copyTo(arrayOut);
77         StringBuilder message = new StringBuilder();
78         boolean errorFound = false;
79         for (int i = 0; i < INPUTSIZE; i++) {
80             for (int j = 0; j < 1 ; j++) {
81                 // Extract the inputs.
82                 ArgumentsFloatIntFloat args = new ArgumentsFloatIntFloat();
83                 args.inV = arrayInV[i];
84                 args.inN = arrayInN[i];
85                 // Figure out what the outputs should have been.
86                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
87                 CoreMathVerifier.computeRootn(args, target);
88                 // Validate the outputs.
89                 boolean valid = true;
90                 if (!args.out.couldBe(arrayOut[i * 1 + j])) {
91                     valid = false;
92                 }
93                 if (!valid) {
94                     if (!errorFound) {
95                         errorFound = true;
96                         message.append("Input inV: ");
97                         appendVariableToMessage(message, args.inV);
98                         message.append("\n");
99                         message.append("Input inN: ");
100                         appendVariableToMessage(message, args.inN);
101                         message.append("\n");
102                         message.append("Expected output out: ");
103                         appendVariableToMessage(message, args.out);
104                         message.append("\n");
105                         message.append("Actual   output out: ");
106                         appendVariableToMessage(message, arrayOut[i * 1 + j]);
107                         if (!args.out.couldBe(arrayOut[i * 1 + j])) {
108                             message.append(" FAIL");
109                         }
110                         message.append("\n");
111                         message.append("Errors at");
112                     }
113                     message.append(" [");
114                     message.append(Integer.toString(i));
115                     message.append(", ");
116                     message.append(Integer.toString(j));
117                     message.append("]");
118                 }
119             }
120         }
121         assertFalse("Incorrect output for checkRootnFloatIntFloat" +
122                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
123     }
124 
checkRootnFloat2Int2Float2()125     private void checkRootnFloat2Int2Float2() {
126         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 2, 0xcb32d88el, false);
127         Allocation inN = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 2, 0xcb32d886l, false);
128         try {
129             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 2), INPUTSIZE);
130             script.set_gAllocInN(inN);
131             script.forEach_testRootnFloat2Int2Float2(inV, out);
132             verifyResultsRootnFloat2Int2Float2(inV, inN, out, false);
133         } catch (Exception e) {
134             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testRootnFloat2Int2Float2: " + e.toString());
135         }
136         try {
137             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 2), INPUTSIZE);
138             scriptRelaxed.set_gAllocInN(inN);
139             scriptRelaxed.forEach_testRootnFloat2Int2Float2(inV, out);
140             verifyResultsRootnFloat2Int2Float2(inV, inN, out, true);
141         } catch (Exception e) {
142             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testRootnFloat2Int2Float2: " + e.toString());
143         }
144     }
145 
verifyResultsRootnFloat2Int2Float2(Allocation inV, Allocation inN, Allocation out, boolean relaxed)146     private void verifyResultsRootnFloat2Int2Float2(Allocation inV, Allocation inN, Allocation out, boolean relaxed) {
147         float[] arrayInV = new float[INPUTSIZE * 2];
148         Arrays.fill(arrayInV, (float) 42);
149         inV.copyTo(arrayInV);
150         int[] arrayInN = new int[INPUTSIZE * 2];
151         Arrays.fill(arrayInN, (int) 42);
152         inN.copyTo(arrayInN);
153         float[] arrayOut = new float[INPUTSIZE * 2];
154         Arrays.fill(arrayOut, (float) 42);
155         out.copyTo(arrayOut);
156         StringBuilder message = new StringBuilder();
157         boolean errorFound = false;
158         for (int i = 0; i < INPUTSIZE; i++) {
159             for (int j = 0; j < 2 ; j++) {
160                 // Extract the inputs.
161                 ArgumentsFloatIntFloat args = new ArgumentsFloatIntFloat();
162                 args.inV = arrayInV[i * 2 + j];
163                 args.inN = arrayInN[i * 2 + j];
164                 // Figure out what the outputs should have been.
165                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
166                 CoreMathVerifier.computeRootn(args, target);
167                 // Validate the outputs.
168                 boolean valid = true;
169                 if (!args.out.couldBe(arrayOut[i * 2 + j])) {
170                     valid = false;
171                 }
172                 if (!valid) {
173                     if (!errorFound) {
174                         errorFound = true;
175                         message.append("Input inV: ");
176                         appendVariableToMessage(message, args.inV);
177                         message.append("\n");
178                         message.append("Input inN: ");
179                         appendVariableToMessage(message, args.inN);
180                         message.append("\n");
181                         message.append("Expected output out: ");
182                         appendVariableToMessage(message, args.out);
183                         message.append("\n");
184                         message.append("Actual   output out: ");
185                         appendVariableToMessage(message, arrayOut[i * 2 + j]);
186                         if (!args.out.couldBe(arrayOut[i * 2 + j])) {
187                             message.append(" FAIL");
188                         }
189                         message.append("\n");
190                         message.append("Errors at");
191                     }
192                     message.append(" [");
193                     message.append(Integer.toString(i));
194                     message.append(", ");
195                     message.append(Integer.toString(j));
196                     message.append("]");
197                 }
198             }
199         }
200         assertFalse("Incorrect output for checkRootnFloat2Int2Float2" +
201                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
202     }
203 
checkRootnFloat3Int3Float3()204     private void checkRootnFloat3Int3Float3() {
205         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 3, 0xc0e54547l, false);
206         Allocation inN = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 3, 0xc0e5453fl, false);
207         try {
208             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 3), INPUTSIZE);
209             script.set_gAllocInN(inN);
210             script.forEach_testRootnFloat3Int3Float3(inV, out);
211             verifyResultsRootnFloat3Int3Float3(inV, inN, out, false);
212         } catch (Exception e) {
213             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testRootnFloat3Int3Float3: " + e.toString());
214         }
215         try {
216             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 3), INPUTSIZE);
217             scriptRelaxed.set_gAllocInN(inN);
218             scriptRelaxed.forEach_testRootnFloat3Int3Float3(inV, out);
219             verifyResultsRootnFloat3Int3Float3(inV, inN, out, true);
220         } catch (Exception e) {
221             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testRootnFloat3Int3Float3: " + e.toString());
222         }
223     }
224 
verifyResultsRootnFloat3Int3Float3(Allocation inV, Allocation inN, Allocation out, boolean relaxed)225     private void verifyResultsRootnFloat3Int3Float3(Allocation inV, Allocation inN, Allocation out, boolean relaxed) {
226         float[] arrayInV = new float[INPUTSIZE * 4];
227         Arrays.fill(arrayInV, (float) 42);
228         inV.copyTo(arrayInV);
229         int[] arrayInN = new int[INPUTSIZE * 4];
230         Arrays.fill(arrayInN, (int) 42);
231         inN.copyTo(arrayInN);
232         float[] arrayOut = new float[INPUTSIZE * 4];
233         Arrays.fill(arrayOut, (float) 42);
234         out.copyTo(arrayOut);
235         StringBuilder message = new StringBuilder();
236         boolean errorFound = false;
237         for (int i = 0; i < INPUTSIZE; i++) {
238             for (int j = 0; j < 3 ; j++) {
239                 // Extract the inputs.
240                 ArgumentsFloatIntFloat args = new ArgumentsFloatIntFloat();
241                 args.inV = arrayInV[i * 4 + j];
242                 args.inN = arrayInN[i * 4 + j];
243                 // Figure out what the outputs should have been.
244                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
245                 CoreMathVerifier.computeRootn(args, target);
246                 // Validate the outputs.
247                 boolean valid = true;
248                 if (!args.out.couldBe(arrayOut[i * 4 + j])) {
249                     valid = false;
250                 }
251                 if (!valid) {
252                     if (!errorFound) {
253                         errorFound = true;
254                         message.append("Input inV: ");
255                         appendVariableToMessage(message, args.inV);
256                         message.append("\n");
257                         message.append("Input inN: ");
258                         appendVariableToMessage(message, args.inN);
259                         message.append("\n");
260                         message.append("Expected output out: ");
261                         appendVariableToMessage(message, args.out);
262                         message.append("\n");
263                         message.append("Actual   output out: ");
264                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
265                         if (!args.out.couldBe(arrayOut[i * 4 + j])) {
266                             message.append(" FAIL");
267                         }
268                         message.append("\n");
269                         message.append("Errors at");
270                     }
271                     message.append(" [");
272                     message.append(Integer.toString(i));
273                     message.append(", ");
274                     message.append(Integer.toString(j));
275                     message.append("]");
276                 }
277             }
278         }
279         assertFalse("Incorrect output for checkRootnFloat3Int3Float3" +
280                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
281     }
282 
checkRootnFloat4Int4Float4()283     private void checkRootnFloat4Int4Float4() {
284         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 4, 0xb697b200l, false);
285         Allocation inN = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 4, 0xb697b1f8l, false);
286         try {
287             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 4), INPUTSIZE);
288             script.set_gAllocInN(inN);
289             script.forEach_testRootnFloat4Int4Float4(inV, out);
290             verifyResultsRootnFloat4Int4Float4(inV, inN, out, false);
291         } catch (Exception e) {
292             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testRootnFloat4Int4Float4: " + e.toString());
293         }
294         try {
295             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 4), INPUTSIZE);
296             scriptRelaxed.set_gAllocInN(inN);
297             scriptRelaxed.forEach_testRootnFloat4Int4Float4(inV, out);
298             verifyResultsRootnFloat4Int4Float4(inV, inN, out, true);
299         } catch (Exception e) {
300             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testRootnFloat4Int4Float4: " + e.toString());
301         }
302     }
303 
verifyResultsRootnFloat4Int4Float4(Allocation inV, Allocation inN, Allocation out, boolean relaxed)304     private void verifyResultsRootnFloat4Int4Float4(Allocation inV, Allocation inN, Allocation out, boolean relaxed) {
305         float[] arrayInV = new float[INPUTSIZE * 4];
306         Arrays.fill(arrayInV, (float) 42);
307         inV.copyTo(arrayInV);
308         int[] arrayInN = new int[INPUTSIZE * 4];
309         Arrays.fill(arrayInN, (int) 42);
310         inN.copyTo(arrayInN);
311         float[] arrayOut = new float[INPUTSIZE * 4];
312         Arrays.fill(arrayOut, (float) 42);
313         out.copyTo(arrayOut);
314         StringBuilder message = new StringBuilder();
315         boolean errorFound = false;
316         for (int i = 0; i < INPUTSIZE; i++) {
317             for (int j = 0; j < 4 ; j++) {
318                 // Extract the inputs.
319                 ArgumentsFloatIntFloat args = new ArgumentsFloatIntFloat();
320                 args.inV = arrayInV[i * 4 + j];
321                 args.inN = arrayInN[i * 4 + j];
322                 // Figure out what the outputs should have been.
323                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
324                 CoreMathVerifier.computeRootn(args, target);
325                 // Validate the outputs.
326                 boolean valid = true;
327                 if (!args.out.couldBe(arrayOut[i * 4 + j])) {
328                     valid = false;
329                 }
330                 if (!valid) {
331                     if (!errorFound) {
332                         errorFound = true;
333                         message.append("Input inV: ");
334                         appendVariableToMessage(message, args.inV);
335                         message.append("\n");
336                         message.append("Input inN: ");
337                         appendVariableToMessage(message, args.inN);
338                         message.append("\n");
339                         message.append("Expected output out: ");
340                         appendVariableToMessage(message, args.out);
341                         message.append("\n");
342                         message.append("Actual   output out: ");
343                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
344                         if (!args.out.couldBe(arrayOut[i * 4 + j])) {
345                             message.append(" FAIL");
346                         }
347                         message.append("\n");
348                         message.append("Errors at");
349                     }
350                     message.append(" [");
351                     message.append(Integer.toString(i));
352                     message.append(", ");
353                     message.append(Integer.toString(j));
354                     message.append("]");
355                 }
356             }
357         }
358         assertFalse("Incorrect output for checkRootnFloat4Int4Float4" +
359                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
360     }
361 
testRootn()362     public void testRootn() {
363         checkRootnFloatIntFloat();
364         checkRootnFloat2Int2Float2();
365         checkRootnFloat3Int3Float3();
366         checkRootnFloat4Int4Float4();
367     }
368 }
369