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