Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2014 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/gen_runtime.
     18 
     19 package android.renderscript.cts;
     20 
     21 import android.renderscript.Allocation;
     22 import android.renderscript.RSRuntimeException;
     23 import android.renderscript.Element;
     24 
     25 public class TestFastLength extends RSBaseCompute {
     26 
     27     private ScriptC_TestFastLength script;
     28     private ScriptC_TestFastLengthRelaxed scriptRelaxed;
     29 
     30     @Override
     31     protected void setUp() throws Exception {
     32         super.setUp();
     33         script = new ScriptC_TestFastLength(mRS);
     34         scriptRelaxed = new ScriptC_TestFastLengthRelaxed(mRS);
     35     }
     36 
     37     public class ArgumentsFloatFloat {
     38         public float inV;
     39         public Target.Floaty out;
     40     }
     41 
     42     private void checkFastLengthFloatFloat() {
     43         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0xebac65aea2660e8fl, false);
     44         try {
     45             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
     46             script.forEach_testFastLengthFloatFloat(inV, out);
     47             verifyResultsFastLengthFloatFloat(inV, out, false);
     48         } catch (Exception e) {
     49             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastLengthFloatFloat: " + e.toString());
     50         }
     51         try {
     52             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
     53             scriptRelaxed.forEach_testFastLengthFloatFloat(inV, out);
     54             verifyResultsFastLengthFloatFloat(inV, out, true);
     55         } catch (Exception e) {
     56             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastLengthFloatFloat: " + e.toString());
     57         }
     58     }
     59 
     60     private void verifyResultsFastLengthFloatFloat(Allocation inV, Allocation out, boolean relaxed) {
     61         float[] arrayInV = new float[INPUTSIZE * 1];
     62         inV.copyTo(arrayInV);
     63         float[] arrayOut = new float[INPUTSIZE * 1];
     64         out.copyTo(arrayOut);
     65         for (int i = 0; i < INPUTSIZE; i++) {
     66             ArgumentsFloatFloat args = new ArgumentsFloatFloat();
     67             // Create the appropriate sized arrays in args
     68             // Fill args with the input values
     69             args.inV = arrayInV[i];
     70             Target target = new Target(relaxed);
     71             CoreMathVerifier.computeFastLength(args, target);
     72 
     73             // Compare the expected outputs to the actual values returned by RS.
     74             boolean valid = true;
     75             if (!args.out.couldBe(arrayOut[i])) {
     76                 valid = false;
     77             }
     78             if (!valid) {
     79                 StringBuilder message = new StringBuilder();
     80                 message.append("Input inV: ");
     81                 message.append(String.format("%14.8g {%8x} %15a",
     82                         arrayInV[i], Float.floatToRawIntBits(arrayInV[i]), arrayInV[i]));
     83                 message.append("\n");
     84                 message.append("Expected output out: ");
     85                 message.append(args.out.toString());
     86                 message.append("\n");
     87                 message.append("Actual   output out: ");
     88                 message.append(String.format("%14.8g {%8x} %15a",
     89                         arrayOut[i], Float.floatToRawIntBits(arrayOut[i]), arrayOut[i]));
     90                 if (!args.out.couldBe(arrayOut[i])) {
     91                     message.append(" FAIL");
     92                 }
     93                 message.append("\n");
     94                 assertTrue("Incorrect output for checkFastLengthFloatFloat" +
     95                         (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), valid);
     96             }
     97         }
     98     }
     99 
    100     public class ArgumentsFloatNFloat {
    101         public float[] inV;
    102         public Target.Floaty out;
    103     }
    104 
    105     private void checkFastLengthFloat2Float() {
    106         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 2, 0x95f43650f85e6cadl, false);
    107         try {
    108             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
    109             script.forEach_testFastLengthFloat2Float(inV, out);
    110             verifyResultsFastLengthFloat2Float(inV, out, false);
    111         } catch (Exception e) {
    112             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastLengthFloat2Float: " + e.toString());
    113         }
    114         try {
    115             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
    116             scriptRelaxed.forEach_testFastLengthFloat2Float(inV, out);
    117             verifyResultsFastLengthFloat2Float(inV, out, true);
    118         } catch (Exception e) {
    119             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastLengthFloat2Float: " + e.toString());
    120         }
    121     }
    122 
    123     private void verifyResultsFastLengthFloat2Float(Allocation inV, Allocation out, boolean relaxed) {
    124         float[] arrayInV = new float[INPUTSIZE * 2];
    125         inV.copyTo(arrayInV);
    126         float[] arrayOut = new float[INPUTSIZE * 1];
    127         out.copyTo(arrayOut);
    128         for (int i = 0; i < INPUTSIZE; i++) {
    129             ArgumentsFloatNFloat args = new ArgumentsFloatNFloat();
    130             // Create the appropriate sized arrays in args
    131             args.inV = new float[2];
    132             // Fill args with the input values
    133             for (int j = 0; j < 2 ; j++) {
    134                 args.inV[j] = arrayInV[i * 2 + j];
    135             }
    136             Target target = new Target(relaxed);
    137             CoreMathVerifier.computeFastLength(args, target);
    138 
    139             // Compare the expected outputs to the actual values returned by RS.
    140             boolean valid = true;
    141             if (!args.out.couldBe(arrayOut[i])) {
    142                 valid = false;
    143             }
    144             if (!valid) {
    145                 StringBuilder message = new StringBuilder();
    146                 for (int j = 0; j < 2 ; j++) {
    147                     message.append("Input inV: ");
    148                     message.append(String.format("%14.8g {%8x} %15a",
    149                             arrayInV[i * 2 + j], Float.floatToRawIntBits(arrayInV[i * 2 + j]), arrayInV[i * 2 + j]));
    150                     message.append("\n");
    151                 }
    152                 message.append("Expected output out: ");
    153                 message.append(args.out.toString());
    154                 message.append("\n");
    155                 message.append("Actual   output out: ");
    156                 message.append(String.format("%14.8g {%8x} %15a",
    157                         arrayOut[i], Float.floatToRawIntBits(arrayOut[i]), arrayOut[i]));
    158                 if (!args.out.couldBe(arrayOut[i])) {
    159                     message.append(" FAIL");
    160                 }
    161                 message.append("\n");
    162                 assertTrue("Incorrect output for checkFastLengthFloat2Float" +
    163                         (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), valid);
    164             }
    165         }
    166     }
    167 
    168     private void checkFastLengthFloat3Float() {
    169         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 3, 0x95f440f25764fb0el, false);
    170         try {
    171             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
    172             script.forEach_testFastLengthFloat3Float(inV, out);
    173             verifyResultsFastLengthFloat3Float(inV, out, false);
    174         } catch (Exception e) {
    175             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastLengthFloat3Float: " + e.toString());
    176         }
    177         try {
    178             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
    179             scriptRelaxed.forEach_testFastLengthFloat3Float(inV, out);
    180             verifyResultsFastLengthFloat3Float(inV, out, true);
    181         } catch (Exception e) {
    182             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastLengthFloat3Float: " + e.toString());
    183         }
    184     }
    185 
    186     private void verifyResultsFastLengthFloat3Float(Allocation inV, Allocation out, boolean relaxed) {
    187         float[] arrayInV = new float[INPUTSIZE * 4];
    188         inV.copyTo(arrayInV);
    189         float[] arrayOut = new float[INPUTSIZE * 1];
    190         out.copyTo(arrayOut);
    191         for (int i = 0; i < INPUTSIZE; i++) {
    192             ArgumentsFloatNFloat args = new ArgumentsFloatNFloat();
    193             // Create the appropriate sized arrays in args
    194             args.inV = new float[3];
    195             // Fill args with the input values
    196             for (int j = 0; j < 3 ; j++) {
    197                 args.inV[j] = arrayInV[i * 4 + j];
    198             }
    199             Target target = new Target(relaxed);
    200             CoreMathVerifier.computeFastLength(args, target);
    201 
    202             // Compare the expected outputs to the actual values returned by RS.
    203             boolean valid = true;
    204             if (!args.out.couldBe(arrayOut[i])) {
    205                 valid = false;
    206             }
    207             if (!valid) {
    208                 StringBuilder message = new StringBuilder();
    209                 for (int j = 0; j < 3 ; j++) {
    210                     message.append("Input inV: ");
    211                     message.append(String.format("%14.8g {%8x} %15a",
    212                             arrayInV[i * 4 + j], Float.floatToRawIntBits(arrayInV[i * 4 + j]), arrayInV[i * 4 + j]));
    213                     message.append("\n");
    214                 }
    215                 message.append("Expected output out: ");
    216                 message.append(args.out.toString());
    217                 message.append("\n");
    218                 message.append("Actual   output out: ");
    219                 message.append(String.format("%14.8g {%8x} %15a",
    220                         arrayOut[i], Float.floatToRawIntBits(arrayOut[i]), arrayOut[i]));
    221                 if (!args.out.couldBe(arrayOut[i])) {
    222                     message.append(" FAIL");
    223                 }
    224                 message.append("\n");
    225                 assertTrue("Incorrect output for checkFastLengthFloat3Float" +
    226                         (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), valid);
    227             }
    228         }
    229     }
    230 
    231     private void checkFastLengthFloat4Float() {
    232         Allocation inV = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 4, 0x95f44b93b66b896fl, false);
    233         try {
    234             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
    235             script.forEach_testFastLengthFloat4Float(inV, out);
    236             verifyResultsFastLengthFloat4Float(inV, out, false);
    237         } catch (Exception e) {
    238             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastLengthFloat4Float: " + e.toString());
    239         }
    240         try {
    241             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
    242             scriptRelaxed.forEach_testFastLengthFloat4Float(inV, out);
    243             verifyResultsFastLengthFloat4Float(inV, out, true);
    244         } catch (Exception e) {
    245             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testFastLengthFloat4Float: " + e.toString());
    246         }
    247     }
    248 
    249     private void verifyResultsFastLengthFloat4Float(Allocation inV, Allocation out, boolean relaxed) {
    250         float[] arrayInV = new float[INPUTSIZE * 4];
    251         inV.copyTo(arrayInV);
    252         float[] arrayOut = new float[INPUTSIZE * 1];
    253         out.copyTo(arrayOut);
    254         for (int i = 0; i < INPUTSIZE; i++) {
    255             ArgumentsFloatNFloat args = new ArgumentsFloatNFloat();
    256             // Create the appropriate sized arrays in args
    257             args.inV = new float[4];
    258             // Fill args with the input values
    259             for (int j = 0; j < 4 ; j++) {
    260                 args.inV[j] = arrayInV[i * 4 + j];
    261             }
    262             Target target = new Target(relaxed);
    263             CoreMathVerifier.computeFastLength(args, target);
    264 
    265             // Compare the expected outputs to the actual values returned by RS.
    266             boolean valid = true;
    267             if (!args.out.couldBe(arrayOut[i])) {
    268                 valid = false;
    269             }
    270             if (!valid) {
    271                 StringBuilder message = new StringBuilder();
    272                 for (int j = 0; j < 4 ; j++) {
    273                     message.append("Input inV: ");
    274                     message.append(String.format("%14.8g {%8x} %15a",
    275                             arrayInV[i * 4 + j], Float.floatToRawIntBits(arrayInV[i * 4 + j]), arrayInV[i * 4 + j]));
    276                     message.append("\n");
    277                 }
    278                 message.append("Expected output out: ");
    279                 message.append(args.out.toString());
    280                 message.append("\n");
    281                 message.append("Actual   output out: ");
    282                 message.append(String.format("%14.8g {%8x} %15a",
    283                         arrayOut[i], Float.floatToRawIntBits(arrayOut[i]), arrayOut[i]));
    284                 if (!args.out.couldBe(arrayOut[i])) {
    285                     message.append(" FAIL");
    286                 }
    287                 message.append("\n");
    288                 assertTrue("Incorrect output for checkFastLengthFloat4Float" +
    289                         (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), valid);
    290             }
    291         }
    292     }
    293 
    294     public void testFastLength() {
    295         checkFastLengthFloatFloat();
    296         checkFastLengthFloat2Float();
    297         checkFastLengthFloat3Float();
    298         checkFastLengthFloat4Float();
    299     }
    300 }
    301