From 4b7acd1df6610fbe24eab04b72763de152f24176 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Fri, 28 Jun 2013 19:58:34 +0900 Subject: [PATCH] Add args to dicttool test. Change-Id: I0667e0a5a6f6db3964cfcca5c8f083b9ceb41a2e --- .../makedict/BinaryDictIOUtilsTests.java | 15 +++-- .../inputmethod/latin/dicttool/Test.java | 67 ++++++++++++++++--- 2 files changed, 66 insertions(+), 16 deletions(-) diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java index d33142ce0..9331da44b 100644 --- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java +++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java @@ -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 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."); } } diff --git a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Test.java b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Test.java index 827c5e3a9..df5ea35bb 100644 --- a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Test.java +++ b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Test.java @@ -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 mAllTestMethods = new ArrayList(); + private ArrayList mUsedTestMethods = new ArrayList(); 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); + } } }