Merge "Add tests to dicttool test."

main
Jean Chalard 2013-06-20 08:34:37 +00:00 committed by Android (Google) Code Review
commit 93f74bafbb
13 changed files with 252 additions and 73 deletions

View File

@ -0,0 +1,81 @@
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.inputmethod.latin;
import com.android.inputmethod.latin.makedict.BinaryDictInputOutput.FusionDictionaryBufferInterface;
/**
* This class provides an implementation for the FusionDictionary buffer interface that is backed
* by a simpled byte array. It allows to create a binary dictionary in memory.
*/
public final class ByteArrayWrapper implements FusionDictionaryBufferInterface {
private byte[] mBuffer;
private int mPosition;
public ByteArrayWrapper(final byte[] buffer) {
mBuffer = buffer;
mPosition = 0;
}
@Override
public int readUnsignedByte() {
return mBuffer[mPosition++] & 0xFF;
}
@Override
public int readUnsignedShort() {
final int retval = readUnsignedByte();
return (retval << 8) + readUnsignedByte();
}
@Override
public int readUnsignedInt24() {
final int retval = readUnsignedShort();
return (retval << 8) + readUnsignedByte();
}
@Override
public int readInt() {
final int retval = readUnsignedShort();
return (retval << 16) + readUnsignedShort();
}
@Override
public int position() {
return mPosition;
}
@Override
public void position(int position) {
mPosition = position;
}
@Override
public void put(final byte b) {
mBuffer[mPosition++] = b;
}
@Override
public int limit() {
return mBuffer.length - 1;
}
@Override
public int capacity() {
return mBuffer.length;
}
}

View File

@ -53,64 +53,6 @@ public final class UserHistoryDictIOUtils {
public int getFrequency(final String word1, final String word2);
}
public static final class ByteArrayWrapper implements FusionDictionaryBufferInterface {
private byte[] mBuffer;
private int mPosition;
public ByteArrayWrapper(final byte[] buffer) {
mBuffer = buffer;
mPosition = 0;
}
@Override
public int readUnsignedByte() {
return mBuffer[mPosition++] & 0xFF;
}
@Override
public int readUnsignedShort() {
final int retval = readUnsignedByte();
return (retval << 8) + readUnsignedByte();
}
@Override
public int readUnsignedInt24() {
final int retval = readUnsignedShort();
return (retval << 8) + readUnsignedByte();
}
@Override
public int readInt() {
final int retval = readUnsignedShort();
return (retval << 16) + readUnsignedShort();
}
@Override
public int position() {
return mPosition;
}
@Override
public void position(int position) {
mPosition = position;
}
@Override
public void put(final byte b) {
mBuffer[mPosition++] = b;
}
@Override
public int limit() {
return mBuffer.length - 1;
}
@Override
public int capacity() {
return mBuffer.length;
}
}
/**
* Writes dictionary to file.
*/

View File

