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 TestNanHalf extends RSBaseCompute {
29 
30     private ScriptC_TestNanHalf script;
31     private ScriptC_TestNanHalfRelaxed scriptRelaxed;
32 
33     @Override
setUp()34     protected void setUp() throws Exception {
35         super.setUp();
36         script = new ScriptC_TestNanHalf(mRS);
37         scriptRelaxed = new ScriptC_TestNanHalfRelaxed(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 ArgumentsHalf {
48         public Target.Floaty out;
49     }
50 
checkNanHalfHalf()51     private void checkNanHalfHalf() {
52         try {
53             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 1), INPUTSIZE);
54             script.forEach_testNanHalfHalf(out);
55             verifyResultsNanHalfHalf(out, false);
56             out.destroy();
57         } catch (Exception e) {
58             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNanHalfHalf: " + e.toString());
59         }
60         try {
61             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_16, 1), INPUTSIZE);
62             scriptRelaxed.forEach_testNanHalfHalf(out);
63             verifyResultsNanHalfHalf(out, true);
64             out.destroy();
65         } catch (Exception e) {
66             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNanHalfHalf: " + e.toString());
67         }
68     }
69 
verifyResultsNanHalfHalf(Allocation out, boolean relaxed)70     private void verifyResultsNanHalfHalf(Allocation out, boolean relaxed) {
71         short[] arrayOut = new short[INPUTSIZE * 1];
72         Arrays.fill(arrayOut, (short) 42);
73         out.copyTo(arrayOut);
74         StringBuilder message = new StringBuilder();
75         boolean errorFound = false;
76         for (int i = 0; i < INPUTSIZE; i++) {
77             for (int j = 0; j < 1 ; j++) {
78                 // Extract the inputs.
79                 ArgumentsHalf args = new ArgumentsHalf();
80                 // Figure out what the outputs should have been.
81                 Target target = new Target(Target.FunctionType.NORMAL, Target.ReturnType.HALF, relaxed);
82                 CoreMathVerifier.computeNanHalf(args, target);
83                 // Validate the outputs.
84                 boolean valid = true;
85                 if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 1 + j]))) {
86                     valid = false;
87                 }
88                 if (!valid) {
89                     if (!errorFound) {
90                         errorFound = true;
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                         message.append("\n");
97                         message.append("Actual   output out (in double): ");
98                         appendVariableToMessage(message, Float16Utils.convertFloat16ToDouble(arrayOut[i * 1 + j]));
99                         if (!args.out.couldBe(Float16Utils.convertFloat16ToDouble(arrayOut[i * 1 + j]))) {
100                             message.append(" FAIL");
101                         }
102                         message.append("\n");
103                         message.append("Errors at");
104                     }
105                     message.append(" [");
106                     message.append(Integer.toString(i));
107                     message.append(", ");
108                     message.append(Integer.toString(j));
109                     message.append("]");
110                 }
111             }
112         }
113         assertFalse("Incorrect output for checkNanHalfHalf" +
114                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
115     }
116 
testNanHalf()117     public void testNanHalf() {
118         checkNanHalfHalf();
119     }
120 }
121