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