Fix unit test assert helper method

Change-Id: Id03bf0910708d07b45286dccf8f2bfc97a362ba0
main
Tadashi G. Takaoka 2013-05-23 02:06:56 -07:00
parent dbed20ad8d
commit 35091716e7
3 changed files with 70 additions and 50 deletions

View File

@ -55,8 +55,8 @@ public class KeySpecParserSplitTests extends InstrumentationTestCase {
return names.toArray(new String[names.size()]); return names.toArray(new String[names.size()]);
} }
private static void assertArrayEquals(final String message, final Object[] expected, private static <T> void assertArrayEquals(final String message, final T[] expected,
final Object[] actual) { final T[] actual) {
if (expected == actual) { if (expected == actual) {
return; return;
} }
@ -69,8 +69,12 @@ public class KeySpecParserSplitTests extends InstrumentationTestCase {
return; return;
} }
for (int i = 0; i < expected.length; i++) { for (int i = 0; i < expected.length; i++) {
assertEquals(message + " [" + i + "]", final T e = expected[i];
Arrays.toString(expected), Arrays.toString(actual)); final T a = actual[i];
if (e == a) {
continue;
}
assertEquals(message + " [" + i + "]", e, a);
} }
} }

View File

@ -108,13 +108,13 @@ public class InputPointersTests extends AndroidTestCase {
assertNotSame("pointerIds after copy", dst.getPointerIds(), src.getPointerIds()); assertNotSame("pointerIds after copy", dst.getPointerIds(), src.getPointerIds());
assertNotSame("times after copy", dst.getTimes(), src.getTimes()); assertNotSame("times after copy", dst.getTimes(), src.getTimes());
final int size = dst.getPointerSize(); final int size = dst.getPointerSize();
assertArrayEquals("xCoordinates values after copy", assertIntArrayEquals("xCoordinates values after copy",
dst.getXCoordinates(), 0, src.getXCoordinates(), 0, size); dst.getXCoordinates(), 0, src.getXCoordinates(), 0, size);
assertArrayEquals("yCoordinates values after copy", assertIntArrayEquals("yCoordinates values after copy",
dst.getYCoordinates(), 0, src.getYCoordinates(), 0, size); dst.getYCoordinates(), 0, src.getYCoordinates(), 0, size);
assertArrayEquals("pointerIds values after copy", assertIntArrayEquals("pointerIds values after copy",
dst.getPointerIds(), 0, src.getPointerIds(), 0, size); dst.getPointerIds(), 0, src.getPointerIds(), 0, size);
assertArrayEquals("times values after copy", assertIntArrayEquals("times values after copy",
dst.getTimes(), 0, src.getTimes(), 0, size); dst.getTimes(), 0, src.getTimes(), 0, size);
} }
@ -135,34 +135,34 @@ public class InputPointersTests extends AndroidTestCase {
dst.append(src, 0, 0); dst.append(src, 0, 0);
assertEquals("size after append zero", dstLen, dst.getPointerSize()); assertEquals("size after append zero", dstLen, dst.getPointerSize());
assertArrayEquals("xCoordinates after append zero", assertIntArrayEquals("xCoordinates after append zero",
dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen); dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen);
assertArrayEquals("yCoordinates after append zero", assertIntArrayEquals("yCoordinates after append zero",
dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen); dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen);
assertArrayEquals("pointerIds after append zero", assertIntArrayEquals("pointerIds after append zero",
dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen); dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen);
assertArrayEquals("times after append zero", assertIntArrayEquals("times after append zero",
dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen); dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen);
dst.append(src, 0, srcLen); dst.append(src, 0, srcLen);
assertEquals("size after append", dstLen + srcLen, dst.getPointerSize()); assertEquals("size after append", dstLen + srcLen, dst.getPointerSize());
assertTrue("primitive length after append", assertTrue("primitive length after append",
dst.getPointerIds().length >= dstLen + srcLen); dst.getPointerIds().length >= dstLen + srcLen);
assertArrayEquals("original xCoordinates values after append", assertIntArrayEquals("original xCoordinates values after append",
dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen); dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen);
assertArrayEquals("original yCoordinates values after append", assertIntArrayEquals("original yCoordinates values after append",
dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen); dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen);
assertArrayEquals("original pointerIds values after append", assertIntArrayEquals("original pointerIds values after append",
dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen); dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen);
assertArrayEquals("original times values after append", assertIntArrayEquals("original times values after append",
dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen); dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen);
assertArrayEquals("appended xCoordinates values after append", assertIntArrayEquals("appended xCoordinates values after append",
src.getXCoordinates(), 0, dst.getXCoordinates(), dstLen, srcLen); src.getXCoordinates(), 0, dst.getXCoordinates(), dstLen, srcLen);
assertArrayEquals("appended yCoordinates values after append", assertIntArrayEquals("appended yCoordinates values after append",
src.getYCoordinates(), 0, dst.getYCoordinates(), dstLen, srcLen); src.getYCoordinates(), 0, dst.getYCoordinates(), dstLen, srcLen);
assertArrayEquals("appended pointerIds values after append", assertIntArrayEquals("appended pointerIds values after append",
src.getPointerIds(), 0, dst.getPointerIds(), dstLen, srcLen); src.getPointerIds(), 0, dst.getPointerIds(), dstLen, srcLen);
assertArrayEquals("appended times values after append", assertIntArrayEquals("appended times values after append",
src.getTimes(), 0, dst.getTimes(), dstLen, srcLen); src.getTimes(), 0, dst.getTimes(), dstLen, srcLen);
} }
@ -190,47 +190,55 @@ public class InputPointersTests extends AndroidTestCase {
dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, 0, 0); dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, 0, 0);
assertEquals("size after append zero", dstLen, dst.getPointerSize()); assertEquals("size after append zero", dstLen, dst.getPointerSize());
assertArrayEquals("xCoordinates after append zero", assertIntArrayEquals("xCoordinates after append zero",
dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen); dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen);
assertArrayEquals("yCoordinates after append zero", assertIntArrayEquals("yCoordinates after append zero",
dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen); dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen);
assertArrayEquals("pointerIds after append zero", assertIntArrayEquals("pointerIds after append zero",
dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen); dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen);
assertArrayEquals("times after append zero", assertIntArrayEquals("times after append zero",
dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen); dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen);
dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, 0, srcLen); dst.append(srcPointerId, srcTimes, srcXCoords, srcYCoords, 0, srcLen);
assertEquals("size after append", dstLen + srcLen, dst.getPointerSize()); assertEquals("size after append", dstLen + srcLen, dst.getPointerSize());
assertTrue("primitive length after append", assertTrue("primitive length after append",
dst.getPointerIds().length >= dstLen + srcLen); dst.getPointerIds().length >= dstLen + srcLen);
assertArrayEquals("original xCoordinates values after append", assertIntArrayEquals("original xCoordinates values after append",
dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen); dstCopy.getXCoordinates(), 0, dst.getXCoordinates(), 0, dstLen);
assertArrayEquals("original yCoordinates values after append", assertIntArrayEquals("original yCoordinates values after append",
dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen); dstCopy.getYCoordinates(), 0, dst.getYCoordinates(), 0, dstLen);
assertArrayEquals("original pointerIds values after append", assertIntArrayEquals("original pointerIds values after append",
dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen); dstCopy.getPointerIds(), 0, dst.getPointerIds(), 0, dstLen);
assertArrayEquals("original times values after append", assertIntArrayEquals("original times values after append",
dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen); dstCopy.getTimes(), 0, dst.getTimes(), 0, dstLen);
assertArrayEquals("appended xCoordinates values after append", assertIntArrayEquals("appended xCoordinates values after append",
srcXCoords.getPrimitiveArray(), 0, dst.getXCoordinates(), dstLen, srcLen); srcXCoords.getPrimitiveArray(), 0, dst.getXCoordinates(), dstLen, srcLen);
assertArrayEquals("appended yCoordinates values after append", assertIntArrayEquals("appended yCoordinates values after append",
srcYCoords.getPrimitiveArray(), 0, dst.getYCoordinates(), dstLen, srcLen); srcYCoords.getPrimitiveArray(), 0, dst.getYCoordinates(), dstLen, srcLen);
assertArrayEquals("appended pointerIds values after append", assertIntArrayEquals("appended pointerIds values after append",
srcPointerIds, 0, dst.getPointerIds(), dstLen, srcLen); srcPointerIds, 0, dst.getPointerIds(), dstLen, srcLen);
assertArrayEquals("appended times values after append", assertIntArrayEquals("appended times values after append",
srcTimes.getPrimitiveArray(), 0, dst.getTimes(), dstLen, srcLen); srcTimes.getPrimitiveArray(), 0, dst.getTimes(), dstLen, srcLen);
} }
private static void assertArrayEquals(String message, int[] expecteds, int expectedPos, // TODO: Consolidate this method with
int[] actuals, int actualPos, int length) { // {@link ResizableIntArrayTests#assertIntArrayEquals(String,int[],int,int[],int,int)}.
if (expecteds == null && actuals == null) { 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; return;
} }
if (expecteds == null || actuals == null) { if (expecteds == null || actuals == null) {
fail(message + ": expecteds=" + expecteds + " actuals=" + actuals); 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++) { for (int i = 0; i < length; i++) {
assertEquals(message + ": element at " + i, assertEquals(message + " [" + i + "]",
expecteds[i + expectedPos], actuals[i + actualPos]); expecteds[i + expectedPos], actuals[i + actualPos]);
} }
} }

