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 TestFastNormalize extends RSBaseCompute {
29 
30     private ScriptC_TestFastNormalize script;
31     private ScriptC_TestFastNormalizeRelaxed scriptRelaxed;
32 
33     @Override
setUp()34     protected void setUp() throws Exception {
35         super.setUp();
36         script = new ScriptC_TestFastNormalize(mRS);
37         scriptRelaxed = new ScriptC_TestFastNormalizeRelaxed(mRS);
38     }
39 
40     public class ArgumentsFloatFloat {
41         public float inV;
42         public Target.Floaty out;
43     }
44 
checkFastNormalizeFloatFloat()45     private void checkFastNormalizeFloatFloat() {
46         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0x9f8882ecl, false);
47         try {
48             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
49             script.forEach_testFastNormalizeFloatFloat(inV, out);
50             verifyResultsFastNormalizeFloatFloat(inV, out, false);
51         } catch (Exception e) {
52             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastNormalizeFloatFloat: " + e.toString());
53         }
54         try {
55             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
56             scriptRelaxed.forEach_testFastNormalizeFloatFloat(inV, out);
57             verifyResultsFastNormalizeFloatFloat(inV, out, true);
58         } catch (Exception e) {
59             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastNormalizeFloatFloat: " + e.toString());
60         }
61     }
62 
verifyResultsFastNormalizeFloatFloat(Allocation inV, Allocation out, boolean relaxed)63     private void verifyResultsFastNormalizeFloatFloat(Allocation inV, Allocation out, boolean relaxed) {
64         float[] arrayInV = new float[INPUTSIZE * 1];
65         Arrays.fill(arrayInV, (float) 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             ArgumentsFloatFloat args = new ArgumentsFloatFloat();
74             // Create the appropriate sized arrays in args
75             // Fill args with the input values
76             args.inV = arrayInV[i];
77             Target target = new Target(Target.FunctionType.FAST, Target.ReturnType.FLOAT, relaxed);
78             CoreMathVerifier.computeFastNormalize(args, target);
79 
80             // Compare the expected outputs to the actual values returned by RS.
81             boolean valid = true;
82             if (!args.out.couldBe(arrayOut[i])) {
83                 valid = false;
84             }
85             if (!valid) {
86                 if (!errorFound) {
87                     errorFound = true;
88                     message.append("Input inV: ");
89                     appendVariableToMessage(message, arrayInV[i]);
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]);
96                     if (!args.out.couldBe(arrayOut[i])) {
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             }
106         }
107         assertFalse("Incorrect output for checkFastNormalizeFloatFloat" +
108                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
109     }
110 
111     public class ArgumentsFloatNFloatN {
112         public float[] inV;
113         public Target.Floaty[] out;
114     }
115 
checkFastNormalizeFloat2Float2()116     private void checkFastNormalizeFloat2Float2() {
117         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 2, 0x573db70l, false);
118         try {
119             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 2), INPUTSIZE);
120             script.forEach_testFastNormalizeFloat2Float2(inV, out);
121             verifyResultsFastNormalizeFloat2Float2(inV, out, false);
122         } catch (Exception e) {
123             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastNormalizeFloat2Float2: " + e.toString());
124         }
125         try {
126             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 2), INPUTSIZE);
127             scriptRelaxed.forEach_testFastNormalizeFloat2Float2(inV, out);
128             verifyResultsFastNormalizeFloat2Float2(inV, out, true);
129         } catch (Exception e) {
130             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastNormalizeFloat2Float2: " + e.toString());
131         }
132     }
133 
verifyResultsFastNormalizeFloat2Float2(Allocation inV, Allocation out, boolean relaxed)134     private void verifyResultsFastNormalizeFloat2Float2(Allocation inV, Allocation out, boolean relaxed) {
135         float[] arrayInV = new float[INPUTSIZE * 2];
136         Arrays.fill(arrayInV, (float) 42);
137         inV.copyTo(arrayInV);
138         float[] arrayOut = new float[INPUTSIZE * 2];
139         Arrays.fill(arrayOut, (float) 42);
140         out.copyTo(arrayOut);
141         StringBuilder message = new StringBuilder();
142         boolean errorFound = false;
143         for (int i = 0; i < INPUTSIZE; i++) {
144             ArgumentsFloatNFloatN args = new ArgumentsFloatNFloatN();
145             // Create the appropriate sized arrays in args
146             args.inV = new float[2];
147             args.out = new Target.Floaty[2];
148             // Fill args with the input values
149             for (int j = 0; j < 2 ; j++) {
150                 args.inV[j] = arrayInV[i * 2 + j];
151             }
152             Target target = new Target(Target.FunctionType.FAST, Target.ReturnType.FLOAT, relaxed);
153             CoreMathVerifier.computeFastNormalize(args, target);
154 
155             // Compare the expected outputs to the actual values returned by RS.
156             boolean valid = true;
157             for (int j = 0; j < 2 ; j++) {
158                 if (!args.out[j].couldBe(arrayOut[i * 2 + j])) {
159                     valid = false;
160                 }
161             }
162             if (!valid) {
163                 if (!errorFound) {
164                     errorFound = true;
165                     for (int j = 0; j < 2 ; j++) {
166                         message.append("Input inV: ");
167                         appendVariableToMessage(message, arrayInV[i * 2 + j]);
168                         message.append("\n");
169                     }
170                     for (int j = 0; j < 2 ; j++) {
171                         message.append("Expected output out: ");
172                         appendVariableToMessage(message, args.out[j]);
173                         message.append("\n");
174                         message.append("Actual   output out: ");
175                         appendVariableToMessage(message, arrayOut[i * 2 + j]);
176                         if (!args.out[j].couldBe(arrayOut[i * 2 + j])) {
177                             message.append(" FAIL");
178                         }
179                         message.append("\n");
180                     }
181                     message.append("Errors at");
182                 }
183                 message.append(" [");
184                 message.append(Integer.toString(i));
185                 message.append("]");
186             }
187         }
188         assertFalse("Incorrect output for checkFastNormalizeFloat2Float2" +
189                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
190     }
191 
checkFastNormalizeFloat3Float3()192     private void checkFastNormalizeFloat3Float3() {
193         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 3, 0xfb8efc4el, false);
194         try {
195             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 3), INPUTSIZE);
196             script.forEach_testFastNormalizeFloat3Float3(inV, out);
197             verifyResultsFastNormalizeFloat3Float3(inV, out, false);
198         } catch (Exception e) {
199             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastNormalizeFloat3Float3: " + e.toString());
200         }
201         try {
202             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 3), INPUTSIZE);
203             scriptRelaxed.forEach_testFastNormalizeFloat3Float3(inV, out);
204             verifyResultsFastNormalizeFloat3Float3(inV, out, true);
205         } catch (Exception e) {
206             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastNormalizeFloat3Float3: " + e.toString());
207         }
208     }
209 
verifyResultsFastNormalizeFloat3Float3(Allocation inV, Allocation out, boolean relaxed)210     private void verifyResultsFastNormalizeFloat3Float3(Allocation inV, Allocation out, boolean relaxed) {
211         float[] arrayInV = new float[INPUTSIZE * 4];
212         Arrays.fill(arrayInV, (float) 42);
213         inV.copyTo(arrayInV);
214         float[] arrayOut = new float[INPUTSIZE * 4];
215         Arrays.fill(arrayOut, (float) 42);
216         out.copyTo(arrayOut);
217         StringBuilder message = new StringBuilder();
218         boolean errorFound = false;
219         for (int i = 0; i < INPUTSIZE; i++) {
220             ArgumentsFloatNFloatN args = new ArgumentsFloatNFloatN();
221             // Create the appropriate sized arrays in args
222             args.inV = new float[3];
223             args.out = new Target.Floaty[3];
224             // Fill args with the input values
225             for (int j = 0; j < 3 ; j++) {
226                 args.inV[j] = arrayInV[i * 4 + j];
227             }
228             Target target = new Target(Target.FunctionType.FAST, Target.ReturnType.FLOAT, relaxed);
229             CoreMathVerifier.computeFastNormalize(args, target);
230 
231             // Compare the expected outputs to the actual values returned by RS.
232             boolean valid = true;
233             for (int j = 0; j < 3 ; j++) {
234                 if (!args.out[j].couldBe(arrayOut[i * 4 + j])) {
235                     valid = false;
236                 }
237             }
238             if (!valid) {
239                 if (!errorFound) {
240                     errorFound = true;
241                     for (int j = 0; j < 3 ; j++) {
242                         message.append("Input inV: ");
243                         appendVariableToMessage(message, arrayInV[i * 4 + j]);
244                         message.append("\n");
245                     }
246                     for (int j = 0; j < 3 ; j++) {
247                         message.append("Expected output out: ");
248                         appendVariableToMessage(message, args.out[j]);
249                         message.append("\n");
250                         message.append("Actual   output out: ");
251                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
252                         if (!args.out[j].couldBe(arrayOut[i * 4 + j])) {
253                             message.append(" FAIL");
254                         }
255                         message.append("\n");
256                     }
257                     message.append("Errors at");
258                 }
259                 message.append(" [");
260                 message.append(Integer.toString(i));
261                 message.append("]");
262             }
263         }
264         assertFalse("Incorrect output for checkFastNormalizeFloat3Float3" +
265                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
266     }
267 
checkFastNormalizeFloat4Float4()268     private void checkFastNormalizeFloat4Float4() {
269         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 4, 0xf1aa1d2cl, false);
270         try {
271             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 4), INPUTSIZE);
272             script.forEach_testFastNormalizeFloat4Float4(inV, out);
273             verifyResultsFastNormalizeFloat4Float4(inV, out, false);
274         } catch (Exception e) {
275             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastNormalizeFloat4Float4: " + e.toString());
276         }
277         try {
278             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 4), INPUTSIZE);
279             scriptRelaxed.forEach_testFastNormalizeFloat4Float4(inV, out);
280             verifyResultsFastNormalizeFloat4Float4(inV, out, true);
281         } catch (Exception e) {
282             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastNormalizeFloat4Float4: " + e.toString());
283         }
284     }
285 
verifyResultsFastNormalizeFloat4Float4(Allocation inV, Allocation out, boolean relaxed)286     private void verifyResultsFastNormalizeFloat4Float4(Allocation inV, Allocation out, boolean relaxed) {
287         float[] arrayInV = new float[INPUTSIZE * 4];
288         Arrays.fill(arrayInV, (float) 42);
289         inV.copyTo(arrayInV);
290         float[] arrayOut = new float[INPUTSIZE * 4];
291         Arrays.fill(arrayOut, (float) 42);
292         out.copyTo(arrayOut);
293         StringBuilder message = new StringBuilder();
294         boolean errorFound = false;
295         for (int i = 0; i < INPUTSIZE; i++) {
296             ArgumentsFloatNFloatN args = new ArgumentsFloatNFloatN();
297             // Create the appropriate sized arrays in args
298             args.inV = new float[4];
299             args.out = new Target.Floaty[4];
300             // Fill args with the input values
301             for (int j = 0; j < 4 ; j++) {
302                 args.inV[j] = arrayInV[i * 4 + j];
303             }
304             Target target = new Target(Target.FunctionType.FAST, Target.ReturnType.FLOAT, relaxed);
305             CoreMathVerifier.computeFastNormalize(args, target);
306 
307             // Compare the expected outputs to the actual values returned by RS.
308             boolean valid = true;
309             for (int j = 0; j < 4 ; j++) {
310                 if (!args.out[j].couldBe(arrayOut[i * 4 + j])) {
311                     valid = false;
312                 }
313             }
314             if (!valid) {
315                 if (!errorFound) {
316                     errorFound = true;
317                     for (int j = 0; j < 4 ; j++) {
318                         message.append("Input inV: ");
319                         appendVariableToMessage(message, arrayInV[i * 4 + j]);
320                         message.append("\n");
321                     }
322                     for (int j = 0; j < 4 ; j++) {
323                         message.append("Expected output out: ");
324                         appendVariableToMessage(message, args.out[j]);
325                         message.append("\n");
326                         message.append("Actual   output out: ");
327                         appendVariableToMessage(message, arrayOut[i * 4 + j]);
328                         if (!args.out[j].couldBe(arrayOut[i * 4 + j])) {
329                             message.append(" FAIL");
330                         }
331                         message.append("\n");
332                     }
333                     message.append("Errors at");
334                 }
335                 message.append(" [");
336                 message.append(Integer.toString(i));
337                 message.append("]");
338             }
339         }
340         assertFalse("Incorrect output for checkFastNormalizeFloat4Float4" +
341                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
342     }
343 
testFastNormalize()344     public void testFastNormalize() {
345         checkFastNormalizeFloatFloat();
346         checkFastNormalizeFloat2Float2();
347         checkFastNormalizeFloat3Float3();
348         checkFastNormalizeFloat4Float4();
349     }
350 }
351