aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src
diff options
context:
space:
mode:
authorYuichiro Hanada <yhanada@google.com>2013-08-22 22:43:20 +0900
committerYuichiro Hanada <yhanada@google.com>2013-08-23 20:29:25 +0900
commite9a10ff0f026b5ec458f116afc7a75806574cbcd (patch)
treeb50ff7fc7e7b4a71db9410bfea829905f00817fb /tests/src
parentb64157bf4720dfc2aa40ad8e6806459012f81082 (diff)
downloadlatinime-e9a10ff0f026b5ec458f116afc7a75806574cbcd.tar.gz
latinime-e9a10ff0f026b5ec458f116afc7a75806574cbcd.tar.xz
latinime-e9a10ff0f026b5ec458f116afc7a75806574cbcd.zip
Add DictDecoder.readDictionaryBinary.
Bug: 10434720 Change-Id: I14690a6e0f922ed1bab3a4b6c9a457ae84d4c1a4
Diffstat (limited to 'tests/src')
-rw-r--r--tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java35
-rw-r--r--tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java37
-rw-r--r--tests/src/com/android/inputmethod/latin/makedict/Ver3DictDecoderTests.java10
-rw-r--r--tests/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtilsTests.java6
4 files changed, 36 insertions, 52 deletions
diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java
index 5bf9143ed..6d4c05e09 100644
--- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java
+++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java
@@ -33,7 +33,6 @@ import com.android.inputmethod.latin.utils.CollectionUtils;
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
@@ -133,17 +132,15 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
// Utilities for test
/**
- * Makes new DictBuffer according to BUFFER_TYPE.
+ * Makes new DictDecoder according to BUFFER_TYPE.
*/
- private void getDictBuffer(final Ver3DictDecoder dictDecoder, final int bufferType)
- throws FileNotFoundException, IOException {
+ private Ver3DictDecoder getDictDecoder(final File file, final int bufferType) {
if (bufferType == USE_BYTE_BUFFER) {
- dictDecoder.openDictBuffer(
- new Ver3DictDecoder.DictionaryBufferFromReadOnlyByteBufferFactory());
- } else if (bufferType == USE_BYTE_ARRAY) {
- dictDecoder.openDictBuffer(
- new Ver3DictDecoder.DictionaryBufferFromByteArrayFactory());
+ return new Ver3DictDecoder(file, DictDecoder.USE_READONLY_BYTEBUFFER);
+ } else if (bufferType == USE_BYTE_ARRAY) {
+ return new Ver3DictDecoder(file, DictDecoder.USE_BYTEARRAY);
}
+ return null;
}
/**
@@ -284,14 +281,14 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
final SparseArray<List<Integer>> bigrams, final Map<String, List<String>> shortcutMap,
final int bufferType) {
long now, diff = -1;
- final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file);
FusionDictionary dict = null;
try {
- getDictBuffer(dictDecoder, bufferType);
+ final Ver3DictDecoder dictDecoder = getDictDecoder(file, bufferType);
+ dictDecoder.openDictBuffer();
assertNotNull(dictDecoder.getDictBuffer());
now = System.currentTimeMillis();
- dict = BinaryDictDecoderUtils.readDictionaryBinary(dictDecoder, null);
+ dict = dictDecoder.readDictionaryBinary(null);
diff = System.currentTimeMillis() - now;
} catch (IOException e) {
Log.e(TAG, "IOException while reading dictionary", e);
@@ -444,9 +441,9 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
final Map<Integer, Integer> resultFreqs = CollectionUtils.newTreeMap();
long now = -1, diff = -1;
- final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file);
try {
- getDictBuffer(dictDecoder, bufferType);
+ final Ver3DictDecoder dictDecoder = getDictDecoder(file, bufferType);
+ dictDecoder.openDictBuffer();
assertNotNull("Can't get buffer.", dictDecoder.getDictBuffer());
now = System.currentTimeMillis();
BinaryDictIOUtils.readUnigramsAndBigramsBinary(dictDecoder, resultWords, resultFreqs,
@@ -587,10 +584,9 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
addUnigrams(sWords.size(), dict, sWords, null /* shortcutMap */);
timeWritingDictToFile(file, dict, VERSION3_WITH_DYNAMIC_UPDATE);
- final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file);
+ final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file, DictDecoder.USE_BYTEARRAY);
try {
- dictDecoder.openDictBuffer(
- new Ver3DictDecoder.DictionaryBufferFromByteArrayFactory());
+ dictDecoder.openDictBuffer();
} catch (IOException e) {
// ignore
Log.e(TAG, "IOException while opening the buffer", e);
@@ -648,10 +644,9 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
addUnigrams(sWords.size(), dict, sWords, null /* shortcutMap */);
timeWritingDictToFile(file, dict, VERSION3_WITH_DYNAMIC_UPDATE);
- final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file);
+ final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file, DictDecoder.USE_BYTEARRAY);
try {
- dictDecoder.openDictBuffer(
- new Ver3DictDecoder.DictionaryBufferFromByteArrayFactory());
+ dictDecoder.openDictBuffer();
} catch (IOException e) {
// ignore
Log.e(TAG, "IOException while opening the buffer", e);
diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java
index bfdc0407a..901cfdb70 100644
--- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java
+++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java
@@ -31,7 +31,6 @@ import com.android.inputmethod.latin.utils.CollectionUtils;
import java.io.BufferedOutputStream;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
@@ -140,23 +139,13 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
private int getWordPosition(final File file, final String word) {
int position = FormatSpec.NOT_VALID_WORD;
- final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file);
- FileInputStream inStream = null;
+
try {
- inStream = new FileInputStream(file);
- dictDecoder.openDictBuffer(
- new Ver3DictDecoder.DictionaryBufferFromReadOnlyByteBufferFactory());
+ final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file);
+ dictDecoder.openDictBuffer();
position = BinaryDictIOUtils.getTerminalPosition(dictDecoder, word);
} catch (IOException e) {
} catch (UnsupportedFormatException e) {
- } finally {
- if (inStream != null) {
- try {
- inStream.close();
- } catch (IOException e) {
- // do nothing
- }
- }
}
return position;
}
@@ -184,11 +173,10 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
}
private CharGroupInfo findWordFromFile(final File file, final String word) {
- final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file);
CharGroupInfo info = null;
try {
- dictDecoder.openDictBuffer(
- new Ver3DictDecoder.DictionaryBufferFromReadOnlyByteBufferFactory());
+ final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file);
+ dictDecoder.openDictBuffer();
info = findWordByBinaryDictReader(dictDecoder, word);
} catch (IOException e) {
} catch (UnsupportedFormatException e) {
@@ -200,11 +188,12 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
private long insertAndCheckWord(final File file, final String word, final int frequency,
final boolean exist, final ArrayList<WeightedString> bigrams,
final ArrayList<WeightedString> shortcuts) {
- final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file);
BufferedOutputStream outStream = null;
long amountOfTime = -1;
try {
- dictDecoder.openDictBuffer(new DictionaryBufferFromWritableByteBufferFactory());
+ final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file,
+ DictDecoder.USE_WRITABLE_BYTEBUFFER);
+ dictDecoder.openDictBuffer();
outStream = new BufferedOutputStream(new FileOutputStream(file, true));
if (!exist) {
@@ -234,9 +223,10 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
}
private void deleteWord(final File file, final String word) {
- final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file);
try {
- dictDecoder.openDictBuffer(new DictionaryBufferFromWritableByteBufferFactory());
+ final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file,
+ DictDecoder.USE_WRITABLE_BYTEBUFFER);
+ dictDecoder.openDictBuffer();
DynamicBinaryDictIOUtils.deleteWord(dictDecoder, word);
} catch (IOException e) {
} catch (UnsupportedFormatException e) {
@@ -244,10 +234,9 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
}
private void checkReverseLookup(final File file, final String word, final int position) {
- final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file);
+
try {
- final DictBuffer dictBuffer = dictDecoder.openAndGetDictBuffer(
- new Ver3DictDecoder.DictionaryBufferFromReadOnlyByteBufferFactory());
+ final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file);
final FileHeader fileHeader = dictDecoder.readHeader();
assertEquals(word,
BinaryDictDecoderUtils.getWordAtPosition(dictDecoder, fileHeader.mHeaderSize,
diff --git a/tests/src/com/android/inputmethod/latin/makedict/Ver3DictDecoderTests.java b/tests/src/com/android/inputmethod/latin/makedict/Ver3DictDecoderTests.java
index 20e8b4fda..9611599b9 100644
--- a/tests/src/com/android/inputmethod/latin/makedict/Ver3DictDecoderTests.java
+++ b/tests/src/com/android/inputmethod/latin/makedict/Ver3DictDecoderTests.java
@@ -68,9 +68,9 @@ public class Ver3DictDecoderTests extends AndroidTestCase {
}
assertNotNull(testFile);
- final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(testFile);
+ final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(testFile, factory);
try {
- dictDecoder.openDictBuffer(factory);
+ dictDecoder.openDictBuffer();
} catch (Exception e) {
Log.e(TAG, "Failed to open the buffer", e);
}
@@ -78,7 +78,7 @@ public class Ver3DictDecoderTests extends AndroidTestCase {
writeDataToFile(testFile);
try {
- dictDecoder.openDictBuffer(factory);
+ dictDecoder.openDictBuffer();
} catch (Exception e) {
Log.e(TAG, "Raised the exception while opening buffer", e);
}
@@ -110,7 +110,7 @@ public class Ver3DictDecoderTests extends AndroidTestCase {
Log.e(TAG, "IOException while the creating temporary file", e);
}
- final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(testFile);
+ final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(testFile, factory);
// the default return value of getBuffer() must be null.
assertNull("the default return value of getBuffer() is not null",
@@ -122,7 +122,7 @@ public class Ver3DictDecoderTests extends AndroidTestCase {
DictBuffer dictBuffer = null;
try {
- dictBuffer = dictDecoder.openAndGetDictBuffer(factory);
+ dictBuffer = dictDecoder.openAndGetDictBuffer();
} catch (IOException e) {
Log.e(TAG, "Failed to open and get the buffer", e);
}
diff --git a/tests/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtilsTests.java b/tests/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtilsTests.java
index 7b3a01ccf..8831df95e 100644
--- a/tests/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtilsTests.java
+++ b/tests/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtilsTests.java
@@ -21,6 +21,7 @@ import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;
+import com.android.inputmethod.latin.makedict.DictDecoder;
import com.android.inputmethod.latin.makedict.DictEncoder;
import com.android.inputmethod.latin.makedict.FormatSpec;
import com.android.inputmethod.latin.makedict.FusionDictionary;
@@ -142,10 +143,9 @@ public class UserHistoryDictIOUtilsTests extends AndroidTestCase
}
private void readDictFromFile(final File file, final OnAddWordListener listener) {
- final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file);
+ final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file, DictDecoder.USE_BYTEARRAY);
try {
- dictDecoder.openDictBuffer(
- new Ver3DictDecoder.DictionaryBufferFromByteArrayFactory());
+ dictDecoder.openDictBuffer();
} catch (FileNotFoundException e) {
Log.e(TAG, "file not found", e);
} catch (IOException e) {