Home | History | Annotate | Download | only in shadows
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  * Copyright (C) 2011 Eric Bowman
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 package com.xtremelabs.robolectric.shadows;
     18 
     19 import android.util.SparseArray;
     20 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
     21 import com.xtremelabs.robolectric.bytecode.ShadowWrangler;
     22 import org.junit.Test;
     23 import org.junit.runner.RunWith;
     24 
     25 import static org.junit.Assert.assertEquals;
     26 import static org.junit.Assert.assertFalse;
     27 import static org.junit.Assert.assertNull;
     28 import static org.junit.Assert.assertTrue;
     29 
     30 /**
     31  * Tests for shadow implementation of SparseArray. Basically copied & pasted the
     32  * real SparseArray tests from the 2.2 SDK.
     33  *
     34  * @author Eric Bowman (ebowman (at) boboco.ie)
     35  * @since 2011-02-25 11:01
     36  */
     37 @RunWith(WithTestDefaultsRunner.class)
     38 public class SparseArrayTest {
     39     private static final int[] KEYS = {12, 23, 4, 6, 8, 1, 3, -12, 0, -3, 11, 14, -23};
     40     private static final Integer[] VALUES = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
     41     private static final int LENGTH = VALUES.length;
     42     private static final int NON_EXISTED_KEY = 123;
     43     private static final Integer VALUE_FOR_NON_EXISTED_KEY = -1;
     44 
     45     @Test
     46     public void testSparseArrayWithDefaultCapacity() {
     47         ShadowWrangler.getInstance().logMissingInvokedShadowMethods();
     48         SparseArray<Integer> sparseArray = new SparseArray<Integer>();
     49         assertEquals(0, sparseArray.size());
     50 
     51         int length = VALUES.length;
     52 
     53         for (int i = 0; i < length; i++) {
     54             sparseArray.put(KEYS[i], VALUES[i]);
     55             assertEquals(i + 1, sparseArray.size());
     56         }
     57 
     58         for (int i = 0; i < length; i++) {
     59             assertEquals(new Integer(i), sparseArray.get(KEYS[i]));
     60         }
     61 
     62         for (int i = 0; i < length; i++) {
     63             assertEquals(sparseArray.indexOfValue(VALUES[i]), sparseArray.indexOfKey(KEYS[i]));
     64         }
     65 
     66         // for key already exist, old value will be replaced
     67         int existKey = KEYS[0];
     68         Integer oldValue = VALUES[0]; // 0
     69         Integer newValue = 100;
     70         assertEquals(oldValue, sparseArray.get(existKey));
     71         assertEquals(LENGTH, sparseArray.size());
     72         sparseArray.put(existKey, newValue);
     73         assertEquals(newValue, sparseArray.get(existKey));
     74         assertEquals(LENGTH, sparseArray.size());
     75 
     76         assertEquals(VALUE_FOR_NON_EXISTED_KEY,
     77                 sparseArray.get(NON_EXISTED_KEY, VALUE_FOR_NON_EXISTED_KEY));
     78         assertNull(sparseArray.get(NON_EXISTED_KEY)); // the default value is null
     79 
     80         int size = sparseArray.size();
     81         sparseArray.append(NON_EXISTED_KEY, VALUE_FOR_NON_EXISTED_KEY);
     82         assertEquals(size + 1, sparseArray.size());
     83         assertEquals(size, sparseArray.indexOfKey(NON_EXISTED_KEY));
     84         assertEquals(size, sparseArray.indexOfValue(VALUE_FOR_NON_EXISTED_KEY));
     85         assertEquals(NON_EXISTED_KEY, sparseArray.keyAt(size));
     86         assertEquals(VALUE_FOR_NON_EXISTED_KEY, sparseArray.valueAt(size));
     87 
     88         sparseArray.setValueAt(size, VALUES[1]);
     89         assertTrue(VALUE_FOR_NON_EXISTED_KEY != sparseArray.valueAt(size));
     90         assertEquals(VALUES[1], sparseArray.valueAt(size));
     91 
     92         size = sparseArray.size();
     93         assertEquals(VALUES[1], sparseArray.get(KEYS[1]));
     94         assertFalse(VALUE_FOR_NON_EXISTED_KEY == VALUES[1]);
     95         sparseArray.delete(KEYS[1]);
     96         assertEquals(VALUE_FOR_NON_EXISTED_KEY,
     97                 sparseArray.get(KEYS[1], VALUE_FOR_NON_EXISTED_KEY));
     98         assertEquals(size - 1, sparseArray.size());
     99 
    100         size = sparseArray.size();
    101         assertEquals(VALUES[2], sparseArray.get(KEYS[2]));
    102         assertFalse(VALUE_FOR_NON_EXISTED_KEY == VALUES[2]);
    103         sparseArray.remove(KEYS[2]);
    104         assertEquals(VALUE_FOR_NON_EXISTED_KEY,
    105                 sparseArray.get(KEYS[2], VALUE_FOR_NON_EXISTED_KEY));
    106         assertEquals(size - 1, sparseArray.size());
    107 
    108         sparseArray.clear();
    109         assertEquals(0, sparseArray.size());
    110     }
    111 
    112     @Test
    113     public void testSparseArrayWithSpecifiedCapacity() {
    114         SparseArray<Integer> sparseArray = new SparseArray<Integer>(5);
    115         assertEquals(0, sparseArray.size());
    116 
    117         int length = VALUES.length;
    118 
    119         for (int i = 0; i < length; i++) {
    120             sparseArray.put(KEYS[i], VALUES[i]);
    121             assertEquals(i + 1, sparseArray.size());
    122         }
    123 
    124         for (int i = 0; i < length; i++) {
    125             assertEquals(VALUES[i], sparseArray.get(KEYS[i]));
    126         }
    127 
    128         for (int i = 0; i < length; i++) {
    129             assertEquals(sparseArray.indexOfValue(VALUES[i]), sparseArray.indexOfKey(KEYS[i]));
    130         }
    131 
    132         // for key already exist, old value will be replaced
    133         int existKey = KEYS[0];
    134         Integer oldValue = VALUES[0]; // 0
    135         Integer newValue = 100;
    136         assertEquals(oldValue, sparseArray.get(existKey));
    137         assertEquals(LENGTH, sparseArray.size());
    138         sparseArray.put(existKey, newValue);
    139         assertEquals(newValue, sparseArray.get(existKey));
    140         assertEquals(LENGTH, sparseArray.size());
    141 
    142         assertEquals(VALUE_FOR_NON_EXISTED_KEY,
    143                      sparseArray.get(NON_EXISTED_KEY, VALUE_FOR_NON_EXISTED_KEY));
    144         assertNull(sparseArray.get(NON_EXISTED_KEY)); // the default value is null
    145 
    146         int size = sparseArray.size();
    147         sparseArray.append(NON_EXISTED_KEY, VALUE_FOR_NON_EXISTED_KEY);
    148         assertEquals(size + 1, sparseArray.size());
    149         assertEquals(size, sparseArray.indexOfKey(NON_EXISTED_KEY));
    150         assertEquals(size, sparseArray.indexOfValue(VALUE_FOR_NON_EXISTED_KEY));
    151         assertEquals(NON_EXISTED_KEY, sparseArray.keyAt(size));
    152         assertEquals(VALUE_FOR_NON_EXISTED_KEY, sparseArray.valueAt(size));
    153 
    154         sparseArray.setValueAt(size, VALUES[1]);
    155         assertTrue(VALUE_FOR_NON_EXISTED_KEY != sparseArray.valueAt(size));
    156         assertEquals(VALUES[1], sparseArray.valueAt(size));
    157 
    158         size = sparseArray.size();
    159         assertEquals(VALUES[1], sparseArray.get(KEYS[1]));
    160         assertFalse(VALUE_FOR_NON_EXISTED_KEY == VALUES[1]);
    161         sparseArray.delete(KEYS[1]);
    162         assertEquals(VALUE_FOR_NON_EXISTED_KEY,
    163                 sparseArray.get(KEYS[1], VALUE_FOR_NON_EXISTED_KEY));
    164         assertEquals(size - 1, sparseArray.size());
    165 
    166         size = sparseArray.size();
    167         assertEquals(VALUES[2], sparseArray.get(KEYS[2]));
    168         assertFalse(VALUE_FOR_NON_EXISTED_KEY == VALUES[2]);
    169         sparseArray.remove(KEYS[2]);
    170         assertEquals(VALUE_FOR_NON_EXISTED_KEY,
    171                 sparseArray.get(KEYS[2], VALUE_FOR_NON_EXISTED_KEY));
    172         assertEquals(size - 1, sparseArray.size());
    173 
    174         sparseArray.clear();
    175         assertEquals(0, sparseArray.size());
    176     }
    177 }
    178