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 TestNan extends RSBaseCompute {
29 
30     private ScriptC_TestNan script;
31     private ScriptC_TestNanRelaxed scriptRelaxed;
32 
33     @Override
setUp()34     protected void setUp() throws Exception {
35         super.setUp();
36         script = new ScriptC_TestNan(mRS);
37         scriptRelaxed = new ScriptC_TestNanRelaxed(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 ArgumentsUintFloat {
48         public int inV;
49         public Target.Floaty out;
50     }
51 
checkNanUintFloat()52     private void checkNanUintFloat() {
53         Allocation inV = createRandomAllocation(mRS, Element.DataType.UNSIGNED_32, 1, 0xbc42cb366a8a10d2l, false);
54         try {
55             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
56             script.forEach_testNanUintFloat(inV, out);
57             verifyResultsNanUintFloat(inV, out, false);
58             out.destroy();
59         } catch (Exception e) {
60             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNanUintFloat: " + e.toString());
61         }
62         try {
63             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
64             scriptRelaxed.forEach_testNanUintFloat(inV, out);
65             verifyResultsNanUintFloat(inV, out, true);
66             out.destroy();
67         } catch (Exception e) {
68             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNanUintFloat: " + e.toString());
69         }
70         inV.destroy();
71     }
72 
verifyResultsNanUintFloat(Allocation inV, Allocation out, boolean relaxed)73     private void verifyResultsNanUintFloat(Allocation inV, Allocation out, boolean relaxed) {
74         int[] arrayInV = new int[INPUTSIZE * 1];
75         Arrays.fill(arrayInV, (int) 42);
76         inV.copyTo(arrayInV);
77         float[] arrayOut = new float[INPUTSIZE * 1];
78         Arrays.fill(arrayOut, (float) 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                 ArgumentsUintFloat args = new ArgumentsUintFloat();
86                 args.inV = arrayInV[i];
87                 // Figure out what the outputs should have been.
88                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
89                 CoreMathVerifier.computeNan(args, target);
90                 // Validate the outputs.
91                 boolean valid = true;
92                 if (!args.out.couldBe(arrayOut[i * 1 + j])) {
93                     valid = false;
94                 }
95                 if (!valid) {
96                     if (!errorFound) {
97                         errorFound = true;
98                         message.append("Input inV: ");
99                         appendVariableToMessage(message, args.inV);
100                         message.append("\n");
101                         message.append("Expected output out: ");
102                         appendVariableToMessage(message, args.out);
103                         message.append("\n");
104                         message.append("Actual   output out: ");
105                         appendVariableToMessage(message, arrayOut[i * 1 + j]);
106                         if (!args.out.couldBe(arrayOut[i * 1 + j])) {
107                             message.append(" FAIL");
108                         }
109                         message.append("\n");
110                         message.append("Errors at");
111                     }
112                     message.append(" [");
113                     message.append(Integer.toString(i));
114                     message.append(", ");
115                     message.append(Integer.toString(j));
116                     message.append("]");
117                 }
118             }
119         }
120         assertFalse("Incorrect output for checkNanUintFloat" +
121                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
122     }
123 
testNan()124     public void testNan() {
125         checkNanUintFloat();
126     }
127 }
128