View File

@ -19,6 +19,8 @@ package com.android.inputmethod.latin;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest; import android.test.suitebuilder.annotation.SmallTest;
import java.util.Arrays;
@SmallTest @SmallTest
public class ResizableIntArrayTests extends AndroidTestCase { public class ResizableIntArrayTests extends AndroidTestCase {
private static final int DEFAULT_CAPACITY = 48; private static final int DEFAULT_CAPACITY = 48;
@ -186,7 +188,7 @@ public class ResizableIntArrayTests extends AndroidTestCase {
assertEquals("length after copy", dst.getLength(), src.getLength()); assertEquals("length after copy", dst.getLength(), src.getLength());
assertSame("array after copy", array, dst.getPrimitiveArray()); assertSame("array after copy", array, dst.getPrimitiveArray());
assertNotSame("array after copy", dst.getPrimitiveArray(), src.getPrimitiveArray()); assertNotSame("array after copy", dst.getPrimitiveArray(), src.getPrimitiveArray());
assertArrayEquals("values after copy", assertIntArrayEquals("values after copy",
dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength()); dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength());
final int smallerLength = DEFAULT_CAPACITY / 2; final int smallerLength = DEFAULT_CAPACITY / 2;
@ -197,7 +199,7 @@ public class ResizableIntArrayTests extends AndroidTestCase {
assertEquals("length after copy to smaller", dst.getLength(), src.getLength()); assertEquals("length after copy to smaller", dst.getLength(), src.getLength());
assertNotSame("array after copy to smaller", array2, array3); assertNotSame("array after copy to smaller", array2, array3);
assertNotSame("array after copy to smaller", array3, src.getPrimitiveArray()); assertNotSame("array after copy to smaller", array3, src.getPrimitiveArray());
assertArrayEquals("values after copy to smaller", assertIntArrayEquals("values after copy to smaller",
dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength()); dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength());
} }
@ -220,7 +222,7 @@ 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()); assertSame("array after append zero", array, dst.getPrimitiveArray());
assertArrayEquals("values after append zero", assertIntArrayEquals("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);
@ -228,9 +230,9 @@ public class ResizableIntArrayTests extends AndroidTestCase {
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 >= dstLen + srcLen);
assertArrayEquals("original values after append", assertIntArrayEquals("original values after append",
dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen); dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen);
assertArrayEquals("appended values after append", assertIntArrayEquals("appended values after append",
src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen, srcLen); src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen, srcLen);
dst.append(src, 0, srcLen); dst.append(src, 0, srcLen);
@ -238,11 +240,11 @@ public class ResizableIntArrayTests extends AndroidTestCase {
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 >= dstLen + srcLen * 2);
assertArrayEquals("original values after 2nd append", assertIntArrayEquals("original values after 2nd append",
dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen); dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen);
assertArrayEquals("appended values after 2nd append", assertIntArrayEquals("appended values after 2nd append",
src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen, srcLen); src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen, srcLen);
assertArrayEquals("appended values after 2nd append", assertIntArrayEquals("appended values after 2nd append",
src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen + srcLen, srcLen); src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen + srcLen, srcLen);
} }
@ -319,16 +321,22 @@ public class ResizableIntArrayTests extends AndroidTestCase {
} }
} }
private static void assertArrayEquals(String message, int[] expecteds, int expectedPos, private static void assertIntArrayEquals(final String message, final int[] expecteds,
int[] actuals, int actualPos, int length) { final int expectedPos, final int[] actuals, final int actualPos, final int length) {
if (expecteds == null && actuals == null) { if (expecteds == actuals) {
return; return;
} }
if (expecteds == null || actuals == null) { if (expecteds == null || actuals == null) {
fail(message + ": expecteds=" + expecteds + " actuals=" + actuals); 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++) { for (int i = 0; i < length; i++) {
assertEquals(message + ": element at " + i, assertEquals(message + " [" + i + "]",
expecteds[i + expectedPos], actuals[i + actualPos]); expecteds[i + expectedPos], actuals[i + actualPos]);
} }
} }