Home | History | Annotate | Download | only in common
      1 /*
      2  * Copyright (C) 2010 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 com.android.gallery3d.common;
     18 
     19 import android.test.AndroidTestCase;
     20 import android.test.suitebuilder.annotation.SmallTest;
     21 import android.util.Log;
     22 
     23 public class UtilsTest extends AndroidTestCase {
     24     private static final String TAG = "UtilsTest";
     25 
     26     private static final int [] testData = new int [] {
     27         /* outWidth, outHeight, minSideLength, maxNumOfPixels, sample size */
     28         1, 1, BitmapUtils.UNCONSTRAINED, BitmapUtils.UNCONSTRAINED, 1,
     29         1, 1, 1, 1, 1,
     30         100, 100, 100, 10000, 1,
     31         100, 100, 100, 2500, 2,
     32         99, 66, 33, 10000, 2,
     33         66, 99, 33, 10000, 2,
     34         99, 66, 34, 10000, 1,
     35         99, 66, 22, 10000, 4,
     36         99, 66, 16, 10000, 4,
     37 
     38         10000, 10000, 20000, 1000000, 16,
     39 
     40         100, 100, 100, 10000, 1, // 1
     41         100, 100, 50, 10000, 2,  // 2
     42         100, 100, 30, 10000, 4,  // 3->4
     43         100, 100, 22, 10000, 4,  // 4
     44         100, 100, 20, 10000, 8,  // 5->8
     45         100, 100, 11, 10000, 16, // 9->16
     46         100, 100, 5,  10000, 24, // 20->24
     47         100, 100, 2,  10000, 56, // 50->56
     48 
     49         100, 100, 100, 10000 - 1, 2,                  // a bit less than 1
     50         100, 100, 100, 10000 / (2 * 2) - 1, 4,        // a bit less than 2
     51         100, 100, 100, 10000 / (3 * 3) - 1, 4,        // a bit less than 3
     52         100, 100, 100, 10000 / (4 * 4) - 1, 8,        // a bit less than 4
     53         100, 100, 100, 10000 / (8 * 8) - 1, 16,       // a bit less than 8
     54         100, 100, 100, 10000 / (16 * 16) - 1, 24,     // a bit less than 16
     55         100, 100, 100, 10000 / (24 * 24) - 1, 32,     // a bit less than 24
     56         100, 100, 100, 10000 / (32 * 32) - 1, 40,     // a bit less than 32
     57 
     58         640, 480, 480, BitmapUtils.UNCONSTRAINED, 1,  // 1
     59         640, 480, 240, BitmapUtils.UNCONSTRAINED, 2,  // 2
     60         640, 480, 160, BitmapUtils.UNCONSTRAINED, 4,  // 3->4
     61         640, 480, 120, BitmapUtils.UNCONSTRAINED, 4,  // 4
     62         640, 480, 96, BitmapUtils.UNCONSTRAINED,  8,  // 5->8
     63         640, 480, 80, BitmapUtils.UNCONSTRAINED,  8,  // 6->8
     64         640, 480, 60, BitmapUtils.UNCONSTRAINED,  8,  // 8
     65         640, 480, 48, BitmapUtils.UNCONSTRAINED, 16,  // 10->16
     66         640, 480, 40, BitmapUtils.UNCONSTRAINED, 16,  // 12->16
     67         640, 480, 30, BitmapUtils.UNCONSTRAINED, 16,  // 16
     68         640, 480, 24, BitmapUtils.UNCONSTRAINED, 24,  // 20->24
     69         640, 480, 20, BitmapUtils.UNCONSTRAINED, 24,  // 24
     70         640, 480, 16, BitmapUtils.UNCONSTRAINED, 32,  // 30->32
     71         640, 480, 12, BitmapUtils.UNCONSTRAINED, 40,  // 40
     72         640, 480, 10, BitmapUtils.UNCONSTRAINED, 48,  // 48
     73         640, 480, 8, BitmapUtils.UNCONSTRAINED,  64,  // 60->64
     74         640, 480, 6, BitmapUtils.UNCONSTRAINED,  80,  // 80
     75         640, 480, 4, BitmapUtils.UNCONSTRAINED, 120,  // 120
     76         640, 480, 3, BitmapUtils.UNCONSTRAINED, 160,  // 160
     77         640, 480, 2, BitmapUtils.UNCONSTRAINED, 240,  // 240
     78         640, 480, 1, BitmapUtils.UNCONSTRAINED, 480,  // 480
     79 
     80         640, 480, BitmapUtils.UNCONSTRAINED, BitmapUtils.UNCONSTRAINED, 1,
     81         640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480, 1,                  // 1
     82         640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 - 1, 2,              // a bit less than 1
     83         640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 4, 2,              // 2
     84         640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 4 - 1, 4,          // a bit less than 2
     85         640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 9, 4,              // 3
     86         640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 9 - 1, 4,          // a bit less than 3
     87         640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 16, 4,             // 4
     88         640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 16 - 1, 8,         // a bit less than 4
     89         640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 64, 8,             // 8
     90         640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 64 - 1, 16,        // a bit less than 8
     91         640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 256, 16,           // 16
     92         640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / 256 - 1, 24,       // a bit less than 16
     93         640, 480, BitmapUtils.UNCONSTRAINED, 640 * 480 / (24 * 24) - 1, 32, // a bit less than 24
     94     };
     95 
     96     @SmallTest
     97     public void testComputeSampleSize() {
     98 
     99         for (int i = 0; i < testData.length; i += 5) {
    100             int w = testData[i];
    101             int h = testData[i + 1];
    102             int minSide = testData[i + 2];
    103             int maxPixels = testData[i + 3];
    104             int sampleSize = testData[i + 4];
    105             int result = BitmapUtils.computeSampleSize(w, h, minSide, maxPixels);
    106             if (result != sampleSize) {
    107                 Log.v(TAG, w + "x" + h + ", minSide = " + minSide + ", maxPixels = "
    108                         + maxPixels + ", sampleSize = " + sampleSize + ", result = "
    109                         + result);
    110             }
    111             assertTrue(sampleSize == result);
    112         }
    113     }
    114 
    115     public void testAssert() {
    116         // This should not throw an exception.
    117         Utils.assertTrue(true);
    118 
    119         // This should throw an exception.
    120         try {
    121             Utils.assertTrue(false);
    122             fail();
    123         } catch (AssertionError ex) {
    124             // expected.
    125         }
    126     }
    127 
    128     public void testCheckNotNull() {
    129         // These should not throw an expection.
    130         Utils.checkNotNull(new Object());
    131         Utils.checkNotNull(0);
    132         Utils.checkNotNull("");
    133 
    134         // This should throw an expection.
    135         try {
    136             Utils.checkNotNull(null);
    137             fail();
    138         } catch (NullPointerException ex) {
    139             // expected.
    140         }
    141     }
    142 
    143     public void testEquals() {
    144         Object a = new Object();
    145         Object b = new Object();
    146 
    147         assertTrue(Utils.equals(null, null));
    148         assertTrue(Utils.equals(a, a));
    149         assertFalse(Utils.equals(null, a));
    150         assertFalse(Utils.equals(a, null));
    151         assertFalse(Utils.equals(a, b));
    152     }
    153 
    154     public void testIsPowerOf2() {
    155         for (int i = 0; i < 31; i++) {
    156             int v = (1 << i);
    157             assertTrue(Utils.isPowerOf2(v));
    158         }
    159 
    160         int[] f = new int[] {3, 5, 6, 7, 9, 10, 65535, Integer.MAX_VALUE - 1,
    161                 Integer.MAX_VALUE };
    162         for (int v : f) {
    163             assertFalse(Utils.isPowerOf2(v));
    164         }
    165 
    166         int[] e = new int[] {0, -1, -2, -4, -65536, Integer.MIN_VALUE + 1,
    167                 Integer.MIN_VALUE };
    168         for (int v : e) {
    169             try {
    170                 Utils.isPowerOf2(v);
    171                 fail();
    172             } catch (IllegalArgumentException ex) {
    173                 // expected.
    174             }
    175         }
    176     }
    177 
    178     public void testNextPowerOf2() {
    179         int[] q = new int[] {1, 2, 3, 4, 5, 6, 10, 65535, (1 << 30) - 1, (1 << 30)};
    180         int[] a = new int[] {1, 2, 4, 4, 8, 8, 16, 65536, (1 << 30)    , (1 << 30)};
    181 
    182         for (int i = 0; i < q.length; i++) {
    183             assertEquals(a[i], Utils.nextPowerOf2(q[i]));
    184         }
    185 
    186         int[] e = new int[] {0, -1, -2, -4, -65536, (1 << 30) + 1, Integer.MAX_VALUE};
    187 
    188         for (int v : e) {
    189             try {
    190                 Utils.nextPowerOf2(v);
    191                 fail();
    192             } catch (IllegalArgumentException ex) {
    193                 // expected.
    194             }
    195         }
    196     }
    197 
    198     public void testDistance() {
    199         assertFloatEq(0f, Utils.distance(0, 0, 0, 0));
    200         assertFloatEq(1f, Utils.distance(0, 1, 0, 0));
    201         assertFloatEq(1f, Utils.distance(0, 0, 0, 1));
    202         assertFloatEq(2f, Utils.distance(1, 2, 3, 2));
    203         assertFloatEq(5f, Utils.distance(1, 2, 1 + 3, 2 + 4));
    204         assertFloatEq(5f, Utils.distance(1, 2, 1 + 3, 2 + 4));
    205         assertFloatEq(Float.MAX_VALUE, Utils.distance(Float.MAX_VALUE, 0, 0, 0));
    206     }
    207 
    208     public void testClamp() {
    209         assertEquals(1000, Utils.clamp(300, 1000, 2000));
    210         assertEquals(1300, Utils.clamp(1300, 1000, 2000));
    211         assertEquals(2000, Utils.clamp(2300, 1000, 2000));
    212 
    213         assertEquals(0.125f, Utils.clamp(0.1f, 0.125f, 0.5f));
    214         assertEquals(0.25f, Utils.clamp(0.25f, 0.125f, 0.5f));
    215         assertEquals(0.5f, Utils.clamp(0.9f, 0.125f, 0.5f));
    216     }
    217 
    218     public void testIsOpaque() {
    219         assertTrue(Utils.isOpaque(0xFF000000));
    220         assertTrue(Utils.isOpaque(0xFFFFFFFF));
    221         assertTrue(Utils.isOpaque(0xFF123456));
    222 
    223         assertFalse(Utils.isOpaque(0xFEFFFFFF));
    224         assertFalse(Utils.isOpaque(0x8FFFFFFF));
    225         assertFalse(Utils.isOpaque(0x00FF0000));
    226         assertFalse(Utils.isOpaque(0x5500FF00));
    227         assertFalse(Utils.isOpaque(0xAA0000FF));
    228     }
    229 
    230     public static void testSwap() {
    231         Integer[] a = {1, 2, 3};
    232         Utils.swap(a, 0, 2);
    233         assertEquals(a[0].intValue(), 3);
    234         assertEquals(a[1].intValue(), 2);
    235         assertEquals(a[2].intValue(), 1);
    236     }
    237 
    238     public static void assertFloatEq(float expected, float actual) {
    239         if (Math.abs(actual - expected) > 1e-6) {
    240             Log.v(TAG, "expected: " + expected + ", actual: " + actual);
    241             fail();
    242         }
    243     }
    244 }
    245