am e13cc9d7: Fix ResizableIntArrayTests readability

* commit 'e13cc9d7e596c8c9db1b36a772b51e4960137671':
  Fix ResizableIntArrayTests readability
main
Tadashi G. Takaoka 2014-01-07 22:10:50 -08:00 committed by Android Git Automerger
commit 12f569ba95
2 changed files with 90 additions and 69 deletions

View File

@ -34,6 +34,7 @@ public final class ResizableIntArray {
throw new ArrayIndexOutOfBoundsException("length=" + mLength + "; index=" + index); throw new ArrayIndexOutOfBoundsException("length=" + mLength + "; index=" + index);
} }
// TODO: Rename this method to addAt.
public void add(final int index, final int val) { public void add(final int index, final int val) {
if (index < mLength) { if (index < mLength) {
mArray[index] = val; mArray[index] = val;

View File

@ -39,7 +39,8 @@ public class ResizableIntArrayTests extends AndroidTestCase {
int[] array2 = null, array3 = null; int[] array2 = null, array3 = null;
final int limit = DEFAULT_CAPACITY * 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); final int value = i;
src.add(value);
assertEquals("length after add " + i, i + 1, src.getLength()); assertEquals("length after add " + i, i + 1, src.getLength());
if (i == DEFAULT_CAPACITY) { if (i == DEFAULT_CAPACITY) {
array2 = src.getPrimitiveArray(); array2 = src.getPrimitiveArray();
@ -56,7 +57,8 @@ public class ResizableIntArrayTests extends AndroidTestCase {
} }
} }
for (int i = 0; i < limit; i++) { for (int i = 0; i < limit; i++) {
assertEquals("value at " + i, i, src.get(i)); final int value = i;
assertEquals("value at " + i, value, src.get(i));
} }
} }
@ -64,11 +66,13 @@ public class ResizableIntArrayTests extends AndroidTestCase {
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
final int limit = DEFAULT_CAPACITY * 10, step = DEFAULT_CAPACITY * 2; 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); final int value = i;
src.add(i, value);
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.get(i)); final int value = i;
assertEquals("value at " + i, value, src.get(i));
} }
} }
@ -88,9 +92,10 @@ public class ResizableIntArrayTests extends AndroidTestCase {
} }
final int index = DEFAULT_CAPACITY / 2; final int index = DEFAULT_CAPACITY / 2;
src.add(index, 100); final int valueAddAt = 100;
src.add(index, valueAddAt);
assertEquals("legth after add at " + index, index + 1, src.getLength()); 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 " + index, valueAddAt, src.get(index));
assertEquals("value after add at 0", 0, src.get(0)); assertEquals("value after add at 0", 0, src.get(0));
try { try {
final int value = src.get(src.getLength()); final int value = src.get(src.getLength());
@ -104,7 +109,8 @@ public class ResizableIntArrayTests extends AndroidTestCase {
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
final int[] array = src.getPrimitiveArray(); final int[] array = src.getPrimitiveArray();
for (int i = 0; i < DEFAULT_CAPACITY; i++) { for (int i = 0; i < DEFAULT_CAPACITY; i++) {
src.add(i); final int value = i;
src.add(value);
assertEquals("length after add " + i, i + 1, src.getLength()); assertEquals("length after add " + i, i + 1, src.getLength());
} }
@ -116,7 +122,8 @@ public class ResizableIntArrayTests extends AndroidTestCase {
int[] array3 = null; int[] array3 = null;
for (int i = 0; i < DEFAULT_CAPACITY; i++) { for (int i = 0; i < DEFAULT_CAPACITY; i++) {
src.add(i); final int value = i;
src.add(value);
assertEquals("length after add " + i, i + 1, src.getLength()); assertEquals("length after add " + i, i + 1, src.getLength());
if (i == smallerLength) { if (i == smallerLength) {
array3 = src.getPrimitiveArray(); array3 = src.getPrimitiveArray();
@ -133,7 +140,8 @@ public class ResizableIntArrayTests extends AndroidTestCase {
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
final int[] array = src.getPrimitiveArray(); final int[] array = src.getPrimitiveArray();
for (int i = 0; i < DEFAULT_CAPACITY; i++) { for (int i = 0; i < DEFAULT_CAPACITY; i++) {
src.add(i); final int value = i;
src.add(value);
assertEquals("length after add " + i, i + 1, src.getLength()); assertEquals("length after add " + i, i + 1, src.getLength());
} }
@ -144,11 +152,11 @@ public class ResizableIntArrayTests extends AndroidTestCase {
assertNotSame("array after larger setLength", array, array2); assertNotSame("array after larger setLength", array, array2);
assertEquals("array length after larger setLength", largerLength, array2.length); assertEquals("array length after larger setLength", largerLength, array2.length);
for (int i = 0; i < largerLength; i++) { for (int i = 0; i < largerLength; i++) {
final int v = src.get(i); final int value = i;
if (i < DEFAULT_CAPACITY) { if (i < DEFAULT_CAPACITY) {
assertEquals("value at " + i, i, v); assertEquals("value at " + i, value, src.get(i));
} else { } else {
assertEquals("value at " + i, 0, v); assertEquals("value at " + i, 0, src.get(i));
} }
} }
@ -159,7 +167,8 @@ public class ResizableIntArrayTests extends AndroidTestCase {
assertSame("array after smaller setLength", array2, array3); assertSame("array after smaller setLength", array2, array3);
assertEquals("array length after smaller setLength", largerLength, array3.length); assertEquals("array length after smaller setLength", largerLength, array3.length);
for (int i = 0; i < smallerLength; i++) { for (int i = 0; i < smallerLength; i++) {
assertEquals("value at " + i, i, src.get(i)); final int value = i;
assertEquals("value at " + i, value, src.get(i));
} }
} }
@ -167,7 +176,8 @@ public class ResizableIntArrayTests extends AndroidTestCase {
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY); final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
final int limit = DEFAULT_CAPACITY * 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); final int value = i;
src.add(value);
} }
final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY); final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY);
@ -179,7 +189,8 @@ 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);
for (int i = 0; i < DEFAULT_CAPACITY; i++) { for (int i = 0; i < DEFAULT_CAPACITY; i++) {
src.add(i); final int value = i;
src.add(value);
} }
final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY); final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY);
@ -204,119 +215,126 @@ public class ResizableIntArrayTests extends AndroidTestCase {
} }
public void testAppend() { public void testAppend() {
final int srcLen = DEFAULT_CAPACITY; final int srcLength = DEFAULT_CAPACITY;
final ResizableIntArray src = new ResizableIntArray(srcLen); final ResizableIntArray src = new ResizableIntArray(srcLength);
for (int i = 0; i < srcLen; i++) { for (int i = 0; i < srcLength; i++) {
src.add(i); final int value = i;
src.add(value);
} }
final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY * 2); final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY * 2);
final int[] array = dst.getPrimitiveArray(); final int[] array = dst.getPrimitiveArray();
final int dstLen = DEFAULT_CAPACITY / 2; final int dstLength = DEFAULT_CAPACITY / 2;
for (int i = 0; i < dstLen; i++) { for (int i = 0; i < dstLength; i++) {
final int value = -i - 1; final int value = -i - 1;
dst.add(value); dst.add(value);
} }
final ResizableIntArray dstCopy = new ResizableIntArray(dst.getLength()); final ResizableIntArray dstCopy = new ResizableIntArray(dst.getLength());
dstCopy.copy(dst); dstCopy.copy(dst);
dst.append(src, 0, 0); final int startPos = 0;
assertEquals("length after append zero", dstLen, dst.getLength()); dst.append(src, startPos, 0 /* length */);
assertEquals("length after append zero", dstLength, dst.getLength());
assertSame("array after append zero", array, dst.getPrimitiveArray()); assertSame("array after append zero", array, dst.getPrimitiveArray());
assertIntArrayEquals("values after append zero", assertIntArrayEquals("values after append zero", dstCopy.getPrimitiveArray(), startPos,
dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen); dst.getPrimitiveArray(), startPos, dstLength);
dst.append(src, 0, srcLen); dst.append(src, startPos, srcLength);
assertEquals("length after append", dstLen + srcLen, dst.getLength()); assertEquals("length after append", dstLength + srcLength, dst.getLength());
assertSame("array after append", array, dst.getPrimitiveArray()); 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 >= dstLength + srcLength);
assertIntArrayEquals("original values after append", assertIntArrayEquals("original values after append", dstCopy.getPrimitiveArray(), startPos,
dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen); dst.getPrimitiveArray(), startPos, dstLength);
assertIntArrayEquals("appended values after append", assertIntArrayEquals("appended values after append", src.getPrimitiveArray(), startPos,
src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen, srcLen); dst.getPrimitiveArray(), dstLength, srcLength);
dst.append(src, 0, srcLen); dst.append(src, startPos, srcLength);
assertEquals("length after 2nd append", dstLen + srcLen * 2, dst.getLength()); assertEquals("length after 2nd append", dstLength + srcLength * 2, dst.getLength());
assertNotSame("array after 2nd append", array, dst.getPrimitiveArray()); assertNotSame("array after 2nd append", array, dst.getPrimitiveArray());
assertTrue("primitive length after 2nd append", assertTrue("primitive length after 2nd append",
dst.getPrimitiveArray().length >= dstLen + srcLen * 2); dst.getPrimitiveArray().length >= dstLength + srcLength * 2);
assertIntArrayEquals("original values after 2nd append", assertIntArrayEquals("original values after 2nd append",
dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen); dstCopy.getPrimitiveArray(), startPos, dst.getPrimitiveArray(), startPos,
dstLength);
assertIntArrayEquals("appended values after 2nd append", assertIntArrayEquals("appended values after 2nd append",
src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen, srcLen); src.getPrimitiveArray(), startPos, dst.getPrimitiveArray(), dstLength,
srcLength);
assertIntArrayEquals("appended values after 2nd append", assertIntArrayEquals("appended values after 2nd append",
src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen + srcLen, srcLen); src.getPrimitiveArray(), startPos, dst.getPrimitiveArray(), dstLength + srcLength,
srcLength);
} }
public void testFill() { public void testFill() {
final int srcLen = DEFAULT_CAPACITY; final int srcLength = DEFAULT_CAPACITY;
final ResizableIntArray src = new ResizableIntArray(srcLen); final ResizableIntArray src = new ResizableIntArray(srcLength);
for (int i = 0; i < srcLen; i++) { for (int i = 0; i < srcLength; i++) {
src.add(i); final int value = i;
src.add(value);
} }
final int[] array = src.getPrimitiveArray(); final int[] array = src.getPrimitiveArray();
final int startPos = srcLen / 3; final int startPos = srcLength / 3;
final int length = srcLen / 3; final int length = srcLength / 3;
final int endPos = startPos + length; final int endPos = startPos + length;
assertTrue(startPos >= 1); assertTrue(startPos >= 1);
final int value = 123; final int fillValue = 123;
try { try {
src.fill(value, -1, length); src.fill(fillValue, -1 /* startPos */, length);
fail("fill from -1 shouldn't succeed"); fail("fill from -1 shouldn't succeed");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// success // success
} }
try { try {
src.fill(value, startPos, -1); src.fill(fillValue, startPos, -1 /* length */);
fail("fill negative length shouldn't succeed"); fail("fill negative length shouldn't succeed");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// success // success
} }
src.fill(value, startPos, length); src.fill(fillValue, startPos, length);
assertEquals("length after fill", srcLen, src.getLength()); assertEquals("length after fill", srcLength, src.getLength());
assertSame("array after fill", array, src.getPrimitiveArray()); assertSame("array after fill", array, src.getPrimitiveArray());
for (int i = 0; i < srcLen; i++) { for (int i = 0; i < srcLength; i++) {
final int v = src.get(i); final int value = i;
if (i >= startPos && i < endPos) { if (i >= startPos && i < endPos) {
assertEquals("new values after fill at " + i, value, v); assertEquals("new values after fill at " + i, fillValue, src.get(i));
} else { } else {
assertEquals("unmodified values after fill at " + i, i, v); assertEquals("unmodified values after fill at " + i, value, src.get(i));
} }
} }
final int length2 = srcLen * 2 - startPos; final int length2 = srcLength * 2 - startPos;
final int largeEnd = startPos + length2; final int largeEnd = startPos + length2;
assertTrue(largeEnd > srcLen); assertTrue(largeEnd > srcLength);
final int value2 = 456; final int fillValue2 = 456;
src.fill(value2, startPos, length2); src.fill(fillValue2, startPos, length2);
assertEquals("length after large fill", largeEnd, src.getLength()); assertEquals("length after large fill", largeEnd, src.getLength());
assertNotSame("array after large fill", array, src.getPrimitiveArray()); assertNotSame("array after large fill", array, src.getPrimitiveArray());
for (int i = 0; i < largeEnd; i++) { for (int i = 0; i < largeEnd; i++) {
final int v = src.get(i); final int value = i;
if (i >= startPos && i < largeEnd) { if (i >= startPos && i < largeEnd) {
assertEquals("new values after large fill at " + i, value2, v); assertEquals("new values after large fill at " + i, fillValue2, src.get(i));
} else { } else {
assertEquals("unmodified values after large fill at " + i, i, v); assertEquals("unmodified values after large fill at " + i, value, src.get(i));
} }
} }
final int startPos2 = largeEnd + length2; final int startPos2 = largeEnd + length2;
final int endPos2 = startPos2 + length2; final int endPos2 = startPos2 + length2;
final int value3 = 789; final int fillValue3 = 789;
src.fill(value3, startPos2, length2); src.fill(fillValue3, startPos2, length2);
assertEquals("length after disjoint fill", endPos2, src.getLength()); assertEquals("length after disjoint fill", endPos2, src.getLength());
for (int i = 0; i < endPos2; i++) { for (int i = 0; i < endPos2; i++) {
final int v = src.get(i); final int value = i;
if (i >= startPos2 && i < endPos2) { if (i >= startPos2 && i < endPos2) {
assertEquals("new values after disjoint fill at " + i, value3, v); assertEquals("new values after disjoint fill at " + i, fillValue3, src.get(i));
} else if (i >= startPos && i < largeEnd) { } else if (i >= startPos && i < largeEnd) {
assertEquals("unmodified values after disjoint fill at " + i, value2, v); assertEquals("unmodified values after disjoint fill at " + i,
fillValue2, src.get(i));
} else if (i < startPos) { } else if (i < startPos) {
assertEquals("unmodified values after disjoint fill at " + i, i, v); assertEquals("unmodified values after disjoint fill at " + i, value, src.get(i));
} else { } else {
assertEquals("gap values after disjoint fill at " + i, 0, v); assertEquals("gap values after disjoint fill at " + i, 0, src.get(i));
} }
} }
} }
@ -346,12 +364,14 @@ public class ResizableIntArrayTests extends AndroidTestCase {
final int limit = DEFAULT_CAPACITY * 10; final int limit = DEFAULT_CAPACITY * 10;
final int shiftAmount = 20; final int shiftAmount = 20;
for (int i = 0; i < limit; ++i) { for (int i = 0; i < limit; ++i) {
src.add(i, i); final int value = i;
src.add(i, value);
assertEquals("length after add at " + i, i + 1, src.getLength()); assertEquals("length after add at " + i, i + 1, src.getLength());
} }
src.shift(shiftAmount); src.shift(shiftAmount);
for (int i = 0; i < limit - shiftAmount; ++i) { for (int i = 0; i < limit - shiftAmount; ++i) {
assertEquals("value at " + i, i + shiftAmount, src.get(i)); final int oldValue = i + shiftAmount;
assertEquals("value at " + i, oldValue, src.get(i));
} }
} }
} }