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 TestAbs extends RSBaseCompute {
29 
30     private ScriptC_TestAbs script;
31     private ScriptC_TestAbsRelaxed scriptRelaxed;
32 
33     @Override
setUp()34     protected void setUp() throws Exception {
35         super.setUp();
36         script = new ScriptC_TestAbs(mRS);
37         scriptRelaxed = new ScriptC_TestAbsRelaxed(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 ArgumentsCharUchar {
48         public byte inV;
49         public byte out;
50     }
51 
checkAbsCharUchar()52     private void checkAbsCharUchar() {
53         Allocation inV = createRandomAllocation(mRS, Element.DataType.SIGNED_8, 1, 0x79257810f7393ea6l, false);
54         try {
55             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_8, 1), INPUTSIZE);
56             script.forEach_testAbsCharUchar(inV, out);
57             verifyResultsAbsCharUchar(inV, out, false);
58             out.destroy();
59         } catch (Exception e) {
60             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsCharUchar: " + e.toString());
61         }
62         try {
63             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_8, 1), INPUTSIZE);
64             scriptRelaxed.forEach_testAbsCharUchar(inV, out);
65             verifyResultsAbsCharUchar(inV, out, true);
66             out.destroy();
67         } catch (Exception e) {
68             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsCharUchar: " + e.toString());
69         }
70         inV.destroy();
71     }
72 
verifyResultsAbsCharUchar(Allocation inV, Allocation out, boolean relaxed)73     private void verifyResultsAbsCharUchar(Allocation inV, Allocation out, boolean relaxed) {
74         byte[] arrayInV = new byte[INPUTSIZE * 1];
75         Arrays.fill(arrayInV, (byte) 42);
76         inV.copyTo(arrayInV);
77         byte[] arrayOut = new byte[INPUTSIZE * 1];
78         Arrays.fill(arrayOut, (byte) 42);
79         out.copyTo(arrayOut);
80         StringBuilder message = new StringBuilder();
81         boolean errorFound = false;
82         for (int i = 0; i < INPUTSIZE; i++) {
83             for (int j = 0; j < 1 ; j++) {
84                 // Extract the inputs.
85                 ArgumentsCharUchar args = new ArgumentsCharUchar();
86                 args.inV = arrayInV[i];
87                 // Figure out what the outputs should have been.
88                 CoreMathVerifier.computeAbs(args);
89                 // Validate the outputs.
90                 boolean valid = true;
91                 if (args.out != arrayOut[i * 1 + j]) {
92                     valid = false;
93                 }
94                 if (!valid) {
95                     if (!errorFound) {
96                         errorFound = true;
97                         message.append("Input inV: ");
98                         appendVariableToMessage(message, args.inV);
99                         message.append("\n");
100                         message.append("Expected output out: ");
101                         appendVariableToMessage(message, args.out);
102                         message.append("\n");
103                         message.append("Actual   output out: ");
104                         appendVariableToMessage(message, arrayOut[i * 1 + j]);
105                         if (args.out != arrayOut[i * 1 + j]) {
106                             message.append(" FAIL");
107                         }
108                         message.append("\n");
109                         message.append("Errors at");
110                     }
111                     message.append(" [");
112                     message.append(Integer.toString(i));
113                     message.append(", ");
114                     message.append(Integer.toString(j));
115                     message.append("]");
116                 }
117             }
118         }
119         assertFalse("Incorrect output for checkAbsCharUchar" +
120                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
121     }
122 
checkAbsChar2Uchar2()123     private void checkAbsChar2Uchar2() {
124         Allocation inV = createRandomAllocation(mRS, Element.DataType.SIGNED_8, 2, 0xff611dd40e5e407cl, false);
125         try {
126             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_8, 2), INPUTSIZE);
127             script.forEach_testAbsChar2Uchar2(inV, out);
128             verifyResultsAbsChar2Uchar2(inV, out, false);
129             out.destroy();
130         } catch (Exception e) {
131             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsChar2Uchar2: " + e.toString());
132         }
133         try {
134             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_8, 2), INPUTSIZE);
135             scriptRelaxed.forEach_testAbsChar2Uchar2(inV, out);
136             verifyResultsAbsChar2Uchar2(inV, out, true);
137             out.destroy();
138         } catch (Exception e) {
139             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsChar2Uchar2: " + e.toString());
140         }
141         inV.destroy();
142     }
143 
verifyResultsAbsChar2Uchar2(Allocation inV, Allocation out, boolean relaxed)144     private void verifyResultsAbsChar2Uchar2(Allocation inV, Allocation out, boolean relaxed) {
145         byte[] arrayInV = new byte[INPUTSIZE * 2];
146         Arrays.fill(arrayInV, (byte) 42);
147         inV.copyTo(arrayInV);
148         byte[] arrayOut = new byte[INPUTSIZE * 2];
149         Arrays.fill(arrayOut, (byte) 42);
150         out.copyTo(arrayOut);
151         StringBuilder message = new StringBuilder();
152         boolean errorFound = false;
153         for (int i = 0; i < INPUTSIZE; i++) {
154             for (int j = 0; j < 2 ; j++) {
155                 // Extract the inputs.
156                 ArgumentsCharUchar args = new ArgumentsCharUchar();
157                 args.inV = arrayInV[i * 2 + j];
158                 // Figure out what the outputs should have been.
159                 CoreMathVerifier.computeAbs(args);
160                 // Validate the outputs.
161                 boolean valid = true;
162                 if (args.out != arrayOut[i * 2 + j]) {
163                     valid = false;
164                 }
165                 if (!valid) {
166                     if (!errorFound) {
167                         errorFound = true;
168                         message.append("Input inV: ");
169                         appendVariableToMessage(message, args.inV);
170                         message.append("\n");
171                         message.append("Expected output out: ");
172                         appendVariableToMessage(message, args.out);
173                         message.append("\n");
174                         message.append("Actual   output out: ");
175                         appendVariableToMessage(message, arrayOut[i * 2 + j]);
176                         if (args.out != arrayOut[i * 2 + j]) {
177                             message.append(" FAIL");
178                         }
179                         message.append("\n");
180                         message.append("Errors at");
181                     }
182                     message.append(" [");
183                     message.append(Integer.toString(i));
184                     message.append(", ");
185                     message.append(Integer.toString(j));
186                     message.append("]");
187                 }
188             }
189         }
190         assertFalse("Incorrect output for checkAbsChar2Uchar2" +
191                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
192     }
193 
checkAbsChar3Uchar3()194     private void checkAbsChar3Uchar3() {
195         Allocation inV = createRandomAllocation(mRS, Element.DataType.SIGNED_8, 3, 0xff62e6ef0479615al, false);
196         try {
197             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_8, 3), INPUTSIZE);
198             script.forEach_testAbsChar3Uchar3(inV, out);
199             verifyResultsAbsChar3Uchar3(inV, out, false);
200             out.destroy();
201         } catch (Exception e) {
202             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsChar3Uchar3: " + e.toString());
203         }
204         try {
205             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_8, 3), INPUTSIZE);
206             scriptRelaxed.forEach_testAbsChar3Uchar3(inV, out);
207             verifyResultsAbsChar3Uchar3(inV, out, true);
208             out.destroy();
209         } catch (Exception e) {
210             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsChar3Uchar3: " + e.toString());
211         }
212         inV.destroy();
213     }
214 
verifyResultsAbsChar3Uchar3(Allocation inV, Allocation out, boolean relaxed)215     private void verifyResultsAbsChar3Uchar3(Allocation inV, Allocation out, boolean relaxed) {
216         byte[] arrayInV = new byte[INPUTSIZE * 4];
217         Arrays.fill(arrayInV, (byte) 42);
218         inV.copyTo(arrayInV);
219         byte[] arrayOut = new byte[INPUTSIZE * 4];
220         Arrays.fill(arrayOut, (byte) 42);
221         out.copyTo(arrayOut);
222         StringBuilder message = new StringBuilder();
223         boolean errorFound = false;
224         for (int i = 0; i < INPUTSIZE; i++) {
225             for (int j = 0; j < 3 ; j++) {
226                 // Extract the inputs.
227                 ArgumentsCharUchar args = new ArgumentsCharUchar();
228                 args.inV = arrayInV[i * 4 + j];
229                 // Figure out what the outputs should have been.
230                 CoreMathVerifier.computeAbs(args);
231                 // Validate the outputs.
232                 boolean valid = true;
233                 if (args.out != arrayOut[i * 4 + j]) {
234                     valid = false;
235                 }
236                 if (!valid) {
237                     if (!errorFound) {
238                         errorFound = true;
239                         message.append("Input inV: ");
240                         appendVariableToMessage(message, args.inV);
241                         message.append("\n");
242                         message.append("Expected output out: ");
243                         appendVariableToMessage(message, args.out);
244                         message.append("\n");
245                         message.append("Actual   output out: ");
246                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
247                         if (args.out != arrayOut[i * 4 + j]) {
248                             message.append(" FAIL");
249                         }
250                         message.append("\n");
251                         message.append("Errors at");
252                     }
253                     message.append(" [");
254                     message.append(Integer.toString(i));
255                     message.append(", ");
256                     message.append(Integer.toString(j));
257                     message.append("]");
258                 }
259             }
260         }
261         assertFalse("Incorrect output for checkAbsChar3Uchar3" +
262                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
263     }
264 
checkAbsChar4Uchar4()265     private void checkAbsChar4Uchar4() {
266         Allocation inV = createRandomAllocation(mRS, Element.DataType.SIGNED_8, 4, 0xff64b009fa948238l, false);
267         try {
268             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_8, 4), INPUTSIZE);
269             script.forEach_testAbsChar4Uchar4(inV, out);
270             verifyResultsAbsChar4Uchar4(inV, out, false);
271             out.destroy();
272         } catch (Exception e) {
273             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsChar4Uchar4: " + e.toString());
274         }
275         try {
276             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_8, 4), INPUTSIZE);
277             scriptRelaxed.forEach_testAbsChar4Uchar4(inV, out);
278             verifyResultsAbsChar4Uchar4(inV, out, true);
279             out.destroy();
280         } catch (Exception e) {
281             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsChar4Uchar4: " + e.toString());
282         }
283         inV.destroy();
284     }
285 
verifyResultsAbsChar4Uchar4(Allocation inV, Allocation out, boolean relaxed)286     private void verifyResultsAbsChar4Uchar4(Allocation inV, Allocation out, boolean relaxed) {
287         byte[] arrayInV = new byte[INPUTSIZE * 4];
288         Arrays.fill(arrayInV, (byte) 42);
289         inV.copyTo(arrayInV);
290         byte[] arrayOut = new byte[INPUTSIZE * 4];
291         Arrays.fill(arrayOut, (byte) 42);
292         out.copyTo(arrayOut);
293         StringBuilder message = new StringBuilder();
294         boolean errorFound = false;
295         for (int i = 0; i < INPUTSIZE; i++) {
296             for (int j = 0; j < 4 ; j++) {
297                 // Extract the inputs.
298                 ArgumentsCharUchar args = new ArgumentsCharUchar();
299                 args.inV = arrayInV[i * 4 + j];
300                 // Figure out what the outputs should have been.
301                 CoreMathVerifier.computeAbs(args);
302                 // Validate the outputs.
303                 boolean valid = true;
304                 if (args.out != arrayOut[i * 4 + j]) {
305                     valid = false;
306                 }
307                 if (!valid) {
308                     if (!errorFound) {
309                         errorFound = true;
310                         message.append("Input inV: ");
311                         appendVariableToMessage(message, args.inV);
312                         message.append("\n");
313                         message.append("Expected output out: ");
314                         appendVariableToMessage(message, args.out);
315                         message.append("\n");
316                         message.append("Actual   output out: ");
317                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
318                         if (args.out != arrayOut[i * 4 + j]) {
319                             message.append(" FAIL");
320                         }
321                         message.append("\n");
322                         message.append("Errors at");
323                     }
324                     message.append(" [");
325                     message.append(Integer.toString(i));
326                     message.append(", ");
327                     message.append(Integer.toString(j));
328                     message.append("]");
329                 }
330             }
331         }
332         assertFalse("Incorrect output for checkAbsChar4Uchar4" +
333                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
334     }
335 
336     public class ArgumentsShortUshort {
337         public short inV;
338         public short out;
339     }
340 
checkAbsShortUshort()341     private void checkAbsShortUshort() {
342         Allocation inV = createRandomAllocation(mRS, Element.DataType.SIGNED_16, 1, 0xfab837da064819cl, false);
343         try {
344             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_16, 1), INPUTSIZE);
345             script.forEach_testAbsShortUshort(inV, out);
346             verifyResultsAbsShortUshort(inV, out, false);
347             out.destroy();
348         } catch (Exception e) {
349             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsShortUshort: " + e.toString());
350         }
351         try {
352             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_16, 1), INPUTSIZE);
353             scriptRelaxed.forEach_testAbsShortUshort(inV, out);
354             verifyResultsAbsShortUshort(inV, out, true);
355             out.destroy();
356         } catch (Exception e) {
357             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsShortUshort: " + e.toString());
358         }
359         inV.destroy();
360     }
361 
verifyResultsAbsShortUshort(Allocation inV, Allocation out, boolean relaxed)362     private void verifyResultsAbsShortUshort(Allocation inV, Allocation out, boolean relaxed) {
363         short[] arrayInV = new short[INPUTSIZE * 1];
364         Arrays.fill(arrayInV, (short) 42);
365         inV.copyTo(arrayInV);
366         short[] arrayOut = new short[INPUTSIZE * 1];
367         Arrays.fill(arrayOut, (short) 42);
368         out.copyTo(arrayOut);
369         StringBuilder message = new StringBuilder();
370         boolean errorFound = false;
371         for (int i = 0; i < INPUTSIZE; i++) {
372             for (int j = 0; j < 1 ; j++) {
373                 // Extract the inputs.
374                 ArgumentsShortUshort args = new ArgumentsShortUshort();
375                 args.inV = arrayInV[i];
376                 // Figure out what the outputs should have been.
377                 CoreMathVerifier.computeAbs(args);
378                 // Validate the outputs.
379                 boolean valid = true;
380                 if (args.out != arrayOut[i * 1 + j]) {
381                     valid = false;
382                 }
383                 if (!valid) {
384                     if (!errorFound) {
385                         errorFound = true;
386                         message.append("Input inV: ");
387                         appendVariableToMessage(message, args.inV);
388                         message.append("\n");
389                         message.append("Expected output out: ");
390                         appendVariableToMessage(message, args.out);
391                         message.append("\n");
392                         message.append("Actual   output out: ");
393                         appendVariableToMessage(message, arrayOut[i * 1 + j]);
394                         if (args.out != arrayOut[i * 1 + j]) {
395                             message.append(" FAIL");
396                         }
397                         message.append("\n");
398                         message.append("Errors at");
399                     }
400                     message.append(" [");
401                     message.append(Integer.toString(i));
402                     message.append(", ");
403                     message.append(Integer.toString(j));
404                     message.append("]");
405                 }
406             }
407         }
408         assertFalse("Incorrect output for checkAbsShortUshort" +
409                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
410     }
411 
checkAbsShort2Ushort2()412     private void checkAbsShort2Ushort2() {
413         Allocation inV = createRandomAllocation(mRS, Element.DataType.SIGNED_16, 2, 0x231450e16856b93el, false);
414         try {
415             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_16, 2), INPUTSIZE);
416             script.forEach_testAbsShort2Ushort2(inV, out);
417             verifyResultsAbsShort2Ushort2(inV, out, false);
418             out.destroy();
419         } catch (Exception e) {
420             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsShort2Ushort2: " + e.toString());
421         }
422         try {
423             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_16, 2), INPUTSIZE);
424             scriptRelaxed.forEach_testAbsShort2Ushort2(inV, out);
425             verifyResultsAbsShort2Ushort2(inV, out, true);
426             out.destroy();
427         } catch (Exception e) {
428             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsShort2Ushort2: " + e.toString());
429         }
430         inV.destroy();
431     }
432 
verifyResultsAbsShort2Ushort2(Allocation inV, Allocation out, boolean relaxed)433     private void verifyResultsAbsShort2Ushort2(Allocation inV, Allocation out, boolean relaxed) {
434         short[] arrayInV = new short[INPUTSIZE * 2];
435         Arrays.fill(arrayInV, (short) 42);
436         inV.copyTo(arrayInV);
437         short[] arrayOut = new short[INPUTSIZE * 2];
438         Arrays.fill(arrayOut, (short) 42);
439         out.copyTo(arrayOut);
440         StringBuilder message = new StringBuilder();
441         boolean errorFound = false;
442         for (int i = 0; i < INPUTSIZE; i++) {
443             for (int j = 0; j < 2 ; j++) {
444                 // Extract the inputs.
445                 ArgumentsShortUshort args = new ArgumentsShortUshort();
446                 args.inV = arrayInV[i * 2 + j];
447                 // Figure out what the outputs should have been.
448                 CoreMathVerifier.computeAbs(args);
449                 // Validate the outputs.
450                 boolean valid = true;
451                 if (args.out != arrayOut[i * 2 + j]) {
452                     valid = false;
453                 }
454                 if (!valid) {
455                     if (!errorFound) {
456                         errorFound = true;
457                         message.append("Input inV: ");
458                         appendVariableToMessage(message, args.inV);
459                         message.append("\n");
460                         message.append("Expected output out: ");
461                         appendVariableToMessage(message, args.out);
462                         message.append("\n");
463                         message.append("Actual   output out: ");
464                         appendVariableToMessage(message, arrayOut[i * 2 + j]);
465                         if (args.out != arrayOut[i * 2 + j]) {
466                             message.append(" FAIL");
467                         }
468                         message.append("\n");
469                         message.append("Errors at");
470                     }
471                     message.append(" [");
472                     message.append(Integer.toString(i));
473                     message.append(", ");
474                     message.append(Integer.toString(j));
475                     message.append("]");
476                 }
477             }
478         }
479         assertFalse("Incorrect output for checkAbsShort2Ushort2" +
480                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
481     }
482 
checkAbsShort3Ushort3()483     private void checkAbsShort3Ushort3() {
484         Allocation inV = createRandomAllocation(mRS, Element.DataType.SIGNED_16, 3, 0x23611868beb24a6al, false);
485         try {
486             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_16, 3), INPUTSIZE);
487             script.forEach_testAbsShort3Ushort3(inV, out);
488             verifyResultsAbsShort3Ushort3(inV, out, false);
489             out.destroy();
490         } catch (Exception e) {
491             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsShort3Ushort3: " + e.toString());
492         }
493         try {
494             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_16, 3), INPUTSIZE);
495             scriptRelaxed.forEach_testAbsShort3Ushort3(inV, out);
496             verifyResultsAbsShort3Ushort3(inV, out, true);
497             out.destroy();
498         } catch (Exception e) {
499             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsShort3Ushort3: " + e.toString());
500         }
501         inV.destroy();
502     }
503 
verifyResultsAbsShort3Ushort3(Allocation inV, Allocation out, boolean relaxed)504     private void verifyResultsAbsShort3Ushort3(Allocation inV, Allocation out, boolean relaxed) {
505         short[] arrayInV = new short[INPUTSIZE * 4];
506         Arrays.fill(arrayInV, (short) 42);
507         inV.copyTo(arrayInV);
508         short[] arrayOut = new short[INPUTSIZE * 4];
509         Arrays.fill(arrayOut, (short) 42);
510         out.copyTo(arrayOut);
511         StringBuilder message = new StringBuilder();
512         boolean errorFound = false;
513         for (int i = 0; i < INPUTSIZE; i++) {
514             for (int j = 0; j < 3 ; j++) {
515                 // Extract the inputs.
516                 ArgumentsShortUshort args = new ArgumentsShortUshort();
517                 args.inV = arrayInV[i * 4 + j];
518                 // Figure out what the outputs should have been.
519                 CoreMathVerifier.computeAbs(args);
520                 // Validate the outputs.
521                 boolean valid = true;
522                 if (args.out != arrayOut[i * 4 + j]) {
523                     valid = false;
524                 }
525                 if (!valid) {
526                     if (!errorFound) {
527                         errorFound = true;
528                         message.append("Input inV: ");
529                         appendVariableToMessage(message, args.inV);
530                         message.append("\n");
531                         message.append("Expected output out: ");
532                         appendVariableToMessage(message, args.out);
533                         message.append("\n");
534                         message.append("Actual   output out: ");
535                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
536                         if (args.out != arrayOut[i * 4 + j]) {
537                             message.append(" FAIL");
538                         }
539                         message.append("\n");
540                         message.append("Errors at");
541                     }
542                     message.append(" [");
543                     message.append(Integer.toString(i));
544                     message.append(", ");
545                     message.append(Integer.toString(j));
546                     message.append("]");
547                 }
548             }
549         }
550         assertFalse("Incorrect output for checkAbsShort3Ushort3" +
551                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
552     }
553 
checkAbsShort4Ushort4()554     private void checkAbsShort4Ushort4() {
555         Allocation inV = createRandomAllocation(mRS, Element.DataType.SIGNED_16, 4, 0x23addff0150ddb96l, false);
556         try {
557             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_16, 4), INPUTSIZE);
558             script.forEach_testAbsShort4Ushort4(inV, out);
559             verifyResultsAbsShort4Ushort4(inV, out, false);
560             out.destroy();
561         } catch (Exception e) {
562             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsShort4Ushort4: " + e.toString());
563         }
564         try {
565             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_16, 4), INPUTSIZE);
566             scriptRelaxed.forEach_testAbsShort4Ushort4(inV, out);
567             verifyResultsAbsShort4Ushort4(inV, out, true);
568             out.destroy();
569         } catch (Exception e) {
570             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsShort4Ushort4: " + e.toString());
571         }
572         inV.destroy();
573     }
574 
verifyResultsAbsShort4Ushort4(Allocation inV, Allocation out, boolean relaxed)575     private void verifyResultsAbsShort4Ushort4(Allocation inV, Allocation out, boolean relaxed) {
576         short[] arrayInV = new short[INPUTSIZE * 4];
577         Arrays.fill(arrayInV, (short) 42);
578         inV.copyTo(arrayInV);
579         short[] arrayOut = new short[INPUTSIZE * 4];
580         Arrays.fill(arrayOut, (short) 42);
581         out.copyTo(arrayOut);
582         StringBuilder message = new StringBuilder();
583         boolean errorFound = false;
584         for (int i = 0; i < INPUTSIZE; i++) {
585             for (int j = 0; j < 4 ; j++) {
586                 // Extract the inputs.
587                 ArgumentsShortUshort args = new ArgumentsShortUshort();
588                 args.inV = arrayInV[i * 4 + j];
589                 // Figure out what the outputs should have been.
590                 CoreMathVerifier.computeAbs(args);
591                 // Validate the outputs.
592                 boolean valid = true;
593                 if (args.out != arrayOut[i * 4 + j]) {
594                     valid = false;
595                 }
596                 if (!valid) {
597                     if (!errorFound) {
598                         errorFound = true;
599                         message.append("Input inV: ");
600                         appendVariableToMessage(message, args.inV);
601                         message.append("\n");
602                         message.append("Expected output out: ");
603                         appendVariableToMessage(message, args.out);
604                         message.append("\n");
605                         message.append("Actual   output out: ");
606                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
607                         if (args.out != arrayOut[i * 4 + j]) {
608                             message.append(" FAIL");
609                         }
610                         message.append("\n");
611                         message.append("Errors at");
612                     }
613                     message.append(" [");
614                     message.append(Integer.toString(i));
615                     message.append(", ");
616                     message.append(Integer.toString(j));
617                     message.append("]");
618                 }
619             }
620         }
621         assertFalse("Incorrect output for checkAbsShort4Ushort4" +
622                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
623     }
624 
625     public class ArgumentsIntUint {
626         public int inV;
627         public int out;
628     }
629 
checkAbsIntUint()630     private void checkAbsIntUint() {
631         Allocation inV = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 1, 0x6adb1880ac5b83e6l, false);
632         try {
633             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_32, 1), INPUTSIZE);
634             script.forEach_testAbsIntUint(inV, out);
635             verifyResultsAbsIntUint(inV, out, false);
636             out.destroy();
637         } catch (Exception e) {
638             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsIntUint: " + e.toString());
639         }
640         try {
641             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_32, 1), INPUTSIZE);
642             scriptRelaxed.forEach_testAbsIntUint(inV, out);
643             verifyResultsAbsIntUint(inV, out, true);
644             out.destroy();
645         } catch (Exception e) {
646             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsIntUint: " + e.toString());
647         }
648         inV.destroy();
649     }
650 
verifyResultsAbsIntUint(Allocation inV, Allocation out, boolean relaxed)651     private void verifyResultsAbsIntUint(Allocation inV, Allocation out, boolean relaxed) {
652         int[] arrayInV = new int[INPUTSIZE * 1];
653         Arrays.fill(arrayInV, (int) 42);
654         inV.copyTo(arrayInV);
655         int[] arrayOut = new int[INPUTSIZE * 1];
656         Arrays.fill(arrayOut, (int) 42);
657         out.copyTo(arrayOut);
658         StringBuilder message = new StringBuilder();
659         boolean errorFound = false;
660         for (int i = 0; i < INPUTSIZE; i++) {
661             for (int j = 0; j < 1 ; j++) {
662                 // Extract the inputs.
663                 ArgumentsIntUint args = new ArgumentsIntUint();
664                 args.inV = arrayInV[i];
665                 // Figure out what the outputs should have been.
666                 CoreMathVerifier.computeAbs(args);
667                 // Validate the outputs.
668                 boolean valid = true;
669                 if (args.out != arrayOut[i * 1 + j]) {
670                     valid = false;
671                 }
672                 if (!valid) {
673                     if (!errorFound) {
674                         errorFound = true;
675                         message.append("Input inV: ");
676                         appendVariableToMessage(message, args.inV);
677                         message.append("\n");
678                         message.append("Expected output out: ");
679                         appendVariableToMessage(message, args.out);
680                         message.append("\n");
681                         message.append("Actual   output out: ");
682                         appendVariableToMessage(message, arrayOut[i * 1 + j]);
683                         if (args.out != arrayOut[i * 1 + j]) {
684                             message.append(" FAIL");
685                         }
686                         message.append("\n");
687                         message.append("Errors at");
688                     }
689                     message.append(" [");
690                     message.append(Integer.toString(i));
691                     message.append(", ");
692                     message.append(Integer.toString(j));
693                     message.append("]");
694                 }
695             }
696         }
697         assertFalse("Incorrect output for checkAbsIntUint" +
698                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
699     }
700 
checkAbsInt2Uint2()701     private void checkAbsInt2Uint2() {
702         Allocation inV = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 2, 0xc8728053938616fal, false);
703         try {
704             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_32, 2), INPUTSIZE);
705             script.forEach_testAbsInt2Uint2(inV, out);
706             verifyResultsAbsInt2Uint2(inV, out, false);
707             out.destroy();
708         } catch (Exception e) {
709             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsInt2Uint2: " + e.toString());
710         }
711         try {
712             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_32, 2), INPUTSIZE);
713             scriptRelaxed.forEach_testAbsInt2Uint2(inV, out);
714             verifyResultsAbsInt2Uint2(inV, out, true);
715             out.destroy();
716         } catch (Exception e) {
717             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsInt2Uint2: " + e.toString());
718         }
719         inV.destroy();
720     }
721 
verifyResultsAbsInt2Uint2(Allocation inV, Allocation out, boolean relaxed)722     private void verifyResultsAbsInt2Uint2(Allocation inV, Allocation out, boolean relaxed) {
723         int[] arrayInV = new int[INPUTSIZE * 2];
724         Arrays.fill(arrayInV, (int) 42);
725         inV.copyTo(arrayInV);
726         int[] arrayOut = new int[INPUTSIZE * 2];
727         Arrays.fill(arrayOut, (int) 42);
728         out.copyTo(arrayOut);
729         StringBuilder message = new StringBuilder();
730         boolean errorFound = false;
731         for (int i = 0; i < INPUTSIZE; i++) {
732             for (int j = 0; j < 2 ; j++) {
733                 // Extract the inputs.
734                 ArgumentsIntUint args = new ArgumentsIntUint();
735                 args.inV = arrayInV[i * 2 + j];
736                 // Figure out what the outputs should have been.
737                 CoreMathVerifier.computeAbs(args);
738                 // Validate the outputs.
739                 boolean valid = true;
740                 if (args.out != arrayOut[i * 2 + j]) {
741                     valid = false;
742                 }
743                 if (!valid) {
744                     if (!errorFound) {
745                         errorFound = true;
746                         message.append("Input inV: ");
747                         appendVariableToMessage(message, args.inV);
748                         message.append("\n");
749                         message.append("Expected output out: ");
750                         appendVariableToMessage(message, args.out);
751                         message.append("\n");
752                         message.append("Actual   output out: ");
753                         appendVariableToMessage(message, arrayOut[i * 2 + j]);
754                         if (args.out != arrayOut[i * 2 + j]) {
755                             message.append(" FAIL");
756                         }
757                         message.append("\n");
758                         message.append("Errors at");
759                     }
760                     message.append(" [");
761                     message.append(Integer.toString(i));
762                     message.append(", ");
763                     message.append(Integer.toString(j));
764                     message.append("]");
765                 }
766             }
767         }
768         assertFalse("Incorrect output for checkAbsInt2Uint2" +
769                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
770     }
771 
checkAbsInt3Uint3()772     private void checkAbsInt3Uint3() {
773         Allocation inV = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 3, 0xc8728af4f28ddbeel, false);
774         try {
775             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_32, 3), INPUTSIZE);
776             script.forEach_testAbsInt3Uint3(inV, out);
777             verifyResultsAbsInt3Uint3(inV, out, false);
778             out.destroy();
779         } catch (Exception e) {
780             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsInt3Uint3: " + e.toString());
781         }
782         try {
783             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_32, 3), INPUTSIZE);
784             scriptRelaxed.forEach_testAbsInt3Uint3(inV, out);
785             verifyResultsAbsInt3Uint3(inV, out, true);
786             out.destroy();
787         } catch (Exception e) {
788             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsInt3Uint3: " + e.toString());
789         }
790         inV.destroy();
791     }
792 
verifyResultsAbsInt3Uint3(Allocation inV, Allocation out, boolean relaxed)793     private void verifyResultsAbsInt3Uint3(Allocation inV, Allocation out, boolean relaxed) {
794         int[] arrayInV = new int[INPUTSIZE * 4];
795         Arrays.fill(arrayInV, (int) 42);
796         inV.copyTo(arrayInV);
797         int[] arrayOut = new int[INPUTSIZE * 4];
798         Arrays.fill(arrayOut, (int) 42);
799         out.copyTo(arrayOut);
800         StringBuilder message = new StringBuilder();
801         boolean errorFound = false;
802         for (int i = 0; i < INPUTSIZE; i++) {
803             for (int j = 0; j < 3 ; j++) {
804                 // Extract the inputs.
805                 ArgumentsIntUint args = new ArgumentsIntUint();
806                 args.inV = arrayInV[i * 4 + j];
807                 // Figure out what the outputs should have been.
808                 CoreMathVerifier.computeAbs(args);
809                 // Validate the outputs.
810                 boolean valid = true;
811                 if (args.out != arrayOut[i * 4 + j]) {
812                     valid = false;
813                 }
814                 if (!valid) {
815                     if (!errorFound) {
816                         errorFound = true;
817                         message.append("Input inV: ");
818                         appendVariableToMessage(message, args.inV);
819                         message.append("\n");
820                         message.append("Expected output out: ");
821                         appendVariableToMessage(message, args.out);
822                         message.append("\n");
823                         message.append("Actual   output out: ");
824                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
825                         if (args.out != arrayOut[i * 4 + j]) {
826                             message.append(" FAIL");
827                         }
828                         message.append("\n");
829                         message.append("Errors at");
830                     }
831                     message.append(" [");
832                     message.append(Integer.toString(i));
833                     message.append(", ");
834                     message.append(Integer.toString(j));
835                     message.append("]");
836                 }
837             }
838         }
839         assertFalse("Incorrect output for checkAbsInt3Uint3" +
840                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
841     }
842 
checkAbsInt4Uint4()843     private void checkAbsInt4Uint4() {
844         Allocation inV = createRandomAllocation(mRS, Element.DataType.SIGNED_32, 4, 0xc87295965195a0e2l, false);
845         try {
846             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_32, 4), INPUTSIZE);
847             script.forEach_testAbsInt4Uint4(inV, out);
848             verifyResultsAbsInt4Uint4(inV, out, false);
849             out.destroy();
850         } catch (Exception e) {
851             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsInt4Uint4: " + e.toString());
852         }
853         try {
854             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.UNSIGNED_32, 4), INPUTSIZE);
855             scriptRelaxed.forEach_testAbsInt4Uint4(inV, out);
856             verifyResultsAbsInt4Uint4(inV, out, true);
857             out.destroy();
858         } catch (Exception e) {
859             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testAbsInt4Uint4: " + e.toString());
860         }
861         inV.destroy();
862     }
863 
verifyResultsAbsInt4Uint4(Allocation inV, Allocation out, boolean relaxed)864     private void verifyResultsAbsInt4Uint4(Allocation inV, Allocation out, boolean relaxed) {
865         int[] arrayInV = new int[INPUTSIZE * 4];
866         Arrays.fill(arrayInV, (int) 42);
867         inV.copyTo(arrayInV);
868         int[] arrayOut = new int[INPUTSIZE * 4];
869         Arrays.fill(arrayOut, (int) 42);
870         out.copyTo(arrayOut);
871         StringBuilder message = new StringBuilder();
872         boolean errorFound = false;
873         for (int i = 0; i < INPUTSIZE; i++) {
874             for (int j = 0; j < 4 ; j++) {
875                 // Extract the inputs.
876                 ArgumentsIntUint args = new ArgumentsIntUint();
877                 args.inV = arrayInV[i * 4 + j];
878                 // Figure out what the outputs should have been.
879                 CoreMathVerifier.computeAbs(args);
880                 // Validate the outputs.
881                 boolean valid = true;
882                 if (args.out != arrayOut[i * 4 + j]) {
883                     valid = false;
884                 }
885                 if (!valid) {
886                     if (!errorFound) {
887                         errorFound = true;
888                         message.append("Input inV: ");
889                         appendVariableToMessage(message, args.inV);
890                         message.append("\n");
891                         message.append("Expected output out: ");
892                         appendVariableToMessage(message, args.out);
893                         message.append("\n");
894                         message.append("Actual   output out: ");
895                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
896                         if (args.out != arrayOut[i * 4 + j]) {
897                             message.append(" FAIL");
898                         }
899                         message.append("\n");
900                         message.append("Errors at");
901                     }
902                     message.append(" [");
903                     message.append(Integer.toString(i));
904                     message.append(", ");
905                     message.append(Integer.toString(j));
906                     message.append("]");
907                 }
908             }
909         }
910         assertFalse("Incorrect output for checkAbsInt4Uint4" +
911                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
912     }
913 
testAbs()914     public void testAbs() {
915         checkAbsCharUchar();
916         checkAbsChar2Uchar2();
917         checkAbsChar3Uchar3();
918         checkAbsChar4Uchar4();
919         checkAbsShortUshort();
920         checkAbsShort2Ushort2();
921         checkAbsShort3Ushort3();
922         checkAbsShort4Ushort4();
923         checkAbsIntUint();
924         checkAbsInt2Uint2();
925         checkAbsInt3Uint3();
926         checkAbsInt4Uint4();
927     }
928 }
929