diff options
Diffstat (limited to 'tests/src/com/android/inputmethod/latin')
7 files changed, 258 insertions, 147 deletions
diff --git a/tests/src/com/android/inputmethod/latin/ExpandableDictionaryTests.java b/tests/src/com/android/inputmethod/latin/ExpandableDictionaryTests.java new file mode 100644 index 000000000..ecf3af736 --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/ExpandableDictionaryTests.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin; + +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.SmallTest; + +/** + * Unit test for ExpandableDictionary + */ +@SmallTest +public class ExpandableDictionaryTests extends AndroidTestCase { + + private final static int UNIGRAM_FREQ = 50; + + public void testAddWordAndGetWordFrequency() { + final ExpandableDictionary dict = new ExpandableDictionary(Dictionary.TYPE_USER); + + // Add words + dict.addWord("abcde", "abcde", UNIGRAM_FREQ); + dict.addWord("abcef", null, UNIGRAM_FREQ + 1); + + // Check words + assertFalse(dict.isValidWord("abcde")); + assertEquals(UNIGRAM_FREQ, dict.getWordFrequency("abcde")); + assertTrue(dict.isValidWord("abcef")); + assertEquals(UNIGRAM_FREQ+1, dict.getWordFrequency("abcef")); + + dict.addWord("abc", null, UNIGRAM_FREQ + 2); + assertTrue(dict.isValidWord("abc")); + assertEquals(UNIGRAM_FREQ + 2, dict.getWordFrequency("abc")); + + // Add existing word with lower frequency + dict.addWord("abc", null, UNIGRAM_FREQ); + assertEquals(UNIGRAM_FREQ + 2, dict.getWordFrequency("abc")); + + // Add existing word with higher frequency + dict.addWord("abc", null, UNIGRAM_FREQ + 3); + assertEquals(UNIGRAM_FREQ + 3, dict.getWordFrequency("abc")); + } +} diff --git a/tests/src/com/android/inputmethod/latin/InputLogicTests.java b/tests/src/com/android/inputmethod/latin/InputLogicTests.java index 6cc4befae..fe92be618 100644 --- a/tests/src/com/android/inputmethod/latin/InputLogicTests.java +++ b/tests/src/com/android/inputmethod/latin/InputLogicTests.java @@ -305,5 +305,33 @@ public class InputLogicTests extends InputTestsBase { assertEquals("resume suggestion on backspace", 8, BaseInputConnection.getComposingSpanEnd(mEditText.getText())); } + + private void helperTestComposing(final String wordToType, final boolean shouldBeComposing) { + mEditText.setText(""); + type(wordToType); + assertEquals("start composing inside text", shouldBeComposing ? 0 : -1, + BaseInputConnection.getComposingSpanStart(mEditText.getText())); + assertEquals("start composing inside text", shouldBeComposing ? wordToType.length() : -1, + BaseInputConnection.getComposingSpanEnd(mEditText.getText())); + } + + public void testStartComposing() { + // Should start composing on a letter + helperTestComposing("a", true); + type(" "); // To reset the composing state + // Should not start composing on quote + helperTestComposing("'", false); + type(" "); + helperTestComposing("'-", false); + type(" "); + // Should not start composing on dash + helperTestComposing("-", false); + type(" "); + helperTestComposing("-'", false); + type(" "); + helperTestComposing("a-", true); + type(" "); + helperTestComposing("a'", true); + } // TODO: Add some tests for non-BMP characters } diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java index f3ca2b147..72ec5a302 100644 --- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java +++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java @@ -22,29 +22,30 @@ import android.test.suitebuilder.annotation.LargeTest; import android.util.Log; import android.util.SparseArray; +import com.android.inputmethod.latin.makedict.BinaryDictDecoderUtils.CharEncoding; import com.android.inputmethod.latin.makedict.BinaryDictDecoderUtils.DictBuffer; import com.android.inputmethod.latin.makedict.FormatSpec.FileHeader; -import com.android.inputmethod.latin.makedict.FusionDictionary.CharGroup; +import com.android.inputmethod.latin.makedict.FusionDictionary.PtNode; import com.android.inputmethod.latin.makedict.FusionDictionary.PtNodeArray; import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString; +import com.android.inputmethod.latin.utils.ByteArrayDictBuffer; import com.android.inputmethod.latin.utils.CollectionUtils; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Map.Entry; import java.util.Random; import java.util.Set; +import java.util.TreeMap; /** - * Unit tests for BinaryDictDecoderUtils and BinaryDictEncoder. + * Unit tests for BinaryDictDecoderUtils and BinaryDictEncoderUtils. */ @LargeTest public class BinaryDictDecoderEncoderTests extends AndroidTestCase { @@ -54,16 +55,19 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { private static final int UNIGRAM_FREQ = 10; private static final int BIGRAM_FREQ = 50; private static final int TOLERANCE_OF_BIGRAM_FREQ = 5; + private static final int NUM_OF_NODES_HAVING_SHORTCUTS = 50; + private static final int NUM_OF_SHORTCUTS = 5; private static final int USE_BYTE_ARRAY = 1; private static final int USE_BYTE_BUFFER = 2; - private static final List<String> sWords = CollectionUtils.newArrayList(); + private static final ArrayList<String> sWords = CollectionUtils.newArrayList(); private static final SparseArray<List<Integer>> sEmptyBigrams = CollectionUtils.newSparseArray(); private static final SparseArray<List<Integer>> sStarBigrams = CollectionUtils.newSparseArray(); private static final SparseArray<List<Integer>> sChainBigrams = CollectionUtils.newSparseArray(); + private static final HashMap<String, List<String>> sShortcuts = CollectionUtils.newHashMap(); private static final FormatSpec.FormatOptions VERSION2 = new FormatSpec.FormatOptions(2); private static final FormatSpec.FormatOptions VERSION3_WITHOUT_DYNAMIC_UPDATE = @@ -96,6 +100,16 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { for (int i = 1; i < sWords.size(); ++i) { sStarBigrams.get(0).add(i); } + + sShortcuts.clear(); + for (int i = 0; i < NUM_OF_NODES_HAVING_SHORTCUTS; ++i) { + final int from = Math.abs(random.nextInt()) % sWords.size(); + sShortcuts.put(sWords.get(from), new ArrayList<String>()); + for (int j = 0; j < NUM_OF_SHORTCUTS; ++j) { + final int to = Math.abs(random.nextInt()) % sWords.size(); + sShortcuts.get(sWords.get(from)).add(sWords.get(to)); + } + } } private int[] generateCodePointSet(final int codePointSetSize, final Random random) { @@ -105,7 +119,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { if (r < 0) continue; // Don't insert 0~0x20, but insert any other code point. // Code points are in the range 0~0x10FFFF. - final int candidateCodePoint = (int)(0x20 + r % (Character.MAX_CODE_POINT - 0x20)); + final int candidateCodePoint = 0x20 + r % (Character.MAX_CODE_POINT - 0x20); // Code points between MIN_ and MAX_SURROGATE are not valid on their own. if (candidateCodePoint >= Character.MIN_SURROGATE && candidateCodePoint <= Character.MAX_SURROGATE) continue; @@ -118,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; } /** @@ -165,7 +177,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { * Adds unigrams to the dictionary. */ private void addUnigrams(final int number, final FusionDictionary dict, - final List<String> words, final Map<String, List<String>> shortcutMap) { + final List<String> words, final HashMap<String, List<String>> shortcutMap) { for (int i = 0; i < number; ++i) { final String word = words.get(i); final ArrayList<WeightedString> shortcuts = CollectionUtils.newArrayList(); @@ -204,17 +216,14 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { long now = -1, diff = -1; try { - final FileOutputStream out = new FileOutputStream(file); + final DictEncoder dictEncoder = new Ver3DictEncoder(file); now = System.currentTimeMillis(); // If you need to dump the dict to a textual file, uncomment the line below and the // function above // dumpToCombinedFileForDebug(file, "/tmp/foo"); - BinaryDictEncoder.writeDictionaryBinary(out, dict, formatOptions); + dictEncoder.writeDictionary(dict, formatOptions); diff = System.currentTimeMillis() - now; - - out.flush(); - out.close(); } catch (IOException e) { Log.e(TAG, "IO exception while writing file", e); } catch (UnsupportedFormatException e) { @@ -225,33 +234,35 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { } private void checkDictionary(final FusionDictionary dict, final List<String> words, - final SparseArray<List<Integer>> bigrams, final Map<String, List<String>> shortcutMap) { + final SparseArray<List<Integer>> bigrams, + final HashMap<String, List<String>> shortcutMap) { assertNotNull(dict); // check unigram for (final String word : words) { - final CharGroup cg = FusionDictionary.findWordInTree(dict.mRootNodeArray, word); - assertNotNull(cg); + final PtNode ptNode = FusionDictionary.findWordInTree(dict.mRootNodeArray, word); + assertNotNull(ptNode); } // check bigram for (int i = 0; i < bigrams.size(); ++i) { final int w1 = bigrams.keyAt(i); for (final int w2 : bigrams.valueAt(i)) { - final CharGroup cg = FusionDictionary.findWordInTree(dict.mRootNodeArray, + final PtNode ptNode = FusionDictionary.findWordInTree(dict.mRootNodeArray, words.get(w1)); - assertNotNull(words.get(w1) + "," + words.get(w2), cg.getBigram(words.get(w2))); + assertNotNull(words.get(w1) + "," + words.get(w2), ptNode.getBigram(words.get(w2))); } } // check shortcut if (shortcutMap != null) { - for (final Map.Entry<String, List<String>> entry : shortcutMap.entrySet()) { - final CharGroup group = FusionDictionary.findWordInTree(dict.mRootNodeArray, + for (final Entry<String, List<String>> entry : shortcutMap.entrySet()) { + assertTrue(words.contains(entry.getKey())); + final PtNode ptNode = FusionDictionary.findWordInTree(dict.mRootNodeArray, entry.getKey()); for (final String word : entry.getValue()) { assertNotNull("shortcut not found: " + entry.getKey() + ", " + word, - group.getShortcut(word)); + ptNode.getShortcut(word)); } } } @@ -268,17 +279,17 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { // Tests for readDictionaryBinary and writeDictionaryBinary private long timeReadingAndCheckDict(final File file, final List<String> words, - final SparseArray<List<Integer>> bigrams, final Map<String, List<String>> shortcutMap, - final int bufferType) { + final SparseArray<List<Integer>> bigrams, + final HashMap<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); @@ -292,7 +303,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { // Tests for readDictionaryBinary and writeDictionaryBinary private String runReadAndWrite(final List<String> words, - final SparseArray<List<Integer>> bigrams, final Map<String, List<String>> shortcuts, + final SparseArray<List<Integer>> bigrams, final HashMap<String, List<String>> shortcuts, final int bufferType, final FormatSpec.FormatOptions formatOptions, final String message) { File file = null; @@ -325,6 +336,28 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { formatOptions, "chain")); results.add(runReadAndWrite(sWords, sStarBigrams, null /* shortcuts */, bufferType, formatOptions, "star")); + results.add(runReadAndWrite(sWords, sEmptyBigrams, sShortcuts, bufferType, formatOptions, + "unigram with shortcuts")); + results.add(runReadAndWrite(sWords, sChainBigrams, sShortcuts, bufferType, formatOptions, + "chain with shortcuts")); + results.add(runReadAndWrite(sWords, sStarBigrams, sShortcuts, bufferType, formatOptions, + "star with shortcuts")); + } + + // Unit test for CharEncoding.readString and CharEncoding.writeString. + public void testCharEncoding() { + // the max length of a word in sWords is less than 50. + // See generateWords. + final byte[] buffer = new byte[50 * 3]; + final DictBuffer dictBuffer = new ByteArrayDictBuffer(buffer); + for (final String word : sWords) { + Log.d("testReadAndWriteString", "write : " + word); + Arrays.fill(buffer, (byte)0); + CharEncoding.writeString(buffer, 0, word); + dictBuffer.position(0); + final String str = CharEncoding.readString(dictBuffer); + assertEquals(word, str); + } } public void testReadAndWriteWithByteBuffer() { @@ -355,9 +388,9 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { private void checkWordMap(final List<String> expectedWords, final SparseArray<List<Integer>> expectedBigrams, - final Map<Integer, String> resultWords, - final Map<Integer, Integer> resultFrequencies, - final Map<Integer, ArrayList<PendingAttribute>> resultBigrams) { + final TreeMap<Integer, String> resultWords, + final TreeMap<Integer, Integer> resultFrequencies, + final TreeMap<Integer, ArrayList<PendingAttribute>> resultBigrams) { // check unigrams final Set<String> actualWordsSet = new HashSet<String>(resultWords.values()); final Set<String> expectedWordsSet = new HashSet<String>(expectedWords); @@ -368,7 +401,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { } // check bigrams - final Map<String, List<String>> expBigrams = new HashMap<String, List<String>>(); + final HashMap<String, List<String>> expBigrams = new HashMap<String, List<String>>(); for (int i = 0; i < expectedBigrams.size(); ++i) { final String word1 = expectedWords.get(expectedBigrams.keyAt(i)); for (int w2 : expectedBigrams.valueAt(i)) { @@ -379,7 +412,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { } } - final Map<String, List<String>> actBigrams = new HashMap<String, List<String>>(); + final HashMap<String, List<String>> actBigrams = new HashMap<String, List<String>>(); for (Entry<Integer, ArrayList<PendingAttribute>> entry : resultBigrams.entrySet()) { final String word1 = resultWords.get(entry.getKey()); final int unigramFreq = resultFrequencies.get(entry.getKey()); @@ -403,19 +436,18 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { final SparseArray<List<Integer>> bigrams, final int bufferType) { FileInputStream inStream = null; - final Map<Integer, String> resultWords = CollectionUtils.newTreeMap(); - final Map<Integer, ArrayList<PendingAttribute>> resultBigrams = + final TreeMap<Integer, String> resultWords = CollectionUtils.newTreeMap(); + final TreeMap<Integer, ArrayList<PendingAttribute>> resultBigrams = CollectionUtils.newTreeMap(); - final Map<Integer, Integer> resultFreqs = CollectionUtils.newTreeMap(); + final TreeMap<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, - resultBigrams); + dictDecoder.readUnigramsAndBigramsBinary(resultWords, resultFreqs, resultBigrams); diff = System.currentTimeMillis() - now; } catch (IOException e) { Log.e(TAG, "IOException", e); @@ -435,7 +467,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { return diff; } - private String runReadUnigramsAndBigramsBinary(final List<String> words, + private String runReadUnigramsAndBigramsBinary(final ArrayList<String> words, final SparseArray<List<Integer>> bigrams, final int bufferType, final FormatSpec.FormatOptions formatOptions, final String message) { File file = null; @@ -464,8 +496,8 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { + " : " + message + " : " + outputOptions(bufferType, formatOptions); } - private void runReadUnigramsAndBigramsTests(final List<String> results, final int bufferType, - final FormatSpec.FormatOptions formatOptions) { + private void runReadUnigramsAndBigramsTests(final ArrayList<String> results, + final int bufferType, final FormatSpec.FormatOptions formatOptions) { results.add(runReadUnigramsAndBigramsBinary(sWords, sEmptyBigrams, bufferType, formatOptions, "unigram")); results.add(runReadUnigramsAndBigramsBinary(sWords, sChainBigrams, bufferType, @@ -475,7 +507,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { } public void testReadUnigramsAndBigramsBinaryWithByteBuffer() { - final List<String> results = CollectionUtils.newArrayList(); + final ArrayList<String> results = CollectionUtils.newArrayList(); runReadUnigramsAndBigramsTests(results, USE_BYTE_BUFFER, VERSION2); runReadUnigramsAndBigramsTests(results, USE_BYTE_BUFFER, VERSION3_WITHOUT_DYNAMIC_UPDATE); @@ -487,7 +519,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { } public void testReadUnigramsAndBigramsBinaryWithByteArray() { - final List<String> results = CollectionUtils.newArrayList(); + final ArrayList<String> results = CollectionUtils.newArrayList(); runReadUnigramsAndBigramsTests(results, USE_BYTE_ARRAY, VERSION2); runReadUnigramsAndBigramsTests(results, USE_BYTE_ARRAY, VERSION3_WITHOUT_DYNAMIC_UPDATE); @@ -512,8 +544,8 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { return null; } if (fileHeader == null) return null; - return BinaryDictDecoderUtils.getWordAtAddress(dictBuffer, fileHeader.mHeaderSize, - address - fileHeader.mHeaderSize, fileHeader.mFormatOptions).mWord; + return BinaryDictDecoderUtils.getWordAtPosition(dictDecoder, fileHeader.mHeaderSize, + address, fileHeader.mFormatOptions).mWord; } private long runGetTerminalPosition(final Ver3DictDecoder dictDecoder, final String word, @@ -523,7 +555,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { int position = -1; try { final long now = System.nanoTime(); - position = BinaryDictIOUtils.getTerminalPosition(dictDecoder, word); + position = dictDecoder.getTerminalPosition(word); diff = System.nanoTime() - now; } catch (IOException e) { Log.e(TAG, "IOException while getTerminalPosition", e); @@ -552,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); @@ -565,16 +596,13 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { try { // too long word final String longWord = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; - assertEquals(FormatSpec.NOT_VALID_WORD, - BinaryDictIOUtils.getTerminalPosition(dictDecoder, longWord)); + assertEquals(FormatSpec.NOT_VALID_WORD, dictDecoder.getTerminalPosition(longWord)); // null - assertEquals(FormatSpec.NOT_VALID_WORD, - BinaryDictIOUtils.getTerminalPosition(dictDecoder, null)); + assertEquals(FormatSpec.NOT_VALID_WORD, dictDecoder.getTerminalPosition(null)); // empty string - assertEquals(FormatSpec.NOT_VALID_WORD, - BinaryDictIOUtils.getTerminalPosition(dictDecoder, "")); + assertEquals(FormatSpec.NOT_VALID_WORD, dictDecoder.getTerminalPosition("")); } catch (IOException e) { } catch (UnsupportedFormatException e) { } @@ -613,10 +641,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); @@ -625,16 +652,16 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase { try { MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD, - BinaryDictIOUtils.getTerminalPosition(dictDecoder, sWords.get(0))); + dictDecoder.getTerminalPosition(sWords.get(0))); DynamicBinaryDictIOUtils.deleteWord(dictDecoder, sWords.get(0)); assertEquals(FormatSpec.NOT_VALID_WORD, - BinaryDictIOUtils.getTerminalPosition(dictDecoder, sWords.get(0))); + dictDecoder.getTerminalPosition(sWords.get(0))); MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD, - BinaryDictIOUtils.getTerminalPosition(dictDecoder, sWords.get(5))); + dictDecoder.getTerminalPosition(sWords.get(5))); DynamicBinaryDictIOUtils.deleteWord(dictDecoder, sWords.get(5)); assertEquals(FormatSpec.NOT_VALID_WORD, - BinaryDictIOUtils.getTerminalPosition(dictDecoder, sWords.get(5))); + dictDecoder.getTerminalPosition(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 74c20a38c..8e0c6dfe2 100644 --- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java +++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java @@ -22,8 +22,6 @@ import android.test.suitebuilder.annotation.LargeTest; import android.util.Log; import com.android.inputmethod.latin.makedict.BinaryDictDecoderUtils.DictBuffer; -import com.android.inputmethod.latin.makedict.DictDecoder. - DictionaryBufferFromWritableByteBufferFactory; import com.android.inputmethod.latin.makedict.FormatSpec.FileHeader; import com.android.inputmethod.latin.makedict.FusionDictionary.PtNodeArray; import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString; @@ -31,7 +29,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; @@ -87,8 +84,8 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase { return builder.toString(); } - private static void printCharGroup(final CharGroupInfo info) { - Log.d(TAG, " CharGroup at " + info.mOriginalAddress); + private static void printPtNode(final PtNodeInfo info) { + Log.d(TAG, " PtNode at " + info.mOriginalAddress); Log.d(TAG, " flags = " + info.mFlags); Log.d(TAG, " parentAddress = " + info.mParentAddress); Log.d(TAG, " characters = " + new String(info.mCharacters, 0, @@ -112,15 +109,16 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase { Log.d(TAG, " end address = " + info.mEndAddress); } - private static void printNode(final DictBuffer dictBuffer, + private static void printNode(final Ver3DictDecoder dictDecoder, final FormatSpec.FormatOptions formatOptions) { + final DictBuffer dictBuffer = dictDecoder.getDictBuffer(); Log.d(TAG, "Node at " + dictBuffer.position()); - final int count = BinaryDictDecoderUtils.readCharGroupCount(dictBuffer); - Log.d(TAG, " charGroupCount = " + count); + final int count = BinaryDictDecoderUtils.readPtNodeCount(dictBuffer); + Log.d(TAG, " ptNodeCount = " + count); for (int i = 0; i < count; ++i) { - final CharGroupInfo currentInfo = BinaryDictDecoderUtils.readCharGroup(dictBuffer, - dictBuffer.position(), formatOptions); - printCharGroup(currentInfo); + final PtNodeInfo currentInfo = dictDecoder.readPtNode(dictBuffer.position(), + formatOptions); + printPtNode(currentInfo); } if (formatOptions.mSupportsDynamicUpdate) { final int forwardLinkAddress = dictBuffer.readUnsignedInt24(); @@ -128,45 +126,56 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase { } } + @SuppressWarnings("unused") private static void printBinaryFile(final Ver3DictDecoder dictDecoder) throws IOException, UnsupportedFormatException { final FileHeader fileHeader = dictDecoder.readHeader(); - final DictBuffer buffer = dictDecoder.getDictBuffer(); - while (buffer.position() < buffer.limit()) { - printNode(buffer, fileHeader.mFormatOptions); + final DictBuffer dictBuffer = dictDecoder.getDictBuffer(); + while (dictBuffer.position() < dictBuffer.limit()) { + printNode(dictDecoder, fileHeader.mFormatOptions); } } 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()); - position = BinaryDictIOUtils.getTerminalPosition(dictDecoder, word); + final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file); + position = dictDecoder.getTerminalPosition(word); } catch (IOException e) { } catch (UnsupportedFormatException e) { - } finally { - if (inStream != null) { - try { - inStream.close(); - } catch (IOException e) { - // do nothing - } - } } return position; } - private CharGroupInfo findWordFromFile(final File file, final String word) { + /** + * Find a word using the Ver3DictDecoder. + * + * @param dictDecoder the dict decoder + * @param word the word searched + * @return the found ptNodeInfo + * @throws IOException + * @throws UnsupportedFormatException + */ + private static PtNodeInfo findWordByBinaryDictReader(final Ver3DictDecoder dictDecoder, + final String word) throws IOException, UnsupportedFormatException { + int position = dictDecoder.getTerminalPosition(word); + final DictBuffer dictBuffer = dictDecoder.getDictBuffer(); + if (position != FormatSpec.NOT_VALID_WORD) { + dictBuffer.position(0); + final FileHeader header = dictDecoder.readHeader(); + dictBuffer.position(position); + return dictDecoder.readPtNode(position, header.mFormatOptions); + } + return null; + } + + private PtNodeInfo findWordFromFile(final File file, final String word) { final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file); - CharGroupInfo info = null; + PtNodeInfo info = null; try { - dictDecoder.openDictBuffer( - new Ver3DictDecoder.DictionaryBufferFromReadOnlyByteBufferFactory()); - info = BinaryDictIOUtils.findWordByBinaryDictReader(dictDecoder, word); + dictDecoder.openDictBuffer(); + info = findWordByBinaryDictReader(dictDecoder, word); } catch (IOException e) { } catch (UnsupportedFormatException e) { } @@ -177,11 +186,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) { @@ -211,9 +221,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) { @@ -221,15 +232,13 @@ 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.getWordAtAddress(dictDecoder.getDictBuffer(), - fileHeader.mHeaderSize, position - fileHeader.mHeaderSize, - fileHeader.mFormatOptions).mWord); + BinaryDictDecoderUtils.getWordAtPosition(dictDecoder, fileHeader.mHeaderSize, + position, fileHeader.mFormatOptions).mWord); } catch (IOException e) { Log.e(TAG, "Raised an IOException while looking up a word", e); } catch (UnsupportedFormatException e) { @@ -252,9 +261,8 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase { dict.add("abcd", 10, null, false); try { - final FileOutputStream out = new FileOutputStream(file); - BinaryDictEncoder.writeDictionaryBinary(out, dict, FORMAT_OPTIONS); - out.close(); + final DictEncoder dictEncoder = new Ver3DictEncoder(file); + dictEncoder.writeDictionary(dict, FORMAT_OPTIONS); } catch (IOException e) { fail("IOException while writing an initial dictionary : " + e); } catch (UnsupportedFormatException e) { @@ -304,9 +312,8 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase { dict.add("efgh", 15, null, false); try { - final FileOutputStream out = new FileOutputStream(file); - BinaryDictEncoder.writeDictionaryBinary(out, dict, FORMAT_OPTIONS); - out.close(); + final DictEncoder dictEncoder = new Ver3DictEncoder(file); + dictEncoder.writeDictionary(dict, FORMAT_OPTIONS); } catch (IOException e) { fail("IOException while writing an initial dictionary : " + e); } catch (UnsupportedFormatException e) { @@ -319,7 +326,7 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase { insertAndCheckWord(file, "banana", 0, false, null, null); insertAndCheckWord(file, "recursive", 60, true, banana, null); - final CharGroupInfo info = findWordFromFile(file, "recursive"); + final PtNodeInfo info = findWordFromFile(file, "recursive"); int bananaPos = getWordPosition(file, "banana"); assertNotNull(info.mBigrams); assertEquals(info.mBigrams.size(), 1); @@ -342,9 +349,8 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase { dict.add("initial", 10, null, false); try { - final FileOutputStream out = new FileOutputStream(file); - BinaryDictEncoder.writeDictionaryBinary(out, dict, FORMAT_OPTIONS); - out.close(); + final DictEncoder dictEncoder = new Ver3DictEncoder(file); + dictEncoder.writeDictionary(dict, FORMAT_OPTIONS); } catch (IOException e) { assertTrue(false); } catch (UnsupportedFormatException e) { 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/personalization/UserHistoryDictionaryTests.java b/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java index 99ccb1a75..1fd1b8a81 100644 --- a/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java +++ b/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java @@ -86,7 +86,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { final Random random) { final List<String> words = generateWords(numberOfWords, random); final UserHistoryPredictionDictionary dict = - PersonalizationDictionaryHelper.getUserHistoryPredictionDictionary(getContext(), + PersonalizationHelper.getUserHistoryPredictionDictionary(getContext(), testFilenameSuffix /* locale */, mPrefs); // Add random words to the user history dictionary. addToDict(dict, words); diff --git a/tests/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtilsTests.java b/tests/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtilsTests.java index eca12c0d8..72b9478d4 100644 --- a/tests/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtilsTests.java +++ b/tests/src/com/android/inputmethod/latin/utils/UserHistoryDictIOUtilsTests.java @@ -21,17 +21,19 @@ 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; -import com.android.inputmethod.latin.makedict.FusionDictionary.CharGroup; +import com.android.inputmethod.latin.makedict.FusionDictionary.PtNode; import com.android.inputmethod.latin.makedict.Ver3DictDecoder; +import com.android.inputmethod.latin.makedict.Ver3DictEncoder; import com.android.inputmethod.latin.personalization.UserHistoryDictionaryBigramList; import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.BigramDictionaryInterface; import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.OnAddWordListener; import java.io.File; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -87,12 +89,12 @@ public class UserHistoryDictIOUtilsTests extends AndroidTestCase private void checkWordInFusionDict(final FusionDictionary dict, final String word, final ArrayList<String> expectedBigrams) { - final CharGroup group = FusionDictionary.findWordInTree(dict.mRootNodeArray, word); - assertNotNull(group); - assertTrue(group.isTerminal()); + final PtNode ptNode = FusionDictionary.findWordInTree(dict.mRootNodeArray, word); + assertNotNull(ptNode); + assertTrue(ptNode.isTerminal()); for (final String bigram : expectedBigrams) { - assertNotNull(group.getBigram(bigram)); + assertNotNull(ptNode.getBigram(bigram)); } } @@ -136,21 +138,14 @@ public class UserHistoryDictIOUtilsTests extends AndroidTestCase private void writeDictToFile(final File file, final UserHistoryDictionaryBigramList bigramList) { - try { - final FileOutputStream out = new FileOutputStream(file); - UserHistoryDictIOUtils.writeDictionaryBinary(out, this, bigramList, FORMAT_OPTIONS); - out.flush(); - out.close(); - } catch (IOException e) { - Log.e(TAG, "IO exception while writing file", e); - } + final DictEncoder dictEncoder = new Ver3DictEncoder(file); + UserHistoryDictIOUtils.writeDictionary(dictEncoder, this, bigramList, FORMAT_OPTIONS); } 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) { |