aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src
diff options
context:
space:
mode:
Diffstat (limited to 'tests/src')
-rw-r--r--tests/src/com/android/inputmethod/keyboard/SpacebarTextTests.java70
-rw-r--r--tests/src/com/android/inputmethod/latin/InputPointersTests.java122
-rw-r--r--tests/src/com/android/inputmethod/latin/ResizableIntArrayTests.java333
3 files changed, 430 insertions, 95 deletions
diff --git a/tests/src/com/android/inputmethod/keyboard/SpacebarTextTests.java b/tests/src/com/android/inputmethod/keyboard/SpacebarTextTests.java
index a34e0ef8b..87501eeb2 100644
--- a/tests/src/com/android/inputmethod/keyboard/SpacebarTextTests.java
+++ b/tests/src/com/android/inputmethod/keyboard/SpacebarTextTests.java
@@ -46,7 +46,7 @@ public class SpacebarTextTests extends AndroidTestCase {
public void testAllFullDisplayName() {
for (final InputMethodSubtype subtype : mSubtypesList) {
final String subtypeName = SubtypeLocale.getSubtypeDisplayName(subtype, mRes);
- final String spacebarText = LatinKeyboardView.getFullDisplayName(subtype, mRes);
+ final String spacebarText = MainKeyboardView.getFullDisplayName(subtype, mRes);
final String languageName =
SubtypeLocale.getSubtypeLocaleDisplayName(subtype.getLocale());
if (SubtypeLocale.isNoLanguage(subtype)) {
@@ -60,7 +60,7 @@ public class SpacebarTextTests extends AndroidTestCase {
public void testAllMiddleDisplayName() {
for (final InputMethodSubtype subtype : mSubtypesList) {
final String subtypeName = SubtypeLocale.getSubtypeDisplayName(subtype, mRes);
- final String spacebarText = LatinKeyboardView.getMiddleDisplayName(subtype);
+ final String spacebarText = MainKeyboardView.getMiddleDisplayName(subtype);
if (SubtypeLocale.isNoLanguage(subtype)) {
assertEquals(subtypeName,
SubtypeLocale.getKeyboardLayoutSetName(subtype), spacebarText);
@@ -76,7 +76,7 @@ public class SpacebarTextTests extends AndroidTestCase {
for (final InputMethodSubtype subtype : mSubtypesList) {
final String subtypeName = SubtypeLocale.getSubtypeDisplayName(subtype, mRes);
final Locale locale = SubtypeLocale.getSubtypeLocale(subtype);
- final String spacebarText = LatinKeyboardView.getShortDisplayName(subtype);
+ final String spacebarText = MainKeyboardView.getShortDisplayName(subtype);
final String languageCode = StringUtils.toTitleCase(locale.getLanguage(), locale);
if (SubtypeLocale.isNoLanguage(subtype)) {
assertEquals(subtypeName, "", spacebarText);
@@ -117,31 +117,31 @@ public class SpacebarTextTests extends AndroidTestCase {
context, SubtypeLocale.NO_LANGUAGE, "qwerty");
assertEquals("en_US", "English (US)",
- LatinKeyboardView.getFullDisplayName(EN_US, mRes));
+ MainKeyboardView.getFullDisplayName(EN_US, mRes));
assertEquals("en_GB", "English (UK)",
- LatinKeyboardView.getFullDisplayName(EN_GB, mRes));
+ MainKeyboardView.getFullDisplayName(EN_GB, mRes));
assertEquals("fr ", "Français",
- LatinKeyboardView.getFullDisplayName(FR, mRes));
+ MainKeyboardView.getFullDisplayName(FR, mRes));
assertEquals("fr_CA", "Français (Canada)",
- LatinKeyboardView.getFullDisplayName(FR_CA, mRes));
+ MainKeyboardView.getFullDisplayName(FR_CA, mRes));
assertEquals("de ", "Deutsch",
- LatinKeyboardView.getFullDisplayName(DE, mRes));
+ MainKeyboardView.getFullDisplayName(DE, mRes));
assertEquals("zz ", "QWERTY",
- LatinKeyboardView.getFullDisplayName(ZZ, mRes));
-
- assertEquals("en_US", "English", LatinKeyboardView.getMiddleDisplayName(EN_US));
- assertEquals("en_GB", "English", LatinKeyboardView.getMiddleDisplayName(EN_GB));
- assertEquals("fr ", "Français", LatinKeyboardView.getMiddleDisplayName(FR));
- assertEquals("fr_CA", "Français", LatinKeyboardView.getMiddleDisplayName(FR_CA));
- assertEquals("de ", "Deutsch", LatinKeyboardView.getMiddleDisplayName(DE));
- assertEquals("zz ", "QWERTY", LatinKeyboardView.getMiddleDisplayName(ZZ));
-
- assertEquals("en_US", "En", LatinKeyboardView.getShortDisplayName(EN_US));
- assertEquals("en_GB", "En", LatinKeyboardView.getShortDisplayName(EN_GB));
- assertEquals("fr ", "Fr", LatinKeyboardView.getShortDisplayName(FR));
- assertEquals("fr_CA", "Fr", LatinKeyboardView.getShortDisplayName(FR_CA));
- assertEquals("de ", "De", LatinKeyboardView.getShortDisplayName(DE));
- assertEquals("zz ", "", LatinKeyboardView.getShortDisplayName(ZZ));
+ MainKeyboardView.getFullDisplayName(ZZ, mRes));
+
+ assertEquals("en_US", "English", MainKeyboardView.getMiddleDisplayName(EN_US));
+ assertEquals("en_GB", "English", MainKeyboardView.getMiddleDisplayName(EN_GB));
+ assertEquals("fr ", "Français", MainKeyboardView.getMiddleDisplayName(FR));
+ assertEquals("fr_CA", "Français", MainKeyboardView.getMiddleDisplayName(FR_CA));
+ assertEquals("de ", "Deutsch", MainKeyboardView.getMiddleDisplayName(DE));
+ assertEquals("zz ", "QWERTY", MainKeyboardView.getMiddleDisplayName(ZZ));
+
+ assertEquals("en_US", "En", MainKeyboardView.getShortDisplayName(EN_US));
+ assertEquals("en_GB", "En", MainKeyboardView.getShortDisplayName(EN_GB));
+ assertEquals("fr ", "Fr", MainKeyboardView.getShortDisplayName(FR));
+ assertEquals("fr_CA", "Fr", MainKeyboardView.getShortDisplayName(FR_CA));
+ assertEquals("de ", "De", MainKeyboardView.getShortDisplayName(DE));
+ assertEquals("zz ", "", MainKeyboardView.getShortDisplayName(ZZ));
}
public void testAdditionalSubtype() {
@@ -155,22 +155,22 @@ public class SpacebarTextTests extends AndroidTestCase {
SubtypeLocale.NO_LANGUAGE, "azerty", null);
assertEquals("fr qwertz", "Français (QWERTZ)",
- LatinKeyboardView.getFullDisplayName(FR_QWERTZ, mRes));
+ MainKeyboardView.getFullDisplayName(FR_QWERTZ, mRes));
assertEquals("de qwerty", "Deutsch (QWERTY)",
- LatinKeyboardView.getFullDisplayName(DE_QWERTY, mRes));
+ MainKeyboardView.getFullDisplayName(DE_QWERTY, mRes));
assertEquals("en_US azerty", "English (US) (AZERTY)",
- LatinKeyboardView.getFullDisplayName(US_AZERTY, mRes));
+ MainKeyboardView.getFullDisplayName(US_AZERTY, mRes));
assertEquals("zz azerty", "AZERTY",
- LatinKeyboardView.getFullDisplayName(ZZ_AZERTY, mRes));
+ MainKeyboardView.getFullDisplayName(ZZ_AZERTY, mRes));
- assertEquals("fr qwertz", "Français", LatinKeyboardView.getMiddleDisplayName(FR_QWERTZ));
- assertEquals("de qwerty", "Deutsch", LatinKeyboardView.getMiddleDisplayName(DE_QWERTY));
- assertEquals("en_US azerty", "English", LatinKeyboardView.getMiddleDisplayName(US_AZERTY));
- assertEquals("zz azerty", "AZERTY", LatinKeyboardView.getMiddleDisplayName(ZZ_AZERTY));
+ assertEquals("fr qwertz", "Français", MainKeyboardView.getMiddleDisplayName(FR_QWERTZ));
+ assertEquals("de qwerty", "Deutsch", MainKeyboardView.getMiddleDisplayName(DE_QWERTY));
+ assertEquals("en_US azerty", "English", MainKeyboardView.getMiddleDisplayName(US_AZERTY));
+ assertEquals("zz azerty", "AZERTY", MainKeyboardView.getMiddleDisplayName(ZZ_AZERTY));
- assertEquals("fr qwertz", "Fr", LatinKeyboardView.getShortDisplayName(FR_QWERTZ));
- assertEquals("de qwerty", "De", LatinKeyboardView.getShortDisplayName(DE_QWERTY));
- assertEquals("en_US azerty", "En", LatinKeyboardView.getShortDisplayName(US_AZERTY));
- assertEquals("zz azerty", "", LatinKeyboardView.getShortDisplayName(ZZ_AZERTY));
+ assertEquals("fr qwertz", "Fr", MainKeyboardView.getShortDisplayName(FR_QWERTZ));
+ assertEquals("de qwerty", "De", MainKeyboardView.getShortDisplayName(DE_QWERTY));
+ assertEquals("en_US azerty", "En", MainKeyboardView.getShortDisplayName(US_AZERTY));
+ assertEquals("zz azerty", "", MainKeyboardView.getShortDisplayName(ZZ_AZERTY));
}
}
diff --git a/tests/src/com/android/inputmethod/latin/InputPointersTests.java b/tests/src/com/android/inputmethod/latin/InputPointersTests.java
index 524921e25..6f04f3ebb 100644
--- a/tests/src/com/android/inputmethod/latin/InputPointersTests.java
+++ b/tests/src/com/android/inputmethod/latin/InputPointersTests.java
@@ -19,9 +19,11 @@ package com.android.inputmethod.latin;
import android.test.AndroidTestCase;
public class InputPointersTests extends AndroidTestCase {
+ private static final int DEFAULT_CAPACITY = 48;
+
public void testNewInstance() {
- final InputPointers src = new InputPointers();
- assertEquals("newInstance size", 0, src.getPointerSize());
+ 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());
@@ -29,26 +31,26 @@ public class InputPointersTests extends AndroidTestCase {
}
public void testReset() {
- final InputPointers src = new InputPointers();
+ 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("after reset size", 0, src.getPointerSize());
- assertNotSame("after reset xCoordinates", xCoordinates, src.getXCoordinates());
- assertNotSame("after reset yCoordinates", yCoordinates, src.getYCoordinates());
- assertNotSame("after reset pointerIds", pointerIds, src.getPointerIds());
- assertNotSame("after reset times", times, src.getTimes());
+ 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();
+ final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
final int limit = src.getXCoordinates().length * 2 + 10;
for (int i = 0; i < limit; i++) {
src.addPointer(i, i * 2, i * 3, i * 4);
- assertEquals("after add " + i, i + 1, src.getPointerSize());
+ assertEquals("size after add " + i, i + 1, src.getPointerSize());
}
for (int i = 0; i < limit; i++) {
assertEquals("xCoordinates at " + i, i, src.getXCoordinates()[i]);
@@ -59,11 +61,11 @@ public class InputPointersTests extends AndroidTestCase {
}
public void testAddAt() {
- final InputPointers src = new InputPointers();
+ final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
final int limit = 1000, step = 100;
for (int i = 0; i < limit; i += step) {
src.addPointer(i, i, i * 2, i * 3, i * 4);
- assertEquals("after add at " + i, i + 1, src.getPointerSize());
+ assertEquals("size after add at " + i, i + 1, src.getPointerSize());
}
for (int i = 0; i < limit; i += step) {
assertEquals("xCoordinates at " + i, i, src.getXCoordinates()[i]);
@@ -74,90 +76,90 @@ public class InputPointersTests extends AndroidTestCase {
}
public void testSet() {
- final InputPointers src = new InputPointers();
+ final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
final int limit = src.getXCoordinates().length * 2 + 10;
for (int i = 0; i < limit; i++) {
src.addPointer(i, i * 2, i * 3, i * 4);
}
- final InputPointers dst = new InputPointers();
+ final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
dst.set(src);
- assertEquals("after set size", dst.getPointerSize(), src.getPointerSize());
- assertSame("after set xCoordinates", dst.getXCoordinates(), src.getXCoordinates());
- assertSame("after set yCoordinates", dst.getYCoordinates(), src.getYCoordinates());
- assertSame("after set pointerIds", dst.getPointerIds(), src.getPointerIds());
- assertSame("after set times", dst.getTimes(), src.getTimes());
+ 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();
+ final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
final int limit = 100;
for (int i = 0; i < limit; i++) {
src.addPointer(i, i * 2, i * 3, i * 4);
}
- final InputPointers dst = new InputPointers();
+ final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
dst.copy(src);
- assertEquals("after copy size", dst.getPointerSize(), src.getPointerSize());
- assertNotSame("after copy xCoordinates", dst.getXCoordinates(), src.getXCoordinates());
- assertNotSame("after copy yCoordinates", dst.getYCoordinates(), src.getYCoordinates());
- assertNotSame("after copy pointerIds", dst.getPointerIds(), src.getPointerIds());
- assertNotSame("after copy times", dst.getTimes(), src.getTimes());
+ 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();
- assertArrayEquals("after copy xCoordinates values",
+ assertArrayEquals("xCoordinates values after copy",
dst.getXCoordinates(), 0, src.getXCoordinates(), 0, size);
- assertArrayEquals("after copy yCoordinates values",
+ assertArrayEquals("yCoordinates values after copy",
dst.getYCoordinates(), 0, src.getYCoordinates(), 0, size);
- assertArrayEquals("after copy pointerIds values",
+ assertArrayEquals("pointerIds values after copy",
dst.getPointerIds(), 0, src.getPointerIds(), 0, size);
- assertArrayEquals("after copy times values",
+ assertArrayEquals("times values after copy",
dst.getTimes(), 0, src.getTimes(), 0, size);
}
public void testAppend() {
- final InputPointers src = new InputPointers();
+ final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
final int srcLen = 100;
for (int i = 0; i < srcLen; i++) {
src.addPointer(i, i * 2, i * 3, i * 4);
}
final int dstLen = 50;
- final InputPointers dst = new InputPointers();
+ final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
for (int i = 0; i < dstLen; i++) {
final int value = -i - 1;
dst.addPointer(value * 4, value * 3, value * 2, value);
}
- final InputPointers dstCopy = new InputPointers();
+ final InputPointers dstCopy = new InputPointers(DEFAULT_CAPACITY);
dstCopy.copy(dst);
dst.append(src, 0, 0);
- assertEquals("after append zero size", dstLen, dst.getPointerSize());
- assertArrayEquals("after append zero xCoordinates", dstCopy.getXCoordinates(), 0,
- dst.getXCoordinates(), 0, dstLen);
- assertArrayEquals("after append zero yCoordinates", dstCopy.getYCoordinates(), 0,
- dst.getYCoordinates(), 0, dstLen);
- assertArrayEquals("after append zero pointerIds", dstCopy.getPointerIds(), 0,
- dst.getPointerIds(), 0, dstLen);
- assertArrayEquals("after append zero times", dstCopy.getTimes(), 0,
- dst.getTimes(), 0, dstLen);
+ assertEquals("size after append zero", dstLen, dst.getPointerSize());
+ assertArrayEquals("xCoordinates after append zero",
+ dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen);
+ assertArrayEquals("yCoordinates after append zero",
+ dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen);
+ assertArrayEquals("pointerIds after append zero",
+ dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen);
+ assertArrayEquals("times after append zero",
+ dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen);
dst.append(src, 0, srcLen);
- assertEquals("after append size", dstLen + srcLen, dst.getPointerSize());
- assertTrue("after append size primitive length",
+ assertEquals("size after append", dstLen + srcLen, dst.getPointerSize());
+ assertTrue("primitive length after append",
dst.getPointerIds().length >= dstLen + srcLen);
- assertArrayEquals("after append xCoordinates", dstCopy.getXCoordinates(), 0,
- dst.getXCoordinates(), 0, dstLen);
- assertArrayEquals("after append yCoordinates", dstCopy.getYCoordinates(), 0,
- dst.getYCoordinates(), 0, dstLen);
- assertArrayEquals("after append pointerIds", dstCopy.getPointerIds(), 0,
- dst.getPointerIds(), 0, dstLen);
- assertArrayEquals("after append times", dstCopy.getTimes(), 0,
- dst.getTimes(), 0, dstLen);
- assertArrayEquals("after append xCoordinates", dst.getXCoordinates(), dstLen,
- src.getXCoordinates(), 0, srcLen);
- assertArrayEquals("after append yCoordinates", dst.getYCoordinates(), dstLen,
- src.getYCoordinates(), 0, srcLen);
- assertArrayEquals("after append pointerIds", dst.getPointerIds(), dstLen,
- src.getPointerIds(), 0, srcLen);
- assertArrayEquals("after append times", dst.getTimes(), dstLen,
- src.getTimes(), 0, srcLen);
+ assertArrayEquals("original xCoordinates values after append",
+ dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen);
+ assertArrayEquals("original yCoordinates values after append",
+ dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen);
+ assertArrayEquals("original pointerIds values after append",
+ dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen);
+ assertArrayEquals("original times values after append",
+ dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen);
+ assertArrayEquals("appended xCoordinates values after append",
+ src.getXCoordinates(), 0, dst.getXCoordinates(), dstLen, srcLen);
+ assertArrayEquals("appended yCoordinates values after append",
+ src.getYCoordinates(), 0, dst.getYCoordinates(), dstLen, srcLen);
+ assertArrayEquals("appended pointerIds values after append",
+ src.getPointerIds(), 0, dst.getPointerIds(), dstLen, srcLen);
+ assertArrayEquals("appended times values after append",
+ src.getTimes(), 0, dst.getTimes(), dstLen, srcLen);
}
private static void assertArrayEquals(String message, int[] expecteds, int expectedPos,
diff --git a/tests/src/com/android/inputmethod/latin/ResizableIntArrayTests.java b/tests/src/com/android/inputmethod/latin/ResizableIntArrayTests.java
new file mode 100644
index 000000000..995fc14ea
--- /dev/null
+++ b/tests/src/com/android/inputmethod/latin/ResizableIntArrayTests.java
@@ -0,0 +1,333 @@
+/*
+ * 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;
+
+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++) {
+ src.add(i);
+ 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++) {
+ assertEquals("value at " + i, i, 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) {
+ src.add(i, i);
+ assertEquals("length after add at " + i, i + 1, src.getLength());
+ }
+ for (int i = 0; i < limit; i += step) {
+ assertEquals("value at " + i, i, src.get(i));
+ }
+ }
+
+ public void testGet() {
+ final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
+ try {
+ final int value = src.get(0);
+ fail("get(0) shouldn't succeed");
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // success
+ }
+ try {
+ final int value = src.get(DEFAULT_CAPACITY);
+ fail("get(DEFAULT_CAPACITY) shouldn't succeed");
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // success
+ }
+
+ final int index = DEFAULT_CAPACITY / 2;
+ src.add(index, 100);
+ assertEquals("legth after add at " + index, index + 1, src.getLength());
+ assertEquals("value after add at " + index, 100, src.get(index));
+ assertEquals("value after add at 0", 0, src.get(0));
+ try {
+ final int value = 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++) {
+ src.add(i);
+ 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++) {
+ src.add(i);
+ 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++) {
+ src.add(i);
+ 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 v = src.get(i);
+ if (i < DEFAULT_CAPACITY) {
+ assertEquals("value at " + i, i, v);
+ } else {
+ assertEquals("value at " + i, 0, v);
+ }
+ }
+
+ 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++) {
+ assertEquals("value at " + i, i, 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++) {
+ src.add(i);
+ }
+
+ 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++) {
+ src.add(i);
+ }
+
+ 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());
+ assertArrayEquals("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());
+ assertArrayEquals("values after copy to smaller",
+ dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength());
+ }
+
+ public void testAppend() {
+ final int srcLen = DEFAULT_CAPACITY;
+ final ResizableIntArray src = new ResizableIntArray(srcLen);
+ for (int i = 0; i < srcLen; i++) {
+ src.add(i);
+ }
+ final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY * 2);
+ final int[] array = dst.getPrimitiveArray();
+ final int dstLen = DEFAULT_CAPACITY / 2;
+ for (int i = 0; i < dstLen; i++) {
+ final int value = -i - 1;
+ dst.add(value);
+ }
+ final ResizableIntArray dstCopy = new ResizableIntArray(dst.getLength());
+ dstCopy.copy(dst);
+
+ dst.append(src, 0, 0);
+ assertEquals("length after append zero", dstLen, dst.getLength());
+ assertSame("array after append zero", array, dst.getPrimitiveArray());
+ assertArrayEquals("values after append zero",
+ dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen);
+
+ dst.append(src, 0, srcLen);
+ assertEquals("length after append", dstLen + srcLen, dst.getLength());
+ assertSame("array after append", array, dst.getPrimitiveArray());
+ assertTrue("primitive length after append",
+ dst.getPrimitiveArray().length >= dstLen + srcLen);
+ assertArrayEquals("original values after append",
+ dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen);
+ assertArrayEquals("appended values after append",
+ src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen, srcLen);
+
+ dst.append(src, 0, srcLen);
+ assertEquals("length after 2nd append", dstLen + srcLen * 2, dst.getLength());
+ assertNotSame("array after 2nd append", array, dst.getPrimitiveArray());
+ assertTrue("primitive length after 2nd append",
+ dst.getPrimitiveArray().length >= dstLen + srcLen * 2);
+ assertArrayEquals("original values after 2nd append",
+ dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen);
+ assertArrayEquals("appended values after 2nd append",
+ src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen, srcLen);
+ assertArrayEquals("appended values after 2nd append",
+ src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen + srcLen, srcLen);
+ }
+
+ public void testFill() {
+ final int srcLen = DEFAULT_CAPACITY;
+ final ResizableIntArray src = new ResizableIntArray(srcLen);
+ for (int i = 0; i < srcLen; i++) {
+ src.add(i);
+ }
+ final int[] array = src.getPrimitiveArray();
+
+ final int startPos = srcLen / 3;
+ final int length = srcLen / 3;
+ final int endPos = startPos + length;
+ assertTrue(startPos >= 1);
+ final int value = 123;
+ try {
+ src.fill(value, -1, length);
+ fail("fill from -1 shouldn't succeed");
+ } catch (IllegalArgumentException e) {
+ // success
+ }
+ try {
+ src.fill(value, startPos, -1);
+ fail("fill negative length shouldn't succeed");
+ } catch (IllegalArgumentException e) {
+ // success
+ }
+
+ src.fill(value, startPos, length);
+ assertEquals("length after fill", srcLen, src.getLength());
+ assertSame("array after fill", array, src.getPrimitiveArray());
+ for (int i = 0; i < srcLen; i++) {
+ final int v = src.get(i);
+ if (i >= startPos && i < endPos) {
+ assertEquals("new values after fill at " + i, value, v);
+ } else {
+ assertEquals("unmodified values after fill at " + i, i, v);
+ }
+ }
+
+ final int length2 = srcLen * 2 - startPos;
+ final int largeEnd = startPos + length2;
+ assertTrue(largeEnd > srcLen);
+ final int value2 = 456;
+ src.fill(value2, 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 v = src.get(i);
+ if (i >= startPos && i < largeEnd) {
+ assertEquals("new values after large fill at " + i, value2, v);
+ } else {
+ assertEquals("unmodified values after large fill at " + i, i, v);
+ }
+ }
+
+ final int startPos2 = largeEnd + length2;
+ final int endPos2 = startPos2 + length2;
+ final int value3 = 789;
+ src.fill(value3, startPos2, length2);
+ assertEquals("length after disjoint fill", endPos2, src.getLength());
+ for (int i = 0; i < endPos2; i++) {
+ final int v = src.get(i);
+ if (i >= startPos2 && i < endPos2) {
+ assertEquals("new values after disjoint fill at " + i, value3, v);
+ } else if (i >= startPos && i < largeEnd) {
+ assertEquals("unmodified values after disjoint fill at " + i, value2, v);
+ } else if (i < startPos) {
+ assertEquals("unmodified values after disjoint fill at " + i, i, v);
+ } else {
+ assertEquals("gap values after disjoint fill at " + i, 0, v);
+ }
+ }
+ }
+
+ private static void assertArrayEquals(String message, int[] expecteds, int expectedPos,
+ int[] actuals, int actualPos, int length) {
+ if (expecteds == null && actuals == null) {
+ return;
+ }
+ if (expecteds == null || actuals == null) {
+ fail(message + ": expecteds=" + expecteds + " actuals=" + actuals);
+ }
+ for (int i = 0; i < length; i++) {
+ assertEquals(message + ": element at " + i,
+ expecteds[i + expectedPos], actuals[i + actualPos]);
+ }
+ }
+}