Fix failing tests

- Version 3 is not supported
- Now passing the right string to open v4 dicts. Fix the tests for this.

Change-Id: I7829330c3568a715b96396ba4e4e69c6e17775ab
main
Jean Chalard 2013-12-16 14:17:33 +09:00
parent 2c25c7c9e1
commit b868375763
7 changed files with 22 additions and 43 deletions

View File

@ -188,7 +188,6 @@ public final class FormatSpec {
// us to change the format during development while having testing devices remove // us to change the format during development while having testing devices remove
// older files with each upgrade, while still having a readable versioning scheme. // older files with each upgrade, while still having a readable versioning scheme.
public static final int VERSION2 = 2; public static final int VERSION2 = 2;
public static final int VERSION3 = 3;
public static final int VERSION4 = 400; public static final int VERSION4 = 400;
static final int MINIMUM_SUPPORTED_VERSION = VERSION2; static final int MINIMUM_SUPPORTED_VERSION = VERSION2;
static final int MAXIMUM_SUPPORTED_VERSION = VERSION4; static final int MAXIMUM_SUPPORTED_VERSION = VERSION4;

View File

@ -68,7 +68,7 @@ public class Ver2DictEncoder implements DictEncoder {
@Override @Override
public void writeDictionary(final FusionDictionary dict, final FormatOptions formatOptions) public void writeDictionary(final FusionDictionary dict, final FormatOptions formatOptions)
throws IOException, UnsupportedFormatException { throws IOException, UnsupportedFormatException {
if (formatOptions.mVersion > FormatSpec.VERSION3) { if (formatOptions.mVersion > FormatSpec.VERSION2) {
throw new UnsupportedFormatException( throw new UnsupportedFormatException(
"The given format options has wrong version number : " "The given format options has wrong version number : "
+ formatOptions.mVersion); + formatOptions.mVersion);

View File

@ -56,7 +56,8 @@ public class Ver4DictEncoder implements DictEncoder {
} }
if (!BinaryDictionary.createEmptyDictFile(mDictPlacedDir.getAbsolutePath(), if (!BinaryDictionary.createEmptyDictFile(mDictPlacedDir.getAbsolutePath(),
FormatSpec.VERSION4, dict.mOptions.mAttributes)) { FormatSpec.VERSION4, dict.mOptions.mAttributes)) {
throw new IOException("Cannot create dictionary file"); throw new IOException("Cannot create dictionary file : "
+ mDictPlacedDir.getAbsolutePath());
} }
final BinaryDictionary binaryDict = new BinaryDictionary(mDictPlacedDir.getAbsolutePath(), final BinaryDictionary binaryDict = new BinaryDictionary(mDictPlacedDir.getAbsolutePath(),
0l, mDictPlacedDir.length(), true /* useFullEditDistance */, 0l, mDictPlacedDir.length(), true /* useFullEditDistance */,

View File

@ -169,8 +169,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
long now = -1, diff = -1; long now = -1, diff = -1;
try { try {
final DictEncoder dictEncoder = BinaryDictUtils.getDictEncoder(file, formatOptions, final DictEncoder dictEncoder = BinaryDictUtils.getDictEncoder(file, formatOptions);
getContext().getCacheDir());
now = System.currentTimeMillis(); now = System.currentTimeMillis();
// If you need to dump the dict to a textual file, uncomment the line below and the // If you need to dump the dict to a textual file, uncomment the line below and the
@ -316,7 +315,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_BUFFER, runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_BUFFER,
BinaryDictUtils.VERSION2_OPTIONS); BinaryDictUtils.VERSION2_OPTIONS);
runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_BUFFER, runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_BUFFER,
BinaryDictUtils.VERSION3_OPTIONS); BinaryDictUtils.VERSION4_OPTIONS_WITHOUT_TIMESTAMP);
runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_BUFFER, runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_BUFFER,
BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP); BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP);
@ -331,7 +330,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_ARRAY, runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_ARRAY,
BinaryDictUtils.VERSION2_OPTIONS); BinaryDictUtils.VERSION2_OPTIONS);
runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_ARRAY, runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_ARRAY,
BinaryDictUtils.VERSION3_OPTIONS); BinaryDictUtils.VERSION4_OPTIONS_WITHOUT_TIMESTAMP);
runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_ARRAY, runReadAndWriteTests(results, BinaryDictUtils.USE_BYTE_ARRAY,
BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP); BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP);
@ -390,8 +389,6 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
private long timeAndCheckReadUnigramsAndBigramsBinary(final File file, final List<String> words, private long timeAndCheckReadUnigramsAndBigramsBinary(final File file, final List<String> words,
final SparseArray<List<Integer>> bigrams, final int bufferType) { final SparseArray<List<Integer>> bigrams, final int bufferType) {
FileInputStream inStream = null;
final TreeMap<Integer, String> resultWords = CollectionUtils.newTreeMap(); final TreeMap<Integer, String> resultWords = CollectionUtils.newTreeMap();
final TreeMap<Integer, ArrayList<PendingAttribute>> resultBigrams = final TreeMap<Integer, ArrayList<PendingAttribute>> resultBigrams =
CollectionUtils.newTreeMap(); CollectionUtils.newTreeMap();
@ -407,14 +404,6 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
Log.e(TAG, "IOException", e); Log.e(TAG, "IOException", e);
} catch (UnsupportedFormatException e) { } catch (UnsupportedFormatException e) {
Log.e(TAG, "UnsupportedFormatException", e); Log.e(TAG, "UnsupportedFormatException", e);
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
// do nothing
}
}
} }
checkWordMap(words, bigrams, resultWords, resultFreqs, resultBigrams); checkWordMap(words, bigrams, resultWords, resultFreqs, resultBigrams);
@ -461,7 +450,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
runReadUnigramsAndBigramsTests(results, BinaryDictUtils.USE_BYTE_BUFFER, runReadUnigramsAndBigramsTests(results, BinaryDictUtils.USE_BYTE_BUFFER,
BinaryDictUtils.VERSION2_OPTIONS); BinaryDictUtils.VERSION2_OPTIONS);
runReadUnigramsAndBigramsTests(results, BinaryDictUtils.USE_BYTE_BUFFER, runReadUnigramsAndBigramsTests(results, BinaryDictUtils.USE_BYTE_BUFFER,
BinaryDictUtils.VERSION3_OPTIONS); BinaryDictUtils.VERSION4_OPTIONS_WITHOUT_TIMESTAMP);
runReadUnigramsAndBigramsTests(results, BinaryDictUtils.USE_BYTE_BUFFER, runReadUnigramsAndBigramsTests(results, BinaryDictUtils.USE_BYTE_BUFFER,
BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP); BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP);
@ -476,7 +465,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
runReadUnigramsAndBigramsTests(results, BinaryDictUtils.USE_BYTE_ARRAY, runReadUnigramsAndBigramsTests(results, BinaryDictUtils.USE_BYTE_ARRAY,
BinaryDictUtils.VERSION2_OPTIONS); BinaryDictUtils.VERSION2_OPTIONS);
runReadUnigramsAndBigramsTests(results, BinaryDictUtils.USE_BYTE_ARRAY, runReadUnigramsAndBigramsTests(results, BinaryDictUtils.USE_BYTE_ARRAY,
BinaryDictUtils.VERSION3_OPTIONS); BinaryDictUtils.VERSION4_OPTIONS_WITHOUT_TIMESTAMP);
runReadUnigramsAndBigramsTests(results, BinaryDictUtils.USE_BYTE_ARRAY, runReadUnigramsAndBigramsTests(results, BinaryDictUtils.USE_BYTE_ARRAY,
BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP); BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP);
@ -590,14 +579,14 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
runGetTerminalPositionTests(BinaryDictUtils.USE_BYTE_ARRAY, runGetTerminalPositionTests(BinaryDictUtils.USE_BYTE_ARRAY,
BinaryDictUtils.VERSION2_OPTIONS); BinaryDictUtils.VERSION2_OPTIONS);
runGetTerminalPositionTests(BinaryDictUtils.USE_BYTE_ARRAY, runGetTerminalPositionTests(BinaryDictUtils.USE_BYTE_ARRAY,
BinaryDictUtils.VERSION3_OPTIONS); BinaryDictUtils.VERSION4_OPTIONS_WITHOUT_TIMESTAMP);
runGetTerminalPositionTests(BinaryDictUtils.USE_BYTE_ARRAY, runGetTerminalPositionTests(BinaryDictUtils.USE_BYTE_ARRAY,
BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP); BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP);
runGetTerminalPositionTests(BinaryDictUtils.USE_BYTE_BUFFER, runGetTerminalPositionTests(BinaryDictUtils.USE_BYTE_BUFFER,
BinaryDictUtils.VERSION2_OPTIONS); BinaryDictUtils.VERSION2_OPTIONS);
runGetTerminalPositionTests(BinaryDictUtils.USE_BYTE_BUFFER, runGetTerminalPositionTests(BinaryDictUtils.USE_BYTE_BUFFER,
BinaryDictUtils.VERSION3_OPTIONS); BinaryDictUtils.VERSION4_OPTIONS_WITHOUT_TIMESTAMP);
runGetTerminalPositionTests(BinaryDictUtils.USE_BYTE_BUFFER, runGetTerminalPositionTests(BinaryDictUtils.USE_BYTE_BUFFER,
BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP); BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP);
@ -633,6 +622,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
} }
public void testDeleteWord() throws IOException, UnsupportedFormatException { public void testDeleteWord() throws IOException, UnsupportedFormatException {
runTestDeleteWord(BinaryDictUtils.VERSION3_OPTIONS); runTestDeleteWord(BinaryDictUtils.VERSION4_OPTIONS_WITHOUT_TIMESTAMP);
runTestDeleteWord(BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP);
} }
} }

