am e1652d75
: Fix the build : move offdevice tests in an off device file
* commit 'e1652d7570e0eac3415be49e071975370e1f89ae': Fix the build : move offdevice tests in an off device file
This commit is contained in:
commit
5a4d31cc6d
2 changed files with 92 additions and 62 deletions
|
@ -24,10 +24,6 @@ import android.util.SparseArray;
|
|||
|
||||
import com.android.inputmethod.latin.BinaryDictionary;
|
||||
import com.android.inputmethod.latin.common.CodePointUtils;
|
||||
import com.android.inputmethod.latin.dicttool.BinaryDictOffdeviceUtils;
|
||||
import com.android.inputmethod.latin.dicttool.Compress;
|
||||
import com.android.inputmethod.latin.dicttool.Crypt;
|
||||
import com.android.inputmethod.latin.dicttool.BinaryDictOffdeviceUtils.DecoderChainSpec;
|
||||
import com.android.inputmethod.latin.makedict.BinaryDictDecoderUtils.CharEncoding;
|
||||
import com.android.inputmethod.latin.makedict.BinaryDictDecoderUtils.DictBuffer;
|
||||
import com.android.inputmethod.latin.makedict.FormatSpec.DictionaryOptions;
|
||||
|
@ -737,61 +733,4 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
|
|||
assertTrue(wordSet.isEmpty());
|
||||
assertTrue(bigramSet.isEmpty());
|
||||
}
|
||||
|
||||
public void runTestHeaderReaderProcessorWithOneSpec(final boolean compress, final boolean crypt)
|
||||
throws IOException {
|
||||
final String dictName = "testHeaderReaderProcessor";
|
||||
final String dictVersion = Long.toString(System.currentTimeMillis());
|
||||
final FormatOptions formatOptions = BinaryDictUtils.STATIC_OPTIONS;
|
||||
final int MAX_NUMBER_OF_OPTIONS_TO_ADD = 5;
|
||||
final HashMap<String, String> options = new HashMap<>();
|
||||
// Required attributes
|
||||
options.put("dictionary", "main:en_US");
|
||||
options.put("locale", "en_US");
|
||||
options.put("version", Integer.toString(mRandom.nextInt()));
|
||||
// Add some random options for test
|
||||
final int numberOfOptionsToAdd = mRandom.nextInt() % (MAX_NUMBER_OF_OPTIONS_TO_ADD + 1);
|
||||
for (int i = 0; i < numberOfOptionsToAdd; ++i) {
|
||||
options.put(sWordsWithVariousCodePoints.get(2 * i),
|
||||
sWordsWithVariousCodePoints.get(2 * 1 + 1));
|
||||
}
|
||||
final FusionDictionary dict = new FusionDictionary(new PtNodeArray(),
|
||||
new DictionaryOptions(options));
|
||||
addUnigrams(sWords.size(), dict, sWords, null);
|
||||
File file = BinaryDictUtils.getDictFile(dictName, dictVersion, formatOptions,
|
||||
getContext().getCacheDir());
|
||||
timeWritingDictToFile(file, dict, formatOptions);
|
||||
|
||||
if (compress) {
|
||||
final File rawFile = file;
|
||||
file = BinaryDictUtils.getDictFile(dictName + "compress", dictVersion, formatOptions,
|
||||
getContext().getCacheDir());
|
||||
final Compress.Compressor compressCommand = new Compress.Compressor();
|
||||
compressCommand.setArgs(new String[] { rawFile.getPath(), file.getPath() });
|
||||
compressCommand.run();
|
||||
}
|
||||
if (crypt) {
|
||||
final File rawFile = file;
|
||||
file = BinaryDictUtils.getDictFile(dictName + "crypt", dictVersion, formatOptions,
|
||||
getContext().getCacheDir());
|
||||
final Crypt.Encrypter cryptCommand = new Crypt.Encrypter();
|
||||
cryptCommand.setArgs(new String[] { rawFile.getPath(), file.getPath() });
|
||||
cryptCommand.run();
|
||||
}
|
||||
|
||||
final DecoderChainSpec<DictionaryHeader> spec =
|
||||
BinaryDictOffdeviceUtils.decodeDictionaryForProcess(file,
|
||||
new BinaryDictOffdeviceUtils.HeaderReaderProcessor());
|
||||
assertNotNull("Can't decode a dictionary we just wrote : " + file, spec);
|
||||
final DictionaryHeader header = spec.mResult;
|
||||
assertEquals("raw" + (crypt ? " > encryption" : "") + (compress ? " > compression" : ""),
|
||||
spec.describeChain());
|
||||
assertEquals(header.mDictionaryOptions.mAttributes, options);
|
||||
}
|
||||
|
||||
public void testHeaderReaderProcessor() throws IOException {
|
||||
runTestHeaderReaderProcessorWithOneSpec(false /* compress */, false /* crypt */);
|
||||
runTestHeaderReaderProcessorWithOneSpec(true /* compress */, false /* crypt */);
|
||||
runTestHeaderReaderProcessorWithOneSpec(true /* compress */, true /* crypt */);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,10 +16,17 @@
|
|||
|
||||
package com.android.inputmethod.latin.dicttool;
|
||||
|
||||
import com.android.inputmethod.latin.common.CodePointUtils;
|
||||
import com.android.inputmethod.latin.dicttool.BinaryDictOffdeviceUtils;
|
||||
import com.android.inputmethod.latin.dicttool.Compress;
|
||||
import com.android.inputmethod.latin.dicttool.Crypt;
|
||||
import com.android.inputmethod.latin.dicttool.BinaryDictOffdeviceUtils.DecoderChainSpec;
|
||||
import com.android.inputmethod.latin.makedict.BinaryDictIOUtils;
|
||||
import com.android.inputmethod.latin.makedict.BinaryDictUtils;
|
||||
import com.android.inputmethod.latin.makedict.DictDecoder;
|
||||
import com.android.inputmethod.latin.makedict.DictEncoder;
|
||||
import com.android.inputmethod.latin.makedict.DictionaryHeader;
|
||||
import com.android.inputmethod.latin.makedict.FormatSpec;
|
||||
import com.android.inputmethod.latin.makedict.FormatSpec.DictionaryOptions;
|
||||
import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions;
|
||||
import com.android.inputmethod.latin.makedict.FusionDictionary;
|
||||
|
@ -35,13 +42,37 @@ import java.io.File;
|
|||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Unit tests for BinaryDictOffdeviceUtils
|
||||
*/
|
||||
public class BinaryDictOffdeviceUtilsTests extends TestCase {
|
||||
private static final int TEST_FREQ = 37; // Some arbitrary value unlikely to happen by chance
|
||||
private static final int CODE_POINT_SET_SIZE = 300;
|
||||
final Random mRandom;
|
||||
private static final ArrayList<String> sWords = new ArrayList<>();
|
||||
|
||||
public BinaryDictOffdeviceUtilsTests(final long seed, final int maxUnigrams) {
|
||||
super();
|
||||
mRandom = new Random(seed);
|
||||
sWords.clear();
|
||||
generateWords(maxUnigrams, mRandom);
|
||||
}
|
||||
|
||||
private static void generateWords(final int maxUnigrams, final Random random) {
|
||||
final int[] codePointSet = CodePointUtils.generateCodePointSet(
|
||||
CODE_POINT_SET_SIZE, random);
|
||||
final Set<String> wordSet = new HashSet<>();
|
||||
while (wordSet.size() < maxUnigrams) {
|
||||
wordSet.add(CodePointUtils.generateWord(random, codePointSet));
|
||||
}
|
||||
sWords.addAll(wordSet);
|
||||
}
|
||||
|
||||
public void testGetRawDictWorks() throws IOException, UnsupportedFormatException {
|
||||
final String VERSION = "1";
|
||||
|
@ -70,7 +101,7 @@ public class BinaryDictOffdeviceUtilsTests extends TestCase {
|
|||
try (final OutputStream out = Compress.getCompressedStream(
|
||||
new BufferedOutputStream(new FileOutputStream(dst)))) {
|
||||
final DictEncoder dictEncoder = new Ver2DictEncoder(out);
|
||||
dictEncoder.writeDictionary(dict, new FormatOptions(2, false));
|
||||
dictEncoder.writeDictionary(dict, new FormatOptions(FormatSpec.VERSION202, false));
|
||||
}
|
||||
|
||||
// Test for an actually compressed dictionary and its contents
|
||||
|
@ -119,4 +150,64 @@ public class BinaryDictOffdeviceUtilsTests extends TestCase {
|
|||
assertNull("Wrongly identified data file",
|
||||
BinaryDictOffdeviceUtils.getRawDictionaryOrNull(gzDst));
|
||||
}
|
||||
|
||||
public void runTestHeaderReaderProcessorWithOneSpec(final boolean compress, final boolean crypt)
|
||||
throws IOException, UnsupportedFormatException {
|
||||
final String dictName = "testHeaderReaderProcessor";
|
||||
final String dictVersion = Long.toString(System.currentTimeMillis());
|
||||
final FormatOptions formatOptions = BinaryDictUtils.STATIC_OPTIONS;
|
||||
final int MAX_NUMBER_OF_OPTIONS_TO_ADD = 5;
|
||||
final HashMap<String, String> options = new HashMap<>();
|
||||
// Required attributes
|
||||
options.put("dictionary", "main:en_US");
|
||||
options.put("locale", "en_US");
|
||||
options.put("version", Integer.toString(mRandom.nextInt()));
|
||||
// Add some random options for test
|
||||
final int numberOfOptionsToAdd = mRandom.nextInt() % (MAX_NUMBER_OF_OPTIONS_TO_ADD + 1);
|
||||
for (int i = 0; i < numberOfOptionsToAdd; ++i) {
|
||||
options.put(sWords.get(2 * i), sWords.get(2 * 1 + 1));
|
||||
}
|
||||
final FusionDictionary dict = new FusionDictionary(new PtNodeArray(),
|
||||
new DictionaryOptions(options));
|
||||
|
||||
for (int i = 0; i < sWords.size(); ++i) {
|
||||
final String word = sWords.get(i);
|
||||
dict.add(word, new ProbabilityInfo(TEST_FREQ), null /* shortcuts */,
|
||||
false /* isNotAWord */, false /* isPossiblyOffensive */);
|
||||
}
|
||||
|
||||
File file = File.createTempFile(dictName, ".tmp");
|
||||
final DictEncoder dictEncoder = BinaryDictUtils.getDictEncoder(file, formatOptions);
|
||||
dictEncoder.writeDictionary(dict, formatOptions);
|
||||
|
||||
if (compress) {
|
||||
final File rawFile = file;
|
||||
file = File.createTempFile(dictName + ".compress", ".tmp");
|
||||
final Compress.Compressor compressCommand = new Compress.Compressor();
|
||||
compressCommand.setArgs(new String[] { rawFile.getPath(), file.getPath() });
|
||||
compressCommand.run();
|
||||
}
|
||||
if (crypt) {
|
||||
final File rawFile = file;
|
||||
file = File.createTempFile(dictName + ".crypt", ".tmp");
|
||||
final Crypt.Encrypter cryptCommand = new Crypt.Encrypter();
|
||||
cryptCommand.setArgs(new String[] { rawFile.getPath(), file.getPath() });
|
||||
cryptCommand.run();
|
||||
}
|
||||
|
||||
final DecoderChainSpec<DictionaryHeader> spec =
|
||||
BinaryDictOffdeviceUtils.decodeDictionaryForProcess(file,
|
||||
new BinaryDictOffdeviceUtils.HeaderReaderProcessor());
|
||||
assertNotNull("Can't decode a dictionary we just wrote : " + file, spec);
|
||||
final DictionaryHeader header = spec.mResult;
|
||||
assertEquals("raw" + (crypt ? " > encryption" : "") + (compress ? " > compression" : ""),
|
||||
spec.describeChain());
|
||||
assertEquals(header.mDictionaryOptions.mAttributes, options);
|
||||
}
|
||||
|
||||
public void testHeaderReaderProcessor() throws IOException, UnsupportedFormatException {
|
||||
runTestHeaderReaderProcessorWithOneSpec(false /* compress */, false /* crypt */);
|
||||
runTestHeaderReaderProcessorWithOneSpec(true /* compress */, false /* crypt */);
|
||||
runTestHeaderReaderProcessorWithOneSpec(true /* compress */, true /* crypt */);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue