From 36799b2aa2982ec17341cd2c5ed81e608bcee8c6 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Wed, 29 Oct 2014 11:37:21 +0900 Subject: Move InputPointers and ResizableIntArray to common Bug: 18108776 Change-Id: Ie5c0430aa41a8d6c58bf510a10173223d7dfe9fe --- .../inputmethod/latin/common/InputPointers.java | 157 +++++++++ .../latin/common/ResizableIntArray.java | 155 +++++++++ .../latin/touchinputconsumer/GestureConsumer.java | 2 +- .../keyboard/KeyboardActionListener.java | 2 +- .../inputmethod/keyboard/PointerTracker.java | 2 +- .../keyboard/internal/BatchInputArbiter.java | 2 +- .../internal/GestureStrokeDrawingPoints.java | 2 +- .../internal/GestureStrokeRecognitionPoints.java | 4 +- .../internal/GestureTrailDrawingPoints.java | 2 +- .../inputmethod/latin/BinaryDictionary.java | 1 + .../android/inputmethod/latin/InputPointers.java | 189 ----------- .../inputmethod/latin/LastComposedWord.java | 1 + .../com/android/inputmethod/latin/LatinIME.java | 1 + .../android/inputmethod/latin/WordComposer.java | 1 + .../inputmethod/latin/inputlogic/InputLogic.java | 2 +- .../latin/inputlogic/InputLogicHandler.java | 2 +- .../inputmethod/latin/utils/ResizableIntArray.java | 155 --------- .../inputmethod/latin/InputPointersTests.java | 326 ------------------ .../android/inputmethod/latin/InputTestsBase.java | 1 + .../latin/common/InputPointersTests.java | 324 ++++++++++++++++++ .../latin/common/ResizableIntArrayTests.java | 377 +++++++++++++++++++++ .../latin/utils/ResizableIntArrayTests.java | 377 --------------------- tools/dicttool/Android.mk | 4 +- 23 files changed, 1029 insertions(+), 1060 deletions(-) create mode 100644 common/src/com/android/inputmethod/latin/common/InputPointers.java create mode 100644 common/src/com/android/inputmethod/latin/common/ResizableIntArray.java delete mode 100644 java/src/com/android/inputmethod/latin/InputPointers.java delete mode 100644 java/src/com/android/inputmethod/latin/utils/ResizableIntArray.java delete mode 100644 tests/src/com/android/inputmethod/latin/InputPointersTests.java create mode 100644 tests/src/com/android/inputmethod/latin/common/InputPointersTests.java create mode 100644 tests/src/com/android/inputmethod/latin/common/ResizableIntArrayTests.java delete mode 100644 tests/src/com/android/inputmethod/latin/utils/ResizableIntArrayTests.java diff --git a/common/src/com/android/inputmethod/latin/common/InputPointers.java b/common/src/com/android/inputmethod/latin/common/InputPointers.java new file mode 100644 index 000000000..40131aca4 --- /dev/null +++ b/common/src/com/android/inputmethod/latin/common/InputPointers.java @@ -0,0 +1,157 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.common; + +import com.android.inputmethod.annotations.UsedForTesting; + +// TODO: This class is not thread-safe. +public final class InputPointers { + private static final boolean DEBUG_TIME = false; + + private final int mDefaultCapacity; + private final ResizableIntArray mXCoordinates; + private final ResizableIntArray mYCoordinates; + private final ResizableIntArray mPointerIds; + private final ResizableIntArray mTimes; + + public InputPointers(int defaultCapacity) { + mDefaultCapacity = defaultCapacity; + mXCoordinates = new ResizableIntArray(defaultCapacity); + mYCoordinates = new ResizableIntArray(defaultCapacity); + mPointerIds = new ResizableIntArray(defaultCapacity); + mTimes = new ResizableIntArray(defaultCapacity); + } + + private void fillWithLastTimeUntil(final int index) { + final int fromIndex = mTimes.getLength(); + // Fill the gap with the latest time. + // See {@link #getTime(int)} and {@link #isValidTimeStamps()}. + if (fromIndex <= 0) { + return; + } + final int fillLength = index - fromIndex + 1; + if (fillLength <= 0) { + return; + } + final int lastTime = mTimes.get(fromIndex - 1); + mTimes.fill(lastTime, fromIndex, fillLength); + } + + public void addPointerAt(int index, int x, int y, int pointerId, int time) { + mXCoordinates.addAt(index, x); + mYCoordinates.addAt(index, y); + mPointerIds.addAt(index, pointerId); + if (DEBUG_TIME) { + fillWithLastTimeUntil(index); + } + mTimes.addAt(index, time); + } + + @UsedForTesting + public void addPointer(int x, int y, int pointerId, int time) { + mXCoordinates.add(x); + mYCoordinates.add(y); + mPointerIds.add(pointerId); + mTimes.add(time); + } + + public void set(InputPointers ip) { + mXCoordinates.set(ip.mXCoordinates); + mYCoordinates.set(ip.mYCoordinates); + mPointerIds.set(ip.mPointerIds); + mTimes.set(ip.mTimes); + } + + public void copy(InputPointers ip) { + mXCoordinates.copy(ip.mXCoordinates); + mYCoordinates.copy(ip.mYCoordinates); + mPointerIds.copy(ip.mPointerIds); + mTimes.copy(ip.mTimes); + } + + /** + * Append the times, x-coordinates and y-coordinates in the specified {@link ResizableIntArray} + * to the end of this. + * @param pointerId the pointer id of the source. + * @param times the source {@link ResizableIntArray} to read the event times from. + * @param xCoordinates the source {@link ResizableIntArray} to read the x-coordinates from. + * @param yCoordinates the source {@link ResizableIntArray} to read the y-coordinates from. + * @param startPos the starting index of the data in {@code times} and etc. + * @param length the number of data to be appended. + */ + public void append(int pointerId, ResizableIntArray times, ResizableIntArray xCoordinates, + ResizableIntArray yCoordinates, int startPos, int length) { + if (length == 0) { + return; + } + mXCoordinates.append(xCoordinates, startPos, length); + mYCoordinates.append(yCoordinates, startPos, length); + mPointerIds.fill(pointerId, mPointerIds.getLength(), length); + mTimes.append(times, startPos, length); + } + + /** + * Shift to the left by elementCount, discarding elementCount pointers at the start. + * @param elementCount how many elements to shift. + */ + public void shift(final int elementCount) { + mXCoordinates.shift(elementCount); + mYCoordinates.shift(elementCount); + mPointerIds.shift(elementCount); + mTimes.shift(elementCount); + } + + public void reset() { + final int defaultCapacity = mDefaultCapacity; + mXCoordinates.reset(defaultCapacity); + mYCoordinates.reset(defaultCapacity); + mPointerIds.reset(defaultCapacity); + mTimes.reset(defaultCapacity); + } + + public int getPointerSize() { + return mXCoordinates.getLength(); + } + + public int[] getXCoordinates() { + return mXCoordinates.getPrimitiveArray(); + } + + public int[] getYCoordinates() { + return mYCoordinates.getPrimitiveArray(); + } + + public int[] getPointerIds() { + return mPointerIds.getPrimitiveArray(); + } + + /** + * Gets the time each point was registered, in milliseconds, relative to the first event in the + * sequence. + * @return The time each point was registered, in milliseconds, relative to the first event in + * the sequence. + */ + public int[] getTimes() { + return mTimes.getPrimitiveArray(); + } + + @Override + public String toString() { + return "size=" + getPointerSize() + " id=" + mPointerIds + " time=" + mTimes + + " x=" + mXCoordinates + " y=" + mYCoordinates; + } +} diff --git a/common/src/com/android/inputmethod/latin/common/ResizableIntArray.java b/common/src/com/android/inputmethod/latin/common/ResizableIntArray.java new file mode 100644 index 000000000..ea23d8a33 --- /dev/null +++ b/common/src/com/android/inputmethod/latin/common/ResizableIntArray.java @@ -0,0 +1,155 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.common; + +import java.util.Arrays; + +// TODO: This class is not thread-safe. +public final class ResizableIntArray { + private int[] mArray; + private int mLength; + + public ResizableIntArray(final int capacity) { + reset(capacity); + } + + public int get(final int index) { + if (index < mLength) { + return mArray[index]; + } + throw new ArrayIndexOutOfBoundsException("length=" + mLength + "; index=" + index); + } + + public void addAt(final int index, final int val) { + if (index < mLength) { + mArray[index] = val; + } else { + mLength = index; + add(val); + } + } + + public void add(final int val) { + final int currentLength = mLength; + ensureCapacity(currentLength + 1); + mArray[currentLength] = val; + mLength = currentLength + 1; + } + + /** + * Calculate the new capacity of {@code mArray}. + * @param minimumCapacity the minimum capacity that the {@code mArray} should have. + * @return the new capacity that the {@code mArray} should have. Returns zero when there is no + * need to expand {@code mArray}. + */ + private int calculateCapacity(final int minimumCapacity) { + final int currentCapcity = mArray.length; + if (currentCapcity < minimumCapacity) { + final int nextCapacity = currentCapcity * 2; + // The following is the same as return Math.max(minimumCapacity, nextCapacity); + return minimumCapacity > nextCapacity ? minimumCapacity : nextCapacity; + } + return 0; + } + + private void ensureCapacity(final int minimumCapacity) { + final int newCapacity = calculateCapacity(minimumCapacity); + if (newCapacity > 0) { + // TODO: Implement primitive array pool. + mArray = Arrays.copyOf(mArray, newCapacity); + } + } + + public int getLength() { + return mLength; + } + + public void setLength(final int newLength) { + ensureCapacity(newLength); + mLength = newLength; + } + + public void reset(final int capacity) { + // TODO: Implement primitive array pool. + mArray = new int[capacity]; + mLength = 0; + } + + public int[] getPrimitiveArray() { + return mArray; + } + + public void set(final ResizableIntArray ip) { + // TODO: Implement primitive array pool. + mArray = ip.mArray; + mLength = ip.mLength; + } + + public void copy(final ResizableIntArray ip) { + final int newCapacity = calculateCapacity(ip.mLength); + if (newCapacity > 0) { + // TODO: Implement primitive array pool. + mArray = new int[newCapacity]; + } + System.arraycopy(ip.mArray, 0, mArray, 0, ip.mLength); + mLength = ip.mLength; + } + + public void append(final ResizableIntArray src, final int startPos, final int length) { + if (length == 0) { + return; + } + final int currentLength = mLength; + final int newLength = currentLength + length; + ensureCapacity(newLength); + System.arraycopy(src.mArray, startPos, mArray, currentLength, length); + mLength = newLength; + } + + public void fill(final int value, final int startPos, final int length) { + if (startPos < 0 || length < 0) { + throw new IllegalArgumentException("startPos=" + startPos + "; length=" + length); + } + final int endPos = startPos + length; + ensureCapacity(endPos); + Arrays.fill(mArray, startPos, endPos, value); + if (mLength < endPos) { + mLength = endPos; + } + } + + /** + * Shift to the left by elementCount, discarding elementCount pointers at the start. + * @param elementCount how many elements to shift. + */ + public void shift(final int elementCount) { + System.arraycopy(mArray, elementCount, mArray, 0, mLength - elementCount); + mLength -= elementCount; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder(); + for (int i = 0; i < mLength; i++) { + if (i != 0) { + sb.append(","); + } + sb.append(mArray[i]); + } + return "[" + sb + "]"; + } +} diff --git a/java-overridable/src/com/android/inputmethod/latin/touchinputconsumer/GestureConsumer.java b/java-overridable/src/com/android/inputmethod/latin/touchinputconsumer/GestureConsumer.java index 1d60e0cf5..4a44aaa94 100644 --- a/java-overridable/src/com/android/inputmethod/latin/touchinputconsumer/GestureConsumer.java +++ b/java-overridable/src/com/android/inputmethod/latin/touchinputconsumer/GestureConsumer.java @@ -19,8 +19,8 @@ package com.android.inputmethod.latin.touchinputconsumer; import android.view.inputmethod.EditorInfo; import com.android.inputmethod.keyboard.Keyboard; -import com.android.inputmethod.latin.InputPointers; import com.android.inputmethod.latin.SuggestedWords; +import com.android.inputmethod.latin.common.InputPointers; import com.android.inputmethod.latin.inputlogic.PrivateCommandPerformer; import java.util.List; diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java b/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java index ac66f7c6a..cdd632bc8 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardActionListener.java @@ -16,8 +16,8 @@ package com.android.inputmethod.keyboard; -import com.android.inputmethod.latin.InputPointers; import com.android.inputmethod.latin.common.Constants; +import com.android.inputmethod.latin.common.InputPointers; public interface KeyboardActionListener { /** diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java index fe6270fb5..5a540ff17 100644 --- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java +++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java @@ -31,9 +31,9 @@ import com.android.inputmethod.keyboard.internal.GestureStrokeDrawingPoints; import com.android.inputmethod.keyboard.internal.GestureStrokeRecognitionParams; import com.android.inputmethod.keyboard.internal.PointerTrackerQueue; import com.android.inputmethod.keyboard.internal.TypingTimeRecorder; -import com.android.inputmethod.latin.InputPointers; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.common.Constants; +import com.android.inputmethod.latin.common.InputPointers; import com.android.inputmethod.latin.define.DebugFlags; import com.android.inputmethod.latin.settings.Settings; import com.android.inputmethod.latin.utils.CoordinateUtils; diff --git a/java/src/com/android/inputmethod/keyboard/internal/BatchInputArbiter.java b/java/src/com/android/inputmethod/keyboard/internal/BatchInputArbiter.java index efb365e42..77d0e7a90 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/BatchInputArbiter.java +++ b/java/src/com/android/inputmethod/keyboard/internal/BatchInputArbiter.java @@ -16,8 +16,8 @@ package com.android.inputmethod.keyboard.internal; -import com.android.inputmethod.latin.InputPointers; import com.android.inputmethod.latin.common.Constants; +import com.android.inputmethod.latin.common.InputPointers; /** * This class arbitrates batch input. diff --git a/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeDrawingPoints.java b/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeDrawingPoints.java index 7d09e9d2f..07ef00924 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeDrawingPoints.java +++ b/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeDrawingPoints.java @@ -16,7 +16,7 @@ package com.android.inputmethod.keyboard.internal; -import com.android.inputmethod.latin.utils.ResizableIntArray; +import com.android.inputmethod.latin.common.ResizableIntArray; /** * This class holds drawing points to represent a gesture stroke on the screen. diff --git a/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeRecognitionPoints.java b/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeRecognitionPoints.java index 99ec18f4d..3e901114a 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeRecognitionPoints.java +++ b/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeRecognitionPoints.java @@ -18,9 +18,9 @@ package com.android.inputmethod.keyboard.internal; import android.util.Log; -import com.android.inputmethod.latin.InputPointers; import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.utils.ResizableIntArray; +import com.android.inputmethod.latin.common.InputPointers; +import com.android.inputmethod.latin.common.ResizableIntArray; /** * This class holds event points to recognize a gesture stroke. diff --git a/java/src/com/android/inputmethod/keyboard/internal/GestureTrailDrawingPoints.java b/java/src/com/android/inputmethod/keyboard/internal/GestureTrailDrawingPoints.java index 67683e247..4d998e443 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/GestureTrailDrawingPoints.java +++ b/java/src/com/android/inputmethod/keyboard/internal/GestureTrailDrawingPoints.java @@ -24,7 +24,7 @@ import android.graphics.Rect; import android.os.SystemClock; import com.android.inputmethod.latin.common.Constants; -import com.android.inputmethod.latin.utils.ResizableIntArray; +import com.android.inputmethod.latin.common.ResizableIntArray; /** * This class holds drawing points to represent a gesture trail. The gesture trail may contain diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java index 1c4ff9659..8e9b5c6f6 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java @@ -24,6 +24,7 @@ import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.common.Constants; +import com.android.inputmethod.latin.common.InputPointers; import com.android.inputmethod.latin.common.StringUtils; import com.android.inputmethod.latin.makedict.DictionaryHeader; import com.android.inputmethod.latin.makedict.FormatSpec; diff --git a/java/src/com/android/inputmethod/latin/InputPointers.java b/java/src/com/android/inputmethod/latin/InputPointers.java deleted file mode 100644 index d57a881c0..000000000 --- a/java/src/com/android/inputmethod/latin/InputPointers.java +++ /dev/null @@ -1,189 +0,0 @@ -/* - * Copyright (C) 2012 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin; - -import android.util.Log; -import android.util.SparseIntArray; - -import com.android.inputmethod.annotations.UsedForTesting; -import com.android.inputmethod.latin.define.DebugFlags; -import com.android.inputmethod.latin.utils.ResizableIntArray; - -// TODO: This class is not thread-safe. -public final class InputPointers { - private static final String TAG = InputPointers.class.getSimpleName(); - private static final boolean DEBUG_TIME = false; - - private final int mDefaultCapacity; - private final ResizableIntArray mXCoordinates; - private final ResizableIntArray mYCoordinates; - private final ResizableIntArray mPointerIds; - private final ResizableIntArray mTimes; - - public InputPointers(int defaultCapacity) { - mDefaultCapacity = defaultCapacity; - mXCoordinates = new ResizableIntArray(defaultCapacity); - mYCoordinates = new ResizableIntArray(defaultCapacity); - mPointerIds = new ResizableIntArray(defaultCapacity); - mTimes = new ResizableIntArray(defaultCapacity); - } - - private void fillWithLastTimeUntil(final int index) { - final int fromIndex = mTimes.getLength(); - // Fill the gap with the latest time. - // See {@link #getTime(int)} and {@link #isValidTimeStamps()}. - if (fromIndex <= 0) { - return; - } - final int fillLength = index - fromIndex + 1; - if (fillLength <= 0) { - return; - } - final int lastTime = mTimes.get(fromIndex - 1); - mTimes.fill(lastTime, fromIndex, fillLength); - } - - public void addPointerAt(int index, int x, int y, int pointerId, int time) { - mXCoordinates.addAt(index, x); - mYCoordinates.addAt(index, y); - mPointerIds.addAt(index, pointerId); - if (DebugFlags.DEBUG_ENABLED || DEBUG_TIME) { - fillWithLastTimeUntil(index); - } - mTimes.addAt(index, time); - } - - @UsedForTesting - void addPointer(int x, int y, int pointerId, int time) { - mXCoordinates.add(x); - mYCoordinates.add(y); - mPointerIds.add(pointerId); - mTimes.add(time); - } - - public void set(InputPointers ip) { - mXCoordinates.set(ip.mXCoordinates); - mYCoordinates.set(ip.mYCoordinates); - mPointerIds.set(ip.mPointerIds); - mTimes.set(ip.mTimes); - } - - public void copy(InputPointers ip) { - mXCoordinates.copy(ip.mXCoordinates); - mYCoordinates.copy(ip.mYCoordinates); - mPointerIds.copy(ip.mPointerIds); - mTimes.copy(ip.mTimes); - } - - /** - * Append the times, x-coordinates and y-coordinates in the specified {@link ResizableIntArray} - * to the end of this. - * @param pointerId the pointer id of the source. - * @param times the source {@link ResizableIntArray} to read the event times from. - * @param xCoordinates the source {@link ResizableIntArray} to read the x-coordinates from. - * @param yCoordinates the source {@link ResizableIntArray} to read the y-coordinates from. - * @param startPos the starting index of the data in {@code times} and etc. - * @param length the number of data to be appended. - */ - public void append(int pointerId, ResizableIntArray times, ResizableIntArray xCoordinates, - ResizableIntArray yCoordinates, int startPos, int length) { - if (length == 0) { - return; - } - mXCoordinates.append(xCoordinates, startPos, length); - mYCoordinates.append(yCoordinates, startPos, length); - mPointerIds.fill(pointerId, mPointerIds.getLength(), length); - mTimes.append(times, startPos, length); - } - - /** - * Shift to the left by elementCount, discarding elementCount pointers at the start. - * @param elementCount how many elements to shift. - */ - public void shift(final int elementCount) { - mXCoordinates.shift(elementCount); - mYCoordinates.shift(elementCount); - mPointerIds.shift(elementCount); - mTimes.shift(elementCount); - } - - public void reset() { - final int defaultCapacity = mDefaultCapacity; - mXCoordinates.reset(defaultCapacity); - mYCoordinates.reset(defaultCapacity); - mPointerIds.reset(defaultCapacity); - mTimes.reset(defaultCapacity); - } - - public int getPointerSize() { - return mXCoordinates.getLength(); - } - - public int[] getXCoordinates() { - return mXCoordinates.getPrimitiveArray(); - } - - public int[] getYCoordinates() { - return mYCoordinates.getPrimitiveArray(); - } - - public int[] getPointerIds() { - return mPointerIds.getPrimitiveArray(); - } - - /** - * Gets the time each point was registered, in milliseconds, relative to the first event in the - * sequence. - * @return The time each point was registered, in milliseconds, relative to the first event in - * the sequence. - */ - public int[] getTimes() { - if (DebugFlags.DEBUG_ENABLED || DEBUG_TIME) { - if (!isValidTimeStamps()) { - throw new RuntimeException("Time stamps are invalid."); - } - } - return mTimes.getPrimitiveArray(); - } - - @Override - public String toString() { - return "size=" + getPointerSize() + " id=" + mPointerIds + " time=" + mTimes - + " x=" + mXCoordinates + " y=" + mYCoordinates; - } - - private boolean isValidTimeStamps() { - final int[] times = mTimes.getPrimitiveArray(); - final int[] pointerIds = mPointerIds.getPrimitiveArray(); - final SparseIntArray lastTimeOfPointers = new SparseIntArray(); - final int size = getPointerSize(); - for (int i = 0; i < size; ++i) { - final int pointerId = pointerIds[i]; - final int time = times[i]; - final int lastTime = lastTimeOfPointers.get(pointerId, time); - if (time < lastTime) { - // dump - for (int j = 0; j < size; ++j) { - Log.d(TAG, "--- (" + j + ") " + times[j]); - } - return false; - } - lastTimeOfPointers.put(pointerId, time); - } - return true; - } -} diff --git a/java/src/com/android/inputmethod/latin/LastComposedWord.java b/java/src/com/android/inputmethod/latin/LastComposedWord.java index c4c389411..9fcdb2229 100644 --- a/java/src/com/android/inputmethod/latin/LastComposedWord.java +++ b/java/src/com/android/inputmethod/latin/LastComposedWord.java @@ -20,6 +20,7 @@ import android.text.TextUtils; import com.android.inputmethod.event.Event; import com.android.inputmethod.latin.common.Constants; +import com.android.inputmethod.latin.common.InputPointers; import java.util.ArrayList; diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index ff82087ae..5fa28d571 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -77,6 +77,7 @@ import com.android.inputmethod.keyboard.TextDecoratorUi; import com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.common.Constants; +import com.android.inputmethod.latin.common.InputPointers; import com.android.inputmethod.latin.define.DebugFlags; import com.android.inputmethod.latin.define.ProductionFlags; import com.android.inputmethod.latin.inputlogic.InputLogic; diff --git a/java/src/com/android/inputmethod/latin/WordComposer.java b/java/src/com/android/inputmethod/latin/WordComposer.java index 35436f59c..0b77f2ce3 100644 --- a/java/src/com/android/inputmethod/latin/WordComposer.java +++ b/java/src/com/android/inputmethod/latin/WordComposer.java @@ -20,6 +20,7 @@ import com.android.inputmethod.event.CombinerChain; import com.android.inputmethod.event.Event; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.common.Constants; +import com.android.inputmethod.latin.common.InputPointers; import com.android.inputmethod.latin.common.StringUtils; import com.android.inputmethod.latin.define.DebugFlags; import com.android.inputmethod.latin.utils.CoordinateUtils; diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java index f3a287320..bafea178e 100644 --- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java +++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java @@ -41,7 +41,6 @@ import com.android.inputmethod.keyboard.TextDecorator; import com.android.inputmethod.keyboard.TextDecoratorUiOperator; import com.android.inputmethod.latin.Dictionary; import com.android.inputmethod.latin.DictionaryFacilitator; -import com.android.inputmethod.latin.InputPointers; import com.android.inputmethod.latin.LastComposedWord; import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.NgramContext; @@ -52,6 +51,7 @@ import com.android.inputmethod.latin.SuggestedWords; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.WordComposer; import com.android.inputmethod.latin.common.Constants; +import com.android.inputmethod.latin.common.InputPointers; import com.android.inputmethod.latin.common.StringUtils; import com.android.inputmethod.latin.define.DebugFlags; import com.android.inputmethod.latin.settings.SettingsValues; diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java index 5f391dd9b..ddc4ad99c 100644 --- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java +++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java @@ -21,10 +21,10 @@ import android.os.HandlerThread; import android.os.Message; import com.android.inputmethod.compat.LooperCompatUtils; -import com.android.inputmethod.latin.InputPointers; import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.SuggestedWords; import com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback; +import com.android.inputmethod.latin.common.InputPointers; /** * A helper to manage deferred tasks for the input logic. diff --git a/java/src/com/android/inputmethod/latin/utils/ResizableIntArray.java b/java/src/com/android/inputmethod/latin/utils/ResizableIntArray.java deleted file mode 100644 index 64c9e2cff..000000000 --- a/java/src/com/android/inputmethod/latin/utils/ResizableIntArray.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright (C) 2012 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.utils; - -import java.util.Arrays; - -// TODO: This class is not thread-safe. -public final class ResizableIntArray { - private int[] mArray; - private int mLength; - - public ResizableIntArray(final int capacity) { - reset(capacity); - } - - public int get(final int index) { - if (index < mLength) { - return mArray[index]; - } - throw new ArrayIndexOutOfBoundsException("length=" + mLength + "; index=" + index); - } - - public void addAt(final int index, final int val) { - if (index < mLength) { - mArray[index] = val; - } else { - mLength = index; - add(val); - } - } - - public void add(final int val) { - final int currentLength = mLength; - ensureCapacity(currentLength + 1); - mArray[currentLength] = val; - mLength = currentLength + 1; - } - - /** - * Calculate the new capacity of {@code mArray}. - * @param minimumCapacity the minimum capacity that the {@code mArray} should have. - * @return the new capacity that the {@code mArray} should have. Returns zero when there is no - * need to expand {@code mArray}. - */ - private int calculateCapacity(final int minimumCapacity) { - final int currentCapcity = mArray.length; - if (currentCapcity < minimumCapacity) { - final int nextCapacity = currentCapcity * 2; - // The following is the same as return Math.max(minimumCapacity, nextCapacity); - return minimumCapacity > nextCapacity ? minimumCapacity : nextCapacity; - } - return 0; - } - - private void ensureCapacity(final int minimumCapacity) { - final int newCapacity = calculateCapacity(minimumCapacity); - if (newCapacity > 0) { - // TODO: Implement primitive array pool. - mArray = Arrays.copyOf(mArray, newCapacity); - } - } - - public int getLength() { - return mLength; - } - - public void setLength(final int newLength) { - ensureCapacity(newLength); - mLength = newLength; - } - - public void reset(final int capacity) { - // TODO: Implement primitive array pool. - mArray = new int[capacity]; - mLength = 0; - } - - public int[] getPrimitiveArray() { - return mArray; - } - - public void set(final ResizableIntArray ip) { - // TODO: Implement primitive array pool. - mArray = ip.mArray; - mLength = ip.mLength; - } - - public void copy(final ResizableIntArray ip) { - final int newCapacity = calculateCapacity(ip.mLength); - if (newCapacity > 0) { - // TODO: Implement primitive array pool. - mArray = new int[newCapacity]; - } - System.arraycopy(ip.mArray, 0, mArray, 0, ip.mLength); - mLength = ip.mLength; - } - - public void append(final ResizableIntArray src, final int startPos, final int length) { - if (length == 0) { - return; - } - final int currentLength = mLength; - final int newLength = currentLength + length; - ensureCapacity(newLength); - System.arraycopy(src.mArray, startPos, mArray, currentLength, length); - mLength = newLength; - } - - public void fill(final int value, final int startPos, final int length) { - if (startPos < 0 || length < 0) { - throw new IllegalArgumentException("startPos=" + startPos + "; length=" + length); - } - final int endPos = startPos + length; - ensureCapacity(endPos); - Arrays.fill(mArray, startPos, endPos, value); - if (mLength < endPos) { - mLength = endPos; - } - } - - /** - * Shift to the left by elementCount, discarding elementCount pointers at the start. - * @param elementCount how many elements to shift. - */ - public void shift(final int elementCount) { - System.arraycopy(mArray, elementCount, mArray, 0, mLength - elementCount); - mLength -= elementCount; - } - - @Override - public String toString() { - final StringBuilder sb = new StringBuilder(); - for (int i = 0; i < mLength; i++) { - if (i != 0) { - sb.append(","); - } - sb.append(mArray[i]); - } - return "[" + sb + "]"; - } -} diff --git a/tests/src/com/android/inputmethod/latin/InputPointersTests.java b/tests/src/com/android/inputmethod/latin/InputPointersTests.java deleted file mode 100644 index 1a47cddf4..000000000 --- a/tests/src/com/android/inputmethod/latin/InputPointersTests.java +++ /dev/null @@ -1,326 +0,0 @@ -/* - * Copyright (C) 2012 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin; - -import android.test.AndroidTestCase; -import android.test.suitebuilder.annotation.SmallTest; - -import com.android.inputmethod.latin.utils.ResizableIntArray; - -import java.util.Arrays; - -@SmallTest -public class InputPointersTests extends AndroidTestCase { - private static final int DEFAULT_CAPACITY = 48; - - public void testNewInstance() { - final InputPointers src = new InputPointers(DEFAULT_CAPACITY); - assertEquals("new instance size", 0, src.getPointerSize()); - assertNotNull("new instance xCoordinates", src.getXCoordinates()); - assertNotNull("new instance yCoordinates", src.getYCoordinates()); - assertNotNull("new instance pointerIds", src.getPointerIds()); - assertNotNull("new instance times", src.getTimes()); - } - - public void testReset() { - final InputPointers src = new InputPointers(DEFAULT_CAPACITY); - final int[] xCoordinates = src.getXCoordinates(); - final int[] yCoordinates = src.getXCoordinates(); - final int[] pointerIds = src.getXCoordinates(); - final int[] times = src.getXCoordinates(); - - src.reset(); - assertEquals("size after reset", 0, src.getPointerSize()); - assertNotSame("xCoordinates after reset", xCoordinates, src.getXCoordinates()); - assertNotSame("yCoordinates after reset", yCoordinates, src.getYCoordinates()); - assertNotSame("pointerIds after reset", pointerIds, src.getPointerIds()); - assertNotSame("times after reset", times, src.getTimes()); - } - - public void testAdd() { - final InputPointers src = new InputPointers(DEFAULT_CAPACITY); - final int limit = src.getXCoordinates().length * 2 + 10; - for (int i = 0; i < limit; i++) { - final int x = i; - final int y = i * 2; - final int pointerId = i * 3; - final int time = i * 4; - src.addPointer(x, y, pointerId, time); - assertEquals("size after add " + i, i + 1, src.getPointerSize()); - } - for (int i = 0; i < limit; i++) { - final int x = i; - final int y = i * 2; - final int pointerId = i * 3; - final int time = i * 4; - assertEquals("xCoordinates at " + i, x, src.getXCoordinates()[i]); - assertEquals("yCoordinates at " + i, y, src.getYCoordinates()[i]); - assertEquals("pointerIds at " + i, pointerId, src.getPointerIds()[i]); - assertEquals("times at " + i, time, src.getTimes()[i]); - } - } - - public void testAddAt() { - final InputPointers src = new InputPointers(DEFAULT_CAPACITY); - final int limit = 1000, step = 100; - for (int i = 0; i < limit; i += step) { - final int x = i; - final int y = i * 2; - final int pointerId = i * 3; - final int time = i * 4; - src.addPointerAt(i, x, y, pointerId, time); - assertEquals("size after add at " + i, i + 1, src.getPointerSize()); - } - for (int i = 0; i < limit; i += step) { - final int x = i; - final int y = i * 2; - final int pointerId = i * 3; - final int time = i * 4; - assertEquals("xCoordinates at " + i, x, src.getXCoordinates()[i]); - assertEquals("yCoordinates at " + i, y, src.getYCoordinates()[i]); - assertEquals("pointerIds at " + i, pointerId, src.getPointerIds()[i]); - assertEquals("times at " + i, time, src.getTimes()[i]); - } - } - - public void testSet() { - final InputPointers src = new InputPointers(DEFAULT_CAPACITY); - final int limit = src.getXCoordinates().length * 2 + 10; - for (int i = 0; i < limit; i++) { - final int x = i; - final int y = i * 2; - final int pointerId = i * 3; - final int time = i * 4; - src.addPointer(x, y, pointerId, time); - } - final InputPointers dst = new InputPointers(DEFAULT_CAPACITY); - dst.set(src); - assertEquals("size after set", dst.getPointerSize(), src.getPointerSize()); - assertSame("xCoordinates after set", dst.getXCoordinates(), src.getXCoordinates()); - assertSame("yCoordinates after set", dst.getYCoordinates(), src.getYCoordinates()); - assertSame("pointerIds after set", dst.getPointerIds(), src.getPointerIds()); - assertSame("times after set", dst.getTimes(), src.getTimes()); - } - - public void testCopy() { - final InputPointers src = new InputPointers(DEFAULT_CAPACITY); - final int limit = 100; - for (int i = 0; i < limit; i++) { - final int x = i; - final int y = i * 2; - final int pointerId = i * 3; - final int time = i * 4; - src.addPointer(x, y, pointerId, time); - } - final InputPointers dst = new InputPointers(DEFAULT_CAPACITY); - dst.copy(src); - assertEquals("size after copy", dst.getPointerSize(), src.getPointerSize()); - assertNotSame("xCoordinates after copy", dst.getXCoordinates(), src.getXCoordinates()); - assertNotSame("yCoordinates after copy", dst.getYCoordinates(), src.getYCoordinates()); - assertNotSame("pointerIds after copy", dst.getPointerIds(), src.getPointerIds()); - assertNotSame("times after copy", dst.getTimes(), src.getTimes()); - final int size = dst.getPointerSize(); - assertIntArrayEquals("xCoordinates values after copy", - dst.getXCoordinates(), 0, src.getXCoordinates(), 0, size); - assertIntArrayEquals("yCoordinates values after copy", - dst.getYCoordinates(), 0, src.getYCoordinates(), 0, size); - assertIntArrayEquals("pointerIds values after copy", - dst.getPointerIds(), 0, src.getPointerIds(), 0, size); - assertIntArrayEquals("times values after copy", - dst.getTimes(), 0, src.getTimes(), 0, size); - } - - public void testAppend() { - final int dstLength = 50; - final InputPointers dst = new InputPointers(DEFAULT_CAPACITY); - for (int i = 0; i < dstLength; i++) { - final int x = i * 4; - final int y = i * 3; - final int pointerId = i * 2; - final int time = i; - dst.addPointer(x, y, pointerId, time); - } - final InputPointers dstCopy = new InputPointers(DEFAULT_CAPACITY); - dstCopy.copy(dst); - - final ResizableIntArray srcXCoords = new ResizableIntArray(DEFAULT_CAPACITY); - final ResizableIntArray srcYCoords = new ResizableIntArray(DEFAULT_CAPACITY); - final ResizableIntArray srcPointerIds = new ResizableIntArray(DEFAULT_CAPACITY); - final ResizableIntArray srcTimes = new ResizableIntArray(DEFAULT_CAPACITY); - final int srcLength = 100; - final int srcPointerId = 10; - for (int i = 0; i < srcLength; i++) { - final int x = i; - final int y = i * 2; - // The time value must be larger than dst. - final int time = i * 4 + dstLength; - srcXCoords.add(x); - srcYCoords.add(y); - srcPointerIds.add(srcPointerId); - srcTimes.add(time); - } - - final int startPos = 0; - dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, - startPos, 0 /* length */); - assertEquals("size after append zero", dstLength, dst.getPointerSize()); - assertIntArrayEquals("xCoordinates after append zero", - dstCopy.getXCoordinates(), startPos, dst.getXCoordinates(), startPos, dstLength); - assertIntArrayEquals("yCoordinates after append zero", - dstCopy.getYCoordinates(), startPos, dst.getYCoordinates(), startPos, dstLength); - assertIntArrayEquals("pointerIds after append zero", - dstCopy.getPointerIds(), startPos, dst.getPointerIds(), startPos, dstLength); - assertIntArrayEquals("times after append zero", - dstCopy.getTimes(), startPos, dst.getTimes(), startPos, dstLength); - - dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, - startPos, srcLength); - assertEquals("size after append", dstLength + srcLength, dst.getPointerSize()); - assertTrue("primitive length after append", - dst.getPointerIds().length >= dstLength + srcLength); - assertIntArrayEquals("original xCoordinates values after append", - dstCopy.getXCoordinates(), startPos, dst.getXCoordinates(), startPos, dstLength); - assertIntArrayEquals("original yCoordinates values after append", - dstCopy.getYCoordinates(), startPos, dst.getYCoordinates(), startPos, dstLength); - assertIntArrayEquals("original pointerIds values after append", - dstCopy.getPointerIds(), startPos, dst.getPointerIds(), startPos, dstLength); - assertIntArrayEquals("original times values after append", - dstCopy.getTimes(), startPos, dst.getTimes(), startPos, dstLength); - assertIntArrayEquals("appended xCoordinates values after append", - srcXCoords.getPrimitiveArray(), startPos, dst.getXCoordinates(), - dstLength, srcLength); - assertIntArrayEquals("appended yCoordinates values after append", - srcYCoords.getPrimitiveArray(), startPos, dst.getYCoordinates(), - dstLength, srcLength); - assertIntArrayEquals("appended pointerIds values after append", - srcPointerIds.getPrimitiveArray(), startPos, dst.getPointerIds(), - dstLength, srcLength); - assertIntArrayEquals("appended times values after append", - srcTimes.getPrimitiveArray(), startPos, dst.getTimes(), dstLength, srcLength); - } - - public void testAppendResizableIntArray() { - final int dstLength = 50; - final InputPointers dst = new InputPointers(DEFAULT_CAPACITY); - for (int i = 0; i < dstLength; i++) { - final int x = i * 4; - final int y = i * 3; - final int pointerId = i * 2; - final int time = i; - dst.addPointer(x, y, pointerId, time); - } - final InputPointers dstCopy = new InputPointers(DEFAULT_CAPACITY); - dstCopy.copy(dst); - - final int srcLength = 100; - final int srcPointerId = 1; - final int[] srcPointerIds = new int[srcLength]; - 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 < srcLength; i++) { - // The time value must be larger than dst. - final int time = i * 2 + dstLength; - final int x = i * 3; - final int y = i * 4; - srcTimes.add(time); - srcXCoords.add(x); - srcYCoords.add(y); - } - - dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, 0, 0); - assertEquals("size after append zero", dstLength, dst.getPointerSize()); - assertIntArrayEquals("xCoordinates after append zero", - dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLength); - assertIntArrayEquals("yCoordinates after append zero", - dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLength); - assertIntArrayEquals("pointerIds after append zero", - dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLength); - assertIntArrayEquals("times after append zero", - dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLength); - - dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, 0, srcLength); - assertEquals("size after append", dstLength + srcLength, dst.getPointerSize()); - assertTrue("primitive length after append", - dst.getPointerIds().length >= dstLength + srcLength); - assertIntArrayEquals("original xCoordinates values after append", - dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLength); - assertIntArrayEquals("original yCoordinates values after append", - dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLength); - assertIntArrayEquals("original pointerIds values after append", - dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLength); - assertIntArrayEquals("original times values after append", - dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLength); - assertIntArrayEquals("appended xCoordinates values after append", - srcXCoords.getPrimitiveArray(), 0, dst.getXCoordinates(), dstLength, srcLength); - assertIntArrayEquals("appended yCoordinates values after append", - srcYCoords.getPrimitiveArray(), 0, dst.getYCoordinates(), dstLength, srcLength); - assertIntArrayEquals("appended pointerIds values after append", - srcPointerIds, 0, dst.getPointerIds(), dstLength, srcLength); - assertIntArrayEquals("appended times values after append", - srcTimes.getPrimitiveArray(), 0, dst.getTimes(), dstLength, srcLength); - } - - // TODO: Consolidate this method with - // {@link ResizableIntArrayTests#assertIntArrayEquals(String,int[],int,int[],int,int)}. - private static void assertIntArrayEquals(final String message, final int[] expecteds, - final int expectedPos, final int[] actuals, final int actualPos, final int length) { - if (expecteds == actuals) { - return; - } - if (expecteds == null || actuals == null) { - assertEquals(message, Arrays.toString(expecteds), Arrays.toString(actuals)); - return; - } - if (expecteds.length < expectedPos + length || actuals.length < actualPos + length) { - fail(message + ": insufficient length: expecteds=" + Arrays.toString(expecteds) - + " actuals=" + Arrays.toString(actuals)); - return; - } - for (int i = 0; i < length; i++) { - assertEquals(message + " [" + i + "]", - expecteds[i + expectedPos], actuals[i + actualPos]); - } - } - - public void testShift() { - final InputPointers src = new InputPointers(DEFAULT_CAPACITY); - final int limit = 100; - final int shiftAmount = 20; - for (int i = 0; i < limit; i++) { - final int x = i; - final int y = i * 2; - final int pointerId = i * 3; - final int time = i * 4; - src.addPointer(x, y, pointerId, time); - } - src.shift(shiftAmount); - assertEquals("length after shift", src.getPointerSize(), limit - shiftAmount); - for (int i = 0; i < limit - shiftAmount; ++i) { - final int oldIndex = i + shiftAmount; - final int x = oldIndex; - final int y = oldIndex * 2; - final int pointerId = oldIndex * 3; - final int time = oldIndex * 4; - assertEquals("xCoordinates at " + i, x, src.getXCoordinates()[i]); - assertEquals("yCoordinates at " + i, y, src.getYCoordinates()[i]); - assertEquals("pointerIds at " + i, pointerId, src.getPointerIds()[i]); - assertEquals("times at " + i, time, src.getTimes()[i]); - } - } -} diff --git a/tests/src/com/android/inputmethod/latin/InputTestsBase.java b/tests/src/com/android/inputmethod/latin/InputTestsBase.java index 161336aa6..926a2d3e1 100644 --- a/tests/src/com/android/inputmethod/latin/InputTestsBase.java +++ b/tests/src/com/android/inputmethod/latin/InputTestsBase.java @@ -43,6 +43,7 @@ import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.latin.Dictionary.PhonyDictionary; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.common.Constants; +import com.android.inputmethod.latin.common.InputPointers; import com.android.inputmethod.latin.common.StringUtils; import com.android.inputmethod.latin.settings.DebugSettings; import com.android.inputmethod.latin.settings.Settings; diff --git a/tests/src/com/android/inputmethod/latin/common/InputPointersTests.java b/tests/src/com/android/inputmethod/latin/common/InputPointersTests.java new file mode 100644 index 000000000..6b3490de8 --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/common/InputPointersTests.java @@ -0,0 +1,324 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.common; + +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.SmallTest; + +import java.util.Arrays; + +@SmallTest +public class InputPointersTests extends AndroidTestCase { + private static final int DEFAULT_CAPACITY = 48; + + public void testNewInstance() { + final InputPointers src = new InputPointers(DEFAULT_CAPACITY); + assertEquals("new instance size", 0, src.getPointerSize()); + assertNotNull("new instance xCoordinates", src.getXCoordinates()); + assertNotNull("new instance yCoordinates", src.getYCoordinates()); + assertNotNull("new instance pointerIds", src.getPointerIds()); + assertNotNull("new instance times", src.getTimes()); + } + + public void testReset() { + final InputPointers src = new InputPointers(DEFAULT_CAPACITY); + final int[] xCoordinates = src.getXCoordinates(); + final int[] yCoordinates = src.getXCoordinates(); + final int[] pointerIds = src.getXCoordinates(); + final int[] times = src.getXCoordinates(); + + src.reset(); + assertEquals("size after reset", 0, src.getPointerSize()); + assertNotSame("xCoordinates after reset", xCoordinates, src.getXCoordinates()); + assertNotSame("yCoordinates after reset", yCoordinates, src.getYCoordinates()); + assertNotSame("pointerIds after reset", pointerIds, src.getPointerIds()); + assertNotSame("times after reset", times, src.getTimes()); + } + + public void testAdd() { + final InputPointers src = new InputPointers(DEFAULT_CAPACITY); + final int limit = src.getXCoordinates().length * 2 + 10; + for (int i = 0; i < limit; i++) { + final int x = i; + final int y = i * 2; + final int pointerId = i * 3; + final int time = i * 4; + src.addPointer(x, y, pointerId, time); + assertEquals("size after add " + i, i + 1, src.getPointerSize()); + } + for (int i = 0; i < limit; i++) { + final int x = i; + final int y = i * 2; + final int pointerId = i * 3; + final int time = i * 4; + assertEquals("xCoordinates at " + i, x, src.getXCoordinates()[i]); + assertEquals("yCoordinates at " + i, y, src.getYCoordinates()[i]); + assertEquals("pointerIds at " + i, pointerId, src.getPointerIds()[i]); + assertEquals("times at " + i, time, src.getTimes()[i]); + } + } + + public void testAddAt() { + final InputPointers src = new InputPointers(DEFAULT_CAPACITY); + final int limit = 1000, step = 100; + for (int i = 0; i < limit; i += step) { + final int x = i; + final int y = i * 2; + final int pointerId = i * 3; + final int time = i * 4; + src.addPointerAt(i, x, y, pointerId, time); + assertEquals("size after add at " + i, i + 1, src.getPointerSize()); + } + for (int i = 0; i < limit; i += step) { + final int x = i; + final int y = i * 2; + final int pointerId = i * 3; + final int time = i * 4; + assertEquals("xCoordinates at " + i, x, src.getXCoordinates()[i]); + assertEquals("yCoordinates at " + i, y, src.getYCoordinates()[i]); + assertEquals("pointerIds at " + i, pointerId, src.getPointerIds()[i]); + assertEquals("times at " + i, time, src.getTimes()[i]); + } + } + + public void testSet() { + final InputPointers src = new InputPointers(DEFAULT_CAPACITY); + final int limit = src.getXCoordinates().length * 2 + 10; + for (int i = 0; i < limit; i++) { + final int x = i; + final int y = i * 2; + final int pointerId = i * 3; + final int time = i * 4; + src.addPointer(x, y, pointerId, time); + } + final InputPointers dst = new InputPointers(DEFAULT_CAPACITY); + dst.set(src); + assertEquals("size after set", dst.getPointerSize(), src.getPointerSize()); + assertSame("xCoordinates after set", dst.getXCoordinates(), src.getXCoordinates()); + assertSame("yCoordinates after set", dst.getYCoordinates(), src.getYCoordinates()); + assertSame("pointerIds after set", dst.getPointerIds(), src.getPointerIds()); + assertSame("times after set", dst.getTimes(), src.getTimes()); + } + + public void testCopy() { + final InputPointers src = new InputPointers(DEFAULT_CAPACITY); + final int limit = 100; + for (int i = 0; i < limit; i++) { + final int x = i; + final int y = i * 2; + final int pointerId = i * 3; + final int time = i * 4; + src.addPointer(x, y, pointerId, time); + } + final InputPointers dst = new InputPointers(DEFAULT_CAPACITY); + dst.copy(src); + assertEquals("size after copy", dst.getPointerSize(), src.getPointerSize()); + assertNotSame("xCoordinates after copy", dst.getXCoordinates(), src.getXCoordinates()); + assertNotSame("yCoordinates after copy", dst.getYCoordinates(), src.getYCoordinates()); + assertNotSame("pointerIds after copy", dst.getPointerIds(), src.getPointerIds()); + assertNotSame("times after copy", dst.getTimes(), src.getTimes()); + final int size = dst.getPointerSize(); + assertIntArrayEquals("xCoordinates values after copy", + dst.getXCoordinates(), 0, src.getXCoordinates(), 0, size); + assertIntArrayEquals("yCoordinates values after copy", + dst.getYCoordinates(), 0, src.getYCoordinates(), 0, size); + assertIntArrayEquals("pointerIds values after copy", + dst.getPointerIds(), 0, src.getPointerIds(), 0, size); + assertIntArrayEquals("times values after copy", + dst.getTimes(), 0, src.getTimes(), 0, size); + } + + public void testAppend() { + final int dstLength = 50; + final InputPointers dst = new InputPointers(DEFAULT_CAPACITY); + for (int i = 0; i < dstLength; i++) { + final int x = i * 4; + final int y = i * 3; + final int pointerId = i * 2; + final int time = i; + dst.addPointer(x, y, pointerId, time); + } + final InputPointers dstCopy = new InputPointers(DEFAULT_CAPACITY); + dstCopy.copy(dst); + + final ResizableIntArray srcXCoords = new ResizableIntArray(DEFAULT_CAPACITY); + final ResizableIntArray srcYCoords = new ResizableIntArray(DEFAULT_CAPACITY); + final ResizableIntArray srcPointerIds = new ResizableIntArray(DEFAULT_CAPACITY); + final ResizableIntArray srcTimes = new ResizableIntArray(DEFAULT_CAPACITY); + final int srcLength = 100; + final int srcPointerId = 10; + for (int i = 0; i < srcLength; i++) { + final int x = i; + final int y = i * 2; + // The time value must be larger than dst. + final int time = i * 4 + dstLength; + srcXCoords.add(x); + srcYCoords.add(y); + srcPointerIds.add(srcPointerId); + srcTimes.add(time); + } + + final int startPos = 0; + dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, + startPos, 0 /* length */); + assertEquals("size after append zero", dstLength, dst.getPointerSize()); + assertIntArrayEquals("xCoordinates after append zero", + dstCopy.getXCoordinates(), startPos, dst.getXCoordinates(), startPos, dstLength); + assertIntArrayEquals("yCoordinates after append zero", + dstCopy.getYCoordinates(), startPos, dst.getYCoordinates(), startPos, dstLength); + assertIntArrayEquals("pointerIds after append zero", + dstCopy.getPointerIds(), startPos, dst.getPointerIds(), startPos, dstLength); + assertIntArrayEquals("times after append zero", + dstCopy.getTimes(), startPos, dst.getTimes(), startPos, dstLength); + + dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, + startPos, srcLength); + assertEquals("size after append", dstLength + srcLength, dst.getPointerSize()); + assertTrue("primitive length after append", + dst.getPointerIds().length >= dstLength + srcLength); + assertIntArrayEquals("original xCoordinates values after append", + dstCopy.getXCoordinates(), startPos, dst.getXCoordinates(), startPos, dstLength); + assertIntArrayEquals("original yCoordinates values after append", + dstCopy.getYCoordinates(), startPos, dst.getYCoordinates(), startPos, dstLength); + assertIntArrayEquals("original pointerIds values after append", + dstCopy.getPointerIds(), startPos, dst.getPointerIds(), startPos, dstLength); + assertIntArrayEquals("original times values after append", + dstCopy.getTimes(), startPos, dst.getTimes(), startPos, dstLength); + assertIntArrayEquals("appended xCoordinates values after append", + srcXCoords.getPrimitiveArray(), startPos, dst.getXCoordinates(), + dstLength, srcLength); + assertIntArrayEquals("appended yCoordinates values after append", + srcYCoords.getPrimitiveArray(), startPos, dst.getYCoordinates(), + dstLength, srcLength); + assertIntArrayEquals("appended pointerIds values after append", + srcPointerIds.getPrimitiveArray(), startPos, dst.getPointerIds(), + dstLength, srcLength); + assertIntArrayEquals("appended times values after append", + srcTimes.getPrimitiveArray(), startPos, dst.getTimes(), dstLength, srcLength); + } + + public void testAppendResizableIntArray() { + final int dstLength = 50; + final InputPointers dst = new InputPointers(DEFAULT_CAPACITY); + for (int i = 0; i < dstLength; i++) { + final int x = i * 4; + final int y = i * 3; + final int pointerId = i * 2; + final int time = i; + dst.addPointer(x, y, pointerId, time); + } + final InputPointers dstCopy = new InputPointers(DEFAULT_CAPACITY); + dstCopy.copy(dst); + + final int srcLength = 100; + final int srcPointerId = 1; + final int[] srcPointerIds = new int[srcLength]; + 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 < srcLength; i++) { + // The time value must be larger than dst. + final int time = i * 2 + dstLength; + final int x = i * 3; + final int y = i * 4; + srcTimes.add(time); + srcXCoords.add(x); + srcYCoords.add(y); + } + + dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, 0, 0); + assertEquals("size after append zero", dstLength, dst.getPointerSize()); + assertIntArrayEquals("xCoordinates after append zero", + dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLength); + assertIntArrayEquals("yCoordinates after append zero", + dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLength); + assertIntArrayEquals("pointerIds after append zero", + dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLength); + assertIntArrayEquals("times after append zero", + dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLength); + + dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, 0, srcLength); + assertEquals("size after append", dstLength + srcLength, dst.getPointerSize()); + assertTrue("primitive length after append", + dst.getPointerIds().length >= dstLength + srcLength); + assertIntArrayEquals("original xCoordinates values after append", + dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLength); + assertIntArrayEquals("original yCoordinates values after append", + dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLength); + assertIntArrayEquals("original pointerIds values after append", + dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLength); + assertIntArrayEquals("original times values after append", + dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLength); + assertIntArrayEquals("appended xCoordinates values after append", + srcXCoords.getPrimitiveArray(), 0, dst.getXCoordinates(), dstLength, srcLength); + assertIntArrayEquals("appended yCoordinates values after append", + srcYCoords.getPrimitiveArray(), 0, dst.getYCoordinates(), dstLength, srcLength); + assertIntArrayEquals("appended pointerIds values after append", + srcPointerIds, 0, dst.getPointerIds(), dstLength, srcLength); + assertIntArrayEquals("appended times values after append", + srcTimes.getPrimitiveArray(), 0, dst.getTimes(), dstLength, srcLength); + } + + // TODO: Consolidate this method with + // {@link ResizableIntArrayTests#assertIntArrayEquals(String,int[],int,int[],int,int)}. + private static void assertIntArrayEquals(final String message, final int[] expecteds, + final int expectedPos, final int[] actuals, final int actualPos, final int length) { + if (expecteds == actuals) { + return; + } + if (expecteds == null || actuals == null) { + assertEquals(message, Arrays.toString(expecteds), Arrays.toString(actuals)); + return; + } + if (expecteds.length < expectedPos + length || actuals.length < actualPos + length) { + fail(message + ": insufficient length: expecteds=" + Arrays.toString(expecteds) + + " actuals=" + Arrays.toString(actuals)); + return; + } + for (int i = 0; i < length; i++) { + assertEquals(message + " [" + i + "]", + expecteds[i + expectedPos], actuals[i + actualPos]); + } + } + + public void testShift() { + final InputPointers src = new InputPointers(DEFAULT_CAPACITY); + final int limit = 100; + final int shiftAmount = 20; + for (int i = 0; i < limit; i++) { + final int x = i; + final int y = i * 2; + final int pointerId = i * 3; + final int time = i * 4; + src.addPointer(x, y, pointerId, time); + } + src.shift(shiftAmount); + assertEquals("length after shift", src.getPointerSize(), limit - shiftAmount); + for (int i = 0; i < limit - shiftAmount; ++i) { + final int oldIndex = i + shiftAmount; + final int x = oldIndex; + final int y = oldIndex * 2; + final int pointerId = oldIndex * 3; + final int time = oldIndex * 4; + assertEquals("xCoordinates at " + i, x, src.getXCoordinates()[i]); + assertEquals("yCoordinates at " + i, y, src.getYCoordinates()[i]); + assertEquals("pointerIds at " + i, pointerId, src.getPointerIds()[i]); + assertEquals("times at " + i, time, src.getTimes()[i]); + } + } +} diff --git a/tests/src/com/android/inputmethod/latin/common/ResizableIntArrayTests.java b/tests/src/com/android/inputmethod/latin/common/ResizableIntArrayTests.java new file mode 100644 index 000000000..bd1629faf --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/common/ResizableIntArrayTests.java @@ -0,0 +1,377 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.common; + +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.SmallTest; + +import java.util.Arrays; + +@SmallTest +public class ResizableIntArrayTests extends AndroidTestCase { + private static final int DEFAULT_CAPACITY = 48; + + public void testNewInstance() { + final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); + final int[] array = src.getPrimitiveArray(); + assertEquals("new instance length", 0, src.getLength()); + assertNotNull("new instance array", array); + assertEquals("new instance array length", DEFAULT_CAPACITY, array.length); + } + + public void testAdd() { + final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); + final int[] array = src.getPrimitiveArray(); + int[] array2 = null, array3 = null; + final int limit = DEFAULT_CAPACITY * 2 + 10; + for (int i = 0; i < limit; i++) { + final int value = i; + src.add(value); + assertEquals("length after add " + i, i + 1, src.getLength()); + if (i == DEFAULT_CAPACITY) { + array2 = src.getPrimitiveArray(); + } + if (i == DEFAULT_CAPACITY * 2) { + array3 = src.getPrimitiveArray(); + } + if (i < DEFAULT_CAPACITY) { + assertSame("array after add " + i, array, src.getPrimitiveArray()); + } else if (i < DEFAULT_CAPACITY * 2) { + assertSame("array after add " + i, array2, src.getPrimitiveArray()); + } else if (i < DEFAULT_CAPACITY * 3) { + assertSame("array after add " + i, array3, src.getPrimitiveArray()); + } + } + for (int i = 0; i < limit; i++) { + final int value = i; + assertEquals("value at " + i, value, src.get(i)); + } + } + + public void testAddAt() { + final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); + final int limit = DEFAULT_CAPACITY * 10, step = DEFAULT_CAPACITY * 2; + for (int i = 0; i < limit; i += step) { + final int value = i; + src.addAt(i, value); + assertEquals("length after add at " + i, i + 1, src.getLength()); + } + for (int i = 0; i < limit; i += step) { + final int value = i; + assertEquals("value at " + i, value, src.get(i)); + } + } + + public void testGet() { + final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); + try { + src.get(0); + fail("get(0) shouldn't succeed"); + } catch (ArrayIndexOutOfBoundsException e) { + // success + } + try { + src.get(DEFAULT_CAPACITY); + fail("get(DEFAULT_CAPACITY) shouldn't succeed"); + } catch (ArrayIndexOutOfBoundsException e) { + // success + } + + final int index = DEFAULT_CAPACITY / 2; + final int valueAddAt = 100; + src.addAt(index, valueAddAt); + assertEquals("legth after add at " + index, index + 1, src.getLength()); + assertEquals("value after add at " + index, valueAddAt, src.get(index)); + assertEquals("value after add at 0", 0, src.get(0)); + try { + src.get(src.getLength()); + fail("get(length) shouldn't succeed"); + } catch (ArrayIndexOutOfBoundsException e) { + // success + } + } + + public void testReset() { + final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); + final int[] array = src.getPrimitiveArray(); + for (int i = 0; i < DEFAULT_CAPACITY; i++) { + final int value = i; + src.add(value); + assertEquals("length after add " + i, i + 1, src.getLength()); + } + + final int smallerLength = DEFAULT_CAPACITY / 2; + src.reset(smallerLength); + final int[] array2 = src.getPrimitiveArray(); + assertEquals("length after reset", 0, src.getLength()); + assertNotSame("array after reset", array, array2); + + int[] array3 = null; + for (int i = 0; i < DEFAULT_CAPACITY; i++) { + final int value = i; + src.add(value); + assertEquals("length after add " + i, i + 1, src.getLength()); + if (i == smallerLength) { + array3 = src.getPrimitiveArray(); + } + if (i < smallerLength) { + assertSame("array after add " + i, array2, src.getPrimitiveArray()); + } else if (i < smallerLength * 2) { + assertSame("array after add " + i, array3, src.getPrimitiveArray()); + } + } + } + + public void testSetLength() { + final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); + final int[] array = src.getPrimitiveArray(); + for (int i = 0; i < DEFAULT_CAPACITY; i++) { + final int value = i; + src.add(value); + assertEquals("length after add " + i, i + 1, src.getLength()); + } + + final int largerLength = DEFAULT_CAPACITY * 2; + src.setLength(largerLength); + final int[] array2 = src.getPrimitiveArray(); + assertEquals("length after larger setLength", largerLength, src.getLength()); + assertNotSame("array after larger setLength", array, array2); + assertEquals("array length after larger setLength", largerLength, array2.length); + for (int i = 0; i < largerLength; i++) { + final int value = i; + if (i < DEFAULT_CAPACITY) { + assertEquals("value at " + i, value, src.get(i)); + } else { + assertEquals("value at " + i, 0, src.get(i)); + } + } + + final int smallerLength = DEFAULT_CAPACITY / 2; + src.setLength(smallerLength); + final int[] array3 = src.getPrimitiveArray(); + assertEquals("length after smaller setLength", smallerLength, src.getLength()); + assertSame("array after smaller setLength", array2, array3); + assertEquals("array length after smaller setLength", largerLength, array3.length); + for (int i = 0; i < smallerLength; i++) { + final int value = i; + assertEquals("value at " + i, value, src.get(i)); + } + } + + public void testSet() { + final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); + final int limit = DEFAULT_CAPACITY * 2 + 10; + for (int i = 0; i < limit; i++) { + final int value = i; + src.add(value); + } + + final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY); + dst.set(src); + assertEquals("length after set", dst.getLength(), src.getLength()); + assertSame("array after set", dst.getPrimitiveArray(), src.getPrimitiveArray()); + } + + public void testCopy() { + final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); + for (int i = 0; i < DEFAULT_CAPACITY; i++) { + final int value = i; + src.add(value); + } + + final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY); + final int[] array = dst.getPrimitiveArray(); + dst.copy(src); + assertEquals("length after copy", dst.getLength(), src.getLength()); + assertSame("array after copy", array, dst.getPrimitiveArray()); + assertNotSame("array after copy", dst.getPrimitiveArray(), src.getPrimitiveArray()); + assertIntArrayEquals("values after copy", + dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength()); + + final int smallerLength = DEFAULT_CAPACITY / 2; + dst.reset(smallerLength); + final int[] array2 = dst.getPrimitiveArray(); + dst.copy(src); + final int[] array3 = dst.getPrimitiveArray(); + assertEquals("length after copy to smaller", dst.getLength(), src.getLength()); + assertNotSame("array after copy to smaller", array2, array3); + assertNotSame("array after copy to smaller", array3, src.getPrimitiveArray()); + assertIntArrayEquals("values after copy to smaller", + dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength()); + } + + public void testAppend() { + final int srcLength = DEFAULT_CAPACITY; + final ResizableIntArray src = new ResizableIntArray(srcLength); + for (int i = 0; i < srcLength; i++) { + final int value = i; + src.add(value); + } + final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY * 2); + final int[] array = dst.getPrimitiveArray(); + final int dstLength = DEFAULT_CAPACITY / 2; + for (int i = 0; i < dstLength; i++) { + final int value = -i - 1; + dst.add(value); + } + final ResizableIntArray dstCopy = new ResizableIntArray(dst.getLength()); + dstCopy.copy(dst); + + final int startPos = 0; + dst.append(src, startPos, 0 /* length */); + assertEquals("length after append zero", dstLength, dst.getLength()); + assertSame("array after append zero", array, dst.getPrimitiveArray()); + assertIntArrayEquals("values after append zero", dstCopy.getPrimitiveArray(), startPos, + dst.getPrimitiveArray(), startPos, dstLength); + + dst.append(src, startPos, srcLength); + assertEquals("length after append", dstLength + srcLength, dst.getLength()); + assertSame("array after append", array, dst.getPrimitiveArray()); + assertTrue("primitive length after append", + dst.getPrimitiveArray().length >= dstLength + srcLength); + assertIntArrayEquals("original values after append", dstCopy.getPrimitiveArray(), startPos, + dst.getPrimitiveArray(), startPos, dstLength); + assertIntArrayEquals("appended values after append", src.getPrimitiveArray(), startPos, + dst.getPrimitiveArray(), dstLength, srcLength); + + dst.append(src, startPos, srcLength); + assertEquals("length after 2nd append", dstLength + srcLength * 2, dst.getLength()); + assertNotSame("array after 2nd append", array, dst.getPrimitiveArray()); + assertTrue("primitive length after 2nd append", + dst.getPrimitiveArray().length >= dstLength + srcLength * 2); + assertIntArrayEquals("original values after 2nd append", + dstCopy.getPrimitiveArray(), startPos, dst.getPrimitiveArray(), startPos, + dstLength); + assertIntArrayEquals("appended values after 2nd append", + src.getPrimitiveArray(), startPos, dst.getPrimitiveArray(), dstLength, + srcLength); + assertIntArrayEquals("appended values after 2nd append", + src.getPrimitiveArray(), startPos, dst.getPrimitiveArray(), dstLength + srcLength, + srcLength); + } + + public void testFill() { + final int srcLength = DEFAULT_CAPACITY; + final ResizableIntArray src = new ResizableIntArray(srcLength); + for (int i = 0; i < srcLength; i++) { + final int value = i; + src.add(value); + } + final int[] array = src.getPrimitiveArray(); + + final int startPos = srcLength / 3; + final int length = srcLength / 3; + final int endPos = startPos + length; + assertTrue(startPos >= 1); + final int fillValue = 123; + try { + src.fill(fillValue, -1 /* startPos */, length); + fail("fill from -1 shouldn't succeed"); + } catch (IllegalArgumentException e) { + // success + } + try { + src.fill(fillValue, startPos, -1 /* length */); + fail("fill negative length shouldn't succeed"); + } catch (IllegalArgumentException e) { + // success + } + + src.fill(fillValue, startPos, length); + assertEquals("length after fill", srcLength, src.getLength()); + assertSame("array after fill", array, src.getPrimitiveArray()); + for (int i = 0; i < srcLength; i++) { + final int value = i; + if (i >= startPos && i < endPos) { + assertEquals("new values after fill at " + i, fillValue, src.get(i)); + } else { + assertEquals("unmodified values after fill at " + i, value, src.get(i)); + } + } + + final int length2 = srcLength * 2 - startPos; + final int largeEnd = startPos + length2; + assertTrue(largeEnd > srcLength); + final int fillValue2 = 456; + src.fill(fillValue2, startPos, length2); + assertEquals("length after large fill", largeEnd, src.getLength()); + assertNotSame("array after large fill", array, src.getPrimitiveArray()); + for (int i = 0; i < largeEnd; i++) { + final int value = i; + if (i >= startPos && i < largeEnd) { + assertEquals("new values after large fill at " + i, fillValue2, src.get(i)); + } else { + assertEquals("unmodified values after large fill at " + i, value, src.get(i)); + } + } + + final int startPos2 = largeEnd + length2; + final int endPos2 = startPos2 + length2; + final int fillValue3 = 789; + src.fill(fillValue3, startPos2, length2); + assertEquals("length after disjoint fill", endPos2, src.getLength()); + for (int i = 0; i < endPos2; i++) { + final int value = i; + if (i >= startPos2 && i < endPos2) { + assertEquals("new values after disjoint fill at " + i, fillValue3, src.get(i)); + } else if (i >= startPos && i < largeEnd) { + assertEquals("unmodified values after disjoint fill at " + i, + fillValue2, src.get(i)); + } else if (i < startPos) { + assertEquals("unmodified values after disjoint fill at " + i, value, src.get(i)); + } else { + assertEquals("gap values after disjoint fill at " + i, 0, src.get(i)); + } + } + } + + private static void assertIntArrayEquals(final String message, final int[] expecteds, + final int expectedPos, final int[] actuals, final int actualPos, final int length) { + if (expecteds == actuals) { + return; + } + if (expecteds == null || actuals == null) { + assertEquals(message, Arrays.toString(expecteds), Arrays.toString(actuals)); + return; + } + if (expecteds.length < expectedPos + length || actuals.length < actualPos + length) { + fail(message + ": insufficient length: expecteds=" + Arrays.toString(expecteds) + + " actuals=" + Arrays.toString(actuals)); + return; + } + for (int i = 0; i < length; i++) { + assertEquals(message + " [" + i + "]", + expecteds[i + expectedPos], actuals[i + actualPos]); + } + } + + public void testShift() { + final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); + final int limit = DEFAULT_CAPACITY * 10; + final int shiftAmount = 20; + for (int i = 0; i < limit; ++i) { + final int value = i; + src.addAt(i, value); + assertEquals("length after add at " + i, i + 1, src.getLength()); + } + src.shift(shiftAmount); + for (int i = 0; i < limit - shiftAmount; ++i) { + final int oldValue = i + shiftAmount; + assertEquals("value at " + i, oldValue, src.get(i)); + } + } +} diff --git a/tests/src/com/android/inputmethod/latin/utils/ResizableIntArrayTests.java b/tests/src/com/android/inputmethod/latin/utils/ResizableIntArrayTests.java deleted file mode 100644 index 7519becbc..000000000 --- a/tests/src/com/android/inputmethod/latin/utils/ResizableIntArrayTests.java +++ /dev/null @@ -1,377 +0,0 @@ -/* - * Copyright (C) 2012 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.utils; - -import android.test.AndroidTestCase; -import android.test.suitebuilder.annotation.SmallTest; - -import java.util.Arrays; - -@SmallTest -public class ResizableIntArrayTests extends AndroidTestCase { - private static final int DEFAULT_CAPACITY = 48; - - public void testNewInstance() { - final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); - final int[] array = src.getPrimitiveArray(); - assertEquals("new instance length", 0, src.getLength()); - assertNotNull("new instance array", array); - assertEquals("new instance array length", DEFAULT_CAPACITY, array.length); - } - - public void testAdd() { - final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); - final int[] array = src.getPrimitiveArray(); - int[] array2 = null, array3 = null; - final int limit = DEFAULT_CAPACITY * 2 + 10; - for (int i = 0; i < limit; i++) { - final int value = i; - src.add(value); - assertEquals("length after add " + i, i + 1, src.getLength()); - if (i == DEFAULT_CAPACITY) { - array2 = src.getPrimitiveArray(); - } - if (i == DEFAULT_CAPACITY * 2) { - array3 = src.getPrimitiveArray(); - } - if (i < DEFAULT_CAPACITY) { - assertSame("array after add " + i, array, src.getPrimitiveArray()); - } else if (i < DEFAULT_CAPACITY * 2) { - assertSame("array after add " + i, array2, src.getPrimitiveArray()); - } else if (i < DEFAULT_CAPACITY * 3) { - assertSame("array after add " + i, array3, src.getPrimitiveArray()); - } - } - for (int i = 0; i < limit; i++) { - final int value = i; - assertEquals("value at " + i, value, src.get(i)); - } - } - - public void testAddAt() { - final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); - final int limit = DEFAULT_CAPACITY * 10, step = DEFAULT_CAPACITY * 2; - for (int i = 0; i < limit; i += step) { - final int value = i; - src.addAt(i, value); - assertEquals("length after add at " + i, i + 1, src.getLength()); - } - for (int i = 0; i < limit; i += step) { - final int value = i; - assertEquals("value at " + i, value, src.get(i)); - } - } - - public void testGet() { - final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); - try { - src.get(0); - fail("get(0) shouldn't succeed"); - } catch (ArrayIndexOutOfBoundsException e) { - // success - } - try { - src.get(DEFAULT_CAPACITY); - fail("get(DEFAULT_CAPACITY) shouldn't succeed"); - } catch (ArrayIndexOutOfBoundsException e) { - // success - } - - final int index = DEFAULT_CAPACITY / 2; - final int valueAddAt = 100; - src.addAt(index, valueAddAt); - assertEquals("legth after add at " + index, index + 1, src.getLength()); - assertEquals("value after add at " + index, valueAddAt, src.get(index)); - assertEquals("value after add at 0", 0, src.get(0)); - try { - src.get(src.getLength()); - fail("get(length) shouldn't succeed"); - } catch (ArrayIndexOutOfBoundsException e) { - // success - } - } - - public void testReset() { - final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); - final int[] array = src.getPrimitiveArray(); - for (int i = 0; i < DEFAULT_CAPACITY; i++) { - final int value = i; - src.add(value); - assertEquals("length after add " + i, i + 1, src.getLength()); - } - - final int smallerLength = DEFAULT_CAPACITY / 2; - src.reset(smallerLength); - final int[] array2 = src.getPrimitiveArray(); - assertEquals("length after reset", 0, src.getLength()); - assertNotSame("array after reset", array, array2); - - int[] array3 = null; - for (int i = 0; i < DEFAULT_CAPACITY; i++) { - final int value = i; - src.add(value); - assertEquals("length after add " + i, i + 1, src.getLength()); - if (i == smallerLength) { - array3 = src.getPrimitiveArray(); - } - if (i < smallerLength) { - assertSame("array after add " + i, array2, src.getPrimitiveArray()); - } else if (i < smallerLength * 2) { - assertSame("array after add " + i, array3, src.getPrimitiveArray()); - } - } - } - - public void testSetLength() { - final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); - final int[] array = src.getPrimitiveArray(); - for (int i = 0; i < DEFAULT_CAPACITY; i++) { - final int value = i; - src.add(value); - assertEquals("length after add " + i, i + 1, src.getLength()); - } - - final int largerLength = DEFAULT_CAPACITY * 2; - src.setLength(largerLength); - final int[] array2 = src.getPrimitiveArray(); - assertEquals("length after larger setLength", largerLength, src.getLength()); - assertNotSame("array after larger setLength", array, array2); - assertEquals("array length after larger setLength", largerLength, array2.length); - for (int i = 0; i < largerLength; i++) { - final int value = i; - if (i < DEFAULT_CAPACITY) { - assertEquals("value at " + i, value, src.get(i)); - } else { - assertEquals("value at " + i, 0, src.get(i)); - } - } - - final int smallerLength = DEFAULT_CAPACITY / 2; - src.setLength(smallerLength); - final int[] array3 = src.getPrimitiveArray(); - assertEquals("length after smaller setLength", smallerLength, src.getLength()); - assertSame("array after smaller setLength", array2, array3); - assertEquals("array length after smaller setLength", largerLength, array3.length); - for (int i = 0; i < smallerLength; i++) { - final int value = i; - assertEquals("value at " + i, value, src.get(i)); - } - } - - public void testSet() { - final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); - final int limit = DEFAULT_CAPACITY * 2 + 10; - for (int i = 0; i < limit; i++) { - final int value = i; - src.add(value); - } - - final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY); - dst.set(src); - assertEquals("length after set", dst.getLength(), src.getLength()); - assertSame("array after set", dst.getPrimitiveArray(), src.getPrimitiveArray()); - } - - public void testCopy() { - final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); - for (int i = 0; i < DEFAULT_CAPACITY; i++) { - final int value = i; - src.add(value); - } - - final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY); - final int[] array = dst.getPrimitiveArray(); - dst.copy(src); - assertEquals("length after copy", dst.getLength(), src.getLength()); - assertSame("array after copy", array, dst.getPrimitiveArray()); - assertNotSame("array after copy", dst.getPrimitiveArray(), src.getPrimitiveArray()); - assertIntArrayEquals("values after copy", - dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength()); - - final int smallerLength = DEFAULT_CAPACITY / 2; - dst.reset(smallerLength); - final int[] array2 = dst.getPrimitiveArray(); - dst.copy(src); - final int[] array3 = dst.getPrimitiveArray(); - assertEquals("length after copy to smaller", dst.getLength(), src.getLength()); - assertNotSame("array after copy to smaller", array2, array3); - assertNotSame("array after copy to smaller", array3, src.getPrimitiveArray()); - assertIntArrayEquals("values after copy to smaller", - dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength()); - } - - public void testAppend() { - final int srcLength = DEFAULT_CAPACITY; - final ResizableIntArray src = new ResizableIntArray(srcLength); - for (int i = 0; i < srcLength; i++) { - final int value = i; - src.add(value); - } - final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY * 2); - final int[] array = dst.getPrimitiveArray(); - final int dstLength = DEFAULT_CAPACITY / 2; - for (int i = 0; i < dstLength; i++) { - final int value = -i - 1; - dst.add(value); - } - final ResizableIntArray dstCopy = new ResizableIntArray(dst.getLength()); - dstCopy.copy(dst); - - final int startPos = 0; - dst.append(src, startPos, 0 /* length */); - assertEquals("length after append zero", dstLength, dst.getLength()); - assertSame("array after append zero", array, dst.getPrimitiveArray()); - assertIntArrayEquals("values after append zero", dstCopy.getPrimitiveArray(), startPos, - dst.getPrimitiveArray(), startPos, dstLength); - - dst.append(src, startPos, srcLength); - assertEquals("length after append", dstLength + srcLength, dst.getLength()); - assertSame("array after append", array, dst.getPrimitiveArray()); - assertTrue("primitive length after append", - dst.getPrimitiveArray().length >= dstLength + srcLength); - assertIntArrayEquals("original values after append", dstCopy.getPrimitiveArray(), startPos, - dst.getPrimitiveArray(), startPos, dstLength); - assertIntArrayEquals("appended values after append", src.getPrimitiveArray(), startPos, - dst.getPrimitiveArray(), dstLength, srcLength); - - dst.append(src, startPos, srcLength); - assertEquals("length after 2nd append", dstLength + srcLength * 2, dst.getLength()); - assertNotSame("array after 2nd append", array, dst.getPrimitiveArray()); - assertTrue("primitive length after 2nd append", - dst.getPrimitiveArray().length >= dstLength + srcLength * 2); - assertIntArrayEquals("original values after 2nd append", - dstCopy.getPrimitiveArray(), startPos, dst.getPrimitiveArray(), startPos, - dstLength); - assertIntArrayEquals("appended values after 2nd append", - src.getPrimitiveArray(), startPos, dst.getPrimitiveArray(), dstLength, - srcLength); - assertIntArrayEquals("appended values after 2nd append", - src.getPrimitiveArray(), startPos, dst.getPrimitiveArray(), dstLength + srcLength, - srcLength); - } - - public void testFill() { - final int srcLength = DEFAULT_CAPACITY; - final ResizableIntArray src = new ResizableIntArray(srcLength); - for (int i = 0; i < srcLength; i++) { - final int value = i; - src.add(value); - } - final int[] array = src.getPrimitiveArray(); - - final int startPos = srcLength / 3; - final int length = srcLength / 3; - final int endPos = startPos + length; - assertTrue(startPos >= 1); - final int fillValue = 123; - try { - src.fill(fillValue, -1 /* startPos */, length); - fail("fill from -1 shouldn't succeed"); - } catch (IllegalArgumentException e) { - // success - } - try { - src.fill(fillValue, startPos, -1 /* length */); - fail("fill negative length shouldn't succeed"); - } catch (IllegalArgumentException e) { - // success - } - - src.fill(fillValue, startPos, length); - assertEquals("length after fill", srcLength, src.getLength()); - assertSame("array after fill", array, src.getPrimitiveArray()); - for (int i = 0; i < srcLength; i++) { - final int value = i; - if (i >= startPos && i < endPos) { - assertEquals("new values after fill at " + i, fillValue, src.get(i)); - } else { - assertEquals("unmodified values after fill at " + i, value, src.get(i)); - } - } - - final int length2 = srcLength * 2 - startPos; - final int largeEnd = startPos + length2; - assertTrue(largeEnd > srcLength); - final int fillValue2 = 456; - src.fill(fillValue2, startPos, length2); - assertEquals("length after large fill", largeEnd, src.getLength()); - assertNotSame("array after large fill", array, src.getPrimitiveArray()); - for (int i = 0; i < largeEnd; i++) { - final int value = i; - if (i >= startPos && i < largeEnd) { - assertEquals("new values after large fill at " + i, fillValue2, src.get(i)); - } else { - assertEquals("unmodified values after large fill at " + i, value, src.get(i)); - } - } - - final int startPos2 = largeEnd + length2; - final int endPos2 = startPos2 + length2; - final int fillValue3 = 789; - src.fill(fillValue3, startPos2, length2); - assertEquals("length after disjoint fill", endPos2, src.getLength()); - for (int i = 0; i < endPos2; i++) { - final int value = i; - if (i >= startPos2 && i < endPos2) { - assertEquals("new values after disjoint fill at " + i, fillValue3, src.get(i)); - } else if (i >= startPos && i < largeEnd) { - assertEquals("unmodified values after disjoint fill at " + i, - fillValue2, src.get(i)); - } else if (i < startPos) { - assertEquals("unmodified values after disjoint fill at " + i, value, src.get(i)); - } else { - assertEquals("gap values after disjoint fill at " + i, 0, src.get(i)); - } - } - } - - private static void assertIntArrayEquals(final String message, final int[] expecteds, - final int expectedPos, final int[] actuals, final int actualPos, final int length) { - if (expecteds == actuals) { - return; - } - if (expecteds == null || actuals == null) { - assertEquals(message, Arrays.toString(expecteds), Arrays.toString(actuals)); - return; - } - if (expecteds.length < expectedPos + length || actuals.length < actualPos + length) { - fail(message + ": insufficient length: expecteds=" + Arrays.toString(expecteds) - + " actuals=" + Arrays.toString(actuals)); - return; - } - for (int i = 0; i < length; i++) { - assertEquals(message + " [" + i + "]", - expecteds[i + expectedPos], actuals[i + actualPos]); - } - } - - public void testShift() { - final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); - final int limit = DEFAULT_CAPACITY * 10; - final int shiftAmount = 20; - for (int i = 0; i < limit; ++i) { - final int value = i; - src.addAt(i, value); - assertEquals("length after add at " + i, i + 1, src.getLength()); - } - src.shift(shiftAmount); - for (int i = 0; i < limit - shiftAmount; ++i) { - final int oldValue = i + shiftAmount; - assertEquals("value at " + i, oldValue, src.get(i)); - } - } -} diff --git a/tools/dicttool/Android.mk b/tools/dicttool/Android.mk index 90be411ec..81c0706c1 100644 --- a/tools/dicttool/Android.mk +++ b/tools/dicttool/Android.mk @@ -47,7 +47,6 @@ LATINIME_SRC_FILES_FOR_DICTTOOL := \ latin/BinaryDictionary.java \ latin/DicTraverseSession.java \ latin/Dictionary.java \ - latin/InputPointers.java \ latin/LastComposedWord.java \ latin/NgramContext.java \ latin/SuggestedWords.java \ @@ -59,8 +58,7 @@ LATINIME_SRC_FILES_FOR_DICTTOOL := \ latin/utils/CoordinateUtils.java \ latin/utils/FileUtils.java \ latin/utils/JniUtils.java \ - latin/utils/LocaleUtils.java \ - latin/utils/ResizableIntArray.java + latin/utils/LocaleUtils.java LATINIME_OVERRIDABLE_SRC_FILES_FOR_DICTTOOL := \ latin/define/DebugFlags.java -- cgit v1.2.3-83-g751a