am 799263c3: am 966af8b5: Merge "Make BinaryDictionaryTests use current time as a seed."

* commit '799263c377f5d93272d4194e5f8029bcfbd2119e':
  Make BinaryDictionaryTests use current time as a seed.
main
Keisuke Kuroyanagi 2013-10-09 18:54:42 -07:00 committed by Android Git Automerger
commit 7f8769971b
1 changed files with 60 additions and 54 deletions

View File

@ -18,6 +18,7 @@ package com.android.inputmethod.latin;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.LargeTest; import android.test.suitebuilder.annotation.LargeTest;
import android.text.TextUtils;
import android.util.Pair; import android.util.Pair;
import com.android.inputmethod.latin.makedict.CodePointUtils; import com.android.inputmethod.latin.makedict.CodePointUtils;
@ -126,7 +127,7 @@ public class BinaryDictionaryTests extends AndroidTestCase {
public void testRandomlyAddUnigramWord() { public void testRandomlyAddUnigramWord() {
final int wordCount = 1000; final int wordCount = 1000;
final int codePointSetSize = 50; final int codePointSetSize = 50;
final int seed = 123456789; final long seed = System.currentTimeMillis();
File dictFile = null; File dictFile = null;
try { try {
@ -223,7 +224,8 @@ public class BinaryDictionaryTests extends AndroidTestCase {
final int wordCount = 100; final int wordCount = 100;
final int bigramCount = 1000; final int bigramCount = 1000;
final int codePointSetSize = 50; final int codePointSetSize = 50;
final int seed = 11111; final long seed = System.currentTimeMillis();
final Random random = new Random(seed);
File dictFile = null; File dictFile = null;
try { try {
@ -234,43 +236,42 @@ public class BinaryDictionaryTests extends AndroidTestCase {
BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(), BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
0 /* offset */, dictFile.length(), true /* useFullEditDistance */, 0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */); Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
final ArrayList<String> words = new ArrayList<String>(); final ArrayList<String> words = new ArrayList<String>();
// Test a word that isn't contained within the dictionary. final ArrayList<Pair<String, String>> bigramWords = new ArrayList<Pair<String,String>>();
final Random random = new Random(seed);
final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random); final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random);
final int[] unigramProbabilities = new int[wordCount]; final HashMap<String, Integer> unigramProbabilities = new HashMap<String, Integer>();
final HashMap<Pair<String, String>, Integer> bigramProbabilities =
new HashMap<Pair<String, String>, Integer>();
for (int i = 0; i < wordCount; ++i) { for (int i = 0; i < wordCount; ++i) {
final String word = CodePointUtils.generateWord(random, codePointSet); final String word = CodePointUtils.generateWord(random, codePointSet);
words.add(word); words.add(word);
final int unigramProbability = random.nextInt(0xFF); final int unigramProbability = random.nextInt(0xFF);
unigramProbabilities[i] = unigramProbability; unigramProbabilities.put(word, unigramProbability);
binaryDictionary.addUnigramWord(word, unigramProbability); binaryDictionary.addUnigramWord(word, unigramProbability);
} }
final int[][] probabilities = new int[wordCount][wordCount];
for (int i = 0; i < wordCount; ++i) {
for (int j = 0; j < wordCount; ++j) {
probabilities[i][j] = Dictionary.NOT_A_PROBABILITY;
}
}
for (int i = 0; i < bigramCount; i++) { for (int i = 0; i < bigramCount; i++) {
final int word0Index = random.nextInt(wordCount); final String word0 = words.get(random.nextInt(wordCount));
final int word1Index = random.nextInt(wordCount); final String word1 = words.get(random.nextInt(wordCount));
final String word0 = words.get(word0Index); if (TextUtils.equals(word0, word1)) {
final String word1 = words.get(word1Index); continue;
}
final Pair<String, String> bigram = new Pair<String, String>(word0, word1);
bigramWords.add(bigram);
final int bigramProbability = random.nextInt(0xF); final int bigramProbability = random.nextInt(0xF);
probabilities[word0Index][word1Index] = binaryDictionary.calculateProbability( bigramProbabilities.put(bigram, bigramProbability);
unigramProbabilities[word1Index], bigramProbability);
binaryDictionary.addBigramWords(word0, word1, bigramProbability); binaryDictionary.addBigramWords(word0, word1, bigramProbability);
} }
for (int i = 0; i < words.size(); i++) { for (final Pair<String, String> bigram : bigramWords) {
for (int j = 0; j < words.size(); j++) { final int unigramProbability = unigramProbabilities.get(bigram.second);
assertEquals(probabilities[i][j], final int bigramProbability = bigramProbabilities.get(bigram);
binaryDictionary.getBigramProbability(words.get(i), words.get(j))); final int probability = binaryDictionary.calculateProbability(unigramProbability,
} bigramProbability);
assertEquals(probability,
binaryDictionary.getBigramProbability(bigram.first, bigram.second));
} }
dictFile.delete(); dictFile.delete();
@ -419,8 +420,8 @@ public class BinaryDictionaryTests extends AndroidTestCase {
final int wordCount = 100; final int wordCount = 100;
final int bigramCount = 1000; final int bigramCount = 1000;
final int codePointSetSize = 30; final int codePointSetSize = 30;
// TODO: Use various seeds such as a current timestamp to make this test more random. final long seed = System.currentTimeMillis();
final int seed = 314159265; final Random random = new Random(seed);
File dictFile = null; File dictFile = null;
try { try {
@ -432,35 +433,32 @@ public class BinaryDictionaryTests extends AndroidTestCase {
BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(), BinaryDictionary binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(),
0 /* offset */, dictFile.length(), true /* useFullEditDistance */, 0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */); Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
final ArrayList<String> words = new ArrayList<String>(); final ArrayList<String> words = new ArrayList<String>();
// Test a word that isn't contained within the dictionary. final ArrayList<Pair<String, String>> bigramWords = new ArrayList<Pair<String,String>>();
final Random random = new Random(seed);
final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random); final int[] codePointSet = CodePointUtils.generateCodePointSet(codePointSetSize, random);
final int[] unigramProbabilities = new int[wordCount]; final HashMap<String, Integer> unigramProbabilities = new HashMap<String, Integer>();
final HashMap<Pair<String, String>, Integer> bigramProbabilities =
new HashMap<Pair<String, String>, Integer>();
for (int i = 0; i < wordCount; ++i) { for (int i = 0; i < wordCount; ++i) {
final String word = CodePointUtils.generateWord(random, codePointSet); final String word = CodePointUtils.generateWord(random, codePointSet);
words.add(word); words.add(word);
final int unigramProbability = random.nextInt(0xFF); final int unigramProbability = random.nextInt(0xFF);
unigramProbabilities[i] = unigramProbability; unigramProbabilities.put(word, unigramProbability);
binaryDictionary.addUnigramWord(word, unigramProbability); binaryDictionary.addUnigramWord(word, unigramProbability);
} }
final int[][] probabilities = new int[wordCount][wordCount];
for (int i = 0; i < wordCount; ++i) {
for (int j = 0; j < wordCount; ++j) {
probabilities[i][j] = Dictionary.NOT_A_PROBABILITY;
}
}
for (int i = 0; i < bigramCount; i++) { for (int i = 0; i < bigramCount; i++) {
final int word0Index = random.nextInt(wordCount); final String word0 = words.get(random.nextInt(wordCount));
final int word1Index = random.nextInt(wordCount); final String word1 = words.get(random.nextInt(wordCount));
final String word0 = words.get(word0Index); if (TextUtils.equals(word0, word1)) {
final String word1 = words.get(word1Index); continue;
}
final Pair<String, String> bigram = new Pair<String, String>(word0, word1);
bigramWords.add(bigram);
final int bigramProbability = random.nextInt(0xF); final int bigramProbability = random.nextInt(0xF);
probabilities[word0Index][word1Index] = binaryDictionary.calculateProbability( bigramProbabilities.put(bigram, bigramProbability);
unigramProbabilities[word1Index], bigramProbability);
binaryDictionary.addBigramWords(word0, word1, bigramProbability); binaryDictionary.addBigramWords(word0, word1, bigramProbability);
} }
@ -470,12 +468,15 @@ public class BinaryDictionaryTests extends AndroidTestCase {
0 /* offset */, dictFile.length(), true /* useFullEditDistance */, 0 /* offset */, dictFile.length(), true /* useFullEditDistance */,
Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */); Locale.getDefault(), TEST_LOCALE, true /* isUpdatable */);
for (int i = 0; i < words.size(); i++) { for (final Pair<String, String> bigram : bigramWords) {
for (int j = 0; j < words.size(); j++) { final int unigramProbability = unigramProbabilities.get(bigram.second);
assertEquals(probabilities[i][j], final int bigramProbability = bigramProbabilities.get(bigram);
binaryDictionary.getBigramProbability(words.get(i), words.get(j))); final int probability = binaryDictionary.calculateProbability(unigramProbability,
} bigramProbability);
assertEquals(probability,
binaryDictionary.getBigramProbability(bigram.first, bigram.second));
} }
dictFile.delete(); dictFile.delete();
} }
@ -487,8 +488,8 @@ public class BinaryDictionaryTests extends AndroidTestCase {
final float addBigramProb = 0.8f; final float addBigramProb = 0.8f;
final float removeBigramProb = 0.2f; final float removeBigramProb = 0.2f;
final int codePointSetSize = 30; final int codePointSetSize = 30;
final int seed = 141421356;
final long seed = System.currentTimeMillis();
final Random random = new Random(seed); final Random random = new Random(seed);
File dictFile = null; File dictFile = null;
@ -539,6 +540,9 @@ public class BinaryDictionaryTests extends AndroidTestCase {
} }
final String word0 = words.get(word0Index); final String word0 = words.get(word0Index);
final String word1 = words.get(word1Index); final String word1 = words.get(word1Index);
if (TextUtils.equals(word0, word1)) {
continue;
}
final int bigramProbability = random.nextInt(0xF); final int bigramProbability = random.nextInt(0xF);
final Pair<String, String> bigram = new Pair<String, String>(word0, word1); final Pair<String, String> bigram = new Pair<String, String>(word0, word1);
bigramWords.add(bigram); bigramWords.add(bigram);
@ -586,8 +590,8 @@ public class BinaryDictionaryTests extends AndroidTestCase {
public void testAddManyUnigramsAndFlushWithGC() { public void testAddManyUnigramsAndFlushWithGC() {
final int flashWithGCIterationCount = 3; final int flashWithGCIterationCount = 3;
final int codePointSetSize = 50; final int codePointSetSize = 50;
final int seed = 22360679;
final long seed = System.currentTimeMillis();
final Random random = new Random(seed); final Random random = new Random(seed);
File dictFile = null; File dictFile = null;
@ -632,8 +636,7 @@ public class BinaryDictionaryTests extends AndroidTestCase {
final int codePointSetSize = 50; final int codePointSetSize = 50;
final int unigramCountPerIteration = 1000; final int unigramCountPerIteration = 1000;
final int bigramCountPerIteration = 2000; final int bigramCountPerIteration = 2000;
final int seed = 1123581321; final long seed = System.currentTimeMillis();
final Random random = new Random(seed); final Random random = new Random(seed);
File dictFile = null; File dictFile = null;
@ -661,6 +664,9 @@ public class BinaryDictionaryTests extends AndroidTestCase {
for (int j = 0; j < bigramCountPerIteration; j++) { for (int j = 0; j < bigramCountPerIteration; j++) {
final String word0 = words.get(random.nextInt(words.size())); final String word0 = words.get(random.nextInt(words.size()));
final String word1 = words.get(random.nextInt(words.size())); final String word1 = words.get(random.nextInt(words.size()));
if (TextUtils.equals(word0, word1)) {
continue;
}
bigrams.add(new Pair<String, String>(word0, word1)); bigrams.add(new Pair<String, String>(word0, word1));
final int bigramProbability = random.nextInt(0xF); final int bigramProbability = random.nextInt(0xF);
binaryDictionary.addBigramWords(word0, word1, bigramProbability); binaryDictionary.addBigramWords(word0, word1, bigramProbability);