View File

@ -237,8 +237,7 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
dict.add("abcd", 10, null, false); dict.add("abcd", 10, null, false);
try { try {
final DictEncoder dictEncoder = BinaryDictUtils.getDictEncoder(file, formatOptions, final DictEncoder dictEncoder = BinaryDictUtils.getDictEncoder(file, formatOptions);
getContext().getCacheDir());
dictEncoder.writeDictionary(dict, formatOptions); dictEncoder.writeDictionary(dict, formatOptions);
} catch (IOException e) { } catch (IOException e) {
fail("IOException while writing an initial dictionary : " + e); fail("IOException while writing an initial dictionary : " + e);
@ -289,7 +288,6 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
} }
public void testInsertWord() { public void testInsertWord() {
runTestInsertWord(BinaryDictUtils.VERSION3_OPTIONS);
runTestInsertWord(BinaryDictUtils.VERSION4_OPTIONS_WITHOUT_TIMESTAMP); runTestInsertWord(BinaryDictUtils.VERSION4_OPTIONS_WITHOUT_TIMESTAMP);
runTestInsertWord(BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP); runTestInsertWord(BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP);
} }
@ -307,8 +305,7 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
dict.add("efgh", 15, null, false); dict.add("efgh", 15, null, false);
try { try {
final DictEncoder dictEncoder = BinaryDictUtils.getDictEncoder(file, formatOptions, final DictEncoder dictEncoder = BinaryDictUtils.getDictEncoder(file, formatOptions);
getContext().getCacheDir());
dictEncoder.writeDictionary(dict, formatOptions); dictEncoder.writeDictionary(dict, formatOptions);
} catch (IOException e) { } catch (IOException e) {
fail("IOException while writing an initial dictionary : " + e); fail("IOException while writing an initial dictionary : " + e);
@ -330,7 +327,6 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
} }
public void testInsertWordWithBigrams() { public void testInsertWordWithBigrams() {
runTestInsertWordWithBigrams(BinaryDictUtils.VERSION3_OPTIONS);
runTestInsertWordWithBigrams(BinaryDictUtils.VERSION4_OPTIONS_WITHOUT_TIMESTAMP); runTestInsertWordWithBigrams(BinaryDictUtils.VERSION4_OPTIONS_WITHOUT_TIMESTAMP);
runTestInsertWordWithBigrams(BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP); runTestInsertWordWithBigrams(BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP);
} }
@ -347,8 +343,7 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
dict.add("initial", 10, null, false); dict.add("initial", 10, null, false);
try { try {
final DictEncoder dictEncoder = BinaryDictUtils.getDictEncoder(file, formatOptions, final DictEncoder dictEncoder = BinaryDictUtils.getDictEncoder(file, formatOptions);
getContext().getCacheDir());
dictEncoder.writeDictionary(dict, formatOptions); dictEncoder.writeDictionary(dict, formatOptions);
} catch (IOException e) { } catch (IOException e) {
assertTrue(false); assertTrue(false);
@ -379,7 +374,6 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
} }
public void testRandomWords() { public void testRandomWords() {
runTestRandomWords(BinaryDictUtils.VERSION3_OPTIONS);
runTestRandomWords(BinaryDictUtils.VERSION4_OPTIONS_WITHOUT_TIMESTAMP); runTestRandomWords(BinaryDictUtils.VERSION4_OPTIONS_WITHOUT_TIMESTAMP);
runTestRandomWords(BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP); runTestRandomWords(BinaryDictUtils.VERSION4_OPTIONS_WITH_TIMESTAMP);
} }

