Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2015 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 package android.renderscript.cts;
     18 
     19 import android.renderscript.Allocation;
     20 import android.renderscript.Element;
     21 import android.renderscript.Type;
     22 import java.util.Random;
     23 import android.util.Log;
     24 
     25 public class RsAllocationCopyTest extends RSBaseCompute {
     26 
     27     public void test_RsAllocationCopy1D_Byte() {
     28         Random random = new Random(0x172d8ab9);
     29         int width = random.nextInt(512);
     30         int arr_len = width;
     31         int offset = random.nextInt(arr_len);
     32         int count = random.nextInt(arr_len - offset);
     33 
     34         byte[] inArray = new byte[arr_len];
     35         byte[] outArray = new byte[arr_len];
     36         random.nextBytes(inArray);
     37 
     38         Type.Builder typeBuilder = new Type.Builder(mRS, Element.I8(mRS));
     39         typeBuilder.setX(width);
     40         Allocation aIn = Allocation.createTyped(mRS, typeBuilder.create());
     41         Allocation aOut = Allocation.createTyped(mRS, typeBuilder.create());
     42         aIn.copyFrom(inArray);
     43         aOut.copyFrom(outArray);
     44 
     45         ScriptC_rsallocationcopy s = new ScriptC_rsallocationcopy(mRS);
     46         s.set_aIn1D(aIn);
     47         s.set_aOut1D(aOut);
     48         s.set_xOff(offset);
     49         s.set_xCount(count);
     50         s.invoke_test1D();
     51         mRS.finish();
     52         aOut.copyTo(outArray);
     53 
     54         boolean result = true;
     55         for (int i = 0; i < arr_len; i++) {
     56             if (offset <= i && i < offset + count) {
     57                 if (inArray[i] != outArray[i]) {
     58                     result = false;
     59                     break;
     60                 }
     61             } else {
     62                 if (outArray[i] != 0) {
     63                     result = false;
     64                     break;
     65                 }
     66             }
     67         }
     68         assertTrue("test_rsAllocationCopy1D_Byte failed, output array does not match input",
     69                    result);
     70     }
     71 
     72     public void test_RsAllocationCopy1D_Short() {
     73         Random random = new Random(0x172d8ab9);
     74         int width = random.nextInt(512);
     75         int arr_len = width;
     76         int offset = random.nextInt(arr_len);
     77         int count = random.nextInt(arr_len - offset);
     78 
     79         short[] inArray = new short[arr_len];
     80         short[] outArray = new short[arr_len];
     81         for (int i = 0; i < arr_len; i++) {
     82             inArray[i] = (short)random.nextInt();
     83         }
     84 
     85         Type.Builder typeBuilder = new Type.Builder(mRS, Element.I16(mRS));
     86         typeBuilder.setX(width);
     87         Allocation aIn = Allocation.createTyped(mRS, typeBuilder.create());
     88         Allocation aOut = Allocation.createTyped(mRS, typeBuilder.create());
     89         aIn.copyFrom(inArray);
     90         aOut.copyFrom(outArray);
     91 
     92         ScriptC_rsallocationcopy s = new ScriptC_rsallocationcopy(mRS);
     93         s.set_aIn1D(aIn);
     94         s.set_aOut1D(aOut);
     95         s.set_xOff(offset);
     96         s.set_xCount(count);
     97         s.invoke_test1D();
     98         mRS.finish();
     99         aOut.copyTo(outArray);
    100 
    101         boolean result = true;
    102         for (int i = 0; i < arr_len; i++) {
    103             if (offset <= i && i < offset + count) {
    104                 if (inArray[i] != outArray[i]) {
    105                     result = false;
    106                     break;
    107                 }
    108             } else {
    109                 if (outArray[i] != 0) {
    110                     result = false;
    111                     break;
    112                 }
    113             }
    114         }
    115         assertTrue("test_rsAllocationCopy1D_Short failed, output array does not match input",
    116                    result);
    117     }
    118 
    119     public void test_RsAllocationCopy1D_Int() {
    120         Random random = new Random(0x172d8ab9);
    121         int width = random.nextInt(512);
    122         int arr_len = width;
    123         int offset = random.nextInt(arr_len);
    124         int count = random.nextInt(arr_len - offset);
    125 
    126         int[] inArray = new int[arr_len];
    127         int[] outArray = new int[arr_len];
    128         for (int i = 0; i < arr_len; i++) {
    129             inArray[i] = random.nextInt();
    130         }
    131 
    132         Type.Builder typeBuilder = new Type.Builder(mRS, Element.I32(mRS));
    133         typeBuilder.setX(width);
    134         Allocation aIn = Allocation.createTyped(mRS, typeBuilder.create());
    135         Allocation aOut = Allocation.createTyped(mRS, typeBuilder.create());
    136         aIn.copyFrom(inArray);
    137         aOut.copyFrom(outArray);
    138 
    139         ScriptC_rsallocationcopy s = new ScriptC_rsallocationcopy(mRS);
    140         s.set_aIn1D(aIn);
    141         s.set_aOut1D(aOut);
    142         s.set_xOff(offset);
    143         s.set_xCount(count);
    144         s.invoke_test1D();
    145         mRS.finish();
    146         aOut.copyTo(outArray);
    147 
    148         boolean result = true;
    149         for (int i = 0; i < arr_len; i++) {
    150             if (offset <= i && i < offset + count) {
    151                 if (inArray[i] != outArray[i]) {
    152                     result = false;
    153                     break;
    154                 }
    155             } else {
    156                 if (outArray[i] != 0) {
    157                     result = false;
    158                     break;
    159                 }
    160             }
    161         }
    162         assertTrue("test_rsAllocationCopy1D_Int failed, output array does not match input",
    163                    result);
    164     }
    165 
    166     public void test_RsAllocationCopy1D_Float() {
    167         Random random = new Random(0x172d8ab9);
    168         int width = random.nextInt(512);
    169         int arr_len = width;
    170         int offset = random.nextInt(arr_len);
    171         int count = random.nextInt(arr_len - offset);
    172 
    173         float[] inArray = new float[arr_len];
    174         float[] outArray = new float[arr_len];
    175         for (int i = 0; i < arr_len; i++) {
    176             inArray[i] = random.nextFloat();
    177         }
    178 
    179         Type.Builder typeBuilder = new Type.Builder(mRS, Element.F32(mRS));
    180         typeBuilder.setX(width);
    181         Allocation aIn = Allocation.createTyped(mRS, typeBuilder.create());
    182         Allocation aOut = Allocation.createTyped(mRS, typeBuilder.create());
    183         aIn.copyFrom(inArray);
    184         aOut.copyFrom(outArray);
    185 
    186         ScriptC_rsallocationcopy s = new ScriptC_rsallocationcopy(mRS);
    187         s.set_aIn1D(aIn);
    188         s.set_aOut1D(aOut);
    189         s.set_xOff(offset);
    190         s.set_xCount(count);
    191         s.invoke_test1D();
    192         mRS.finish();
    193         aOut.copyTo(outArray);
    194 
    195 
    196         boolean result = true;
    197         for (int i = 0; i < arr_len; i++) {
    198             if (offset <= i && i < offset + count) {
    199                 if (inArray[i] != outArray[i]) {
    200                     result = false;
    201                     break;
    202                 }
    203             } else {
    204                 if (outArray[i] != 0) {
    205                     result = false;
    206                     break;
    207                 }
    208             }
    209         }
    210         assertTrue("test_rsAllocationCopy1D_Float failed, output array does not match input",
    211                    result);
    212     }
    213 
    214     public void test_RsAllocationCopy1D_Long() {
    215         Random random = new Random(0x172d8ab9);
    216         int width = random.nextInt(512);
    217         int arr_len = width;
    218         int offset = random.nextInt(arr_len);
    219         int count = random.nextInt(arr_len - offset);
    220 
    221         long[] inArray = new long[arr_len];
    222         long[] outArray = new long[arr_len];
    223         for (int i = 0; i < arr_len; i++) {
    224             inArray[i] = random.nextLong();
    225         }
    226 
    227         Type.Builder typeBuilder = new Type.Builder(mRS, Element.I64(mRS));
    228         typeBuilder.setX(width);
    229         Allocation aIn = Allocation.createTyped(mRS, typeBuilder.create());
    230         Allocation aOut = Allocation.createTyped(mRS, typeBuilder.create());
    231         aIn.copyFrom(inArray);
    232         aOut.copyFrom(outArray);
    233 
    234         ScriptC_rsallocationcopy s = new ScriptC_rsallocationcopy(mRS);
    235         s.set_aIn1D(aIn);
    236         s.set_aOut1D(aOut);
    237         s.set_xOff(offset);
    238         s.set_xCount(count);
    239         s.invoke_test1D();
    240         mRS.finish();
    241         aOut.copyTo(outArray);
    242 
    243         boolean result = true;
    244         for (int i = 0; i < arr_len; i++) {
    245             if (offset <= i && i < offset + count) {
    246                 if (inArray[i] != outArray[i]) {
    247                     result = false;
    248                     break;
    249                 }
    250             } else {
    251                 if (outArray[i] != 0) {
    252                     result = false;
    253                     break;
    254                 }
    255             }
    256         }
    257         assertTrue("test_rsAllocationCopy1D_Long failed, output array does not match input",
    258                    result);
    259     }
    260 
    261 
    262     public void test_RsAllocationCopy2D_Byte() {
    263         Random random = new Random(0x172d8ab9);
    264         int width = random.nextInt(128);
    265         int height = random.nextInt(128);
    266         int xOff = random.nextInt(width);
    267         int yOff = random.nextInt(height);
    268         int xCount = random.nextInt(width - xOff);
    269         int yCount = random.nextInt(height - yOff);
    270         int arr_len = width * height;
    271 
    272         byte[] inArray = new byte[arr_len];
    273         byte[] outArray = new byte[arr_len];
    274         random.nextBytes(inArray);
    275 
    276         Type.Builder typeBuilder = new Type.Builder(mRS, Element.I8(mRS));
    277         typeBuilder.setX(width).setY(height);
    278         Allocation aIn = Allocation.createTyped(mRS, typeBuilder.create());
    279         Allocation aOut = Allocation.createTyped(mRS, typeBuilder.create());
    280         aIn.copyFrom(inArray);
    281         aOut.copyFrom(outArray);
    282 
    283         ScriptC_rsallocationcopy s = new ScriptC_rsallocationcopy(mRS);
    284         s.set_aIn2D(aIn);
    285         s.set_aOut2D(aOut);
    286         s.set_xOff(xOff);
    287         s.set_yOff(yOff);
    288         s.set_xCount(xCount);
    289         s.set_yCount(yCount);
    290         s.invoke_test2D();
    291         mRS.finish();
    292         aOut.copyTo(outArray);
    293 
    294         boolean result = true;
    295         for (int i = 0; i < height; i++) {
    296             for (int j = 0; j < width; j++) {
    297                 int pos = i * width + j;
    298                 if (yOff <= i && i < yOff + yCount &&
    299                     xOff <= j && j < xOff + xCount) {
    300                     if (inArray[pos] != outArray[pos]) {
    301                         result = false;
    302                         break;
    303                     }
    304                 } else {
    305                     if (outArray[pos] != 0) {
    306                         result = false;
    307                         break;
    308                     }
    309                 }
    310             }
    311         }
    312         assertTrue("test_rsAllocationCopy2D_Byte failed, output array does not match input",
    313                    result);
    314     }
    315 
    316     public void test_RsAllocationCopy2D_Short() {
    317         Random random = new Random(0x172d8ab9);
    318         int width = random.nextInt(128);
    319         int height = random.nextInt(128);
    320         int xOff = random.nextInt(width);
    321         int yOff = random.nextInt(height);
    322         int xCount = random.nextInt(width - xOff);
    323         int yCount = random.nextInt(height - yOff);
    324         int arr_len = width * height;
    325 
    326         short[] inArray = new short[arr_len];
    327         short[] outArray = new short[arr_len];
    328         for (int i = 0; i < arr_len; i++) {
    329             inArray[i] = (short)random.nextInt();
    330         }
    331 
    332         Type.Builder typeBuilder = new Type.Builder(mRS, Element.I16(mRS));
    333         typeBuilder.setX(width).setY(height);
    334         Allocation aIn = Allocation.createTyped(mRS, typeBuilder.create());
    335         Allocation aOut = Allocation.createTyped(mRS, typeBuilder.create());
    336         aIn.copyFrom(inArray);
    337         aOut.copyFrom(outArray);
    338 
    339         ScriptC_rsallocationcopy s = new ScriptC_rsallocationcopy(mRS);
    340         s.set_aIn2D(aIn);
    341         s.set_aOut2D(aOut);
    342         s.set_xOff(xOff);
    343         s.set_yOff(yOff);
    344         s.set_xCount(xCount);
    345         s.set_yCount(yCount);
    346         s.invoke_test2D();
    347         mRS.finish();
    348         aOut.copyTo(outArray);
    349 
    350         boolean result = true;
    351         for (int i = 0; i < height; i++) {
    352             for (int j = 0; j < width; j++) {
    353                 int pos = i * width + j;
    354                 if (yOff <= i && i < yOff + yCount &&
    355                     xOff <= j && j < xOff + xCount) {
    356                     if (inArray[pos] != outArray[pos]) {
    357                         result = false;
    358                         break;
    359                     }
    360                 } else {
    361                     if (outArray[pos] != 0) {
    362                         result = false;
    363                         break;
    364                     }
    365                 }
    366             }
    367         }
    368         assertTrue("test_rsAllocationCopy2D_Short failed, output array does not match input",
    369                    result);
    370     }
    371 
    372     public void test_RsAllocationCopy2D_Int() {
    373         Random random = new Random(0x172d8ab9);
    374         int width = random.nextInt(128);
    375         int height = random.nextInt(128);
    376         int xOff = random.nextInt(width);
    377         int yOff = random.nextInt(height);
    378         int xCount = random.nextInt(width - xOff);
    379         int yCount = random.nextInt(height - yOff);
    380         int arr_len = width * height;
    381 
    382         int[] inArray = new int[arr_len];
    383         int[] outArray = new int[arr_len];
    384         for (int i = 0; i < arr_len; i++) {
    385             inArray[i] = random.nextInt();
    386         }
    387 
    388         Type.Builder typeBuilder = new Type.Builder(mRS, Element.I32(mRS));
    389         typeBuilder.setX(width).setY(height);
    390         Allocation aIn = Allocation.createTyped(mRS, typeBuilder.create());
    391         Allocation aOut = Allocation.createTyped(mRS, typeBuilder.create());
    392         aIn.copyFrom(inArray);
    393         aOut.copyFrom(outArray);
    394 
    395         ScriptC_rsallocationcopy s = new ScriptC_rsallocationcopy(mRS);
    396         s.set_aIn2D(aIn);
    397         s.set_aOut2D(aOut);
    398         s.set_xOff(xOff);
    399         s.set_yOff(yOff);
    400         s.set_xCount(xCount);
    401         s.set_yCount(yCount);
    402         s.invoke_test2D();
    403         mRS.finish();
    404         aOut.copyTo(outArray);
    405 
    406         boolean result = true;
    407         for (int i = 0; i < height; i++) {
    408             for (int j = 0; j < width; j++) {
    409                 int pos = i * width + j;
    410                 if (yOff <= i && i < yOff + yCount &&
    411                     xOff <= j && j < xOff + xCount) {
    412                     if (inArray[pos] != outArray[pos]) {
    413                         result = false;
    414                         break;
    415                     }
    416                 } else {
    417                     if (outArray[pos] != 0) {
    418                         result = false;
    419                         break;
    420                     }
    421                 }
    422             }
    423         }
    424         assertTrue("test_rsAllocationCopy2D_Int failed, output array does not match input",
    425                    result);
    426     }
    427 
    428     public void test_RsAllocationCopy2D_Float() {
    429         Random random = new Random(0x172d8ab9);
    430         int width = random.nextInt(128);
    431         int height = random.nextInt(128);
    432         int xOff = random.nextInt(width);
    433         int yOff = random.nextInt(height);
    434         int xCount = random.nextInt(width - xOff);
    435         int yCount = random.nextInt(height - yOff);
    436         int arr_len = width * height;
    437 
    438         float[] inArray = new float[arr_len];
    439         float[] outArray = new float[arr_len];
    440         for (int i = 0; i < arr_len; i++) {
    441             inArray[i] = random.nextFloat();
    442         }
    443 
    444         Type.Builder typeBuilder = new Type.Builder(mRS, Element.F32(mRS));
    445         typeBuilder.setX(width).setY(height);
    446         Allocation aIn = Allocation.createTyped(mRS, typeBuilder.create());
    447         Allocation aOut = Allocation.createTyped(mRS, typeBuilder.create());
    448         aIn.copyFrom(inArray);
    449         aOut.copyFrom(outArray);
    450 
    451         ScriptC_rsallocationcopy s = new ScriptC_rsallocationcopy(mRS);
    452         s.set_aIn2D(aIn);
    453         s.set_aOut2D(aOut);
    454         s.set_xOff(xOff);
    455         s.set_yOff(yOff);
    456         s.set_xCount(xCount);
    457         s.set_yCount(yCount);
    458         s.invoke_test2D();
    459         mRS.finish();
    460         aOut.copyTo(outArray);
    461 
    462         boolean result = true;
    463         for (int i = 0; i < height; i++) {
    464             for (int j = 0; j < width; j++) {
    465                 int pos = i * width + j;
    466                 if (yOff <= i && i < yOff + yCount &&
    467                     xOff <= j && j < xOff + xCount) {
    468                     if (inArray[pos] != outArray[pos]) {
    469                         result = false;
    470                         break;
    471                     }
    472                 } else {
    473                     if (outArray[pos] != 0) {
    474                         result = false;
    475                         break;
    476                     }
    477                 }
    478             }
    479         }
    480         assertTrue("test_rsAllocationCopy2D_Float failed, output array does not match input",
    481                    result);
    482     }
    483 
    484     public void test_RsAllocationCopy2D_Long() {
    485         Random random = new Random(0x172d8ab9);
    486         int width = random.nextInt(128);
    487         int height = random.nextInt(128);
    488         int xOff = random.nextInt(width);
    489         int yOff = random.nextInt(height);
    490         int xCount = random.nextInt(width - xOff);
    491         int yCount = random.nextInt(height - yOff);
    492         int arr_len = width * height;
    493 
    494         long[] inArray = new long[arr_len];
    495         long[] outArray = new long[arr_len];
    496         for (int i = 0; i < arr_len; i++) {
    497             inArray[i] = random.nextLong();
    498         }
    499 
    500         Type.Builder typeBuilder = new Type.Builder(mRS, Element.I64(mRS));
    501         typeBuilder.setX(width).setY(height);
    502         Allocation aIn = Allocation.createTyped(mRS, typeBuilder.create());
    503         Allocation aOut = Allocation.createTyped(mRS, typeBuilder.create());
    504         aIn.copyFrom(inArray);
    505         aOut.copyFrom(outArray);
    506 
    507         ScriptC_rsallocationcopy s = new ScriptC_rsallocationcopy(mRS);
    508         s.set_aIn2D(aIn);
    509         s.set_aOut2D(aOut);
    510         s.set_xOff(xOff);
    511         s.set_yOff(yOff);
    512         s.set_xCount(xCount);
    513         s.set_yCount(yCount);
    514         s.invoke_test2D();
    515         mRS.finish();
    516         aOut.copyTo(outArray);
    517 
    518         boolean result = true;
    519         for (int i = 0; i < height; i++) {
    520             for (int j = 0; j < width; j++) {
    521                 int pos = i * width + j;
    522                 if (yOff <= i && i < yOff + yCount &&
    523                     xOff <= j && j < xOff + xCount) {
    524                     if (inArray[pos] != outArray[pos]) {
    525                         result = false;
    526                         break;
    527                     }
    528                 } else {
    529                     if (outArray[pos] != 0) {
    530                         result = false;
    531                         break;
    532                     }
    533                 }
    534             }
    535         }
    536         assertTrue("test_rsAllocationCopy2D_Long failed, output array does not match input",
    537                    result);
    538     }
    539 }
    540