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