View File

@ -31,8 +31,6 @@ public class BinaryDictUtils {
public static final FormatSpec.FormatOptions VERSION2_OPTIONS = public static final FormatSpec.FormatOptions VERSION2_OPTIONS =
new FormatSpec.FormatOptions(FormatSpec.VERSION2); new FormatSpec.FormatOptions(FormatSpec.VERSION2);
public static final FormatSpec.FormatOptions VERSION3_OPTIONS =
new FormatSpec.FormatOptions(FormatSpec.VERSION3);
public static final FormatSpec.FormatOptions VERSION4_OPTIONS_WITHOUT_TIMESTAMP = 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 VERSION4_OPTIONS_WITH_TIMESTAMP =
@ -48,8 +46,7 @@ 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.VERSION3) {
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);
@ -59,12 +56,13 @@ public class BinaryDictUtils {
} }
} }
public static DictEncoder getDictEncoder(final File file, final FormatOptions formatOptions, public static DictEncoder getDictEncoder(final File file, final FormatOptions formatOptions) {
final File cacheDir) {
if (formatOptions.mVersion == FormatSpec.VERSION4) { if (formatOptions.mVersion == FormatSpec.VERSION4) {
return new Ver4DictEncoder(cacheDir); if (!file.isDirectory()) {
} else if (formatOptions.mVersion == FormatSpec.VERSION3 file.mkdir();
|| formatOptions.mVersion == FormatSpec.VERSION2) { }
return new Ver4DictEncoder(file);
} else if (formatOptions.mVersion == FormatSpec.VERSION2) {
return new Ver2DictEncoder(file); return new Ver2DictEncoder(file);
} else { } else {
throw new RuntimeException("The format option has a wrong version : " throw new RuntimeException("The format option has a wrong version : "

View File

@ -46,7 +46,6 @@ public class DictionaryMaker {
static class Arguments { static class Arguments {
private static final String OPTION_VERSION_2 = "-2"; private static final String OPTION_VERSION_2 = "-2";
private static final String OPTION_VERSION_3 = "-3";
private static final String OPTION_VERSION_4 = "-4"; private static final String OPTION_VERSION_4 = "-4";
private static final String OPTION_INPUT_SOURCE = "-s"; private static final String OPTION_INPUT_SOURCE = "-s";
private static final String OPTION_INPUT_BIGRAM_XML = "-b"; private static final String OPTION_INPUT_BIGRAM_XML = "-b";
@ -158,8 +157,6 @@ public class DictionaryMaker {
if (arg.charAt(0) == '-') { if (arg.charAt(0) == '-') {
if (OPTION_VERSION_2.equals(arg)) { if (OPTION_VERSION_2.equals(arg)) {
// Do nothing, this is the default // Do nothing, this is the default
} else if (OPTION_VERSION_3.equals(arg)) {
outputBinaryFormatVersion = FormatSpec.VERSION3;
} else if (OPTION_VERSION_4.equals(arg)) { } else if (OPTION_VERSION_4.equals(arg)) {
outputBinaryFormatVersion = FormatSpec.VERSION4; outputBinaryFormatVersion = FormatSpec.VERSION4;
} else if (OPTION_HELP.equals(arg)) { } else if (OPTION_HELP.equals(arg)) {