Merge "Update unit tests for new static dict version."
commit
61d43e5c94
|
@ -178,8 +178,10 @@ public final class FormatSpec {
|
||||||
public static final int VERSION401 = 401;
|
public static final int VERSION401 = 401;
|
||||||
public static final int VERSION4 = 402;
|
public static final int VERSION4 = 402;
|
||||||
public static final int VERSION4_DEV = 403;
|
public static final int VERSION4_DEV = 403;
|
||||||
static final int MINIMUM_SUPPORTED_VERSION = VERSION2;
|
static final int MINIMUM_SUPPORTED_STATIC_VERSION = VERSION202;
|
||||||
static final int MAXIMUM_SUPPORTED_VERSION = VERSION4_DEV;
|
static final int MAXIMUM_SUPPORTED_STATIC_VERSION = VERSION202;
|
||||||
|
static final int MINIMUM_SUPPORTED_DYNAMIC_VERSION = VERSION4;
|
||||||
|
static final int MAXIMUM_SUPPORTED_DYNAMIC_VERSION = VERSION4_DEV;
|
||||||
|
|
||||||
// TODO: Make this value adaptative to content data, store it in the header, and
|
// TODO: Make this value adaptative to content data, store it in the header, and
|
||||||
// use it in the reading code.
|
// use it in the reading code.
|
||||||
|
|
|
@ -314,14 +314,14 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
|
||||||
final String dictVersion = Long.toString(System.currentTimeMillis());
|
final String dictVersion = Long.toString(System.currentTimeMillis());
|
||||||
final String codePointTableAttribute = DictionaryHeader.CODE_POINT_TABLE_KEY;
|
final String codePointTableAttribute = DictionaryHeader.CODE_POINT_TABLE_KEY;
|
||||||
final File file = BinaryDictUtils.getDictFile(dictName, dictVersion,
|
final File file = BinaryDictUtils.getDictFile(dictName, dictVersion,
|
||||||
BinaryDictUtils.VERSION201_OPTIONS, getContext().getCacheDir());
|
BinaryDictUtils.STATIC_OPTIONS, getContext().getCacheDir());
|
||||||
|
|
||||||
// Write a test dictionary
|
// Write a test dictionary
|
||||||
final DictEncoder dictEncoder = new Ver2DictEncoder(file,
|
final DictEncoder dictEncoder = new Ver2DictEncoder(file,
|
||||||
Ver2DictEncoder.CODE_POINT_TABLE_ON);
|
Ver2DictEncoder.CODE_POINT_TABLE_ON);
|
||||||
final FormatSpec.FormatOptions formatOptions =
|
final FormatSpec.FormatOptions formatOptions =
|
||||||
new FormatSpec.FormatOptions(
|
new FormatSpec.FormatOptions(
|
||||||
FormatSpec.MINIMUM_SUPPORTED_VERSION_OF_CODE_POINT_TABLE);
|
FormatSpec.MINIMUM_SUPPORTED_STATIC_VERSION);
|
||||||
final FusionDictionary sourcedict = new FusionDictionary(new PtNodeArray(),
|
final FusionDictionary sourcedict = new FusionDictionary(new PtNodeArray(),
|
||||||
BinaryDictUtils.makeDictionaryOptions(dictName, dictVersion, formatOptions));
|
BinaryDictUtils.makeDictionaryOptions(dictName, dictVersion, formatOptions));
|
||||||
addUnigrams(words.size(), sourcedict, words, null /* shortcutMap */);
|
addUnigrams(words.size(), sourcedict, words, null /* shortcutMap */);
|
||||||
|
@ -359,11 +359,11 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
|
||||||
final List<String> results = new ArrayList<>();
|
final List<String> results = new ArrayList<>();
|
||||||
|
|
||||||
runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_BUFFER,
|
runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_BUFFER,
|
||||||
BinaryDictUtils.VERSION2_OPTIONS);
|
BinaryDictUtils.STATIC_OPTIONS);
|
||||||
runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_BUFFER,
|
runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_BUFFER,
|
||||||
BinaryDictUtils.VERSION4_OPTIONS_WITHOUT_TIMESTAMP);
|
BinaryDictUtils.DYNAMIC_OPTIONS_WITHOUT_TIMESTAMP);
|
||||||
runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_BUFFER,
|
runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_BUFFER,
|
||||||
BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP);
|
BinaryDictUtils.DYNAMIC_OPTIONS_WITH_TIMESTAMP);
|
||||||
for (final String result : results) {
|
for (final String result : results) {
|
||||||
Log.d(TAG, result);
|
Log.d(TAG, result);
|
||||||
}
|
}
|
||||||
|
@ -373,11 +373,11 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
|
||||||
final List<String> results = new ArrayList<>();
|
final List<String> results = new ArrayList<>();
|
||||||
|
|
||||||
runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_ARRAY,
|
runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_ARRAY,
|
||||||
BinaryDictUtils.VERSION2_OPTIONS);
|
BinaryDictUtils.STATIC_OPTIONS);
|
||||||
runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_ARRAY,
|
runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_ARRAY,
|
||||||
BinaryDictUtils.VERSION4_OPTIONS_WITHOUT_TIMESTAMP);
|
BinaryDictUtils.DYNAMIC_OPTIONS_WITHOUT_TIMESTAMP);
|
||||||
runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_ARRAY,
|
runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_ARRAY,
|
||||||
BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP);
|
BinaryDictUtils.DYNAMIC_OPTIONS_WITH_TIMESTAMP);
|
||||||
|
|
||||||
for (final String result : results) {
|
for (final String result : results) {
|
||||||
Log.d(TAG, result);
|
Log.d(TAG, result);
|
||||||
|
@ -501,7 +501,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
|
||||||
final ArrayList<String> results = new ArrayList<>();
|
final ArrayList<String> results = new ArrayList<>();
|
||||||
|
|
||||||
runReadUnigramsAndBigramsTests(results, BinaryDictUtils.USE_BYTE_BUFFER,
|
runReadUnigramsAndBigramsTests(results, BinaryDictUtils.USE_BYTE_BUFFER,
|
||||||
BinaryDictUtils.VERSION2_OPTIONS);
|
BinaryDictUtils.STATIC_OPTIONS);
|
||||||
|
|
||||||
for (final String result : results) {
|
for (final String result : results) {
|
||||||
Log.d(TAG, result);
|
Log.d(TAG, result);
|
||||||
|
@ -512,7 +512,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
|
||||||
final ArrayList<String> results = new ArrayList<>();
|
final ArrayList<String> results = new ArrayList<>();
|
||||||
|
|
||||||
runReadUnigramsAndBigramsTests(results, BinaryDictUtils.USE_BYTE_ARRAY,
|
runReadUnigramsAndBigramsTests(results, BinaryDictUtils.USE_BYTE_ARRAY,
|
||||||
BinaryDictUtils.VERSION2_OPTIONS);
|
BinaryDictUtils.STATIC_OPTIONS);
|
||||||
|
|
||||||
for (final String result : results) {
|
for (final String result : results) {
|
||||||
Log.d(TAG, result);
|
Log.d(TAG, result);
|
||||||
|
@ -623,9 +623,9 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
|
||||||
final ArrayList<String> results = new ArrayList<>();
|
final ArrayList<String> results = new ArrayList<>();
|
||||||
|
|
||||||
runGetTerminalPositionTests(BinaryDictUtils.USE_BYTE_ARRAY,
|
runGetTerminalPositionTests(BinaryDictUtils.USE_BYTE_ARRAY,
|
||||||
BinaryDictUtils.VERSION2_OPTIONS);
|
BinaryDictUtils.STATIC_OPTIONS);
|
||||||
runGetTerminalPositionTests(BinaryDictUtils.USE_BYTE_BUFFER,
|
runGetTerminalPositionTests(BinaryDictUtils.USE_BYTE_BUFFER,
|
||||||
BinaryDictUtils.VERSION2_OPTIONS);
|
BinaryDictUtils.STATIC_OPTIONS);
|
||||||
|
|
||||||
for (final String result : results) {
|
for (final String result : results) {
|
||||||
Log.d(TAG, result);
|
Log.d(TAG, result);
|
||||||
|
@ -633,7 +633,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testVer2DictGetWordProperty() {
|
public void testVer2DictGetWordProperty() {
|
||||||
final FormatOptions formatOptions = BinaryDictUtils.VERSION2_OPTIONS;
|
final FormatOptions formatOptions = BinaryDictUtils.STATIC_OPTIONS;
|
||||||
final ArrayList<String> words = sWords;
|
final ArrayList<String> words = sWords;
|
||||||
final HashMap<String, List<String>> shortcuts = sShortcuts;
|
final HashMap<String, List<String>> shortcuts = sShortcuts;
|
||||||
final String dictName = "testGetWordProperty";
|
final String dictName = "testGetWordProperty";
|
||||||
|
@ -669,7 +669,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testVer2DictIteration() {
|
public void testVer2DictIteration() {
|
||||||
final FormatOptions formatOptions = BinaryDictUtils.VERSION2_OPTIONS;
|
final FormatOptions formatOptions = BinaryDictUtils.STATIC_OPTIONS;
|
||||||
final ArrayList<String> words = sWords;
|
final ArrayList<String> words = sWords;
|
||||||
final HashMap<String, List<String>> shortcuts = sShortcuts;
|
final HashMap<String, List<String>> shortcuts = sShortcuts;
|
||||||
final SparseArray<List<Integer>> bigrams = sEmptyBigrams;
|
final SparseArray<List<Integer>> bigrams = sEmptyBigrams;
|
||||||
|
|
|
@ -819,12 +819,18 @@ public class BinaryDictEncoderUtils {
|
||||||
final ArrayList<Entry<Integer, Integer>> codePointOccurrenceArray)
|
final ArrayList<Entry<Integer, Integer>> codePointOccurrenceArray)
|
||||||
throws IOException, UnsupportedFormatException {
|
throws IOException, UnsupportedFormatException {
|
||||||
final int version = formatOptions.mVersion;
|
final int version = formatOptions.mVersion;
|
||||||
if (version < FormatSpec.MINIMUM_SUPPORTED_VERSION
|
if ((version >= FormatSpec.MINIMUM_SUPPORTED_STATIC_VERSION &&
|
||||||
|| version > FormatSpec.MAXIMUM_SUPPORTED_VERSION) {
|
version <= FormatSpec.MAXIMUM_SUPPORTED_STATIC_VERSION) || (
|
||||||
|
version >= FormatSpec.MINIMUM_SUPPORTED_DYNAMIC_VERSION &&
|
||||||
|
version <= FormatSpec.MAXIMUM_SUPPORTED_DYNAMIC_VERSION)) {
|
||||||
|
// Dictionary is valid
|
||||||
|
} else {
|
||||||
throw new UnsupportedFormatException("Requested file format version " + version
|
throw new UnsupportedFormatException("Requested file format version " + version
|
||||||
+ ", but this implementation only supports versions "
|
+ ", but this implementation only supports static versions "
|
||||||
+ FormatSpec.MINIMUM_SUPPORTED_VERSION + " through "
|
+ FormatSpec.MINIMUM_SUPPORTED_STATIC_VERSION + " through "
|
||||||
+ FormatSpec.MAXIMUM_SUPPORTED_VERSION);
|
+ FormatSpec.MAXIMUM_SUPPORTED_STATIC_VERSION + " and dynamic versions "
|
||||||
|
+ FormatSpec.MINIMUM_SUPPORTED_DYNAMIC_VERSION + " through "
|
||||||
|
+ FormatSpec.MAXIMUM_SUPPORTED_DYNAMIC_VERSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteArrayOutputStream headerBuffer = new ByteArrayOutputStream(256);
|
ByteArrayOutputStream headerBuffer = new ByteArrayOutputStream(256);
|
||||||
|
|
|
@ -28,13 +28,11 @@ public class BinaryDictUtils {
|
||||||
|
|
||||||
public static final String TEST_DICT_FILE_EXTENSION = ".testDict";
|
public static final String TEST_DICT_FILE_EXTENSION = ".testDict";
|
||||||
|
|
||||||
public static final FormatSpec.FormatOptions VERSION2_OPTIONS =
|
public static final FormatSpec.FormatOptions STATIC_OPTIONS =
|
||||||
new FormatSpec.FormatOptions(FormatSpec.VERSION2);
|
new FormatSpec.FormatOptions(FormatSpec.VERSION202);
|
||||||
public static final FormatSpec.FormatOptions VERSION201_OPTIONS =
|
public static final FormatSpec.FormatOptions DYNAMIC_OPTIONS_WITHOUT_TIMESTAMP =
|
||||||
new FormatSpec.FormatOptions(FormatSpec.VERSION201);
|
|
||||||
public static final FormatSpec.FormatOptions VERSION4_OPTIONS_WITHOUT_TIMESTAMP =
|
|
||||||
new FormatSpec.FormatOptions(FormatSpec.VERSION4, false /* hasTimestamp */);
|
new FormatSpec.FormatOptions(FormatSpec.VERSION4, false /* hasTimestamp */);
|
||||||
public static final FormatSpec.FormatOptions VERSION4_OPTIONS_WITH_TIMESTAMP =
|
public static final FormatSpec.FormatOptions DYNAMIC_OPTIONS_WITH_TIMESTAMP =
|
||||||
new FormatSpec.FormatOptions(FormatSpec.VERSION4, true /* hasTimestamp */);
|
new FormatSpec.FormatOptions(FormatSpec.VERSION4, true /* hasTimestamp */);
|
||||||
|
|
||||||
public static DictionaryOptions makeDictionaryOptions(final String id, final String version,
|
public static DictionaryOptions makeDictionaryOptions(final String id, final String version,
|
||||||
|
@ -55,7 +53,8 @@ public class BinaryDictUtils {
|
||||||
public static File getDictFile(final String name, final String version,
|
public static File getDictFile(final String name, final String version,
|
||||||
final FormatOptions formatOptions, final File directory) {
|
final FormatOptions formatOptions, final File directory) {
|
||||||
if (formatOptions.mVersion == FormatSpec.VERSION2
|
if (formatOptions.mVersion == FormatSpec.VERSION2
|
||||||
|| formatOptions.mVersion == FormatSpec.VERSION201) {
|
|| formatOptions.mVersion == FormatSpec.VERSION201
|
||||||
|
|| formatOptions.mVersion == FormatSpec.VERSION202) {
|
||||||
return new File(directory, name + "." + version + TEST_DICT_FILE_EXTENSION);
|
return new File(directory, name + "." + version + TEST_DICT_FILE_EXTENSION);
|
||||||
} else if (formatOptions.mVersion == FormatSpec.VERSION4) {
|
} else if (formatOptions.mVersion == FormatSpec.VERSION4) {
|
||||||
return new File(directory, name + "." + version);
|
return new File(directory, name + "." + version);
|
||||||
|
@ -71,7 +70,7 @@ public class BinaryDictUtils {
|
||||||
file.mkdir();
|
file.mkdir();
|
||||||
}
|
}
|
||||||
return new Ver4DictEncoder(file);
|
return new Ver4DictEncoder(file);
|
||||||
} else if (formatOptions.mVersion == FormatSpec.VERSION2) {
|
} else if (formatOptions.mVersion == FormatSpec.VERSION202) {
|
||||||
return new Ver2DictEncoder(file, Ver2DictEncoder.CODE_POINT_TABLE_OFF);
|
return new Ver2DictEncoder(file, Ver2DictEncoder.CODE_POINT_TABLE_OFF);
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException("The format option has a wrong version : "
|
throw new RuntimeException("The format option has a wrong version : "
|
||||||
|
|
Loading…
Reference in New Issue