Merge "Implement ResizableIntArray.setLength and .get"

This commit is contained in:
Tadashi G. Takaoka 2012-07-19 20:44:53 -07:00 committed by Android (Google) Code Review
commit 19ac19e5fd
3 changed files with 165 additions and 32 deletions

View file

@ -24,6 +24,10 @@
*; *;
} }
-keep class com.android.inputmethod.latin.ResizableIntArray {
*;
}
-keep class com.android.inputmethod.latin.spellcheck.SpellCheckerSettingsFragment { -keep class com.android.inputmethod.latin.spellcheck.SpellCheckerSettingsFragment {
*; *;
} }

View file

@ -23,11 +23,18 @@ public class ResizableIntArray {
private int[] mArray; private int[] mArray;
private int mLength; private int mLength;
public ResizableIntArray(int capacity) { public ResizableIntArray(final int capacity) {
reset(capacity); reset(capacity);
} }
public void add(int index, int val) { public int get(final int index) {
if (index < 0 || index >= mLength) {
throw new ArrayIndexOutOfBoundsException("length=" + mLength + "; index=" + index);
}
return mArray[index];
}
public void add(final int index, final int val) {
if (mLength < index + 1) { if (mLength < index + 1) {
mLength = index; mLength = index;
add(val); add(val);
@ -36,14 +43,14 @@ public class ResizableIntArray {
} }
} }
public void add(int val) { public void add(final int val) {
final int nextLength = mLength + 1; final int nextLength = mLength + 1;
ensureCapacity(nextLength); ensureCapacity(nextLength);
mArray[mLength] = val; mArray[mLength] = val;
mLength = nextLength; mLength = nextLength;
} }
private void ensureCapacity(int minimumCapacity) { private void ensureCapacity(final int minimumCapacity) {
if (mArray.length < minimumCapacity) { if (mArray.length < minimumCapacity) {
final int nextCapacity = mArray.length * 2; final int nextCapacity = mArray.length * 2;
// The following is the same as newLength = // The following is the same as newLength =
@ -60,9 +67,12 @@ public class ResizableIntArray {
return mLength; return mLength;
} }
// TODO: Implement setLength(int). public void setLength(final int newLength) {
ensureCapacity(newLength);
mLength = newLength;
}
public void reset(int capacity) { public void reset(final int capacity) {
// TODO: Implement primitive array pool. // TODO: Implement primitive array pool.
mArray = new int[capacity]; mArray = new int[capacity];
mLength = 0; mLength = 0;
@ -72,20 +82,20 @@ public class ResizableIntArray {
return mArray; return mArray;
} }
public void set(ResizableIntArray ip) { public void set(final ResizableIntArray ip) {
// TODO: Implement primitive array pool. // TODO: Implement primitive array pool.
mArray = ip.mArray; mArray = ip.mArray;
mLength = ip.mLength; mLength = ip.mLength;
} }
public void copy(ResizableIntArray ip) { public void copy(final ResizableIntArray ip) {
// TODO: Avoid useless coping of values. // TODO: Avoid useless coping of values.
ensureCapacity(ip.mLength); ensureCapacity(ip.mLength);
System.arraycopy(ip.mArray, 0, mArray, 0, ip.mLength); System.arraycopy(ip.mArray, 0, mArray, 0, ip.mLength);
mLength = ip.mLength; mLength = ip.mLength;
} }
public void append(ResizableIntArray src, int startPos, int length) { public void append(final ResizableIntArray src, final int startPos, final int length) {
final int currentLength = mLength; final int currentLength = mLength;
final int newLength = currentLength + length; final int newLength = currentLength + length;
ensureCapacity(newLength); ensureCapacity(newLength);

View file

@ -22,50 +22,142 @@ public class ResizableIntArrayTests extends AndroidTestCase {
private static final int DEFAULT_CAPACITY = 48; private static final int DEFAULT_CAPACITY = 48;
public void testNewInstance() { public void testNewInstance() {
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
assertEquals("new instance length", 0, src.getLength());
assertNotNull("new instance array", src.getPrimitiveArray());
}
public void testReset() {
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
final int[] array = src.getPrimitiveArray(); final int[] array = src.getPrimitiveArray();
assertEquals("new instance length", 0, src.getLength());
src.reset(DEFAULT_CAPACITY); assertNotNull("new instance array", array);
assertEquals("length after reset", 0, src.getLength()); assertEquals("new instance array length", DEFAULT_CAPACITY, array.length);
assertNotSame("array after reset", array, src.getPrimitiveArray());
} }
public void testAdd() { public void testAdd() {
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
final int limit = src.getPrimitiveArray().length * 2 + 10; final int[] array = src.getPrimitiveArray();
int[] array2 = null, array3 = null;
final int limit = DEFAULT_CAPACITY * 2 + 10;
for (int i = 0; i < limit; i++) { for (int i = 0; i < limit; i++) {
src.add(i); src.add(i);
assertEquals("length after add " + i, i + 1, src.getLength()); 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++) { for (int i = 0; i < limit; i++) {
assertEquals("value at " + i, i, src.getPrimitiveArray()[i]); assertEquals("value at " + i, i, src.get(i));
} }
} }
public void testAddAt() { public void testAddAt() {
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
final int limit = 1000, step = 100; final int limit = DEFAULT_CAPACITY * 10, step = DEFAULT_CAPACITY * 2;
for (int i = 0; i < limit; i += step) { for (int i = 0; i < limit; i += step) {
src.add(i, i); src.add(i, i);
assertEquals("length after add at " + i, i + 1, src.getLength()); assertEquals("length after add at " + i, i + 1, src.getLength());
} }
for (int i = 0; i < limit; i += step) { for (int i = 0; i < limit; i += step) {
assertEquals("value at " + i, i, src.getPrimitiveArray()[i]); 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 < DEFAULT_CAPACITY; i++) {
assertEquals("value at " + i, i, src.get(i));
}
for (int i = DEFAULT_CAPACITY; i < largerLength; i++) {
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++) {
assertEquals("value at " + i, i, src.get(i));
} }
} }
public void testSet() { public void testSet() {
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
final int limit = src.getPrimitiveArray().length * 2 + 10; final int limit = DEFAULT_CAPACITY * 2 + 10;
for (int i = 0; i < limit; i++) { for (int i = 0; i < limit; i++) {
src.add(i); src.add(i);
} }
final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY); final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY);
dst.set(src); dst.set(src);
assertEquals("length after set", dst.getLength(), src.getLength()); assertEquals("length after set", dst.getLength(), src.getLength());
@ -74,27 +166,40 @@ public class ResizableIntArrayTests extends AndroidTestCase {
public void testCopy() { public void testCopy() {
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
final int limit = 100; for (int i = 0; i < DEFAULT_CAPACITY; i++) {
for (int i = 0; i < limit; i++) {
src.add(i); src.add(i);
} }
final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY); final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY);
final int[] array = dst.getPrimitiveArray();
dst.copy(src); dst.copy(src);
assertEquals("length after copy", dst.getLength(), src.getLength()); assertEquals("length after copy", dst.getLength(), src.getLength());
assertSame("array after copy", array, dst.getPrimitiveArray());
assertNotSame("array after copy", dst.getPrimitiveArray(), src.getPrimitiveArray()); assertNotSame("array after copy", dst.getPrimitiveArray(), src.getPrimitiveArray());
final int length = dst.getLength();
assertArrayEquals("values after copy", assertArrayEquals("values after copy",
dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, length); 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() { public void testAppend() {
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); final int srcLen = DEFAULT_CAPACITY;
final int srcLen = 100; final ResizableIntArray src = new ResizableIntArray(srcLen);
for (int i = 0; i < srcLen; i++) { for (int i = 0; i < srcLen; i++) {
src.add(i); src.add(i);
} }
final int dstLen = 50; final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY * 2);
final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY); final int[] array = dst.getPrimitiveArray();
final int dstLen = DEFAULT_CAPACITY / 2;
for (int i = 0; i < dstLen; i++) { for (int i = 0; i < dstLen; i++) {
final int value = -i - 1; final int value = -i - 1;
dst.add(value); dst.add(value);
@ -104,17 +209,31 @@ public class ResizableIntArrayTests extends AndroidTestCase {
dst.append(src, 0, 0); dst.append(src, 0, 0);
assertEquals("length after append zero", dstLen, dst.getLength()); assertEquals("length after append zero", dstLen, dst.getLength());
assertSame("array after append zero", array, dst.getPrimitiveArray());
assertArrayEquals("values after append zero", assertArrayEquals("values after append zero",
dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen); dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen);
dst.append(src, 0, srcLen); dst.append(src, 0, srcLen);
assertEquals("length after append", dstLen + srcLen, dst.getLength()); assertEquals("length after append", dstLen + srcLen, dst.getLength());
assertSame("array after append", array, dst.getPrimitiveArray());
assertTrue("primitive length after append", assertTrue("primitive length after append",
dst.getPrimitiveArray().length >= dstLen + srcLen); dst.getPrimitiveArray().length >= dstLen + srcLen);
assertArrayEquals("original values after append", assertArrayEquals("original values after append",
dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen); dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen);
assertArrayEquals("appended values after append", assertArrayEquals("appended values after append",
src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen, srcLen); 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);
} }
private static void assertArrayEquals(String message, int[] expecteds, int expectedPos, private static void assertArrayEquals(String message, int[] expecteds, int expectedPos,