am 618158e6: Merge "Refactor BinaryDictIOUtilsTests."
* commit '618158e675c27854b0ab2cb53ec2eb2837a593c0': Refactor BinaryDictIOUtilsTests.main
commit
d5c90d9d8b
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package com.android.inputmethod.latin.makedict;
|
package com.android.inputmethod.latin.makedict;
|
||||||
|
|
||||||
|
import com.android.inputmethod.annotations.UsedForTesting;
|
||||||
import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
|
import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -24,6 +25,7 @@ import java.util.ArrayList;
|
||||||
/**
|
/**
|
||||||
* An interface of a binary dictionary updater.
|
* An interface of a binary dictionary updater.
|
||||||
*/
|
*/
|
||||||
|
@UsedForTesting
|
||||||
public interface DictUpdater extends DictDecoder {
|
public interface DictUpdater extends DictDecoder {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -31,6 +33,7 @@ public interface DictUpdater extends DictDecoder {
|
||||||
*
|
*
|
||||||
* @param word the word to be deleted.
|
* @param word the word to be deleted.
|
||||||
*/
|
*/
|
||||||
|
@UsedForTesting
|
||||||
public void deleteWord(final String word) throws IOException, UnsupportedFormatException;
|
public void deleteWord(final String word) throws IOException, UnsupportedFormatException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -43,6 +46,7 @@ public interface DictUpdater extends DictDecoder {
|
||||||
* @param isBlackListEntry whether this should be a blacklist entry.
|
* @param isBlackListEntry whether this should be a blacklist entry.
|
||||||
*/
|
*/
|
||||||
// TODO: Support batch insertion.
|
// TODO: Support batch insertion.
|
||||||
|
@UsedForTesting
|
||||||
public void insertWord(final String word, final int frequency,
|
public void insertWord(final String word, final int frequency,
|
||||||
final ArrayList<WeightedString> bigramStrings,
|
final ArrayList<WeightedString> bigramStrings,
|
||||||
final ArrayList<WeightedString> shortcuts, final boolean isNotAWord,
|
final ArrayList<WeightedString> shortcuts, final boolean isNotAWord,
|
||||||
|
|
|
@ -646,7 +646,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDeleteWord() {
|
private void runTestDeleteWord(final FormatOptions formatOptions) {
|
||||||
final String dictName = "testDeleteWord";
|
final String dictName = "testDeleteWord";
|
||||||
final String dictVersion = Long.toString(System.currentTimeMillis());
|
final String dictVersion = Long.toString(System.currentTimeMillis());
|
||||||
final File file = setUpDictionaryFile(dictName, dictVersion);
|
final File file = setUpDictionaryFile(dictName, dictVersion);
|
||||||
|
@ -655,10 +655,15 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
|
||||||
new FusionDictionary.DictionaryOptions(
|
new FusionDictionary.DictionaryOptions(
|
||||||
new HashMap<String, String>(), false, false));
|
new HashMap<String, String>(), false, false));
|
||||||
addUnigrams(sWords.size(), dict, sWords, null /* shortcutMap */);
|
addUnigrams(sWords.size(), dict, sWords, null /* shortcutMap */);
|
||||||
timeWritingDictToFile(file, dict, VERSION3_WITH_DYNAMIC_UPDATE);
|
timeWritingDictToFile(file, dict, formatOptions);
|
||||||
|
|
||||||
final Ver3DictUpdater dictUpdater = new Ver3DictUpdater(file,
|
final DictUpdater dictUpdater;
|
||||||
DictDecoder.USE_WRITABLE_BYTEBUFFER);
|
if (formatOptions.mVersion == 3) {
|
||||||
|
dictUpdater = new Ver3DictUpdater(file, DictDecoder.USE_WRITABLE_BYTEBUFFER);
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("DictUpdater for version " + formatOptions.mVersion
|
||||||
|
+ " doesn't exist.");
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD,
|
MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD,
|
||||||
|
@ -676,4 +681,8 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
|
||||||
} catch (UnsupportedFormatException e) {
|
} catch (UnsupportedFormatException e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testDeleteWord() {
|
||||||
|
runTestDeleteWord(VERSION3_WITH_DYNAMIC_UPDATE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,6 +45,8 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
|
||||||
|
|
||||||
private static final String TEST_DICT_FILE_EXTENSION = ".testDict";
|
private static final String TEST_DICT_FILE_EXTENSION = ".testDict";
|
||||||
|
|
||||||
|
private static final int VERSION3 = 3;
|
||||||
|
|
||||||
private static final String[] CHARACTERS = {
|
private static final String[] CHARACTERS = {
|
||||||
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
|
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
|
||||||
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
|
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
|
||||||
|
@ -183,11 +185,16 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
|
||||||
// return amount of time to insert a word
|
// return amount of time to insert a word
|
||||||
private long insertAndCheckWord(final File file, final String word, final int frequency,
|
private long insertAndCheckWord(final File file, final String word, final int frequency,
|
||||||
final boolean exist, final ArrayList<WeightedString> bigrams,
|
final boolean exist, final ArrayList<WeightedString> bigrams,
|
||||||
final ArrayList<WeightedString> shortcuts) {
|
final ArrayList<WeightedString> shortcuts, final int formatVersion) {
|
||||||
long amountOfTime = -1;
|
long amountOfTime = -1;
|
||||||
try {
|
try {
|
||||||
final Ver3DictUpdater dictUpdater = new Ver3DictUpdater(file,
|
final DictUpdater dictUpdater;
|
||||||
DictDecoder.USE_WRITABLE_BYTEBUFFER);
|
if (formatVersion == VERSION3) {
|
||||||
|
dictUpdater = new Ver3DictUpdater(file, DictDecoder.USE_WRITABLE_BYTEBUFFER);
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("DictUpdater for version " + formatVersion + " doesn't"
|
||||||
|
+ " exist.");
|
||||||
|
}
|
||||||
|
|
||||||
if (!exist) {
|
if (!exist) {
|
||||||
assertEquals(FormatSpec.NOT_VALID_WORD, getWordPosition(file, word));
|
assertEquals(FormatSpec.NOT_VALID_WORD, getWordPosition(file, word));
|
||||||
|
@ -204,10 +211,15 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
|
||||||
return amountOfTime;
|
return amountOfTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deleteWord(final File file, final String word) {
|
private void deleteWord(final File file, final String word, final int formatVersion) {
|
||||||
try {
|
try {
|
||||||
final Ver3DictUpdater dictUpdater = new Ver3DictUpdater(file,
|
final DictUpdater dictUpdater;
|
||||||
DictDecoder.USE_WRITABLE_BYTEBUFFER);
|
if (formatVersion == VERSION3) {
|
||||||
|
dictUpdater = new Ver3DictUpdater(file, DictDecoder.USE_WRITABLE_BYTEBUFFER);
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("DictUpdater for version " + formatVersion + " doesn't"
|
||||||
|
+ " exist.");
|
||||||
|
}
|
||||||
dictUpdater.deleteWord(word);
|
dictUpdater.deleteWord(word);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
} catch (UnsupportedFormatException e) {
|
} catch (UnsupportedFormatException e) {
|
||||||
|
@ -229,7 +241,7 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testInsertWord() {
|
private void runTestInsertWord(final int formatVersion) {
|
||||||
File file = null;
|
File file = null;
|
||||||
try {
|
try {
|
||||||
file = File.createTempFile("testInsertWord", TEST_DICT_FILE_EXTENSION,
|
file = File.createTempFile("testInsertWord", TEST_DICT_FILE_EXTENSION,
|
||||||
|
@ -253,33 +265,37 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD, getWordPosition(file, "abcd"));
|
MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD, getWordPosition(file, "abcd"));
|
||||||
insertAndCheckWord(file, "abcde", 10, false, null, null);
|
insertAndCheckWord(file, "abcde", 10, false, null, null, formatVersion);
|
||||||
|
|
||||||
insertAndCheckWord(file, "abcdefghijklmn", 10, false, null, null);
|
insertAndCheckWord(file, "abcdefghijklmn", 10, false, null, null, formatVersion);
|
||||||
checkReverseLookup(file, "abcdefghijklmn", getWordPosition(file, "abcdefghijklmn"));
|
checkReverseLookup(file, "abcdefghijklmn", getWordPosition(file, "abcdefghijklmn"));
|
||||||
|
|
||||||
insertAndCheckWord(file, "abcdabcd", 10, false, null, null);
|
insertAndCheckWord(file, "abcdabcd", 10, false, null, null, formatVersion);
|
||||||
checkReverseLookup(file, "abcdabcd", getWordPosition(file, "abcdabcd"));
|
checkReverseLookup(file, "abcdabcd", getWordPosition(file, "abcdabcd"));
|
||||||
|
|
||||||
// update the existing word.
|
// update the existing word.
|
||||||
insertAndCheckWord(file, "abcdabcd", 15, true, null, null);
|
insertAndCheckWord(file, "abcdabcd", 15, true, null, null, formatVersion);
|
||||||
|
|
||||||
// split 1
|
// split 1
|
||||||
insertAndCheckWord(file, "ab", 20, false, null, null);
|
insertAndCheckWord(file, "ab", 20, false, null, null, formatVersion);
|
||||||
|
|
||||||
// split 2
|
// split 2
|
||||||
insertAndCheckWord(file, "ami", 30, false, null, null);
|
insertAndCheckWord(file, "ami", 30, false, null, null, formatVersion);
|
||||||
|
|
||||||
deleteWord(file, "ami");
|
deleteWord(file, "ami", formatVersion);
|
||||||
assertEquals(FormatSpec.NOT_VALID_WORD, getWordPosition(file, "ami"));
|
assertEquals(FormatSpec.NOT_VALID_WORD, getWordPosition(file, "ami"));
|
||||||
|
|
||||||
insertAndCheckWord(file, "abcdabfg", 30, false, null, null);
|
insertAndCheckWord(file, "abcdabfg", 30, false, null, null, formatVersion);
|
||||||
|
|
||||||
deleteWord(file, "abcd");
|
deleteWord(file, "abcd", formatVersion);
|
||||||
assertEquals(FormatSpec.NOT_VALID_WORD, getWordPosition(file, "abcd"));
|
assertEquals(FormatSpec.NOT_VALID_WORD, getWordPosition(file, "abcd"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testInsertWordWithBigrams() {
|
public void testInsertWord() {
|
||||||
|
runTestInsertWord(VERSION3);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void runTestInsertWordWithBigrams(final int formatVersion) {
|
||||||
File file = null;
|
File file = null;
|
||||||
try {
|
try {
|
||||||
file = File.createTempFile("testInsertWordWithBigrams", TEST_DICT_FILE_EXTENSION,
|
file = File.createTempFile("testInsertWordWithBigrams", TEST_DICT_FILE_EXTENSION,
|
||||||
|
@ -306,8 +322,8 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
|
||||||
final ArrayList<WeightedString> banana = new ArrayList<WeightedString>();
|
final ArrayList<WeightedString> banana = new ArrayList<WeightedString>();
|
||||||
banana.add(new WeightedString("banana", 10));
|
banana.add(new WeightedString("banana", 10));
|
||||||
|
|
||||||
insertAndCheckWord(file, "banana", 0, false, null, null);
|
insertAndCheckWord(file, "banana", 0, false, null, null, formatVersion);
|
||||||
insertAndCheckWord(file, "recursive", 60, true, banana, null);
|
insertAndCheckWord(file, "recursive", 60, true, banana, null, formatVersion);
|
||||||
|
|
||||||
final PtNodeInfo info = findWordFromFile(file, "recursive");
|
final PtNodeInfo info = findWordFromFile(file, "recursive");
|
||||||
int bananaPos = getWordPosition(file, "banana");
|
int bananaPos = getWordPosition(file, "banana");
|
||||||
|
@ -316,7 +332,11 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
|
||||||
assertEquals(info.mBigrams.get(0).mAddress, bananaPos);
|
assertEquals(info.mBigrams.get(0).mAddress, bananaPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testRandomWords() {
|
public void testInsertWordWithBigrams() {
|
||||||
|
runTestInsertWordWithBigrams(VERSION3);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void runTestRandomWords(final int formatVersion) {
|
||||||
File file = null;
|
File file = null;
|
||||||
try {
|
try {
|
||||||
file = File.createTempFile("testRandomWord", TEST_DICT_FILE_EXTENSION,
|
file = File.createTempFile("testRandomWord", TEST_DICT_FILE_EXTENSION,
|
||||||
|
@ -345,7 +365,7 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
for (final String word : sWords) {
|
for (final String word : sWords) {
|
||||||
final long diff = insertAndCheckWord(file, word,
|
final long diff = insertAndCheckWord(file, word,
|
||||||
cnt % FormatSpec.MAX_TERMINAL_FREQUENCY, false, null, null);
|
cnt % FormatSpec.MAX_TERMINAL_FREQUENCY, false, null, null, formatVersion);
|
||||||
maxTimeToInsert = Math.max(maxTimeToInsert, diff);
|
maxTimeToInsert = Math.max(maxTimeToInsert, diff);
|
||||||
minTimeToInsert = Math.min(minTimeToInsert, diff);
|
minTimeToInsert = Math.min(minTimeToInsert, diff);
|
||||||
sum += diff;
|
sum += diff;
|
||||||
|
@ -356,8 +376,13 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
|
||||||
MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD, getWordPosition(file, word));
|
MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD, getWordPosition(file, word));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Log.d(TAG, "Test version " + formatVersion);
|
||||||
Log.d(TAG, "max = " + ((double)maxTimeToInsert/1000000) + " ms.");
|
Log.d(TAG, "max = " + ((double)maxTimeToInsert/1000000) + " ms.");
|
||||||
Log.d(TAG, "min = " + ((double)minTimeToInsert/1000000) + " ms.");
|
Log.d(TAG, "min = " + ((double)minTimeToInsert/1000000) + " ms.");
|
||||||
Log.d(TAG, "avg = " + ((double)sum/mMaxUnigrams/1000000) + " ms.");
|
Log.d(TAG, "avg = " + ((double)sum/mMaxUnigrams/1000000) + " ms.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testRandomWords() {
|
||||||
|
runTestRandomWords(VERSION3);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue