Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2012 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.inputmethod.latin.utils;
     18 
     19 import android.test.AndroidTestCase;
     20 import android.test.suitebuilder.annotation.SmallTest;
     21 
     22 import java.util.Arrays;
     23 
     24 @SmallTest
     25 public class ResizableIntArrayTests extends AndroidTestCase {
     26     private static final int DEFAULT_CAPACITY = 48;
     27 
     28     public void testNewInstance() {
     29         final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
     30         final int[] array = src.getPrimitiveArray();
     31         assertEquals("new instance length", 0, src.getLength());
     32         assertNotNull("new instance array", array);
     33         assertEquals("new instance array length", DEFAULT_CAPACITY, array.length);
     34     }
     35 
     36     public void testAdd() {
     37         final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
     38         final int[] array = src.getPrimitiveArray();
     39         int[] array2 = null, array3 = null;
     40         final int limit = DEFAULT_CAPACITY * 2 + 10;
     41         for (int i = 0; i < limit; i++) {
     42             src.add(i);
     43             assertEquals("length after add " + i, i + 1, src.getLength());
     44             if (i == DEFAULT_CAPACITY) {
     45                 array2 = src.getPrimitiveArray();
     46             }
     47             if (i == DEFAULT_CAPACITY * 2) {
     48                 array3 = src.getPrimitiveArray();
     49             }
     50             if (i < DEFAULT_CAPACITY) {
     51                 assertSame("array after add " + i, array, src.getPrimitiveArray());
     52             } else if (i < DEFAULT_CAPACITY * 2) {
     53                 assertSame("array after add " + i, array2, src.getPrimitiveArray());
     54             } else if (i < DEFAULT_CAPACITY * 3) {
     55                 assertSame("array after add " + i, array3, src.getPrimitiveArray());
     56             }
     57         }
     58         for (int i = 0; i < limit; i++) {
     59             assertEquals("value at " + i, i, src.get(i));
     60         }
     61     }
     62 
     63     public void testAddAt() {
     64         final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
     65         final int limit = DEFAULT_CAPACITY * 10, step = DEFAULT_CAPACITY * 2;
     66         for (int i = 0; i < limit; i += step) {
     67             src.add(i, i);
     68             assertEquals("length after add at " + i, i + 1, src.getLength());
     69         }
     70         for (int i = 0; i < limit; i += step) {
     71             assertEquals("value at " + i, i, src.get(i));
     72         }
     73     }
     74 
     75     public void testGet() {
     76         final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
     77         try {
     78             final int value = src.get(0);
     79             fail("get(0) shouldn't succeed");
     80         } catch (ArrayIndexOutOfBoundsException e) {
     81             // success
     82         }
     83         try {
     84             final int value = src.get(DEFAULT_CAPACITY);
     85             fail("get(DEFAULT_CAPACITY) shouldn't succeed");
     86         } catch (ArrayIndexOutOfBoundsException e) {
     87             // success
     88         }
     89 
     90         final int index = DEFAULT_CAPACITY / 2;
     91         src.add(index, 100);
     92         assertEquals("legth after add at " + index, index + 1, src.getLength());
     93         assertEquals("value after add at " + index, 100, src.get(index));
     94         assertEquals("value after add at 0", 0, src.get(0));
     95         try {
     96             final int value = src.get(src.getLength());
     97             fail("get(length) shouldn't succeed");
     98         } catch (ArrayIndexOutOfBoundsException e) {
     99             // success
    100         }
    101     }
    102 
    103     public void testReset() {
    104         final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
    105         final int[] array = src.getPrimitiveArray();
    106         for (int i = 0; i < DEFAULT_CAPACITY; i++) {
    107             src.add(i);
    108             assertEquals("length after add " + i, i + 1, src.getLength());
    109         }
    110 
    111         final int smallerLength = DEFAULT_CAPACITY / 2;
    112         src.reset(smallerLength);
    113         final int[] array2 = src.getPrimitiveArray();
    114         assertEquals("length after reset", 0, src.getLength());
    115         assertNotSame("array after reset", array, array2);
    116 
    117         int[] array3 = null;
    118         for (int i = 0; i < DEFAULT_CAPACITY; i++) {
    119             src.add(i);
    120             assertEquals("length after add " + i, i + 1, src.getLength());
    121             if (i == smallerLength) {
    122                 array3 = src.getPrimitiveArray();
    123             }
    124             if (i < smallerLength) {
    125                 assertSame("array after add " + i, array2, src.getPrimitiveArray());
    126             } else if (i < smallerLength * 2) {
    127                 assertSame("array after add " + i, array3, src.getPrimitiveArray());
    128             }
    129         }
    130     }
    131 
    132     public void testSetLength() {
    133         final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
    134         final int[] array = src.getPrimitiveArray();
    135         for (int i = 0; i < DEFAULT_CAPACITY; i++) {
    136             src.add(i);
    137             assertEquals("length after add " + i, i + 1, src.getLength());
    138         }
    139 
    140         final int largerLength = DEFAULT_CAPACITY * 2;
    141         src.setLength(largerLength);
    142         final int[] array2 = src.getPrimitiveArray();
    143         assertEquals("length after larger setLength", largerLength, src.getLength());
    144         assertNotSame("array after larger setLength", array, array2);
    145         assertEquals("array length after larger setLength", largerLength, array2.length);
    146         for (int i = 0; i < largerLength; i++) {
    147             final int v = src.get(i);
    148             if (i < DEFAULT_CAPACITY) {
    149                 assertEquals("value at " + i, i, v);
    150             } else {
    151                 assertEquals("value at " + i, 0, v);
    152             }
    153         }
    154 
    155         final int smallerLength = DEFAULT_CAPACITY / 2;
    156         src.setLength(smallerLength);
    157         final int[] array3 = src.getPrimitiveArray();
    158         assertEquals("length after smaller setLength", smallerLength, src.getLength());
    159         assertSame("array after smaller setLength", array2, array3);
    160         assertEquals("array length after smaller setLength", largerLength, array3.length);
    161         for (int i = 0; i < smallerLength; i++) {
    162             assertEquals("value at " + i, i, src.get(i));
    163         }
    164     }
    165 
    166     public void testSet() {
    167         final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
    168         final int limit = DEFAULT_CAPACITY * 2 + 10;
    169         for (int i = 0; i < limit; i++) {
    170             src.add(i);
    171         }
    172 
    173         final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY);
    174         dst.set(src);
    175         assertEquals("length after set", dst.getLength(), src.getLength());
    176         assertSame("array after set", dst.getPrimitiveArray(), src.getPrimitiveArray());
    177     }
    178 
    179     public void testCopy() {
    180         final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
    181         for (int i = 0; i < DEFAULT_CAPACITY; i++) {
    182             src.add(i);
    183         }
    184 
    185         final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY);
    186         final int[] array = dst.getPrimitiveArray();
    187         dst.copy(src);
    188         assertEquals("length after copy", dst.getLength(), src.getLength());
    189         assertSame("array after copy", array, dst.getPrimitiveArray());
    190         assertNotSame("array after copy", dst.getPrimitiveArray(), src.getPrimitiveArray());
    191         assertIntArrayEquals("values after copy",
    192                 dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength());
    193 
    194         final int smallerLength = DEFAULT_CAPACITY / 2;
    195         dst.reset(smallerLength);
    196         final int[] array2 = dst.getPrimitiveArray();
    197         dst.copy(src);
    198         final int[] array3 = dst.getPrimitiveArray();
    199         assertEquals("length after copy to smaller", dst.getLength(), src.getLength());
    200         assertNotSame("array after copy to smaller", array2, array3);
    201         assertNotSame("array after copy to smaller", array3, src.getPrimitiveArray());
    202         assertIntArrayEquals("values after copy to smaller",
    203                 dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength());
    204     }
    205 
    206     public void testAppend() {
    207         final int srcLen = DEFAULT_CAPACITY;
    208         final ResizableIntArray src = new ResizableIntArray(srcLen);
    209         for (int i = 0; i < srcLen; i++) {
    210             src.add(i);
    211         }
    212         final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY * 2);
    213         final int[] array = dst.getPrimitiveArray();
    214         final int dstLen = DEFAULT_CAPACITY / 2;
    215         for (int i = 0; i < dstLen; i++) {
    216             final int value = -i - 1;
    217             dst.add(value);
    218         }
    219         final ResizableIntArray dstCopy = new ResizableIntArray(dst.getLength());
    220         dstCopy.copy(dst);
    221 
    222         dst.append(src, 0, 0);
    223         assertEquals("length after append zero", dstLen, dst.getLength());
    224         assertSame("array after append zero", array, dst.getPrimitiveArray());
    225         assertIntArrayEquals("values after append zero",
    226                 dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen);
    227 
    228         dst.append(src, 0, srcLen);
    229         assertEquals("length after append", dstLen + srcLen, dst.getLength());
    230         assertSame("array after append", array, dst.getPrimitiveArray());
    231         assertTrue("primitive length after append",
    232                 dst.getPrimitiveArray().length >= dstLen + srcLen);
    233         assertIntArrayEquals("original values after append",
    234                 dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen);
    235         assertIntArrayEquals("appended values after append",
    236                 src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen, srcLen);
    237 
    238         dst.append(src, 0, srcLen);
    239         assertEquals("length after 2nd append", dstLen + srcLen * 2, dst.getLength());
    240         assertNotSame("array after 2nd append", array, dst.getPrimitiveArray());
    241         assertTrue("primitive length after 2nd append",
    242                 dst.getPrimitiveArray().length >= dstLen + srcLen * 2);
    243         assertIntArrayEquals("original values after 2nd append",
    244                 dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen);
    245         assertIntArrayEquals("appended values after 2nd append",
    246                 src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen, srcLen);
    247         assertIntArrayEquals("appended values after 2nd append",
    248                 src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen + srcLen, srcLen);
    249     }
    250 
    251     public void testFill() {
    252         final int srcLen = DEFAULT_CAPACITY;
    253         final ResizableIntArray src = new ResizableIntArray(srcLen);
    254         for (int i = 0; i < srcLen; i++) {
    255             src.add(i);
    256         }
    257         final int[] array = src.getPrimitiveArray();
    258 
    259         final int startPos = srcLen / 3;
    260         final int length = srcLen / 3;
    261         final int endPos = startPos + length;
    262         assertTrue(startPos >= 1);
    263         final int value = 123;
    264         try {
    265             src.fill(value, -1, length);
    266             fail("fill from -1 shouldn't succeed");
    267         } catch (IllegalArgumentException e) {
    268             // success
    269         }
    270         try {
    271             src.fill(value, startPos, -1);
    272             fail("fill negative length shouldn't succeed");
    273         } catch (IllegalArgumentException e) {
    274             // success
    275         }
    276 
    277         src.fill(value, startPos, length);
    278         assertEquals("length after fill", srcLen, src.getLength());
    279         assertSame("array after fill", array, src.getPrimitiveArray());
    280         for (int i = 0; i < srcLen; i++) {
    281             final int v = src.get(i);
    282             if (i >= startPos && i < endPos) {
    283                 assertEquals("new values after fill at " + i, value, v);
    284             } else {
    285                 assertEquals("unmodified values after fill at " + i, i, v);
    286             }
    287         }
    288 
    289         final int length2 = srcLen * 2 - startPos;
    290         final int largeEnd = startPos + length2;
    291         assertTrue(largeEnd > srcLen);
    292         final int value2 = 456;
    293         src.fill(value2, startPos, length2);
    294         assertEquals("length after large fill", largeEnd, src.getLength());
    295         assertNotSame("array after large fill", array, src.getPrimitiveArray());
    296         for (int i = 0; i < largeEnd; i++) {
    297             final int v = src.get(i);
    298             if (i >= startPos && i < largeEnd) {
    299                 assertEquals("new values after large fill at " + i, value2, v);
    300             } else {
    301                 assertEquals("unmodified values after large fill at " + i, i, v);
    302             }
    303         }
    304 
    305         final int startPos2 = largeEnd + length2;
    306         final int endPos2 = startPos2 + length2;
    307         final int value3 = 789;
    308         src.fill(value3, startPos2, length2);
    309         assertEquals("length after disjoint fill", endPos2, src.getLength());
    310         for (int i = 0; i < endPos2; i++) {
    311             final int v = src.get(i);
    312             if (i >= startPos2 && i < endPos2) {
    313                 assertEquals("new values after disjoint fill at " + i, value3, v);
    314             } else if (i >= startPos && i < largeEnd) {
    315                 assertEquals("unmodified values after disjoint fill at " + i, value2, v);
    316             } else if (i < startPos) {
    317                 assertEquals("unmodified values after disjoint fill at " + i, i, v);
    318             } else {
    319                 assertEquals("gap values after disjoint fill at " + i, 0, v);
    320             }
    321         }
    322     }
    323 
    324     private static void assertIntArrayEquals(final String message, final int[] expecteds,
    325             final int expectedPos, final int[] actuals, final int actualPos, final int length) {
    326         if (expecteds == actuals) {
    327             return;
    328         }
    329         if (expecteds == null || actuals == null) {
    330             assertEquals(message, Arrays.toString(expecteds), Arrays.toString(actuals));
    331             return;
    332         }
    333         if (expecteds.length < expectedPos + length || actuals.length < actualPos + length) {
    334             fail(message + ": insufficient length: expecteds=" + Arrays.toString(expecteds)
    335                     + " actuals=" + Arrays.toString(actuals));
    336             return;
    337         }
    338         for (int i = 0; i < length; i++) {
    339             assertEquals(message + " [" + i + "]",
    340                     expecteds[i + expectedPos], actuals[i + actualPos]);
    341         }
    342     }
    343 
    344     public void testShift() {
    345         final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
    346         final int limit = DEFAULT_CAPACITY * 10;
    347         final int shiftAmount = 20;
    348         for (int i = 0; i < limit; ++i) {
    349             src.add(i, i);
    350             assertEquals("length after add at " + i, i + 1, src.getLength());
    351         }
    352         src.shift(shiftAmount);
    353         for (int i = 0; i < limit - shiftAmount; ++i) {
    354             assertEquals("value at " + i, i + shiftAmount, src.get(i));
    355         }
    356     }
    357 }
    358