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 TestIlogb extends RSBaseCompute {
29 
30     private ScriptC_TestIlogb script;
31     private ScriptC_TestIlogbRelaxed scriptRelaxed;
32 
33     @Override
setUp()34     protected void setUp() throws Exception {
35         super.setUp();
36         script = new ScriptC_TestIlogb(mRS);
37         scriptRelaxed = new ScriptC_TestIlogbRelaxed(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 ArgumentsFloatInt {
48         public float inV;
49         public int out;
50     }
51 
checkIlogbFloatInt()52     private void checkIlogbFloatInt() {
53         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0x6103ca4b5664967bl, false);
54         try {
55             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.SIGNED_32, 1), INPUTSIZE);
56             script.forEach_testIlogbFloatInt(inV, out);
57             verifyResultsIlogbFloatInt(inV, out, false);
58             out.destroy();
59         } catch (Exception e) {
60             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testIlogbFloatInt: " + e.toString());
61         }
62         try {
63             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.SIGNED_32, 1), INPUTSIZE);
64             scriptRelaxed.forEach_testIlogbFloatInt(inV, out);
65             verifyResultsIlogbFloatInt(inV, out, true);
66             out.destroy();
67         } catch (Exception e) {
68             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testIlogbFloatInt: " + e.toString());
69         }
70         inV.destroy();
71     }
72 
verifyResultsIlogbFloatInt(Allocation inV, Allocation out, boolean relaxed)73     private void verifyResultsIlogbFloatInt(Allocation inV, Allocation out, boolean relaxed) {
74         float[] arrayInV = new float[INPUTSIZE * 1];
75         Arrays.fill(arrayInV, (float) 42);
76         inV.copyTo(arrayInV);
77         int[] arrayOut = new int[INPUTSIZE * 1];
78         Arrays.fill(arrayOut, (int) 42);
79         out.copyTo(arrayOut);
80         StringBuilder message = new StringBuilder();
81         boolean errorFound = false;
82         for (int i = 0; i < INPUTSIZE; i++) {
83             for (int j = 0; j < 1 ; j++) {
84                 // Extract the inputs.
85                 ArgumentsFloatInt args = new ArgumentsFloatInt();
86                 args.inV = arrayInV[i];
87                 // Extract the outputs.
88                 args.out = arrayOut[i * 1 + j];
89                 // Ask the CoreMathVerifier to validate.
90                 String errorMessage = CoreMathVerifier.verifyIlogb(args);
91                 boolean valid = errorMessage == null;
92                 if (!valid) {
93                     if (!errorFound) {
94                         errorFound = true;
95                         message.append("Input inV: ");
96                         appendVariableToMessage(message, args.inV);
97                         message.append("\n");
98                         message.append("Output out: ");
99                         appendVariableToMessage(message, args.out);
100                         message.append("\n");
101                         message.append(errorMessage);
102                         message.append("Errors at");
103                     }
104                     message.append(" [");
105                     message.append(Integer.toString(i));
106                     message.append(", ");
107                     message.append(Integer.toString(j));
108                     message.append("]");
109                 }
110             }
111         }
112         assertFalse("Incorrect output for checkIlogbFloatInt" +
113                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
114     }
115 
checkIlogbFloat2Int2()116     private void checkIlogbFloat2Int2() {
117         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 2, 0xb460143cb6f32a61l, false);
118         try {
119             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.SIGNED_32, 2), INPUTSIZE);
120             script.forEach_testIlogbFloat2Int2(inV, out);
121             verifyResultsIlogbFloat2Int2(inV, out, false);
122             out.destroy();
123         } catch (Exception e) {
124             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testIlogbFloat2Int2: " + e.toString());
125         }
126         try {
127             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.SIGNED_32, 2), INPUTSIZE);
128             scriptRelaxed.forEach_testIlogbFloat2Int2(inV, out);
129             verifyResultsIlogbFloat2Int2(inV, out, true);
130             out.destroy();
131         } catch (Exception e) {
132             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testIlogbFloat2Int2: " + e.toString());
133         }
134         inV.destroy();
135     }
136 
verifyResultsIlogbFloat2Int2(Allocation inV, Allocation out, boolean relaxed)137     private void verifyResultsIlogbFloat2Int2(Allocation inV, Allocation out, boolean relaxed) {
138         float[] arrayInV = new float[INPUTSIZE * 2];
139         Arrays.fill(arrayInV, (float) 42);
140         inV.copyTo(arrayInV);
141         int[] arrayOut = new int[INPUTSIZE * 2];
142         Arrays.fill(arrayOut, (int) 42);
143         out.copyTo(arrayOut);
144         StringBuilder message = new StringBuilder();
145         boolean errorFound = false;
146         for (int i = 0; i < INPUTSIZE; i++) {
147             for (int j = 0; j < 2 ; j++) {
148                 // Extract the inputs.
149                 ArgumentsFloatInt args = new ArgumentsFloatInt();
150                 args.inV = arrayInV[i * 2 + j];
151                 // Extract the outputs.
152                 args.out = arrayOut[i * 2 + j];
153                 // Ask the CoreMathVerifier to validate.
154                 String errorMessage = CoreMathVerifier.verifyIlogb(args);
155                 boolean valid = errorMessage == null;
156                 if (!valid) {
157                     if (!errorFound) {
158                         errorFound = true;
159                         message.append("Input inV: ");
160                         appendVariableToMessage(message, args.inV);
161                         message.append("\n");
162                         message.append("Output out: ");
163                         appendVariableToMessage(message, args.out);
164                         message.append("\n");
165                         message.append(errorMessage);
166                         message.append("Errors at");
167                     }
168                     message.append(" [");
169                     message.append(Integer.toString(i));
170                     message.append(", ");
171                     message.append(Integer.toString(j));
172                     message.append("]");
173                 }
174             }
175         }
176         assertFalse("Incorrect output for checkIlogbFloat2Int2" +
177                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
178     }
179 
checkIlogbFloat3Int3()180     private void checkIlogbFloat3Int3() {
181         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 3, 0xb460147c009b3a97l, false);
182         try {
183             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.SIGNED_32, 3), INPUTSIZE);
184             script.forEach_testIlogbFloat3Int3(inV, out);
185             verifyResultsIlogbFloat3Int3(inV, out, false);
186             out.destroy();
187         } catch (Exception e) {
188             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testIlogbFloat3Int3: " + e.toString());
189         }
190         try {
191             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.SIGNED_32, 3), INPUTSIZE);
192             scriptRelaxed.forEach_testIlogbFloat3Int3(inV, out);
193             verifyResultsIlogbFloat3Int3(inV, out, true);
194             out.destroy();
195         } catch (Exception e) {
196             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testIlogbFloat3Int3: " + e.toString());
197         }
198         inV.destroy();
199     }
200 
verifyResultsIlogbFloat3Int3(Allocation inV, Allocation out, boolean relaxed)201     private void verifyResultsIlogbFloat3Int3(Allocation inV, Allocation out, boolean relaxed) {
202         float[] arrayInV = new float[INPUTSIZE * 4];
203         Arrays.fill(arrayInV, (float) 42);
204         inV.copyTo(arrayInV);
205         int[] arrayOut = new int[INPUTSIZE * 4];
206         Arrays.fill(arrayOut, (int) 42);
207         out.copyTo(arrayOut);
208         StringBuilder message = new StringBuilder();
209         boolean errorFound = false;
210         for (int i = 0; i < INPUTSIZE; i++) {
211             for (int j = 0; j < 3 ; j++) {
212                 // Extract the inputs.
213                 ArgumentsFloatInt args = new ArgumentsFloatInt();
214                 args.inV = arrayInV[i * 4 + j];
215                 // Extract the outputs.
216                 args.out = arrayOut[i * 4 + j];
217                 // Ask the CoreMathVerifier to validate.
218                 String errorMessage = CoreMathVerifier.verifyIlogb(args);
219                 boolean valid = errorMessage == null;
220                 if (!valid) {
221                     if (!errorFound) {
222                         errorFound = true;
223                         message.append("Input inV: ");
224                         appendVariableToMessage(message, args.inV);
225                         message.append("\n");
226                         message.append("Output out: ");
227                         appendVariableToMessage(message, args.out);
228                         message.append("\n");
229                         message.append(errorMessage);
230                         message.append("Errors at");
231                     }
232                     message.append(" [");
233                     message.append(Integer.toString(i));
234                     message.append(", ");
235                     message.append(Integer.toString(j));
236                     message.append("]");
237                 }
238             }
239         }
240         assertFalse("Incorrect output for checkIlogbFloat3Int3" +
241                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
242     }
243 
checkIlogbFloat4Int4()244     private void checkIlogbFloat4Int4() {
245         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 4, 0xb46014bb4a434acdl, false);
246         try {
247             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.SIGNED_32, 4), INPUTSIZE);
248             script.forEach_testIlogbFloat4Int4(inV, out);
249             verifyResultsIlogbFloat4Int4(inV, out, false);
250             out.destroy();
251         } catch (Exception e) {
252             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testIlogbFloat4Int4: " + e.toString());
253         }
254         try {
255             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.SIGNED_32, 4), INPUTSIZE);
256             scriptRelaxed.forEach_testIlogbFloat4Int4(inV, out);
257             verifyResultsIlogbFloat4Int4(inV, out, true);
258             out.destroy();
259         } catch (Exception e) {
260             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testIlogbFloat4Int4: " + e.toString());
261         }
262         inV.destroy();
263     }
264 
verifyResultsIlogbFloat4Int4(Allocation inV, Allocation out, boolean relaxed)265     private void verifyResultsIlogbFloat4Int4(Allocation inV, Allocation out, boolean relaxed) {
266         float[] arrayInV = new float[INPUTSIZE * 4];
267         Arrays.fill(arrayInV, (float) 42);
268         inV.copyTo(arrayInV);
269         int[] arrayOut = new int[INPUTSIZE * 4];
270         Arrays.fill(arrayOut, (int) 42);
271         out.copyTo(arrayOut);
272         StringBuilder message = new StringBuilder();
273         boolean errorFound = false;
274         for (int i = 0; i < INPUTSIZE; i++) {
275             for (int j = 0; j < 4 ; j++) {
276                 // Extract the inputs.
277                 ArgumentsFloatInt args = new ArgumentsFloatInt();
278                 args.inV = arrayInV[i * 4 + j];
279                 // Extract the outputs.
280                 args.out = arrayOut[i * 4 + j];
281                 // Ask the CoreMathVerifier to validate.
282                 String errorMessage = CoreMathVerifier.verifyIlogb(args);
283                 boolean valid = errorMessage == null;
284                 if (!valid) {
285                     if (!errorFound) {
286                         errorFound = true;
287                         message.append("Input inV: ");
288                         appendVariableToMessage(message, args.inV);
289                         message.append("\n");
290                         message.append("Output out: ");
291                         appendVariableToMessage(message, args.out);
292                         message.append("\n");
293                         message.append(errorMessage);
294                         message.append("Errors at");
295                     }
296                     message.append(" [");
297                     message.append(Integer.toString(i));
298                     message.append(", ");
299                     message.append(Integer.toString(j));
300                     message.append("]");
301                 }
302             }
303         }
304         assertFalse("Incorrect output for checkIlogbFloat4Int4" +
305                 (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), errorFound);
306     }
307 
testIlogb()308     public void testIlogb() {
309         checkIlogbFloatInt();
310         checkIlogbFloat2Int2();
311         checkIlogbFloat3Int3();
312         checkIlogbFloat4Int4();
313     }
314 }
315