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 TestFastDistance extends RSBaseCompute {
29 
30     private ScriptC_TestFastDistance script;
31     private ScriptC_TestFastDistanceRelaxed scriptRelaxed;
32 
33     @Override
setUp()34     protected void setUp() throws Exception {
35         super.setUp();
36         script = new ScriptC_TestFastDistance(mRS);
37         scriptRelaxed = new ScriptC_TestFastDistanceRelaxed(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 ArgumentsFloatFloatFloat {
48         public float inLeftVector;
49         public float inRightVector;
50         public Target.Floaty out;
51     }
52 
checkFastDistanceFloatFloatFloat()53     private void checkFastDistanceFloatFloatFloat() {
54         Allocation inLeftVector = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0xeb32e5abb9b28b1al, false);
55         Allocation inRightVector = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0x83606d459f1626e3l, false);
56         try {
57             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
58             script.set_gAllocInRightVector(inRightVector);
59             script.forEach_testFastDistanceFloatFloatFloat(inLeftVector, out);
60             verifyResultsFastDistanceFloatFloatFloat(inLeftVector, inRightVector, out, false);
61             out.destroy();
62         } catch (Exception e) {
63             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastDistanceFloatFloatFloat: " + e.toString());
64         }
65         try {
66             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
67             scriptRelaxed.set_gAllocInRightVector(inRightVector);
68             scriptRelaxed.forEach_testFastDistanceFloatFloatFloat(inLeftVector, out);
69             verifyResultsFastDistanceFloatFloatFloat(inLeftVector, inRightVector, out, true);
70             out.destroy();
71         } catch (Exception e) {
72             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastDistanceFloatFloatFloat: " + e.toString());
73         }
74         inLeftVector.destroy();
75         inRightVector.destroy();
76     }
77 
verifyResultsFastDistanceFloatFloatFloat(Allocation inLeftVector, Allocation inRightVector, Allocation out, boolean relaxed)78     private void verifyResultsFastDistanceFloatFloatFloat(Allocation inLeftVector, Allocation inRightVector, Allocation out, boolean relaxed) {
79         float[] arrayInLeftVector = new float[INPUTSIZE * 1];
80         Arrays.fill(arrayInLeftVector, (float) 42);
81         inLeftVector.copyTo(arrayInLeftVector);
82         float[] arrayInRightVector = new float[INPUTSIZE * 1];
83         Arrays.fill(arrayInRightVector, (float) 42);
84         inRightVector.copyTo(arrayInRightVector);
85         float[] arrayOut = new float[INPUTSIZE * 1];
86         Arrays.fill(arrayOut, (float) 42);
87         out.copyTo(arrayOut);
88         StringBuilder message = new StringBuilder();
89         boolean errorFound = false;
90         for (int i = 0; i < INPUTSIZE; i++) {
91             ArgumentsFloatFloatFloat args = new ArgumentsFloatFloatFloat();
92             // Create the appropriate sized arrays in args
93             // Fill args with the input values
94             args.inLeftVector = arrayInLeftVector[i];
95             args.inRightVector = arrayInRightVector[i];
96             Target target = new Target(Target.FunctionType.FAST, Target.ReturnType.FLOAT, relaxed);
97             CoreMathVerifier.computeFastDistance(args, target);
98 
99             // Compare the expected outputs to the actual values returned by RS.
100             boolean valid = true;
101             if (!args.out.couldBe(arrayOut[i])) {
102                 valid = false;
103             }
104             if (!valid) {
105                 if (!errorFound) {
106                     errorFound = true;
107                     message.append("Input inLeftVector: ");
108                     appendVariableToMessage(message, arrayInLeftVector[i]);
109                     message.append("\n");
110                     message.append("Input inRightVector: ");
111                     appendVariableToMessage(message, arrayInRightVector[i]);
112                     message.append("\n");
113                     message.append("Expected output out: ");
114                     appendVariableToMessage(message, args.out);
115                     message.append("\n");
116                     message.append("Actual   output out: ");
117                     appendVariableToMessage(message, arrayOut[i]);
118                     if (!args.out.couldBe(arrayOut[i])) {
119                         message.append(" FAIL");
120                     }
121                     message.append("\n");
122                     message.append("Errors at");
123                 }
124                 message.append(" [");
125                 message.append(Integer.toString(i));
126                 message.append("]");
127             }
128         }
129         assertFalse("Incorrect output for checkFastDistanceFloatFloatFloat" +
130                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
131     }
132 
133     public class ArgumentsFloatNFloatNFloat {
134         public float[] inLeftVector;
135         public float[] inRightVector;
136         public Target.Floaty out;
137     }
138 
checkFastDistanceFloat2Float2Float()139     private void checkFastDistanceFloat2Float2Float() {
140         Allocation inLeftVector = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 2, 0x580238eac7fabd9el, false);
141         Allocation inRightVector = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 2, 0xca3368dd0536a30fl, false);
142         try {
143             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
144             script.set_gAllocInRightVector(inRightVector);
145             script.forEach_testFastDistanceFloat2Float2Float(inLeftVector, out);
146             verifyResultsFastDistanceFloat2Float2Float(inLeftVector, inRightVector, out, false);
147             out.destroy();
148         } catch (Exception e) {
149             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastDistanceFloat2Float2Float: " + e.toString());
150         }
151         try {
152             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
153             scriptRelaxed.set_gAllocInRightVector(inRightVector);
154             scriptRelaxed.forEach_testFastDistanceFloat2Float2Float(inLeftVector, out);
155             verifyResultsFastDistanceFloat2Float2Float(inLeftVector, inRightVector, out, true);
156             out.destroy();
157         } catch (Exception e) {
158             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastDistanceFloat2Float2Float: " + e.toString());
159         }
160         inLeftVector.destroy();
161         inRightVector.destroy();
162     }
163 
verifyResultsFastDistanceFloat2Float2Float(Allocation inLeftVector, Allocation inRightVector, Allocation out, boolean relaxed)164     private void verifyResultsFastDistanceFloat2Float2Float(Allocation inLeftVector, Allocation inRightVector, Allocation out, boolean relaxed) {
165         float[] arrayInLeftVector = new float[INPUTSIZE * 2];
166         Arrays.fill(arrayInLeftVector, (float) 42);
167         inLeftVector.copyTo(arrayInLeftVector);
168         float[] arrayInRightVector = new float[INPUTSIZE * 2];
169         Arrays.fill(arrayInRightVector, (float) 42);
170         inRightVector.copyTo(arrayInRightVector);
171         float[] arrayOut = new float[INPUTSIZE * 1];
172         Arrays.fill(arrayOut, (float) 42);
173         out.copyTo(arrayOut);
174         StringBuilder message = new StringBuilder();
175         boolean errorFound = false;
176         for (int i = 0; i < INPUTSIZE; i++) {
177             ArgumentsFloatNFloatNFloat args = new ArgumentsFloatNFloatNFloat();
178             // Create the appropriate sized arrays in args
179             args.inLeftVector = new float[2];
180             args.inRightVector = new float[2];
181             // Fill args with the input values
182             for (int j = 0; j < 2 ; j++) {
183                 args.inLeftVector[j] = arrayInLeftVector[i * 2 + j];
184             }
185             for (int j = 0; j < 2 ; j++) {
186                 args.inRightVector[j] = arrayInRightVector[i * 2 + j];
187             }
188             Target target = new Target(Target.FunctionType.FAST, Target.ReturnType.FLOAT, relaxed);
189             CoreMathVerifier.computeFastDistance(args, target);
190 
191             // Compare the expected outputs to the actual values returned by RS.
192             boolean valid = true;
193             if (!args.out.couldBe(arrayOut[i])) {
194                 valid = false;
195             }
196             if (!valid) {
197                 if (!errorFound) {
198                     errorFound = true;
199                     for (int j = 0; j < 2 ; j++) {
200                         message.append("Input inLeftVector: ");
201                         appendVariableToMessage(message, arrayInLeftVector[i * 2 + j]);
202                         message.append("\n");
203                     }
204                     for (int j = 0; j < 2 ; j++) {
205                         message.append("Input inRightVector: ");
206                         appendVariableToMessage(message, arrayInRightVector[i * 2 + j]);
207                         message.append("\n");
208                     }
209                     message.append("Expected output out: ");
210                     appendVariableToMessage(message, args.out);
211                     message.append("\n");
212                     message.append("Actual   output out: ");
213                     appendVariableToMessage(message, arrayOut[i]);
214                     if (!args.out.couldBe(arrayOut[i])) {
215                         message.append(" FAIL");
216                     }
217                     message.append("\n");
218                     message.append("Errors at");
219                 }
220                 message.append(" [");
221                 message.append(Integer.toString(i));
222                 message.append("]");
223             }
224         }
225         assertFalse("Incorrect output for checkFastDistanceFloat2Float2Float" +
226                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
227     }
228 
checkFastDistanceFloat3Float3Float()229     private void checkFastDistanceFloat3Float3Float() {
230         Allocation inLeftVector = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 3, 0x26ab5e68e99971ecl, false);
231         Allocation inRightVector = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 3, 0x809cb50caadeec29l, false);
232         try {
233             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
234             script.set_gAllocInRightVector(inRightVector);
235             script.forEach_testFastDistanceFloat3Float3Float(inLeftVector, out);
236             verifyResultsFastDistanceFloat3Float3Float(inLeftVector, inRightVector, out, false);
237             out.destroy();
238         } catch (Exception e) {
239             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastDistanceFloat3Float3Float: " + e.toString());
240         }
241         try {
242             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
243             scriptRelaxed.set_gAllocInRightVector(inRightVector);
244             scriptRelaxed.forEach_testFastDistanceFloat3Float3Float(inLeftVector, out);
245             verifyResultsFastDistanceFloat3Float3Float(inLeftVector, inRightVector, out, true);
246             out.destroy();
247         } catch (Exception e) {
248             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastDistanceFloat3Float3Float: " + e.toString());
249         }
250         inLeftVector.destroy();
251         inRightVector.destroy();
252     }
253 
verifyResultsFastDistanceFloat3Float3Float(Allocation inLeftVector, Allocation inRightVector, Allocation out, boolean relaxed)254     private void verifyResultsFastDistanceFloat3Float3Float(Allocation inLeftVector, Allocation inRightVector, Allocation out, boolean relaxed) {
255         float[] arrayInLeftVector = new float[INPUTSIZE * 4];
256         Arrays.fill(arrayInLeftVector, (float) 42);
257         inLeftVector.copyTo(arrayInLeftVector);
258         float[] arrayInRightVector = new float[INPUTSIZE * 4];
259         Arrays.fill(arrayInRightVector, (float) 42);
260         inRightVector.copyTo(arrayInRightVector);
261         float[] arrayOut = new float[INPUTSIZE * 1];
262         Arrays.fill(arrayOut, (float) 42);
263         out.copyTo(arrayOut);
264         StringBuilder message = new StringBuilder();
265         boolean errorFound = false;
266         for (int i = 0; i < INPUTSIZE; i++) {
267             ArgumentsFloatNFloatNFloat args = new ArgumentsFloatNFloatNFloat();
268             // Create the appropriate sized arrays in args
269             args.inLeftVector = new float[3];
270             args.inRightVector = new float[3];
271             // Fill args with the input values
272             for (int j = 0; j < 3 ; j++) {
273                 args.inLeftVector[j] = arrayInLeftVector[i * 4 + j];
274             }
275             for (int j = 0; j < 3 ; j++) {
276                 args.inRightVector[j] = arrayInRightVector[i * 4 + j];
277             }
278             Target target = new Target(Target.FunctionType.FAST, Target.ReturnType.FLOAT, relaxed);
279             CoreMathVerifier.computeFastDistance(args, target);
280 
281             // Compare the expected outputs to the actual values returned by RS.
282             boolean valid = true;
283             if (!args.out.couldBe(arrayOut[i])) {
284                 valid = false;
285             }
286             if (!valid) {
287                 if (!errorFound) {
288                     errorFound = true;
289                     for (int j = 0; j < 3 ; j++) {
290                         message.append("Input inLeftVector: ");
291                         appendVariableToMessage(message, arrayInLeftVector[i * 4 + j]);
292                         message.append("\n");
293                     }
294                     for (int j = 0; j < 3 ; j++) {
295                         message.append("Input inRightVector: ");
296                         appendVariableToMessage(message, arrayInRightVector[i * 4 + j]);
297                         message.append("\n");
298                     }
299                     message.append("Expected output out: ");
300                     appendVariableToMessage(message, args.out);
301                     message.append("\n");
302                     message.append("Actual   output out: ");
303                     appendVariableToMessage(message, arrayOut[i]);
304                     if (!args.out.couldBe(arrayOut[i])) {
305                         message.append(" FAIL");
306                     }
307                     message.append("\n");
308                     message.append("Errors at");
309                 }
310                 message.append(" [");
311                 message.append(Integer.toString(i));
312                 message.append("]");
313             }
314         }
315         assertFalse("Incorrect output for checkFastDistanceFloat3Float3Float" +
316                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
317     }
318 
checkFastDistanceFloat4Float4Float()319     private void checkFastDistanceFloat4Float4Float() {
320         Allocation inLeftVector = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 4, 0xf55483e70b38263al, false);
321         Allocation inRightVector = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 4, 0x3706013c50873543l, false);
322         try {
323             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
324             script.set_gAllocInRightVector(inRightVector);
325             script.forEach_testFastDistanceFloat4Float4Float(inLeftVector, out);
326             verifyResultsFastDistanceFloat4Float4Float(inLeftVector, inRightVector, out, false);
327             out.destroy();
328         } catch (Exception e) {
329             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastDistanceFloat4Float4Float: " + e.toString());
330         }
331         try {
332             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
333             scriptRelaxed.set_gAllocInRightVector(inRightVector);
334             scriptRelaxed.forEach_testFastDistanceFloat4Float4Float(inLeftVector, out);
335             verifyResultsFastDistanceFloat4Float4Float(inLeftVector, inRightVector, out, true);
336             out.destroy();
337         } catch (Exception e) {
338             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastDistanceFloat4Float4Float: " + e.toString());
339         }
340         inLeftVector.destroy();
341         inRightVector.destroy();
342     }
343 
verifyResultsFastDistanceFloat4Float4Float(Allocation inLeftVector, Allocation inRightVector, Allocation out, boolean relaxed)344     private void verifyResultsFastDistanceFloat4Float4Float(Allocation inLeftVector, Allocation inRightVector, Allocation out, boolean relaxed) {
345         float[] arrayInLeftVector = new float[INPUTSIZE * 4];
346         Arrays.fill(arrayInLeftVector, (float) 42);
347         inLeftVector.copyTo(arrayInLeftVector);
348         float[] arrayInRightVector = new float[INPUTSIZE * 4];
349         Arrays.fill(arrayInRightVector, (float) 42);
350         inRightVector.copyTo(arrayInRightVector);
351         float[] arrayOut = new float[INPUTSIZE * 1];
352         Arrays.fill(arrayOut, (float) 42);
353         out.copyTo(arrayOut);
354         StringBuilder message = new StringBuilder();
355         boolean errorFound = false;
356         for (int i = 0; i < INPUTSIZE; i++) {
357             ArgumentsFloatNFloatNFloat args = new ArgumentsFloatNFloatNFloat();
358             // Create the appropriate sized arrays in args
359             args.inLeftVector = new float[4];
360             args.inRightVector = new float[4];
361             // Fill args with the input values
362             for (int j = 0; j < 4 ; j++) {
363                 args.inLeftVector[j] = arrayInLeftVector[i * 4 + j];
364             }
365             for (int j = 0; j < 4 ; j++) {
366                 args.inRightVector[j] = arrayInRightVector[i * 4 + j];
367             }
368             Target target = new Target(Target.FunctionType.FAST, Target.ReturnType.FLOAT, relaxed);
369             CoreMathVerifier.computeFastDistance(args, target);
370 
371             // Compare the expected outputs to the actual values returned by RS.
372             boolean valid = true;
373             if (!args.out.couldBe(arrayOut[i])) {
374                 valid = false;
375             }
376             if (!valid) {
377                 if (!errorFound) {
378                     errorFound = true;
379                     for (int j = 0; j < 4 ; j++) {
380                         message.append("Input inLeftVector: ");
381                         appendVariableToMessage(message, arrayInLeftVector[i * 4 + j]);
382                         message.append("\n");
383                     }
384                     for (int j = 0; j < 4 ; j++) {
385                         message.append("Input inRightVector: ");
386                         appendVariableToMessage(message, arrayInRightVector[i * 4 + j]);
387                         message.append("\n");
388                     }
389                     message.append("Expected output out: ");
390                     appendVariableToMessage(message, args.out);
391                     message.append("\n");
392                     message.append("Actual   output out: ");
393                     appendVariableToMessage(message, arrayOut[i]);
394                     if (!args.out.couldBe(arrayOut[i])) {
395                         message.append(" FAIL");
396                     }
397                     message.append("\n");
398                     message.append("Errors at");
399                 }
400                 message.append(" [");
401                 message.append(Integer.toString(i));
402                 message.append("]");
403             }
404         }
405         assertFalse("Incorrect output for checkFastDistanceFloat4Float4Float" +
406                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
407     }
408 
testFastDistance()409     public void testFastDistance() {
410         checkFastDistanceFloatFloatFloat();
411         checkFastDistanceFloat2Float2Float();
412         checkFastDistanceFloat3Float3Float();
413         checkFastDistanceFloat4Float4Float();
414     }
415 }
416