Add args to dicttool test.

Change-Id: I0667e0a5a6f6db3964cfcca5c8f083b9ceb41a2e
main
Jean Chalard 2013-06-28 19:58:34 +09:00
parent 8142a7b637
commit 4b7acd1df6
2 changed files with 66 additions and 16 deletions

View File

@ -44,9 +44,10 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
private static final String TAG = BinaryDictIOUtilsTests.class.getSimpleName();
private static final FormatSpec.FormatOptions FORMAT_OPTIONS =
new FormatSpec.FormatOptions(3, true);
private static final int MAX_UNIGRAMS = 1500;
private static final ArrayList<String> sWords = CollectionUtils.newArrayList();
public static final int DEFAULT_MAX_UNIGRAMS = 1500;
private final int mMaxUnigrams;
private static final String[] CHARACTERS = {
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
@ -57,15 +58,17 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
};
public BinaryDictIOUtilsTests() {
this(System.currentTimeMillis());
// 1500 is the default max unigrams
this(System.currentTimeMillis(), DEFAULT_MAX_UNIGRAMS);
}
public BinaryDictIOUtilsTests(final long seed) {
public BinaryDictIOUtilsTests(final long seed, final int maxUnigrams) {
super();
Log.d(TAG, "Seed for test is " + seed);
Log.d(TAG, "Seed for test is " + seed + ", maxUnigrams is " + maxUnigrams);
mMaxUnigrams = maxUnigrams;
final Random random = new Random(seed);
sWords.clear();
for (int i = 0; i < MAX_UNIGRAMS; ++i) {
for (int i = 0; i < maxUnigrams; ++i) {
sWords.add(generateWord(random.nextInt()));
}
}
@ -395,6 +398,6 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
Log.d(TAG, "max = " + ((double)maxTimeToInsert/1000000) + " ms.");
Log.d(TAG, "min = " + ((double)minTimeToInsert/1000000) + " ms.");
Log.d(TAG, "avg = " + ((double)sum/MAX_UNIGRAMS/1000000) + " ms.");
Log.d(TAG, "avg = " + ((double)sum/mMaxUnigrams/1000000) + " ms.");
}
}

View File

@ -19,33 +19,80 @@ package com.android.inputmethod.latin.dicttool;
import com.android.inputmethod.latin.makedict.BinaryDictIOUtilsTests;
import com.android.inputmethod.latin.makedict.BinaryDictInputOutputTest;
import com.android.inputmethod.latin.makedict.FusionDictionaryTest;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
/**
* Dicttool command implementing self-tests.
*/
public class Test extends Dicttool.Command {
public static final String COMMAND = "test";
private long mSeed = System.currentTimeMillis();
private int mMaxUnigrams = BinaryDictIOUtilsTests.DEFAULT_MAX_UNIGRAMS;
private static final Class<?>[] sClassesToTest = {
BinaryDictOffdeviceUtilsTests.class,
FusionDictionaryTest.class,
BinaryDictInputOutputTest.class,
BinaryDictIOUtilsTests.class
};
private ArrayList<Method> mAllTestMethods = new ArrayList<Method>();
private ArrayList<String> mUsedTestMethods = new ArrayList<String>();
public Test() {
for (final Class<?> c : sClassesToTest) {
for (final Method m : c.getDeclaredMethods()) {
if (m.getName().startsWith("test") && Void.TYPE == m.getReturnType()
&& 0 == m.getParameterTypes().length) {
mAllTestMethods.add(m);
}
}
}
}
@Override
public String getHelp() {
return "test";
final StringBuilder s = new StringBuilder("test [-s seed] [-m maxUnigrams] [testName...]\n"
+ "If seed is not specified, the current time is used.\nTest list is:\n");
for (final Method m : mAllTestMethods) {
s.append(" ");
s.append(m.getName());
s.append("\n");
}
return s.toString();
}
@Override
public void run() throws IOException, UnsupportedFormatException {
test();
public void run() throws IllegalAccessException, InstantiationException,
InvocationTargetException {
int i = 0;
while (i < mArgs.length) {
final String arg = mArgs[i++];
if ("-s".equals(arg)) {
mSeed = Long.parseLong(mArgs[i++]);
} else if ("-m".equals(arg)) {
mMaxUnigrams = Integer.parseInt(mArgs[i++]);
} else {
mUsedTestMethods.add(arg);
}
}
runChosenTests();
}
private void test() throws IOException, UnsupportedFormatException {
new BinaryDictOffdeviceUtilsTests().testGetRawDictWorks();
new FusionDictionaryTest().testFusion();
new BinaryDictInputOutputTest().testFlattenNodes();
new BinaryDictIOUtilsTests().testRandomWords();
private void runChosenTests() throws IllegalAccessException, InstantiationException,
InvocationTargetException {
for (final Method m : mAllTestMethods) {
final Class<?> declaringClass = m.getDeclaringClass();
if (!mUsedTestMethods.isEmpty() && !mUsedTestMethods.contains(m.getName())) continue;
final Object instance;
if (BinaryDictIOUtilsTests.class == declaringClass) {
instance = new BinaryDictIOUtilsTests(mSeed, mMaxUnigrams);
} else {
instance = declaringClass.newInstance();
}
m.invoke(instance);
}
}
}