@ -261,7 +261,7 @@ public final class UserHistoryDictionary extends ExpandableDictionary {
inStream = new FileInputStream(file);
inStream.read(buffer);
UserHistoryDictIOUtils.readDictionaryBinary(
new UserHistoryDictIOUtils.ByteArrayWrapper(buffer), listener);
new ByteArrayWrapper(buffer), listener);
} catch (FileNotFoundException e) {
// This is an expected condition: we don't have a user history dictionary for this
// language yet. It will be created sometime later.

View File

@ -153,7 +153,7 @@ public class UserHistoryDictIOUtilsTests extends AndroidTestCase
inStream.read(buffer);
UserHistoryDictIOUtils.readDictionaryBinary(
new UserHistoryDictIOUtils.ByteArrayWrapper(buffer), listener);
new ByteArrayWrapper(buffer), listener);
} catch (FileNotFoundException e) {
Log.e(TAG, "file not found", e);
} catch (IOException e) {

View File

@ -22,8 +22,8 @@ import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;
import android.util.SparseArray;
import com.android.inputmethod.latin.ByteArrayWrapper;
import com.android.inputmethod.latin.CollectionUtils;
import com.android.inputmethod.latin.UserHistoryDictIOUtils;
import com.android.inputmethod.latin.makedict.BinaryDictInputOutput.FusionDictionaryBufferInterface;
import com.android.inputmethod.latin.makedict.FormatSpec.FileHeader;
import com.android.inputmethod.latin.makedict.FusionDictionary.CharGroup;
@ -106,7 +106,7 @@ public class BinaryDictIOTests extends AndroidTestCase {
if (bufferType == USE_BYTE_ARRAY) {
final byte[] array = new byte[(int)file.length()];
inStream.read(array);
return new UserHistoryDictIOUtils.ByteArrayWrapper(array);
return new ByteArrayWrapper(array);
} else if (bufferType == USE_BYTE_BUFFER){
final ByteBuffer buffer = inStream.getChannel().map(
FileChannel.MapMode.READ_ONLY, 0, file.length());

View File

@ -53,12 +53,17 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"\u00FC" /* ü */, "\u00E2" /* â */, "\u00F1" /* ñ */, // accented characters
"\u4E9C" /* 亜 */, "\u4F0A" /* 伊 */, "\u5B87" /* 宇 */, // kanji
"\uD841\uDE28" /* 𠘨 */, "\uD840\uDC0B" /* 𠀋 */, "\uD861\uDeD7" /* 𨛗 */ // surrogate pair
"\uD841\uDE28" /* 𠘨 */, "\uD840\uDC0B" /* 𠀋 */, "\uD861\uDED7" /* 𨛗 */ // surrogate pair
};
public BinaryDictIOUtilsTests() {
this(System.currentTimeMillis());
}
public BinaryDictIOUtilsTests(final long seed) {
super();
final Random random = new Random(123456);
Log.d(TAG, "Seed for test is " + seed);
final Random random = new Random(seed);
sWords.clear();
for (int i = 0; i < MAX_UNIGRAMS; ++i) {
sWords.add(generateWord(random.nextInt()));

View File

@ -16,10 +16,22 @@
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LATINIME_BASE_SOURCE_DIRECTORY := ../../java/src/com/android/inputmethod
BUILD_TOP := ../../../../..
FRAMEWORK_TOP := $(BUILD_TOP)/frameworks/base/core/java
LATINIME_DIR := $(BUILD_TOP)/packages/inputmethods/LatinIME
LATINIME_BASE_SOURCE_DIRECTORY := $(LATINIME_DIR)/java/src/com/android/inputmethod
LATINIME_CORE_SOURCE_DIRECTORY := $(LATINIME_BASE_SOURCE_DIRECTORY)/latin
LATINIME_ANNOTATIONS_SOURCE_DIRECTORY := $(LATINIME_BASE_SOURCE_DIRECTORY)/annotations
MAKEDICT_CORE_SOURCE_DIRECTORY := $(LATINIME_CORE_SOURCE_DIRECTORY)/makedict
DICTTOOL_COMPAT_TESTS_DIRECTORY := compat
DICTTOOL_ONDEVICE_TESTS_DIRECTORY := \
$(LATINIME_DIR)/tests/src/com/android/inputmethod/latin/makedict/
USED_TARGETTED_UTILS := \
$(FRAMEWORK_TOP)/android/util/SparseArray.java \
$(FRAMEWORK_TOP)/com/android/internal/util/ArrayUtils.java \
$(LATINIME_CORE_SOURCE_DIRECTORY)/CollectionUtils.java \
$(LATINIME_CORE_SOURCE_DIRECTORY)/ByteArrayWrapper.java
LOCAL_MAIN_SRC_FILES := $(call all-java-files-under, $(MAKEDICT_CORE_SOURCE_DIRECTORY))
LOCAL_TOOL_SRC_FILES := $(call all-java-files-under, src)
@ -29,7 +41,10 @@ LOCAL_SRC_FILES := $(LOCAL_TOOL_SRC_FILES) \
$(filter-out $(addprefix %/, $(notdir $(LOCAL_TOOL_SRC_FILES))), $(LOCAL_MAIN_SRC_FILES)) \
$(LOCAL_ANNOTATIONS_SRC_FILES) \
$(LATINIME_CORE_SOURCE_DIRECTORY)/Constants.java \
$(call all-java-files-under, tests)
$(call all-java-files-under, tests) \
$(call all-java-files-under, $(DICTTOOL_ONDEVICE_TESTS_DIRECTORY)) \
$(call all-java-files-under, $(DICTTOOL_COMPAT_TESTS_DIRECTORY)) \
$(USED_TARGETTED_UTILS)
LOCAL_JAVA_LIBRARIES := junit

View File

@ -0,0 +1,35 @@
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.test;
import junit.framework.TestCase;
import java.io.File;
/**
* This is a compatibility class that aims at emulating android.test.AndroidTestcase from the
* Android library as simply as possible, and only to the extent that is used by the client classes.
* Its purpose is to provide compatibility without having to pull the whole Android library.
*/
public class AndroidTestCase extends TestCase {
public File getCacheDir() {
return new File(".");
}
public AndroidTestCase getContext() {
return this;
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.test;
import junit.framework.Assert;
/**
* This is a compatibility class that aims at emulating android.test.MoreAsserts from the
* Android library as simply as possible, and only to the extent that is used by the client classes.
* Its purpose is to provide compatibility without having to pull the whole Android library.
*/
public class MoreAsserts {
public static void assertNotEqual(Object unexpected, Object actual) {
if (equal(unexpected, actual)) {
Assert.fail("expected not to be:<" + unexpected + ">");
}
}
private static boolean equal(Object a, Object b) {
return a == b || (a != null && a.equals(b));
}
}

View File

@ -0,0 +1,25 @@
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.test.suitebuilder.annotation;
/**
* This is a compatibility class that aims at emulating the LargeTest annotation from the
* Android library as simply as possible, and only to the extent that is used by the client classes.
* Its purpose is to provide compatibility without having to pull the whole Android library.
*/
public @interface LargeTest {
}

View File

@ -0,0 +1,37 @@
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.util;
/**
* This is a compatibility class that aims at emulating android.util.Log from the
* Android library as simply as possible, and only to the extent that is used by the client classes.
* Its purpose is to provide compatibility without having to pull the whole Android library.
*/
public class Log {
public static void d(final String tag, final String message) {
System.out.println(tag + " : " + message);
}
public static void d(final String tag, final String message, final Exception e) {
System.out.println(tag + " : " + message + " : " + e);
}
public static void e(final String tag, final String message) {
d(tag, message);
}
public static void e(final String tag, final String message, final Exception e) {
e(tag, message, e);
}
}

View File

@ -16,10 +16,16 @@
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;
/**
* Dicttool command implementing self-tests.
*/
public class Test extends Dicttool.Command {
public static final String COMMAND = "test";
@ -37,7 +43,9 @@ public class Test extends Dicttool.Command {
}
private void test() throws IOException, UnsupportedFormatException {
final BinaryDictOffdeviceUtilsTests tests = new BinaryDictOffdeviceUtilsTests();
tests.testGetRawDictWorks();
new BinaryDictOffdeviceUtilsTests().testGetRawDictWorks();
new FusionDictionaryTest().testFusion();
new BinaryDictInputOutputTest().testFlattenNodes();
new BinaryDictIOUtilsTests().testRandomWords();
}
}

View File

@ -73,10 +73,6 @@ public class FusionDictionaryTest extends TestCase {
for (final String word : words) {
if (--limit < 0) return;
final CharGroup cg = FusionDictionary.findWordInTree(dict.mRoot, word);
if (null == cg) {
System.out.println("word " + dumpWord(word));
dumpDict(dict);
}
assertNotNull(cg);
}
}