aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src
diff options
context:
space:
mode:
authorYuichiro Hanada <yhanada@google.com>2013-08-14 12:56:51 +0900
committerYuichiro Hanada <yhanada@google.com>2013-08-16 21:06:23 +0900
commit3a73b37b30d922ea742dc69d676d66c1e8101140 (patch)
treeeb9e0049447622f5f19ca5d16c369e009953c14a /tests/src
parentc2f2d0d211fe4c765cda7a770004d061ff2b7711 (diff)
downloadlatinime-3a73b37b30d922ea742dc69d676d66c1e8101140.tar.gz
latinime-3a73b37b30d922ea742dc69d676d66c1e8101140.tar.xz
latinime-3a73b37b30d922ea742dc69d676d66c1e8101140.zip
Make BinaryDictIOUtils and DynamicBinaryIOUtils use BinaryDictReader.
Change-Id: I191dfe0e05ff3c2c5af99e8beebbb73b097748a3
Diffstat (limited to 'tests/src')
-rw-r--r--tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java45
-rw-r--r--tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java87
2 files changed, 46 insertions, 86 deletions
diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java
index 6d37466b7..e98790b65 100644
--- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java
+++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java
@@ -493,8 +493,8 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
}
// Tests for getTerminalPosition
- private String getWordFromBinary(final FusionDictionaryBufferInterface buffer,
- final int address) {
+ private String getWordFromBinary(final BinaryDictReader reader, final int address) {
+ final FusionDictionaryBufferInterface buffer = reader.getBuffer();
if (buffer.position() != 0) buffer.position(0);
FileHeader header = null;
@@ -510,14 +510,14 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
address - header.mHeaderSize, header.mFormatOptions).mWord;
}
- private long runGetTerminalPosition(final FusionDictionaryBufferInterface buffer,
- final String word, int index, boolean contained) {
+ private long runGetTerminalPosition(final BinaryDictReader reader, final String word, int index,
+ boolean contained) {
final int expectedFrequency = (UNIGRAM_FREQ + index) % 255;
long diff = -1;
int position = -1;
try {
final long now = System.nanoTime();
- position = BinaryDictIOUtils.getTerminalPosition(buffer, word);
+ position = BinaryDictIOUtils.getTerminalPosition(reader, word);
diff = System.nanoTime() - now;
} catch (IOException e) {
Log.e(TAG, "IOException while getTerminalPosition", e);
@@ -526,7 +526,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
}
assertEquals(FormatSpec.NOT_VALID_WORD != position, contained);
- if (contained) assertEquals(getWordFromBinary(buffer, position), word);
+ if (contained) assertEquals(getWordFromBinary(reader, position), word);
return diff;
}
@@ -547,29 +547,27 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
timeWritingDictToFile(file, dict, VERSION3_WITH_DYNAMIC_UPDATE);
final BinaryDictReader reader = new BinaryDictReader(file);
- FusionDictionaryBufferInterface buffer = null;
try {
- buffer = reader.openAndGetBuffer(
- new BinaryDictReader.FusionDictionaryBufferFromByteArrayFactory());
+ reader.openBuffer(new BinaryDictReader.FusionDictionaryBufferFromByteArrayFactory());
} catch (IOException e) {
// ignore
Log.e(TAG, "IOException while opening the buffer", e);
}
- assertNotNull("Can't get the buffer", buffer);
+ assertNotNull("Can't get the buffer", reader.getBuffer());
try {
// too long word
final String longWord = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
assertEquals(FormatSpec.NOT_VALID_WORD,
- BinaryDictIOUtils.getTerminalPosition(buffer, longWord));
+ BinaryDictIOUtils.getTerminalPosition(reader, longWord));
// null
assertEquals(FormatSpec.NOT_VALID_WORD,
- BinaryDictIOUtils.getTerminalPosition(buffer, null));
+ BinaryDictIOUtils.getTerminalPosition(reader, null));
// empty string
assertEquals(FormatSpec.NOT_VALID_WORD,
- BinaryDictIOUtils.getTerminalPosition(buffer, ""));
+ BinaryDictIOUtils.getTerminalPosition(reader, ""));
} catch (IOException e) {
} catch (UnsupportedFormatException e) {
}
@@ -577,7 +575,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
// Test a word that is contained within the dictionary.
long sum = 0;
for (int i = 0; i < sWords.size(); ++i) {
- final long time = runGetTerminalPosition(buffer, sWords.get(i), i, true);
+ final long time = runGetTerminalPosition(reader, sWords.get(i), i, true);
sum += time == -1 ? 0 : time;
}
Log.d(TAG, "per a search : " + (((double)sum) / sWords.size() / 1000000));
@@ -588,7 +586,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
for (int i = 0; i < 1000; ++i) {
final String word = generateWord(random, codePointSet);
if (sWords.indexOf(word) != -1) continue;
- runGetTerminalPosition(buffer, word, i, false);
+ runGetTerminalPosition(reader, word, i, false);
}
}
@@ -608,28 +606,27 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
timeWritingDictToFile(file, dict, VERSION3_WITH_DYNAMIC_UPDATE);
final BinaryDictReader reader = new BinaryDictReader(file);
- FusionDictionaryBufferInterface buffer = null;
try {
- buffer = reader.openAndGetBuffer(
+ reader.openBuffer(
new BinaryDictReader.FusionDictionaryBufferFromByteArrayFactory());
} catch (IOException e) {
// ignore
Log.e(TAG, "IOException while opening the buffer", e);
}
- assertNotNull("Can't get the buffer", buffer);
+ assertNotNull("Can't get the buffer", reader.getBuffer());
try {
MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD,
- BinaryDictIOUtils.getTerminalPosition(buffer, sWords.get(0)));
- DynamicBinaryDictIOUtils.deleteWord(buffer, sWords.get(0));
+ BinaryDictIOUtils.getTerminalPosition(reader, sWords.get(0)));
+ DynamicBinaryDictIOUtils.deleteWord(reader, sWords.get(0));
assertEquals(FormatSpec.NOT_VALID_WORD,
- BinaryDictIOUtils.getTerminalPosition(buffer, sWords.get(0)));
+ BinaryDictIOUtils.getTerminalPosition(reader, sWords.get(0)));
MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD,
- BinaryDictIOUtils.getTerminalPosition(buffer, sWords.get(5)));
- DynamicBinaryDictIOUtils.deleteWord(buffer, sWords.get(5));
+ BinaryDictIOUtils.getTerminalPosition(reader, sWords.get(5)));
+ DynamicBinaryDictIOUtils.deleteWord(reader, sWords.get(5));
assertEquals(FormatSpec.NOT_VALID_WORD,
- BinaryDictIOUtils.getTerminalPosition(buffer, sWords.get(5)));
+ BinaryDictIOUtils.getTerminalPosition(reader, sWords.get(5)));
} catch (IOException e) {
} catch (UnsupportedFormatException e) {
}
diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java
index 011d711de..300fd6a38 100644
--- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java
+++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java
@@ -21,8 +21,9 @@ import android.test.MoreAsserts;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;
-import com.android.inputmethod.latin.makedict.BinaryDictDecoder.ByteBufferWrapper;
import com.android.inputmethod.latin.makedict.BinaryDictDecoder.FusionDictionaryBufferInterface;
+import com.android.inputmethod.latin.makedict.BinaryDictReader.
+ FusionDictionaryBufferFromWritableByteBufferFactory;
import com.android.inputmethod.latin.makedict.FormatSpec.FileHeader;
import com.android.inputmethod.latin.makedict.FusionDictionary.PtNodeArray;
import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
@@ -33,8 +34,6 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.RandomAccessFile;
-import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
@@ -137,12 +136,12 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
private int getWordPosition(final File file, final String word) {
int position = FormatSpec.NOT_VALID_WORD;
+ final BinaryDictReader reader = new BinaryDictReader(file);
FileInputStream inStream = null;
try {
inStream = new FileInputStream(file);
- final FusionDictionaryBufferInterface buffer = new ByteBufferWrapper(
- inStream.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length()));
- position = BinaryDictIOUtils.getTerminalPosition(buffer, word);
+ reader.openBuffer(new BinaryDictReader.FusionDictionaryBufferFromByteBufferFactory());
+ position = BinaryDictIOUtils.getTerminalPosition(reader, word);
} catch (IOException e) {
} catch (UnsupportedFormatException e) {
} finally {
@@ -158,23 +157,13 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
}
private CharGroupInfo findWordFromFile(final File file, final String word) {
- FileInputStream inStream = null;
+ final BinaryDictReader reader = new BinaryDictReader(file);
CharGroupInfo info = null;
try {
- inStream = new FileInputStream(file);
- final FusionDictionaryBufferInterface buffer = new ByteBufferWrapper(
- inStream.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length()));
- info = BinaryDictIOUtils.findWordFromBuffer(buffer, word);
+ reader.openBuffer(new BinaryDictReader.FusionDictionaryBufferFromByteBufferFactory());
+ info = BinaryDictIOUtils.findWordByBinaryDictReader(reader, word);
} catch (IOException e) {
} catch (UnsupportedFormatException e) {
- } finally {
- if (inStream != null) {
- try {
- inStream.close();
- } catch (IOException e) {
- // do nothing
- }
- }
}
return info;
}
@@ -183,42 +172,33 @@ 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) {
- RandomAccessFile raFile = null;
+ final BinaryDictReader reader = new BinaryDictReader(file);
BufferedOutputStream outStream = null;
- FusionDictionaryBufferInterface buffer = null;
long amountOfTime = -1;
try {
- raFile = new RandomAccessFile(file, "rw");
- buffer = new ByteBufferWrapper(raFile.getChannel().map(
- FileChannel.MapMode.READ_WRITE, 0, file.length()));
+ reader.openBuffer(new FusionDictionaryBufferFromWritableByteBufferFactory());
outStream = new BufferedOutputStream(new FileOutputStream(file, true));
if (!exist) {
assertEquals(FormatSpec.NOT_VALID_WORD, getWordPosition(file, word));
}
final long now = System.nanoTime();
- DynamicBinaryDictIOUtils.insertWord(buffer, outStream, word, frequency, bigrams,
+ DynamicBinaryDictIOUtils.insertWord(reader, outStream, word, frequency, bigrams,
shortcuts, false, false);
amountOfTime = System.nanoTime() - now;
outStream.flush();
MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD, getWordPosition(file, word));
outStream.close();
- raFile.close();
} catch (IOException e) {
+ Log.e(TAG, "Raised an IOException while inserting a word", e);
} catch (UnsupportedFormatException e) {
+ Log.e(TAG, "Raised an UnsupportedFormatException error while inserting a word", e);
} finally {
if (outStream != null) {
try {
outStream.close();
} catch (IOException e) {
- // do nothing
- }
- }
- if (raFile != null) {
- try {
- raFile.close();
- } catch (IOException e) {
- // do nothing
+ Log.e(TAG, "Failed to close the output stream", e);
}
}
}
@@ -226,45 +206,28 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
}
private void deleteWord(final File file, final String word) {
- RandomAccessFile raFile = null;
- FusionDictionaryBufferInterface buffer = null;
+ final BinaryDictReader reader = new BinaryDictReader(file);
try {
- raFile = new RandomAccessFile(file, "rw");
- buffer = new ByteBufferWrapper(raFile.getChannel().map(
- FileChannel.MapMode.READ_WRITE, 0, file.length()));
- DynamicBinaryDictIOUtils.deleteWord(buffer, word);
+ reader.openBuffer(new FusionDictionaryBufferFromWritableByteBufferFactory());
+ DynamicBinaryDictIOUtils.deleteWord(reader, word);
} catch (IOException e) {
} catch (UnsupportedFormatException e) {
- } finally {
- if (raFile != null) {
- try {
- raFile.close();
- } catch (IOException e) {
- // do nothing
- }
- }
}
}
private void checkReverseLookup(final File file, final String word, final int position) {
- FileInputStream inStream = null;
+ final BinaryDictReader reader = new BinaryDictReader(file);
try {
- inStream = new FileInputStream(file);
- final FusionDictionaryBufferInterface buffer = new ByteBufferWrapper(
- inStream.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length()));
+ final FusionDictionaryBufferInterface buffer = reader.openAndGetBuffer(
+ new BinaryDictReader.FusionDictionaryBufferFromByteBufferFactory());
final FileHeader header = BinaryDictDecoder.readHeader(buffer);
- assertEquals(word, BinaryDictDecoder.getWordAtAddress(buffer, header.mHeaderSize,
- position - header.mHeaderSize, header.mFormatOptions).mWord);
+ assertEquals(word,
+ BinaryDictDecoder.getWordAtAddress(reader.getBuffer(), header.mHeaderSize,
+ position - header.mHeaderSize, header.mFormatOptions).mWord);
} catch (IOException e) {
+ Log.e(TAG, "Raised an IOException while looking up a word", e);
} catch (UnsupportedFormatException e) {
- } finally {
- if (inStream != null) {
- try {
- inStream.close();
- } catch (IOException e) {
- // do nothing
- }
- }
+ Log.e(TAG, "Raised an UnsupportedFormatException error while looking up a word", e);
}
}