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 TestNativeDistance extends RSBaseCompute {
     26 
     27     private ScriptC_TestNativeDistance script;
     28     private ScriptC_TestNativeDistanceRelaxed scriptRelaxed;
     29 
     30     @Override
     31     protected void setUp() throws Exception {
     32         super.setUp();
     33         script = new ScriptC_TestNativeDistance(mRS);
     34         scriptRelaxed = new ScriptC_TestNativeDistanceRelaxed(mRS);
     35     }
     36 
     37     public class ArgumentsFloatFloatFloat {
     38         public float inLhs;
     39         public float inRhs;
     40         public Target.Floaty out;
     41     }
     42 
     43     private void checkNativeDistanceFloatFloatFloat() {
     44         Allocation inLhs = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0xe0fd4252f8556ff6l, false);
     45         Allocation inRhs = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 1, 0xe0fd4252f8559b4cl, false);
     46         try {
     47             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
     48             script.set_gAllocInRhs(inRhs);
     49             script.forEach_testNativeDistanceFloatFloatFloat(inLhs, out);
     50             verifyResultsNativeDistanceFloatFloatFloat(inLhs, inRhs, out, false);
     51         } catch (Exception e) {
     52             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNativeDistanceFloatFloatFloat: " + e.toString());
     53         }
     54         try {
     55             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
     56             scriptRelaxed.set_gAllocInRhs(inRhs);
     57             scriptRelaxed.forEach_testNativeDistanceFloatFloatFloat(inLhs, out);
     58             verifyResultsNativeDistanceFloatFloatFloat(inLhs, inRhs, out, true);
     59         } catch (Exception e) {
     60             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNativeDistanceFloatFloatFloat: " + e.toString());
     61         }
     62     }
     63 
     64     private void verifyResultsNativeDistanceFloatFloatFloat(Allocation inLhs, Allocation inRhs, Allocation out, boolean relaxed) {
     65         float[] arrayInLhs = new float[INPUTSIZE * 1];
     66         inLhs.copyTo(arrayInLhs);
     67         float[] arrayInRhs = new float[INPUTSIZE * 1];
     68         inRhs.copyTo(arrayInRhs);
     69         float[] arrayOut = new float[INPUTSIZE * 1];
     70         out.copyTo(arrayOut);
     71         for (int i = 0; i < INPUTSIZE; i++) {
     72             ArgumentsFloatFloatFloat args = new ArgumentsFloatFloatFloat();
     73             // Create the appropriate sized arrays in args
     74             // Fill args with the input values
     75             args.inLhs = arrayInLhs[i];
     76             args.inRhs = arrayInRhs[i];
     77             Target target = new Target(relaxed);
     78             CoreMathVerifier.computeNativeDistance(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                 StringBuilder message = new StringBuilder();
     87                 message.append("Input inLhs: ");
     88                 message.append(String.format("%14.8g {%8x} %15a",
     89                         arrayInLhs[i], Float.floatToRawIntBits(arrayInLhs[i]), arrayInLhs[i]));
     90                 message.append("\n");
     91                 message.append("Input inRhs: ");
     92                 message.append(String.format("%14.8g {%8x} %15a",
     93                         arrayInRhs[i], Float.floatToRawIntBits(arrayInRhs[i]), arrayInRhs[i]));
     94                 message.append("\n");
     95                 message.append("Expected output out: ");
     96                 message.append(args.out.toString());
     97                 message.append("\n");
     98                 message.append("Actual   output out: ");
     99                 message.append(String.format("%14.8g {%8x} %15a",
    100                         arrayOut[i], Float.floatToRawIntBits(arrayOut[i]), arrayOut[i]));
    101                 if (!args.out.couldBe(arrayOut[i])) {
    102                     message.append(" FAIL");
    103                 }
    104                 message.append("\n");
    105                 assertTrue("Incorrect output for checkNativeDistanceFloatFloatFloat" +
    106                         (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), valid);
    107             }
    108         }
    109     }
    110 
    111     public class ArgumentsFloatNFloatNFloat {
    112         public float[] inLhs;
    113         public float[] inRhs;
    114         public Target.Floaty out;
    115     }
    116 
    117     private void checkNativeDistanceFloat2Float2Float() {
    118         Allocation inLhs = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 2, 0x28a9ea2ea1fd926al, false);
    119         Allocation inRhs = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 2, 0x28a9ea2ea1fdbdc0l, false);
    120         try {
    121             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
    122             script.set_gAllocInRhs(inRhs);
    123             script.forEach_testNativeDistanceFloat2Float2Float(inLhs, out);
    124             verifyResultsNativeDistanceFloat2Float2Float(inLhs, inRhs, out, false);
    125         } catch (Exception e) {
    126             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNativeDistanceFloat2Float2Float: " + e.toString());
    127         }
    128         try {
    129             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
    130             scriptRelaxed.set_gAllocInRhs(inRhs);
    131             scriptRelaxed.forEach_testNativeDistanceFloat2Float2Float(inLhs, out);
    132             verifyResultsNativeDistanceFloat2Float2Float(inLhs, inRhs, out, true);
    133         } catch (Exception e) {
    134             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNativeDistanceFloat2Float2Float: " + e.toString());
    135         }
    136     }
    137 
    138     private void verifyResultsNativeDistanceFloat2Float2Float(Allocation inLhs, Allocation inRhs, Allocation out, boolean relaxed) {
    139         float[] arrayInLhs = new float[INPUTSIZE * 2];
    140         inLhs.copyTo(arrayInLhs);
    141         float[] arrayInRhs = new float[INPUTSIZE * 2];
    142         inRhs.copyTo(arrayInRhs);
    143         float[] arrayOut = new float[INPUTSIZE * 1];
    144         out.copyTo(arrayOut);
    145         for (int i = 0; i < INPUTSIZE; i++) {
    146             ArgumentsFloatNFloatNFloat args = new ArgumentsFloatNFloatNFloat();
    147             // Create the appropriate sized arrays in args
    148             args.inLhs = new float[2];
    149             args.inRhs = new float[2];
    150             // Fill args with the input values
    151             for (int j = 0; j < 2 ; j++) {
    152                 args.inLhs[j] = arrayInLhs[i * 2 + j];
    153             }
    154             for (int j = 0; j < 2 ; j++) {
    155                 args.inRhs[j] = arrayInRhs[i * 2 + j];
    156             }
    157             Target target = new Target(relaxed);
    158             CoreMathVerifier.computeNativeDistance(args, target);
    159 
    160             // Compare the expected outputs to the actual values returned by RS.
    161             boolean valid = true;
    162             if (!args.out.couldBe(arrayOut[i])) {
    163                 valid = false;
    164             }
    165             if (!valid) {
    166                 StringBuilder message = new StringBuilder();
    167                 for (int j = 0; j < 2 ; j++) {
    168                     message.append("Input inLhs: ");
    169                     message.append(String.format("%14.8g {%8x} %15a",
    170                             arrayInLhs[i * 2 + j], Float.floatToRawIntBits(arrayInLhs[i * 2 + j]), arrayInLhs[i * 2 + j]));
    171                     message.append("\n");
    172                 }
    173                 for (int j = 0; j < 2 ; j++) {
    174                     message.append("Input inRhs: ");
    175                     message.append(String.format("%14.8g {%8x} %15a",
    176                             arrayInRhs[i * 2 + j], Float.floatToRawIntBits(arrayInRhs[i * 2 + j]), arrayInRhs[i * 2 + j]));
    177                     message.append("\n");
    178                 }
    179                 message.append("Expected output out: ");
    180                 message.append(args.out.toString());
    181                 message.append("\n");
    182                 message.append("Actual   output out: ");
    183                 message.append(String.format("%14.8g {%8x} %15a",
    184                         arrayOut[i], Float.floatToRawIntBits(arrayOut[i]), arrayOut[i]));
    185                 if (!args.out.couldBe(arrayOut[i])) {
    186                     message.append(" FAIL");
    187                 }
    188                 message.append("\n");
    189                 assertTrue("Incorrect output for checkNativeDistanceFloat2Float2Float" +
    190                         (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), valid);
    191             }
    192         }
    193     }
    194 
    195     private void checkNativeDistanceFloat3Float3Float() {
    196         Allocation inLhs = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 3, 0x559b398ef213adc4l, false);
    197         Allocation inRhs = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 3, 0x559b398ef213d91al, false);
    198         try {
    199             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
    200             script.set_gAllocInRhs(inRhs);
    201             script.forEach_testNativeDistanceFloat3Float3Float(inLhs, out);
    202             verifyResultsNativeDistanceFloat3Float3Float(inLhs, inRhs, out, false);
    203         } catch (Exception e) {
    204             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNativeDistanceFloat3Float3Float: " + e.toString());
    205         }
    206         try {
    207             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
    208             scriptRelaxed.set_gAllocInRhs(inRhs);
    209             scriptRelaxed.forEach_testNativeDistanceFloat3Float3Float(inLhs, out);
    210             verifyResultsNativeDistanceFloat3Float3Float(inLhs, inRhs, out, true);
    211         } catch (Exception e) {
    212             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNativeDistanceFloat3Float3Float: " + e.toString());
    213         }
    214     }
    215 
    216     private void verifyResultsNativeDistanceFloat3Float3Float(Allocation inLhs, Allocation inRhs, Allocation out, boolean relaxed) {
    217         float[] arrayInLhs = new float[INPUTSIZE * 4];
    218         inLhs.copyTo(arrayInLhs);
    219         float[] arrayInRhs = new float[INPUTSIZE * 4];
    220         inRhs.copyTo(arrayInRhs);
    221         float[] arrayOut = new float[INPUTSIZE * 1];
    222         out.copyTo(arrayOut);
    223         for (int i = 0; i < INPUTSIZE; i++) {
    224             ArgumentsFloatNFloatNFloat args = new ArgumentsFloatNFloatNFloat();
    225             // Create the appropriate sized arrays in args
    226             args.inLhs = new float[3];
    227             args.inRhs = new float[3];
    228             // Fill args with the input values
    229             for (int j = 0; j < 3 ; j++) {
    230                 args.inLhs[j] = arrayInLhs[i * 4 + j];
    231             }
    232             for (int j = 0; j < 3 ; j++) {
    233                 args.inRhs[j] = arrayInRhs[i * 4 + j];
    234             }
    235             Target target = new Target(relaxed);
    236             CoreMathVerifier.computeNativeDistance(args, target);
    237 
    238             // Compare the expected outputs to the actual values returned by RS.
    239             boolean valid = true;
    240             if (!args.out.couldBe(arrayOut[i])) {
    241                 valid = false;
    242             }
    243             if (!valid) {
    244                 StringBuilder message = new StringBuilder();
    245                 for (int j = 0; j < 3 ; j++) {
    246                     message.append("Input inLhs: ");
    247                     message.append(String.format("%14.8g {%8x} %15a",
    248                             arrayInLhs[i * 4 + j], Float.floatToRawIntBits(arrayInLhs[i * 4 + j]), arrayInLhs[i * 4 + j]));
    249                     message.append("\n");
    250                 }
    251                 for (int j = 0; j < 3 ; j++) {
    252                     message.append("Input inRhs: ");
    253                     message.append(String.format("%14.8g {%8x} %15a",
    254                             arrayInRhs[i * 4 + j], Float.floatToRawIntBits(arrayInRhs[i * 4 + j]), arrayInRhs[i * 4 + j]));
    255                     message.append("\n");
    256                 }
    257                 message.append("Expected output out: ");
    258                 message.append(args.out.toString());
    259                 message.append("\n");
    260                 message.append("Actual   output out: ");
    261                 message.append(String.format("%14.8g {%8x} %15a",
    262                         arrayOut[i], Float.floatToRawIntBits(arrayOut[i]), arrayOut[i]));
    263                 if (!args.out.couldBe(arrayOut[i])) {
    264                     message.append(" FAIL");
    265                 }
    266                 message.append("\n");
    267                 assertTrue("Incorrect output for checkNativeDistanceFloat3Float3Float" +
    268                         (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), valid);
    269             }
    270         }
    271     }
    272 
    273     private void checkNativeDistanceFloat4Float4Float() {
    274         Allocation inLhs = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 4, 0x828c88ef4229c91el, false);
    275         Allocation inRhs = createRandomAllocation(mRS, Element.DataType.FLOAT_32, 4, 0x828c88ef4229f474l, false);
    276         try {
    277             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
    278             script.set_gAllocInRhs(inRhs);
    279             script.forEach_testNativeDistanceFloat4Float4Float(inLhs, out);
    280             verifyResultsNativeDistanceFloat4Float4Float(inLhs, inRhs, out, false);
    281         } catch (Exception e) {
    282             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNativeDistanceFloat4Float4Float: " + e.toString());
    283         }
    284         try {
    285             Allocation out = Allocation.createSized(mRS, getElement(mRS, Element.DataType.FLOAT_32, 1), INPUTSIZE);
    286             scriptRelaxed.set_gAllocInRhs(inRhs);
    287             scriptRelaxed.forEach_testNativeDistanceFloat4Float4Float(inLhs, out);
    288             verifyResultsNativeDistanceFloat4Float4Float(inLhs, inRhs, out, true);
    289         } catch (Exception e) {
    290             throw new RSRuntimeException("RenderScript. Can't invoke forEach_testNativeDistanceFloat4Float4Float: " + e.toString());
    291         }
    292     }
    293 
    294     private void verifyResultsNativeDistanceFloat4Float4Float(Allocation inLhs, Allocation inRhs, Allocation out, boolean relaxed) {
    295         float[] arrayInLhs = new float[INPUTSIZE * 4];
    296         inLhs.copyTo(arrayInLhs);
    297         float[] arrayInRhs = new float[INPUTSIZE * 4];
    298         inRhs.copyTo(arrayInRhs);
    299         float[] arrayOut = new float[INPUTSIZE * 1];
    300         out.copyTo(arrayOut);
    301         for (int i = 0; i < INPUTSIZE; i++) {
    302             ArgumentsFloatNFloatNFloat args = new ArgumentsFloatNFloatNFloat();
    303             // Create the appropriate sized arrays in args
    304             args.inLhs = new float[4];
    305             args.inRhs = new float[4];
    306             // Fill args with the input values
    307             for (int j = 0; j < 4 ; j++) {
    308                 args.inLhs[j] = arrayInLhs[i * 4 + j];
    309             }
    310             for (int j = 0; j < 4 ; j++) {
    311                 args.inRhs[j] = arrayInRhs[i * 4 + j];
    312             }
    313             Target target = new Target(relaxed);
    314             CoreMathVerifier.computeNativeDistance(args, target);
    315 
    316             // Compare the expected outputs to the actual values returned by RS.
    317             boolean valid = true;
    318             if (!args.out.couldBe(arrayOut[i])) {
    319                 valid = false;
    320             }
    321             if (!valid) {
    322                 StringBuilder message = new StringBuilder();
    323                 for (int j = 0; j < 4 ; j++) {
    324                     message.append("Input inLhs: ");
    325                     message.append(String.format("%14.8g {%8x} %15a",
    326                             arrayInLhs[i * 4 + j], Float.floatToRawIntBits(arrayInLhs[i * 4 + j]), arrayInLhs[i * 4 + j]));
    327                     message.append("\n");
    328                 }
    329                 for (int j = 0; j < 4 ; j++) {
    330                     message.append("Input inRhs: ");
    331                     message.append(String.format("%14.8g {%8x} %15a",
    332                             arrayInRhs[i * 4 + j], Float.floatToRawIntBits(arrayInRhs[i * 4 + j]), arrayInRhs[i * 4 + j]));
    333                     message.append("\n");
    334                 }
    335                 message.append("Expected output out: ");
    336                 message.append(args.out.toString());
    337                 message.append("\n");
    338                 message.append("Actual   output out: ");
    339                 message.append(String.format("%14.8g {%8x} %15a",
    340                         arrayOut[i], Float.floatToRawIntBits(arrayOut[i]), arrayOut[i]));
    341                 if (!args.out.couldBe(arrayOut[i])) {
    342                     message.append(" FAIL");
    343                 }
    344                 message.append("\n");
    345                 assertTrue("Incorrect output for checkNativeDistanceFloat4Float4Float" +
    346                         (relaxed ? "_relaxed" : "") + ":\n" + message.toString(), valid);
    347             }
    348         }
    349     }
    350 
    351     public void testNativeDistance() {
    352         checkNativeDistanceFloatFloatFloat();
    353         checkNativeDistanceFloat2Float2Float();
    354         checkNativeDistanceFloat3Float3Float();
    355         checkNativeDistanceFloat4Float4Float();
    356     }
    357 }
    358