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 package android.renderscriptlegacy.cts;
     18 
     19 import android.content.res.Resources;
     20 import android.renderscript.Allocation;
     21 import android.renderscript.RSRuntimeException;
     22 
     23 import java.util.Random;
     24 
     25 /**
     26  * This class supplies some utils for renderscript tests
     27  */
     28 public class RSUtils {
     29     private static final double[] sInterestingDoubles = {
     30         0.0,
     31         1.0,
     32         Math.E,
     33         Math.PI,
     34         Math.PI / 2.0,
     35         Math.PI * 2.0,
     36         -0.0,
     37         -1.0,
     38         -Math.E,
     39         -Math.PI,
     40         -Math.PI / 2.0,
     41         -Math.PI * 2.0,
     42     };
     43 
     44     /**
     45      * Fills the array with random doubles.  Values will be between min (inclusive) and
     46      * max (inclusive).
     47      */
     48     public static void genRandomDoubles(long seed, double min, double max, double array[],
     49             boolean includeExtremes) {
     50         Random r = new Random(seed);
     51         int minExponent = Math.min(Math.getExponent(min), 0);
     52         int maxExponent = Math.max(Math.getExponent(max), 0);
     53         if (minExponent < -6 || maxExponent > 6) {
     54             // Use an exponential distribution
     55             int exponentDiff = maxExponent - minExponent;
     56             for (int i = 0; i < array.length; i++) {
     57                 double mantissa = r.nextDouble();
     58                 int exponent = minExponent + r.nextInt(maxExponent - minExponent);
     59                 int sign = (min >= 0) ? 1 : 1 - r.nextInt(2) * 2;  // -1 or 1
     60                 double rand = sign * mantissa * Math.pow(2.0, exponent);
     61                 if (rand < min || rand > max) {
     62                     continue;
     63                 }
     64                 array[i] = rand;
     65             }
     66         } else {
     67             // Use a linear distribution
     68             for (int i = 0; i < array.length; i++) {
     69                 double rand = r.nextDouble();
     70                 array[i] = min + rand * (max - min);
     71             }
     72         }
     73         // Seed a few special numbers we want to be sure to test.
     74         for (int i = 0; i < sInterestingDoubles.length; i++) {
     75             double d = sInterestingDoubles[i];
     76             if (min <= d && d <= max) {
     77                 array[r.nextInt(array.length)] = d;
     78             }
     79         }
     80         array[r.nextInt(array.length)] = min;
     81         array[r.nextInt(array.length)] = max;
     82         if (includeExtremes) {
     83             array[r.nextInt(array.length)] = Double.NaN;
     84             array[r.nextInt(array.length)] = Double.POSITIVE_INFINITY;
     85             array[r.nextInt(array.length)] = Double.NEGATIVE_INFINITY;
     86             array[r.nextInt(array.length)] = Double.MIN_VALUE;
     87             array[r.nextInt(array.length)] = Double.MIN_NORMAL;
     88             array[r.nextInt(array.length)] = Double.MAX_VALUE;
     89             array[r.nextInt(array.length)] = -Double.MIN_VALUE;
     90             array[r.nextInt(array.length)] = -Double.MIN_NORMAL;
     91             array[r.nextInt(array.length)] = -Double.MAX_VALUE;
     92         }
     93     }
     94 
     95     /**
     96      * Fills the array with random floats.  Values will be between min (inclusive) and
     97      * max (inclusive).
     98      */
     99     public static void genRandomFloats(long seed, float min, float max, float array[],
    100             boolean includeExtremes) {
    101         Random r = new Random(seed);
    102         int minExponent = Math.min(Math.getExponent(min), 0);
    103         int maxExponent = Math.max(Math.getExponent(max), 0);
    104         if (minExponent < -6 || maxExponent > 6) {
    105             // Use an exponential distribution
    106             int exponentDiff = maxExponent - minExponent;
    107             for (int i = 0; i < array.length; i++) {
    108                 float mantissa = r.nextFloat();
    109                 int exponent = minExponent + r.nextInt(maxExponent - minExponent);
    110                 int sign = (min >= 0) ? 1 : 1 - r.nextInt(2) * 2;  // -1 or 1
    111                 float rand = sign * mantissa * (float) Math.pow(2.0, exponent);
    112                 if (rand < min || rand > max) {
    113                     continue;
    114                 }
    115                 array[i] = rand;
    116             }
    117         } else {
    118             // Use a linear distribution
    119             for (int i = 0; i < array.length; i++) {
    120                 float rand = r.nextFloat();
    121                 array[i] = min + rand * (max - min);
    122             }
    123         }
    124         // Seed a few special numbers we want to be sure to test.
    125         for (int i = 0; i < sInterestingDoubles.length; i++) {
    126             float f = (float) sInterestingDoubles[i];
    127             if (min <= f && f <= max) {
    128                 array[r.nextInt(array.length)] = f;
    129             }
    130         }
    131         array[r.nextInt(array.length)] = min;
    132         array[r.nextInt(array.length)] = max;
    133         if (includeExtremes) {
    134             array[r.nextInt(array.length)] = Float.NaN;
    135             array[r.nextInt(array.length)] = Float.POSITIVE_INFINITY;
    136             array[r.nextInt(array.length)] = Float.NEGATIVE_INFINITY;
    137             array[r.nextInt(array.length)] = Float.MIN_VALUE;
    138             array[r.nextInt(array.length)] = Float.MIN_NORMAL;
    139             array[r.nextInt(array.length)] = Float.MAX_VALUE;
    140             array[r.nextInt(array.length)] = -Float.MIN_VALUE;
    141             array[r.nextInt(array.length)] = -Float.MIN_NORMAL;
    142             array[r.nextInt(array.length)] = -Float.MAX_VALUE;
    143         }
    144     }
    145 
    146     /**
    147      * Fills the array with random ints.  Values will be between min (inclusive) and
    148      * max (inclusive).
    149      */
    150     public static void genRandomInts(long seed, int min, int max, int array[]) {
    151         Random r = new Random(seed);
    152         for (int i = 0; i < array.length; i++) {
    153             long range = max - min + 1;
    154             array[i] = (int) (min + r.nextLong() % range);
    155         }
    156         array[r.nextInt(array.length)] = min;
    157         array[r.nextInt(array.length)] = max;
    158     }
    159 
    160     /**
    161      * Fills the array with random longs.  If signed is true, negative values can be generated.
    162      * The values will fit within 'numberOfBits'.  This is useful for conversion tests.
    163      */
    164     public static void genRandomLongs(long seed, long array[], boolean signed, int numberOfBits) {
    165       long positiveMask = numberOfBits == 64 ? -1 : ((1l << numberOfBits) - 1);
    166       long negativeMask = ~positiveMask;
    167       Random r = new Random(seed);
    168       for (int i = 0; i < array.length; i++) {
    169           long l = r.nextLong();
    170           if (signed && l < 0) {
    171               l = l | negativeMask;
    172           } else {
    173               l = l & positiveMask;
    174           }
    175           array[i] = l;
    176       }
    177       // Seed a few special numbers we want to be sure to test.
    178       array[r.nextInt(array.length)] = 0l;
    179       array[r.nextInt(array.length)] = 1l;
    180       array[r.nextInt(array.length)] = positiveMask;
    181       if (signed) {
    182           array[r.nextInt(array.length)] = negativeMask;
    183           array[r.nextInt(array.length)] = -1;
    184       }
    185     }
    186 
    187     public static void genRandomInts(long seed, int array[], boolean signed, int numberOfBits) {
    188         long[] longs = new long[array.length];
    189         genRandomLongs(seed, longs, signed, numberOfBits);
    190         for (int i = 0; i < array.length; i++) {
    191             array[i] = (int) longs[i];
    192         }
    193     }
    194 
    195     public static void genRandomShorts(long seed, short array[], boolean signed, int numberOfBits) {
    196         long[] longs = new long[array.length];
    197         genRandomLongs(seed, longs, signed, numberOfBits);
    198         for (int i = 0; i < array.length; i++) {
    199             array[i] = (short) longs[i];
    200         }
    201     }
    202 
    203     public static void genRandomBytes(long seed, byte array[], boolean signed, int numberOfBits) {
    204         long[] longs = new long[array.length];
    205         genRandomLongs(seed, longs, signed, numberOfBits);
    206         for (int i = 0; i < array.length; i++) {
    207             array[i] = (byte) longs[i];
    208         }
    209     }
    210 
    211     // Compares two unsigned long.  Returns < 0 if a < b, 0 if a == b, > 0 if a > b.
    212     public static long compareUnsignedLong(long a, long b) {
    213         long aFirstFourBits = a >>> 60;
    214         long bFirstFourBits = b >>> 60;
    215         long firstFourBitsDiff = aFirstFourBits - bFirstFourBits;
    216         if (firstFourBitsDiff != 0) {
    217             return firstFourBitsDiff;
    218         }
    219         long aRest = a & 0x0fffffffffffffffl;
    220         long bRest = b & 0x0fffffffffffffffl;
    221         return aRest - bRest;
    222     }
    223 }
    224