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     public class ArgumentsUintFloat {
41         public int inV;
42         public Target.Floaty out;
43     }
44 
checkNanUintFloat()45     private void checkNanUintFloat() {
46         Allocation inV = createRandomAllocation(mRS, Element.DataType.UNSIGNED_32, 1, 0x6a8a10d2l, false);
47         try {
48             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
49             script.forEach_testNanUintFloat(inV, out);
50             verifyResultsNanUintFloat(inV, out, false);
51         } catch (Exception e) {
52             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNanUintFloat: " + e.toString());
53         }
54         try {
55             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
56             scriptRelaxed.forEach_testNanUintFloat(inV, out);
57             verifyResultsNanUintFloat(inV, out, true);
58         } catch (Exception e) {
59             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNanUintFloat: " + e.toString());
60         }
61     }
62 
verifyResultsNanUintFloat(Allocation inV, Allocation out, boolean relaxed)63     private void verifyResultsNanUintFloat(Allocation inV, Allocation out, boolean relaxed) {
64         int[] arrayInV = new int[INPUTSIZE * 1];
65         Arrays.fill(arrayInV, (int) 42);
66         inV.copyTo(arrayInV);
67         float[] arrayOut = new float[INPUTSIZE * 1];
68         Arrays.fill(arrayOut, (float) 42);
69         out.copyTo(arrayOut);
70         StringBuilder message = new StringBuilder();
71         boolean errorFound = false;
72         for (int i = 0; i < INPUTSIZE; i++) {
73             for (int j = 0; j < 1 ; j++) {
74                 // Extract the inputs.
75                 ArgumentsUintFloat args = new ArgumentsUintFloat();
76                 args.inV = arrayInV[i];
77                 // Figure out what the outputs should have been.
78                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.FLOAT, relaxed);
79                 CoreMathVerifier.computeNan(args, target);
80                 // Validate the outputs.
81                 boolean valid = true;
82                 if (!args.out.couldBe(arrayOut[i * 1 + j])) {
83                     valid = false;
84                 }
85                 if (!valid) {
86                     if (!errorFound) {
87                         errorFound = true;
88                         message.append("Input inV: ");
89                         appendVariableToMessage(message, args.inV);
90                         message.append("\n");
91                         message.append("Expected output out: ");
92                         appendVariableToMessage(message, args.out);
93                         message.append("\n");
94                         message.append("Actual   output out: ");
95                         appendVariableToMessage(message, arrayOut[i * 1 + j]);
96                         if (!args.out.couldBe(arrayOut[i * 1 + j])) {
97                             message.append(" FAIL");
98                         }
99                         message.append("\n");
100                         message.append("Errors at");
101                     }
102                     message.append(" [");
103                     message.append(Integer.toString(i));
104                     message.append(", ");
105                     message.append(Integer.toString(j));
106                     message.append("]");
107                 }
108             }
109         }
110         assertFalse("Incorrect output for checkNanUintFloat" +
111                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
112     }
113 
testNan()114     public void testNan() {
115         checkNanUintFloat();
116     }
117 }
118