From 64a26b4389abb273afff7699a8c86596defd85bf Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Thu, 23 Aug 2012 17:47:13 +0900 Subject: [PATCH] Fix bug in InputPointers.append Change-Id: I39825757bbae32b66c4b61cb6c4fbcaa2e0c9c47 --- .../inputmethod/latin/InputPointers.java | 2 +- .../inputmethod/latin/InputPointersTests.java | 57 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/java/src/com/android/inputmethod/latin/InputPointers.java b/java/src/com/android/inputmethod/latin/InputPointers.java index c01a373f2..ff2feb51d 100644 --- a/java/src/com/android/inputmethod/latin/InputPointers.java +++ b/java/src/com/android/inputmethod/latin/InputPointers.java @@ -93,7 +93,7 @@ public class InputPointers { } mXCoordinates.append(xCoordinates, startPos, length); mYCoordinates.append(yCoordinates, startPos, length); - mPointerIds.fill(pointerId, startPos, length); + mPointerIds.fill(pointerId, mPointerIds.getLength(), length); mTimes.append(times, startPos, length); } diff --git a/tests/src/com/android/inputmethod/latin/InputPointersTests.java b/tests/src/com/android/inputmethod/latin/InputPointersTests.java index 6f04f3ebb..cc55076c0 100644 --- a/tests/src/com/android/inputmethod/latin/InputPointersTests.java +++ b/tests/src/com/android/inputmethod/latin/InputPointersTests.java @@ -18,6 +18,8 @@ package com.android.inputmethod.latin; import android.test.AndroidTestCase; +import java.util.Arrays; + public class InputPointersTests extends AndroidTestCase { private static final int DEFAULT_CAPACITY = 48; @@ -162,6 +164,61 @@ public class InputPointersTests extends AndroidTestCase { src.getTimes(), 0, dst.getTimes(), dstLen, srcLen); } + public void testAppendResizableIntArray() { + final int srcLen = 100; + final int srcPointerId = 1; + final int[] srcPointerIds = new int[srcLen]; + Arrays.fill(srcPointerIds, srcPointerId); + final ResizableIntArray srcTimes = new ResizableIntArray(DEFAULT_CAPACITY); + final ResizableIntArray srcXCoords = new ResizableIntArray(DEFAULT_CAPACITY); + final ResizableIntArray srcYCoords= new ResizableIntArray(DEFAULT_CAPACITY); + for (int i = 0; i < srcLen; i++) { + srcTimes.add(i * 2); + srcXCoords.add(i * 3); + srcYCoords.add(i * 4); + } + final int dstLen = 50; + final InputPointers dst = new InputPointers(DEFAULT_CAPACITY); + for (int i = 0; i < dstLen; i++) { + final int value = -i - 1; + dst.addPointer(value * 4, value * 3, value * 2, value); + } + final InputPointers dstCopy = new InputPointers(DEFAULT_CAPACITY); + dstCopy.copy(dst); + + dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, 0, 0); + assertEquals("size after append zero", dstLen, dst.getPointerSize()); + assertArrayEquals("xCoordinates after append zero", + dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen); + assertArrayEquals("yCoordinates after append zero", + dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen); + assertArrayEquals("pointerIds after append zero", + dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen); + assertArrayEquals("times after append zero", + dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen); + + dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, 0, srcLen); + assertEquals("size after append", dstLen + srcLen, dst.getPointerSize()); + assertTrue("primitive length after append", + dst.getPointerIds().length >= dstLen + srcLen); + assertArrayEquals("original xCoordinates values after append", + dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen); + assertArrayEquals("original yCoordinates values after append", + dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen); + assertArrayEquals("original pointerIds values after append", + dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen); + assertArrayEquals("original times values after append", + dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen); + assertArrayEquals("appended xCoordinates values after append", + srcXCoords.getPrimitiveArray(), 0, dst.getXCoordinates(), dstLen, srcLen); + assertArrayEquals("appended yCoordinates values after append", + srcYCoords.getPrimitiveArray(), 0, dst.getYCoordinates(), dstLen, srcLen); + assertArrayEquals("appended pointerIds values after append", + srcPointerIds, 0, dst.getPointerIds(), dstLen, srcLen); + assertArrayEquals("appended times values after append", + srcTimes.getPrimitiveArray(), 0, dst.getTimes(), dstLen, srcLen); + } + private static void assertArrayEquals(String message, int[] expecteds, int expectedPos, int[] actuals, int actualPos, int length) { if (expecteds == null && actuals == null) {