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