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");
      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;
     18 
     19 import android.test.AndroidTestCase;
     20 import android.test.suitebuilder.annotation.SmallTest;
     21 
     22 import com.android.inputmethod.latin.utils.ResizableIntArray;
     23 
     24 import java.util.Arrays;
     25 
     26 @SmallTest
     27 public class InputPointersTests extends AndroidTestCase {
     28     private static final int DEFAULT_CAPACITY = 48;
     29 
     30     public void testNewInstance() {
     31         final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
     32         assertEquals("new instance size", 0, src.getPointerSize());
     33         assertNotNull("new instance xCoordinates", src.getXCoordinates());
     34         assertNotNull("new instance yCoordinates", src.getYCoordinates());
     35         assertNotNull("new instance pointerIds", src.getPointerIds());
     36         assertNotNull("new instance times", src.getTimes());
     37     }
     38 
     39     public void testReset() {
     40         final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
     41         final int[] xCoordinates = src.getXCoordinates();
     42         final int[] yCoordinates = src.getXCoordinates();
     43         final int[] pointerIds = src.getXCoordinates();
     44         final int[] times = src.getXCoordinates();
     45 
     46         src.reset();
     47         assertEquals("size after reset", 0, src.getPointerSize());
     48         assertNotSame("xCoordinates after reset", xCoordinates, src.getXCoordinates());
     49         assertNotSame("yCoordinates after reset", yCoordinates, src.getYCoordinates());
     50         assertNotSame("pointerIds after reset", pointerIds, src.getPointerIds());
     51         assertNotSame("times after reset", times, src.getTimes());
     52     }
     53 
     54     public void testAdd() {
     55         final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
     56         final int limit = src.getXCoordinates().length * 2 + 10;
     57         for (int i = 0; i < limit; i++) {
     58             src.addPointer(i, i * 2, i * 3, i * 4);
     59             assertEquals("size after add " + i, i + 1, src.getPointerSize());
     60         }
     61         for (int i = 0; i < limit; i++) {
     62             assertEquals("xCoordinates at " + i, i, src.getXCoordinates()[i]);
     63             assertEquals("yCoordinates at " + i, i * 2, src.getYCoordinates()[i]);
     64             assertEquals("pointerIds at " + i, i * 3, src.getPointerIds()[i]);
     65             assertEquals("times at " + i, i * 4, src.getTimes()[i]);
     66         }
     67     }
     68 
     69     public void testAddAt() {
     70         final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
     71         final int limit = 1000, step = 100;
     72         for (int i = 0; i < limit; i += step) {
     73             src.addPointer(i, i, i * 2, i * 3, i * 4);
     74             assertEquals("size after add at " + i, i + 1, src.getPointerSize());
     75         }
     76         for (int i = 0; i < limit; i += step) {
     77             assertEquals("xCoordinates at " + i, i, src.getXCoordinates()[i]);
     78             assertEquals("yCoordinates at " + i, i * 2, src.getYCoordinates()[i]);
     79             assertEquals("pointerIds at " + i, i * 3, src.getPointerIds()[i]);
     80             assertEquals("times at " + i, i * 4, src.getTimes()[i]);
     81         }
     82     }
     83 
     84     public void testSet() {
     85         final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
     86         final int limit = src.getXCoordinates().length * 2 + 10;
     87         for (int i = 0; i < limit; i++) {
     88             src.addPointer(i, i * 2, i * 3, i * 4);
     89         }
     90         final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
     91         dst.set(src);
     92         assertEquals("size after set", dst.getPointerSize(), src.getPointerSize());
     93         assertSame("xCoordinates after set", dst.getXCoordinates(), src.getXCoordinates());
     94         assertSame("yCoordinates after set", dst.getYCoordinates(), src.getYCoordinates());
     95         assertSame("pointerIds after set", dst.getPointerIds(), src.getPointerIds());
     96         assertSame("times after set", dst.getTimes(), src.getTimes());
     97     }
     98 
     99     public void testCopy() {
    100         final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
    101         final int limit = 100;
    102         for (int i = 0; i < limit; i++) {
    103             src.addPointer(i, i * 2, i * 3, i * 4);
    104         }
    105         final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
    106         dst.copy(src);
    107         assertEquals("size after copy", dst.getPointerSize(), src.getPointerSize());
    108         assertNotSame("xCoordinates after copy", dst.getXCoordinates(), src.getXCoordinates());
    109         assertNotSame("yCoordinates after copy", dst.getYCoordinates(), src.getYCoordinates());
    110         assertNotSame("pointerIds after copy", dst.getPointerIds(), src.getPointerIds());
    111         assertNotSame("times after copy", dst.getTimes(), src.getTimes());
    112         final int size = dst.getPointerSize();
    113         assertIntArrayEquals("xCoordinates values after copy",
    114                 dst.getXCoordinates(), 0, src.getXCoordinates(), 0, size);
    115         assertIntArrayEquals("yCoordinates values after copy",
    116                 dst.getYCoordinates(), 0, src.getYCoordinates(), 0, size);
    117         assertIntArrayEquals("pointerIds values after copy",
    118                 dst.getPointerIds(), 0, src.getPointerIds(), 0, size);
    119         assertIntArrayEquals("times values after copy",
    120                 dst.getTimes(), 0, src.getTimes(), 0, size);
    121     }
    122 
    123     public void testAppend() {
    124         final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
    125         final int srcLen = 100;
    126         for (int i = 0; i < srcLen; i++) {
    127             src.addPointer(i, i * 2, i * 3, i * 4);
    128         }
    129         final int dstLen = 50;
    130         final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
    131         for (int i = 0; i < dstLen; i++) {
    132             final int value = -i - 1;
    133             dst.addPointer(value * 4, value * 3, value * 2, value);
    134         }
    135         final InputPointers dstCopy = new InputPointers(DEFAULT_CAPACITY);
    136         dstCopy.copy(dst);
    137 
    138         dst.append(src, 0, 0);
    139         assertEquals("size after append zero", dstLen, dst.getPointerSize());
    140         assertIntArrayEquals("xCoordinates after append zero",
    141                 dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen);
    142         assertIntArrayEquals("yCoordinates after append zero",
    143                 dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen);
    144         assertIntArrayEquals("pointerIds after append zero",
    145                 dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen);
    146         assertIntArrayEquals("times after append zero",
    147                 dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen);
    148 
    149         dst.append(src, 0, srcLen);
    150         assertEquals("size after append", dstLen + srcLen, dst.getPointerSize());
    151         assertTrue("primitive length after append",
    152                 dst.getPointerIds().length >= dstLen + srcLen);
    153         assertIntArrayEquals("original xCoordinates values after append",
    154                 dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen);
    155         assertIntArrayEquals("original yCoordinates values after append",
    156                 dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen);
    157         assertIntArrayEquals("original pointerIds values after append",
    158                 dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen);
    159         assertIntArrayEquals("original times values after append",
    160                 dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen);
    161         assertIntArrayEquals("appended xCoordinates values after append",
    162                 src.getXCoordinates(), 0, dst.getXCoordinates(), dstLen, srcLen);
    163         assertIntArrayEquals("appended yCoordinates values after append",
    164                 src.getYCoordinates(), 0, dst.getYCoordinates(), dstLen, srcLen);
    165         assertIntArrayEquals("appended pointerIds values after append",
    166                 src.getPointerIds(), 0, dst.getPointerIds(), dstLen, srcLen);
    167         assertIntArrayEquals("appended times values after append",
    168                 src.getTimes(), 0, dst.getTimes(), dstLen, srcLen);
    169     }
    170 
    171     public void testAppendResizableIntArray() {
    172         final int srcLen = 100;
    173         final int srcPointerId = 1;
    174         final int[] srcPointerIds = new int[srcLen];
    175         Arrays.fill(srcPointerIds, srcPointerId);
    176         final ResizableIntArray srcTimes = new ResizableIntArray(DEFAULT_CAPACITY);
    177         final ResizableIntArray srcXCoords = new ResizableIntArray(DEFAULT_CAPACITY);
    178         final ResizableIntArray srcYCoords= new ResizableIntArray(DEFAULT_CAPACITY);
    179         for (int i = 0; i < srcLen; i++) {
    180             srcTimes.add(i * 2);
    181             srcXCoords.add(i * 3);
    182             srcYCoords.add(i * 4);
    183         }
    184         final int dstLen = 50;
    185         final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
    186         for (int i = 0; i < dstLen; i++) {
    187             final int value = -i - 1;
    188             dst.addPointer(value * 4, value * 3, value * 2, value);
    189         }
    190         final InputPointers dstCopy = new InputPointers(DEFAULT_CAPACITY);
    191         dstCopy.copy(dst);
    192 
    193         dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, 0, 0);
    194         assertEquals("size after append zero", dstLen, dst.getPointerSize());
    195         assertIntArrayEquals("xCoordinates after append zero",
    196                 dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen);
    197         assertIntArrayEquals("yCoordinates after append zero",
    198                 dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen);
    199         assertIntArrayEquals("pointerIds after append zero",
    200                 dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen);
    201         assertIntArrayEquals("times after append zero",
    202                 dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen);
    203 
    204         dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, 0, srcLen);
    205         assertEquals("size after append", dstLen + srcLen, dst.getPointerSize());
    206         assertTrue("primitive length after append",
    207                 dst.getPointerIds().length >= dstLen + srcLen);
    208         assertIntArrayEquals("original xCoordinates values after append",
    209                 dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen);
    210         assertIntArrayEquals("original yCoordinates values after append",
    211                 dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen);
    212         assertIntArrayEquals("original pointerIds values after append",
    213                 dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen);
    214         assertIntArrayEquals("original times values after append",
    215                 dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen);
    216         assertIntArrayEquals("appended xCoordinates values after append",
    217                 srcXCoords.getPrimitiveArray(), 0, dst.getXCoordinates(), dstLen, srcLen);
    218         assertIntArrayEquals("appended yCoordinates values after append",
    219                 srcYCoords.getPrimitiveArray(), 0, dst.getYCoordinates(), dstLen, srcLen);
    220         assertIntArrayEquals("appended pointerIds values after append",
    221                 srcPointerIds, 0, dst.getPointerIds(), dstLen, srcLen);
    222         assertIntArrayEquals("appended times values after append",
    223                 srcTimes.getPrimitiveArray(), 0, dst.getTimes(), dstLen, srcLen);
    224     }
    225 
    226     // TODO: Consolidate this method with
    227     // {@link ResizableIntArrayTests#assertIntArrayEquals(String,int[],int,int[],int,int)}.
    228     private static void assertIntArrayEquals(final String message, final int[] expecteds,
    229             final int expectedPos, final int[] actuals, final int actualPos, final int length) {
    230         if (expecteds == actuals) {
    231             return;
    232         }
    233         if (expecteds == null || actuals == null) {
    234             assertEquals(message, Arrays.toString(expecteds), Arrays.toString(actuals));
    235             return;
    236         }
    237         if (expecteds.length < expectedPos + length || actuals.length < actualPos + length) {
    238             fail(message + ": insufficient length: expecteds=" + Arrays.toString(expecteds)
    239                     + " actuals=" + Arrays.toString(actuals));
    240             return;
    241         }
    242         for (int i = 0; i < length; i++) {
    243             assertEquals(message + " [" + i + "]",
    244                     expecteds[i + expectedPos], actuals[i + actualPos]);
    245         }
    246     }
    247 
    248     public void testShift() {
    249         final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
    250         final int limit = 100;
    251         final int shiftAmount = 20;
    252         for (int i = 0; i < limit; i++) {
    253             src.addPointer(i, i * 2, i * 3, i * 4);
    254         }
    255         src.shift(shiftAmount);
    256         for (int i = 0; i < limit - shiftAmount; ++i) {
    257             assertEquals("xCoordinates at " + i, i + shiftAmount, src.getXCoordinates()[i]);
    258             assertEquals("yCoordinates at " + i, (i + shiftAmount) * 2, src.getYCoordinates()[i]);
    259             assertEquals("pointerIds at " + i, (i + shiftAmount) * 3, src.getPointerIds()[i]);
    260             assertEquals("times at " + i, (i + shiftAmount) * 4, src.getTimes()[i]);
    261         }
    262     }
    263 }
    264