aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/org/kelar/inputmethod/latin/common
diff options
context:
space:
mode:
Diffstat (limited to 'tests/src/org/kelar/inputmethod/latin/common')
-rw-r--r--tests/src/org/kelar/inputmethod/latin/common/InputPointersTests.java344
-rw-r--r--tests/src/org/kelar/inputmethod/latin/common/ResizableIntArrayTests.java399
-rw-r--r--tests/src/org/kelar/inputmethod/latin/common/StringUtilsTests.java501
-rw-r--r--tests/src/org/kelar/inputmethod/latin/common/UnicodeSurrogateTests.java45
4 files changed, 1289 insertions, 0 deletions
diff --git a/tests/src/org/kelar/inputmethod/latin/common/InputPointersTests.java b/tests/src/org/kelar/inputmethod/latin/common/InputPointersTests.java
new file mode 100644
index 000000000..e0b98dd13
--- /dev/null
+++ b/tests/src/org/kelar/inputmethod/latin/common/InputPointersTests.java
@@ -0,0 +1,344 @@
+/*
+ * 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 org.kelar.inputmethod.latin.common;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.Arrays;
+
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class InputPointersTests {
+ private static final int DEFAULT_CAPACITY = 48;
+
+ @Test
+ 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());
+ }
+
+ @Test
+ 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());
+ }
+
+ @Test
+ 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]);
+ }
+ }
+
+ @Test
+ 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]);
+ }
+ }
+
+ @Test
+ 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());
+ }
+
+ @Test
+ 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);
+ }
+
+ @Test
+ 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 <code>dst</code>.
+ 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);
+ }
+
+ @Test
+ 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 <code>dst</code>.
+ 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]);
+ }
+ }
+
+ @Test
+ 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/org/kelar/inputmethod/latin/common/ResizableIntArrayTests.java b/tests/src/org/kelar/inputmethod/latin/common/ResizableIntArrayTests.java
new file mode 100644
index 000000000..d2a7a651f
--- /dev/null
+++ b/tests/src/org/kelar/inputmethod/latin/common/ResizableIntArrayTests.java
@@ -0,0 +1,399 @@
+/*
+ * 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 org.kelar.inputmethod.latin.common;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.Arrays;
+
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class ResizableIntArrayTests {
+ private static final int DEFAULT_CAPACITY = 48;
+
+ @Test
+ 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);
+ }
+
+ @Test
+ 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));
+ }
+ }
+
+ @Test
+ 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));
+ }
+ }
+
+ @Test
+ 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
+ }
+ }
+
+ @Test
+ 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());
+ }
+ }
+ }
+
+ @Test
+ 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));
+ }
+ }
+
+ @Test
+ 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());
+ }
+
+ @Test
+ 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());
+ }
+
+ @Test
+ 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);
+ }
+
+ @Test
+ 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]);
+ }
+ }
+
+ @Test
+ 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/org/kelar/inputmethod/latin/common/StringUtilsTests.java b/tests/src/org/kelar/inputmethod/latin/common/StringUtilsTests.java
new file mode 100644
index 000000000..016819ce8
--- /dev/null
+++ b/tests/src/org/kelar/inputmethod/latin/common/StringUtilsTests.java
@@ -0,0 +1,501 @@
+/*
+ * Copyright (C) 2014 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 org.kelar.inputmethod.latin.common;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.Locale;
+
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class StringUtilsTests {
+ private static final Locale US = Locale.US;
+ private static final Locale GERMAN = Locale.GERMAN;
+ private static final Locale TURKEY = new Locale("tr", "TR");
+ private static final Locale GREECE = new Locale("el", "GR");
+
+ private static void assert_toTitleCaseOfKeyLabel(final Locale locale,
+ final String lowerCase, final String expected) {
+ assertEquals(lowerCase + " in " + locale, expected,
+ StringUtils.toTitleCaseOfKeyLabel(lowerCase, locale));
+ }
+
+ @Test
+ public void test_toTitleCaseOfKeyLabel() {
+ assert_toTitleCaseOfKeyLabel(US, null, null);
+ assert_toTitleCaseOfKeyLabel(US, "", "");
+ assert_toTitleCaseOfKeyLabel(US, "aeiou", "AEIOU");
+ // U+00E0: "à" LATIN SMALL LETTER A WITH GRAVE
+ // U+00E8: "è" LATIN SMALL LETTER E WITH GRAVE
+ // U+00EE: "î" LATIN SMALL LETTER I WITH CIRCUMFLEX
+ // U+00F6: "ö" LATIN SMALL LETTER O WITH DIAERESIS
+ // U+016B: "ū" LATIN SMALL LETTER U WITH MACRON
+ // U+00F1: "ñ" LATIN SMALL LETTER N WITH TILDE
+ // U+00E7: "ç" LATIN SMALL LETTER C WITH CEDILLA
+ // U+00C0: "À" LATIN CAPITAL LETTER A WITH GRAVE
+ // U+00C8: "È" LATIN CAPITAL LETTER E WITH GRAVE
+ // U+00CE: "Î" LATIN CAPITAL LETTER I WITH CIRCUMFLEX
+ // U+00D6: "Ö" LATIN CAPITAL LETTER O WITH DIAERESIS
+ // U+016A: "Ū" LATIN CAPITAL LETTER U WITH MACRON
+ // U+00D1: "Ñ" LATIN CAPITAL LETTER N WITH TILDE
+ // U+00C7: "Ç" LATIN CAPITAL LETTER C WITH CEDILLA
+ assert_toTitleCaseOfKeyLabel(US,
+ "\u00E0\u00E8\u00EE\u00F6\u016B\u00F1\u00E7",
+ "\u00C0\u00C8\u00CE\u00D6\u016A\u00D1\u00C7");
+ // U+00DF: "ß" LATIN SMALL LETTER SHARP S
+ // U+015B: "ś" LATIN SMALL LETTER S WITH ACUTE
+ // U+0161: "š" LATIN SMALL LETTER S WITH CARON
+ // U+015A: "Ś" LATIN CAPITAL LETTER S WITH ACUTE
+ // U+0160: "Š" LATIN CAPITAL LETTER S WITH CARONZ
+ assert_toTitleCaseOfKeyLabel(GERMAN,
+ "\u00DF\u015B\u0161",
+ "SS\u015A\u0160");
+ // U+0259: "ə" LATIN SMALL LETTER SCHWA
+ // U+0069: "i" LATIN SMALL LETTER I
+ // U+0131: "ı" LATIN SMALL LETTER DOTLESS I
+ // U+018F: "Ə" LATIN SMALL LETTER SCHWA
+ // U+0130: "İ" LATIN SMALL LETTER I WITH DOT ABOVE
+ // U+0049: "I" LATIN SMALL LETTER I
+ assert_toTitleCaseOfKeyLabel(TURKEY,
+ "\u0259\u0069\u0131",
+ "\u018F\u0130\u0049");
+ // U+03C3: "σ" GREEK SMALL LETTER SIGMA
+ // U+03C2: "ς" GREEK SMALL LETTER FINAL SIGMA
+ // U+03A3: "Σ" GREEK CAPITAL LETTER SIGMA
+ assert_toTitleCaseOfKeyLabel(GREECE,
+ "\u03C3\u03C2",
+ "\u03A3\u03A3");
+ // U+03AC: "ά" GREEK SMALL LETTER ALPHA WITH TONOS
+ // U+03AD: "έ" GREEK SMALL LETTER EPSILON WITH TONOS
+ // U+03AE: "ή" GREEK SMALL LETTER ETA WITH TONOS
+ // U+03AF: "ί" GREEK SMALL LETTER IOTA WITH TONOS
+ // U+03CC: "ό" GREEK SMALL LETTER OMICRON WITH TONOS
+ // U+03CD: "ύ" GREEK SMALL LETTER UPSILON WITH TONOS
+ // U+03CE: "ώ" GREEK SMALL LETTER OMEGA WITH TONOS
+ // U+0386: "Ά" GREEK CAPITAL LETTER ALPHA WITH TONOS
+ // U+0388: "Έ" GREEK CAPITAL LETTER EPSILON WITH TONOS
+ // U+0389: "Ή" GREEK CAPITAL LETTER ETA WITH TONOS
+ // U+038A: "Ί" GREEK CAPITAL LETTER IOTA WITH TONOS
+ // U+038C: "Ό" GREEK CAPITAL LETTER OMICRON WITH TONOS
+ // U+038E: "Ύ" GREEK CAPITAL LETTER UPSILON WITH TONOS
+ // U+038F: "Ώ" GREEK CAPITAL LETTER OMEGA WITH TONOS
+ assert_toTitleCaseOfKeyLabel(GREECE,
+ "\u03AC\u03AD\u03AE\u03AF\u03CC\u03CD\u03CE",
+ "\u0386\u0388\u0389\u038A\u038C\u038E\u038F");
+ // U+03CA: "ϊ" GREEK SMALL LETTER IOTA WITH DIALYTIKA
+ // U+03CB: "ϋ" GREEK SMALL LETTER UPSILON WITH DIALYTIKA
+ // U+0390: "ΐ" GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS
+ // U+03B0: "ΰ" GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS
+ // U+03AA: "Ϊ" GREEK CAPITAL LETTER IOTA WITH DIALYTIKA
+ // U+03AB: "Ϋ" GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA
+ // U+0399: "Ι" GREEK CAPITAL LETTER IOTA
+ // U+03A5: "Υ" GREEK CAPITAL LETTER UPSILON
+ // U+0308: COMBINING DIAERESIS
+ // U+0301: COMBINING GRAVE ACCENT
+ assert_toTitleCaseOfKeyLabel(GREECE,
+ "\u03CA\u03CB\u0390\u03B0",
+ "\u03AA\u03AB\u0399\u0308\u0301\u03A5\u0308\u0301");
+ }
+
+ private static void assert_toTitleCaseOfKeyCode(final Locale locale, final int lowerCase,
+ final int expected) {
+ assertEquals(lowerCase + " in " + locale, expected,
+ StringUtils.toTitleCaseOfKeyCode(lowerCase, locale));
+ }
+
+ @Test
+ public void test_toTitleCaseOfKeyCode() {
+ assert_toTitleCaseOfKeyCode(US, Constants.CODE_ENTER, Constants.CODE_ENTER);
+ assert_toTitleCaseOfKeyCode(US, Constants.CODE_SPACE, Constants.CODE_SPACE);
+ assert_toTitleCaseOfKeyCode(US, Constants.CODE_COMMA, Constants.CODE_COMMA);
+ // U+0069: "i" LATIN SMALL LETTER I
+ // U+0131: "ı" LATIN SMALL LETTER DOTLESS I
+ // U+0130: "İ" LATIN SMALL LETTER I WITH DOT ABOVE
+ // U+0049: "I" LATIN SMALL LETTER I
+ assert_toTitleCaseOfKeyCode(US, 0x0069, 0x0049); // i -> I
+ assert_toTitleCaseOfKeyCode(US, 0x0131, 0x0049); // ı -> I
+ assert_toTitleCaseOfKeyCode(TURKEY, 0x0069, 0x0130); // i -> İ
+ assert_toTitleCaseOfKeyCode(TURKEY, 0x0131, 0x0049); // ı -> I
+ // U+00DF: "ß" LATIN SMALL LETTER SHARP S
+ // The title case of "ß" is "SS".
+ assert_toTitleCaseOfKeyCode(US, 0x00DF, Constants.CODE_UNSPECIFIED);
+ // U+03AC: "ά" GREEK SMALL LETTER ALPHA WITH TONOS
+ // U+0386: "Ά" GREEK CAPITAL LETTER ALPHA WITH TONOS
+ assert_toTitleCaseOfKeyCode(GREECE, 0x03AC, 0x0386);
+ // U+03CA: "ϊ" GREEK SMALL LETTER IOTA WITH DIALYTIKA
+ // U+03AA: "Ϊ" GREEK CAPITAL LETTER IOTA WITH DIALYTIKA
+ assert_toTitleCaseOfKeyCode(GREECE, 0x03CA, 0x03AA);
+ // U+03B0: "ΰ" GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS
+ // The title case of "ΰ" is "\u03A5\u0308\u0301".
+ assert_toTitleCaseOfKeyCode(GREECE, 0x03B0, Constants.CODE_UNSPECIFIED);
+ }
+
+ private static void assert_capitalizeFirstCodePoint(final Locale locale, final String text,
+ final String expected) {
+ assertEquals(text + " in " + locale, expected,
+ StringUtils.capitalizeFirstCodePoint(text, locale));
+ }
+
+ @Test
+ public void test_capitalizeFirstCodePoint() {
+ assert_capitalizeFirstCodePoint(US, "", "");
+ assert_capitalizeFirstCodePoint(US, "a", "A");
+ assert_capitalizeFirstCodePoint(US, "à", "À");
+ assert_capitalizeFirstCodePoint(US, "ß", "SS");
+ assert_capitalizeFirstCodePoint(US, "text", "Text");
+ assert_capitalizeFirstCodePoint(US, "iGoogle", "IGoogle");
+ assert_capitalizeFirstCodePoint(TURKEY, "iyi", "İyi");
+ assert_capitalizeFirstCodePoint(TURKEY, "ısırdı", "Isırdı");
+ assert_capitalizeFirstCodePoint(GREECE, "ά", "Ά");
+ assert_capitalizeFirstCodePoint(GREECE, "άνεση", "Άνεση");
+ }
+
+ private static void assert_capitalizeFirstAndDowncaseRest(final Locale locale,
+ final String text, final String expected) {
+ assertEquals(text + " in " + locale, expected,
+ StringUtils.capitalizeFirstAndDowncaseRest(text, locale));
+ }
+
+ @Test
+ public void test_capitalizeFirstAndDowncaseRest() {
+ assert_capitalizeFirstAndDowncaseRest(US, "", "");
+ assert_capitalizeFirstAndDowncaseRest(US, "a", "A");
+ assert_capitalizeFirstAndDowncaseRest(US, "à", "À");
+ assert_capitalizeFirstAndDowncaseRest(US, "ß", "SS");
+ assert_capitalizeFirstAndDowncaseRest(US, "text", "Text");
+ assert_capitalizeFirstAndDowncaseRest(US, "iGoogle", "Igoogle");
+ assert_capitalizeFirstAndDowncaseRest(US, "invite", "Invite");
+ assert_capitalizeFirstAndDowncaseRest(US, "INVITE", "Invite");
+ assert_capitalizeFirstAndDowncaseRest(TURKEY, "iyi", "İyi");
+ assert_capitalizeFirstAndDowncaseRest(TURKEY, "İYİ", "İyi");
+ assert_capitalizeFirstAndDowncaseRest(TURKEY, "ısırdı", "Isırdı");
+ assert_capitalizeFirstAndDowncaseRest(TURKEY, "ISIRDI", "Isırdı");
+ assert_capitalizeFirstAndDowncaseRest(GREECE, "ά", "Ά");
+ assert_capitalizeFirstAndDowncaseRest(GREECE, "άνεση", "Άνεση");
+ assert_capitalizeFirstAndDowncaseRest(GREECE, "ΆΝΕΣΗ", "Άνεση");
+ }
+
+ @Test
+ public void testContainsInArray() {
+ assertFalse("empty array", StringUtils.containsInArray("key", new String[0]));
+ assertFalse("not in 1 element", StringUtils.containsInArray("key", new String[] {
+ "key1"
+ }));
+ assertFalse("not in 2 elements", StringUtils.containsInArray("key", new String[] {
+ "key1", "key2"
+ }));
+
+ assertTrue("in 1 element", StringUtils.containsInArray("key", new String[] {
+ "key"
+ }));
+ assertTrue("in 2 elements", StringUtils.containsInArray("key", new String[] {
+ "key1", "key"
+ }));
+ }
+
+ @Test
+ public void testContainsInCommaSplittableText() {
+ assertFalse("null", StringUtils.containsInCommaSplittableText("key", null));
+ assertFalse("empty", StringUtils.containsInCommaSplittableText("key", ""));
+ assertFalse("not in 1 element",
+ StringUtils.containsInCommaSplittableText("key", "key1"));
+ assertFalse("not in 2 elements",
+ StringUtils.containsInCommaSplittableText("key", "key1,key2"));
+
+ assertTrue("in 1 element", StringUtils.containsInCommaSplittableText("key", "key"));
+ assertTrue("in 2 elements", StringUtils.containsInCommaSplittableText("key", "key1,key"));
+ }
+
+ @Test
+ public void testRemoveFromCommaSplittableTextIfExists() {
+ assertEquals("null", "", StringUtils.removeFromCommaSplittableTextIfExists("key", null));
+ assertEquals("empty", "", StringUtils.removeFromCommaSplittableTextIfExists("key", ""));
+
+ assertEquals("not in 1 element", "key1",
+ StringUtils.removeFromCommaSplittableTextIfExists("key", "key1"));
+ assertEquals("not in 2 elements", "key1,key2",
+ StringUtils.removeFromCommaSplittableTextIfExists("key", "key1,key2"));
+
+ assertEquals("in 1 element", "",
+ StringUtils.removeFromCommaSplittableTextIfExists("key", "key"));
+ assertEquals("in 2 elements at position 1", "key2",
+ StringUtils.removeFromCommaSplittableTextIfExists("key", "key,key2"));
+ assertEquals("in 2 elements at position 2", "key1",
+ StringUtils.removeFromCommaSplittableTextIfExists("key", "key1,key"));
+ assertEquals("in 3 elements at position 2", "key1,key3",
+ StringUtils.removeFromCommaSplittableTextIfExists("key", "key1,key,key3"));
+
+ assertEquals("in 3 elements at position 1,2,3", "",
+ StringUtils.removeFromCommaSplittableTextIfExists("key", "key,key,key"));
+ assertEquals("in 5 elements at position 2,4", "key1,key3,key5",
+ StringUtils.removeFromCommaSplittableTextIfExists(
+ "key", "key1,key,key3,key,key5"));
+ }
+
+ @Test
+ public void testCapitalizeFirstCodePoint() {
+ assertEquals("SSaa",
+ StringUtils.capitalizeFirstCodePoint("ßaa", Locale.GERMAN));
+ assertEquals("Aßa",
+ StringUtils.capitalizeFirstCodePoint("aßa", Locale.GERMAN));
+ assertEquals("Iab",
+ StringUtils.capitalizeFirstCodePoint("iab", Locale.ENGLISH));
+ assertEquals("CAmElCaSe",
+ StringUtils.capitalizeFirstCodePoint("cAmElCaSe", Locale.ENGLISH));
+ assertEquals("İab",
+ StringUtils.capitalizeFirstCodePoint("iab", new Locale("tr")));
+ assertEquals("AİB",
+ StringUtils.capitalizeFirstCodePoint("AİB", new Locale("tr")));
+ assertEquals("A",
+ StringUtils.capitalizeFirstCodePoint("a", Locale.ENGLISH));
+ assertEquals("A",
+ StringUtils.capitalizeFirstCodePoint("A", Locale.ENGLISH));
+ }
+
+ @Test
+ public void testCapitalizeFirstAndDowncaseRest() {
+ assertEquals("SSaa",
+ StringUtils.capitalizeFirstAndDowncaseRest("ßaa", Locale.GERMAN));
+ assertEquals("Aßa",
+ StringUtils.capitalizeFirstAndDowncaseRest("aßa", Locale.GERMAN));
+ assertEquals("Iab",
+ StringUtils.capitalizeFirstAndDowncaseRest("iab", Locale.ENGLISH));
+ assertEquals("Camelcase",
+ StringUtils.capitalizeFirstAndDowncaseRest("cAmElCaSe", Locale.ENGLISH));
+ assertEquals("İab",
+ StringUtils.capitalizeFirstAndDowncaseRest("iab", new Locale("tr")));
+ assertEquals("Aib",
+ StringUtils.capitalizeFirstAndDowncaseRest("AİB", new Locale("tr")));
+ assertEquals("A",
+ StringUtils.capitalizeFirstAndDowncaseRest("a", Locale.ENGLISH));
+ assertEquals("A",
+ StringUtils.capitalizeFirstAndDowncaseRest("A", Locale.ENGLISH));
+ }
+
+ @Test
+ public void testGetCapitalizationType() {
+ assertEquals(StringUtils.CAPITALIZE_NONE,
+ StringUtils.getCapitalizationType("capitalize"));
+ assertEquals(StringUtils.CAPITALIZE_NONE,
+ StringUtils.getCapitalizationType("cApITalize"));
+ assertEquals(StringUtils.CAPITALIZE_NONE,
+ StringUtils.getCapitalizationType("capitalizE"));
+ assertEquals(StringUtils.CAPITALIZE_NONE,
+ StringUtils.getCapitalizationType("__c a piu$@tali56ze"));
+ assertEquals(StringUtils.CAPITALIZE_FIRST,
+ StringUtils.getCapitalizationType("A__c a piu$@tali56ze"));
+ assertEquals(StringUtils.CAPITALIZE_FIRST,
+ StringUtils.getCapitalizationType("Capitalize"));
+ assertEquals(StringUtils.CAPITALIZE_FIRST,
+ StringUtils.getCapitalizationType(" Capitalize"));
+ assertEquals(StringUtils.CAPITALIZE_ALL,
+ StringUtils.getCapitalizationType("CAPITALIZE"));
+ assertEquals(StringUtils.CAPITALIZE_ALL,
+ StringUtils.getCapitalizationType(" PI26LIE"));
+ assertEquals(StringUtils.CAPITALIZE_NONE,
+ StringUtils.getCapitalizationType(""));
+ }
+
+ @Test
+ public void testIsIdenticalAfterUpcaseIsIdenticalAfterDowncase() {
+ assertFalse(StringUtils.isIdenticalAfterUpcase("capitalize"));
+ assertTrue(StringUtils.isIdenticalAfterDowncase("capitalize"));
+ assertFalse(StringUtils.isIdenticalAfterUpcase("cApITalize"));
+ assertFalse(StringUtils.isIdenticalAfterDowncase("cApITalize"));
+ assertFalse(StringUtils.isIdenticalAfterUpcase("capitalizE"));
+ assertFalse(StringUtils.isIdenticalAfterDowncase("capitalizE"));
+ assertFalse(StringUtils.isIdenticalAfterUpcase("__c a piu$@tali56ze"));
+ assertTrue(StringUtils.isIdenticalAfterDowncase("__c a piu$@tali56ze"));
+ assertFalse(StringUtils.isIdenticalAfterUpcase("A__c a piu$@tali56ze"));
+ assertFalse(StringUtils.isIdenticalAfterDowncase("A__c a piu$@tali56ze"));
+ assertFalse(StringUtils.isIdenticalAfterUpcase("Capitalize"));
+ assertFalse(StringUtils.isIdenticalAfterDowncase("Capitalize"));
+ assertFalse(StringUtils.isIdenticalAfterUpcase(" Capitalize"));
+ assertFalse(StringUtils.isIdenticalAfterDowncase(" Capitalize"));
+ assertTrue(StringUtils.isIdenticalAfterUpcase("CAPITALIZE"));
+ assertFalse(StringUtils.isIdenticalAfterDowncase("CAPITALIZE"));
+ assertTrue(StringUtils.isIdenticalAfterUpcase(" PI26LIE"));
+ assertFalse(StringUtils.isIdenticalAfterDowncase(" PI26LIE"));
+ assertTrue(StringUtils.isIdenticalAfterUpcase(""));
+ assertTrue(StringUtils.isIdenticalAfterDowncase(""));
+ }
+
+ private static void checkCapitalize(final String src, final String dst,
+ final int[] sortedSeparators, final Locale locale) {
+ assertEquals(dst, StringUtils.capitalizeEachWord(src, sortedSeparators, locale));
+ assert(src.equals(dst)
+ == StringUtils.isIdenticalAfterCapitalizeEachWord(src, sortedSeparators));
+ }
+
+ private static final int[] SPACE = { Constants.CODE_SPACE };
+ private static final int[] SPACE_PERIOD = StringUtils.toSortedCodePointArray(" .");
+ private static final int[] SENTENCE_SEPARATORS =
+ StringUtils.toSortedCodePointArray(" \n.!?*()&");
+ private static final int[] WORD_SEPARATORS = StringUtils.toSortedCodePointArray(" \n.!?*,();&");
+
+ @Test
+ public void testCapitalizeEachWord() {
+ checkCapitalize("", "", SPACE, Locale.ENGLISH);
+ checkCapitalize("test", "Test", SPACE, Locale.ENGLISH);
+ checkCapitalize(" test", " Test", SPACE, Locale.ENGLISH);
+ checkCapitalize("Test", "Test", SPACE, Locale.ENGLISH);
+ checkCapitalize(" Test", " Test", SPACE, Locale.ENGLISH);
+ checkCapitalize(".Test", ".test", SPACE, Locale.ENGLISH);
+ checkCapitalize(".Test", ".Test", SPACE_PERIOD, Locale.ENGLISH);
+ checkCapitalize("test and retest", "Test And Retest", SPACE_PERIOD, Locale.ENGLISH);
+ checkCapitalize("Test and retest", "Test And Retest", SPACE_PERIOD, Locale.ENGLISH);
+ checkCapitalize("Test And Retest", "Test And Retest", SPACE_PERIOD, Locale.ENGLISH);
+ checkCapitalize("Test And.Retest ", "Test And.Retest ", SPACE_PERIOD, Locale.ENGLISH);
+ checkCapitalize("Test And.retest ", "Test And.Retest ", SPACE_PERIOD, Locale.ENGLISH);
+ checkCapitalize("Test And.retest ", "Test And.retest ", SPACE, Locale.ENGLISH);
+ checkCapitalize("Test And.Retest ", "Test And.retest ", SPACE, Locale.ENGLISH);
+ checkCapitalize("test and ietest", "Test And İetest", SPACE_PERIOD, new Locale("tr"));
+ checkCapitalize("test and ietest", "Test And Ietest", SPACE_PERIOD, Locale.ENGLISH);
+ checkCapitalize("Test&Retest", "Test&Retest", SENTENCE_SEPARATORS, Locale.ENGLISH);
+ checkCapitalize("Test&retest", "Test&Retest", SENTENCE_SEPARATORS, Locale.ENGLISH);
+ checkCapitalize("test&Retest", "Test&Retest", SENTENCE_SEPARATORS, Locale.ENGLISH);
+ checkCapitalize("rest\nrecreation! And in the end...",
+ "Rest\nRecreation! And In The End...", WORD_SEPARATORS, Locale.ENGLISH);
+ checkCapitalize("lorem ipsum dolor sit amet", "Lorem Ipsum Dolor Sit Amet",
+ WORD_SEPARATORS, Locale.ENGLISH);
+ checkCapitalize("Lorem!Ipsum (Dolor) Sit * Amet", "Lorem!Ipsum (Dolor) Sit * Amet",
+ WORD_SEPARATORS, Locale.ENGLISH);
+ checkCapitalize("Lorem!Ipsum (dolor) Sit * Amet", "Lorem!Ipsum (Dolor) Sit * Amet",
+ WORD_SEPARATORS, Locale.ENGLISH);
+ }
+
+ @Test
+ public void testLooksLikeURL() {
+ assertTrue(StringUtils.lastPartLooksLikeURL("http://www.google."));
+ assertFalse(StringUtils.lastPartLooksLikeURL("word wo"));
+ assertTrue(StringUtils.lastPartLooksLikeURL("/etc/foo"));
+ assertFalse(StringUtils.lastPartLooksLikeURL("left/right"));
+ assertTrue(StringUtils.lastPartLooksLikeURL("www.goo"));
+ assertTrue(StringUtils.lastPartLooksLikeURL("www."));
+ assertFalse(StringUtils.lastPartLooksLikeURL("U.S.A"));
+ assertFalse(StringUtils.lastPartLooksLikeURL("U.S.A."));
+ assertTrue(StringUtils.lastPartLooksLikeURL("rtsp://foo."));
+ assertTrue(StringUtils.lastPartLooksLikeURL("://"));
+ assertFalse(StringUtils.lastPartLooksLikeURL("abc/"));
+ assertTrue(StringUtils.lastPartLooksLikeURL("abc.def/ghi"));
+ assertFalse(StringUtils.lastPartLooksLikeURL("abc.def"));
+ // TODO: ideally this would not look like a URL, but to keep down the complexity of the
+ // code for now True is acceptable.
+ assertTrue(StringUtils.lastPartLooksLikeURL("abc./def"));
+ // TODO: ideally this would not look like a URL, but to keep down the complexity of the
+ // code for now True is acceptable.
+ assertTrue(StringUtils.lastPartLooksLikeURL(".abc/def"));
+ }
+
+ @Test
+ public void testHexStringUtils() {
+ final byte[] bytes = new byte[] { (byte)0x01, (byte)0x11, (byte)0x22, (byte)0x33,
+ (byte)0x55, (byte)0x88, (byte)0xEE };
+ final String bytesStr = StringUtils.byteArrayToHexString(bytes);
+ final byte[] bytes2 = StringUtils.hexStringToByteArray(bytesStr);
+ for (int i = 0; i < bytes.length; ++i) {
+ assertTrue(bytes[i] == bytes2[i]);
+ }
+ final String bytesStr2 = StringUtils.byteArrayToHexString(bytes2);
+ assertTrue(bytesStr.equals(bytesStr2));
+ }
+
+ @Test
+ public void testToCodePointArray() {
+ final String STR_WITH_SUPPLEMENTARY_CHAR = "abcde\uD861\uDED7fgh\u0000\u2002\u2003\u3000xx";
+ final int[] EXPECTED_RESULT = new int[] { 'a', 'b', 'c', 'd', 'e', 0x286D7, 'f', 'g', 'h',
+ 0, 0x2002, 0x2003, 0x3000, 'x', 'x'};
+ final int[] codePointArray = StringUtils.toCodePointArray(STR_WITH_SUPPLEMENTARY_CHAR, 0,
+ STR_WITH_SUPPLEMENTARY_CHAR.length());
+ assertEquals("toCodePointArray, size matches", codePointArray.length,
+ EXPECTED_RESULT.length);
+ for (int i = 0; i < EXPECTED_RESULT.length; ++i) {
+ assertEquals("toCodePointArray position " + i, codePointArray[i], EXPECTED_RESULT[i]);
+ }
+ }
+
+ @Test
+ public void testCopyCodePointsAndReturnCodePointCount() {
+ final String STR_WITH_SUPPLEMENTARY_CHAR = "AbcDE\uD861\uDED7fGh\u0000\u2002\u3000あx";
+ final int[] EXPECTED_RESULT = new int[] { 'A', 'b', 'c', 'D', 'E', 0x286D7,
+ 'f', 'G', 'h', 0, 0x2002, 0x3000, 'あ', 'x'};
+ final int[] EXPECTED_RESULT_DOWNCASE = new int[] { 'a', 'b', 'c', 'd', 'e', 0x286D7,
+ 'f', 'g', 'h', 0, 0x2002, 0x3000, 'あ', 'x'};
+
+ int[] codePointArray = new int[50];
+ int codePointCount = StringUtils.copyCodePointsAndReturnCodePointCount(codePointArray,
+ STR_WITH_SUPPLEMENTARY_CHAR, 0,
+ STR_WITH_SUPPLEMENTARY_CHAR.length(), false /* downCase */);
+ assertEquals("copyCodePointsAndReturnCodePointCount, size matches", codePointCount,
+ EXPECTED_RESULT.length);
+ for (int i = 0; i < codePointCount; ++i) {
+ assertEquals("copyCodePointsAndReturnCodePointCount position " + i, codePointArray[i],
+ EXPECTED_RESULT[i]);
+ }
+
+ codePointCount = StringUtils.copyCodePointsAndReturnCodePointCount(codePointArray,
+ STR_WITH_SUPPLEMENTARY_CHAR, 0,
+ STR_WITH_SUPPLEMENTARY_CHAR.length(), true /* downCase */);
+ assertEquals("copyCodePointsAndReturnCodePointCount downcase, size matches", codePointCount,
+ EXPECTED_RESULT_DOWNCASE.length);
+ for (int i = 0; i < codePointCount; ++i) {
+ assertEquals("copyCodePointsAndReturnCodePointCount position " + i, codePointArray[i],
+ EXPECTED_RESULT_DOWNCASE[i]);
+ }
+
+ final int JAVA_CHAR_COUNT = 8;
+ final int CODEPOINT_COUNT = 7;
+ codePointCount = StringUtils.copyCodePointsAndReturnCodePointCount(codePointArray,
+ STR_WITH_SUPPLEMENTARY_CHAR, 0, JAVA_CHAR_COUNT, false /* downCase */);
+ assertEquals("copyCodePointsAndReturnCodePointCount, size matches", codePointCount,
+ CODEPOINT_COUNT);
+ for (int i = 0; i < codePointCount; ++i) {
+ assertEquals("copyCodePointsAndReturnCodePointCount position " + i, codePointArray[i],
+ EXPECTED_RESULT[i]);
+ }
+
+ boolean exceptionHappened = false;
+ codePointArray = new int[5];
+ try {
+ codePointCount = StringUtils.copyCodePointsAndReturnCodePointCount(codePointArray,
+ STR_WITH_SUPPLEMENTARY_CHAR, 0, JAVA_CHAR_COUNT, false /* downCase */);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ exceptionHappened = true;
+ }
+ assertTrue("copyCodePointsAndReturnCodePointCount throws when array is too small",
+ exceptionHappened);
+ }
+
+ @Test
+ public void testGetTrailingSingleQuotesCount() {
+ assertEquals(0, StringUtils.getTrailingSingleQuotesCount(""));
+ assertEquals(1, StringUtils.getTrailingSingleQuotesCount("'"));
+ assertEquals(5, StringUtils.getTrailingSingleQuotesCount("'''''"));
+ assertEquals(0, StringUtils.getTrailingSingleQuotesCount("a"));
+ assertEquals(0, StringUtils.getTrailingSingleQuotesCount("'this"));
+ assertEquals(1, StringUtils.getTrailingSingleQuotesCount("'word'"));
+ assertEquals(0, StringUtils.getTrailingSingleQuotesCount("I'm"));
+ }
+}
diff --git a/tests/src/org/kelar/inputmethod/latin/common/UnicodeSurrogateTests.java b/tests/src/org/kelar/inputmethod/latin/common/UnicodeSurrogateTests.java
new file mode 100644
index 000000000..7fad18588
--- /dev/null
+++ b/tests/src/org/kelar/inputmethod/latin/common/UnicodeSurrogateTests.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2015 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 org.kelar.inputmethod.latin.common;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class UnicodeSurrogateTests {
+
+ @Test
+ public void testIsLowSurrogate() {
+ assertFalse(UnicodeSurrogate.isLowSurrogate('\uD7FF'));
+ assertTrue(UnicodeSurrogate.isLowSurrogate('\uD83D'));
+ assertFalse(UnicodeSurrogate.isLowSurrogate('\uDC00'));
+ }
+
+ @Test
+ public void testIsHighSurrogate() {
+ assertFalse(UnicodeSurrogate.isHighSurrogate('\uDBFF'));
+ assertTrue(UnicodeSurrogate.isHighSurrogate('\uDE25'));
+ assertFalse(UnicodeSurrogate.isHighSurrogate('\uE000'));
+ }
+}