diff options
Diffstat (limited to 'tests/src/com/android/inputmethod')
-rw-r--r-- | tests/src/com/android/inputmethod/latin/StringUtilsTests.java | 67 | ||||
-rw-r--r-- | tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java | 107 |
2 files changed, 139 insertions, 35 deletions
diff --git a/tests/src/com/android/inputmethod/latin/StringUtilsTests.java b/tests/src/com/android/inputmethod/latin/StringUtilsTests.java index 00cca9d3b..be3494dc7 100644 --- a/tests/src/com/android/inputmethod/latin/StringUtilsTests.java +++ b/tests/src/com/android/inputmethod/latin/StringUtilsTests.java @@ -93,22 +93,24 @@ public class StringUtilsTests extends AndroidTestCase { } private void onePathForCaps(final CharSequence cs, final int expectedResult, final int mask, - final Locale l) { + final Locale l, final boolean hasSpaceBefore) { int oneTimeResult = expectedResult & mask; - assertEquals("After >" + cs + "<", oneTimeResult, StringUtils.getCapsMode(cs, mask, l)); + assertEquals("After >" + cs + "<", oneTimeResult, + StringUtils.getCapsMode(cs, mask, l, hasSpaceBefore)); } - private void allPathsForCaps(final CharSequence cs, final int expectedResult, final Locale l) { + private void allPathsForCaps(final CharSequence cs, final int expectedResult, final Locale l, + final boolean hasSpaceBefore) { final int c = TextUtils.CAP_MODE_CHARACTERS; final int w = TextUtils.CAP_MODE_WORDS; final int s = TextUtils.CAP_MODE_SENTENCES; - onePathForCaps(cs, expectedResult, c | w | s, l); - onePathForCaps(cs, expectedResult, w | s, l); - onePathForCaps(cs, expectedResult, c | s, l); - onePathForCaps(cs, expectedResult, c | w, l); - onePathForCaps(cs, expectedResult, c, l); - onePathForCaps(cs, expectedResult, w, l); - onePathForCaps(cs, expectedResult, s, l); + onePathForCaps(cs, expectedResult, c | w | s, l, hasSpaceBefore); + onePathForCaps(cs, expectedResult, w | s, l, hasSpaceBefore); + onePathForCaps(cs, expectedResult, c | s, l, hasSpaceBefore); + onePathForCaps(cs, expectedResult, c | w, l, hasSpaceBefore); + onePathForCaps(cs, expectedResult, c, l, hasSpaceBefore); + onePathForCaps(cs, expectedResult, w, l, hasSpaceBefore); + onePathForCaps(cs, expectedResult, s, l, hasSpaceBefore); } public void testGetCapsMode() { @@ -116,26 +118,31 @@ public class StringUtilsTests extends AndroidTestCase { final int w = TextUtils.CAP_MODE_WORDS; final int s = TextUtils.CAP_MODE_SENTENCES; Locale l = Locale.ENGLISH; - allPathsForCaps("", c | w | s, l); - allPathsForCaps("Word", c, l); - allPathsForCaps("Word.", c, l); - allPathsForCaps("Word ", c | w, l); - allPathsForCaps("Word. ", c | w | s, l); - allPathsForCaps("Word..", c, l); - allPathsForCaps("Word.. ", c | w | s, l); - allPathsForCaps("Word... ", c | w | s, l); - allPathsForCaps("Word ... ", c | w | s, l); - allPathsForCaps("Word . ", c | w, l); - allPathsForCaps("In the U.S ", c | w, l); - allPathsForCaps("In the U.S. ", c | w, l); - allPathsForCaps("Some stuff (e.g. ", c | w, l); - allPathsForCaps("In the U.S.. ", c | w | s, l); - allPathsForCaps("\"Word.\" ", c | w | s, l); - allPathsForCaps("\"Word\". ", c | w | s, l); - allPathsForCaps("\"Word\" ", c | w, l); + allPathsForCaps("", c | w | s, l, false); + allPathsForCaps("Word", c, l, false); + allPathsForCaps("Word.", c, l, false); + allPathsForCaps("Word ", c | w, l, false); + allPathsForCaps("Word. ", c | w | s, l, false); + allPathsForCaps("Word..", c, l, false); + allPathsForCaps("Word.. ", c | w | s, l, false); + allPathsForCaps("Word... ", c | w | s, l, false); + allPathsForCaps("Word ... ", c | w | s, l, false); + allPathsForCaps("Word . ", c | w, l, false); + allPathsForCaps("In the U.S ", c | w, l, false); + allPathsForCaps("In the U.S. ", c | w, l, false); + allPathsForCaps("Some stuff (e.g. ", c | w, l, false); + allPathsForCaps("In the U.S.. ", c | w | s, l, false); + allPathsForCaps("\"Word.\" ", c | w | s, l, false); + allPathsForCaps("\"Word\". ", c | w | s, l, false); + allPathsForCaps("\"Word\" ", c | w, l, false); + + // Test for phantom space + allPathsForCaps("Word", c | w, l, true); + allPathsForCaps("Word.", c | w | s, l, true); + l = Locale.FRENCH; - allPathsForCaps("\"Word.\" ", c | w, l); - allPathsForCaps("\"Word\". ", c | w | s, l); - allPathsForCaps("\"Word\" ", c | w, l); + allPathsForCaps("\"Word.\" ", c | w, l, false); + allPathsForCaps("\"Word\". ", c | w | s, l, false); + allPathsForCaps("\"Word\" ", c | w, l, false); } } diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java index 328784b1a..24776d536 100644 --- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java +++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java @@ -19,7 +19,7 @@ package com.android.inputmethod.latin.makedict; import com.android.inputmethod.latin.CollectionUtils; import com.android.inputmethod.latin.UserHistoryDictIOUtils; import com.android.inputmethod.latin.makedict.BinaryDictInputOutput.FusionDictionaryBufferInterface; -import com.android.inputmethod.latin.makedict.FormatSpec; +import com.android.inputmethod.latin.makedict.FormatSpec.FileHeader; import com.android.inputmethod.latin.makedict.FusionDictionary.CharGroup; import com.android.inputmethod.latin.makedict.FusionDictionary.Node; import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString; @@ -65,9 +65,12 @@ public class BinaryDictIOTests extends AndroidTestCase { private static final FormatSpec.FormatOptions VERSION2 = new FormatSpec.FormatOptions(2); private static final FormatSpec.FormatOptions VERSION3_WITHOUT_PARENTADDRESS = - new FormatSpec.FormatOptions(3, false); + new FormatSpec.FormatOptions(3, false /* hasParentAddress */); private static final FormatSpec.FormatOptions VERSION3_WITH_PARENTADDRESS = - new FormatSpec.FormatOptions(3, true); + new FormatSpec.FormatOptions(3, true /* hasParentAddress */); + private static final FormatSpec.FormatOptions VERSION3_WITH_LINKEDLIST_NODE = + new FormatSpec.FormatOptions(3, true /* hasParentAddress */, + true /* hasLinkedListNode */); private static final String[] CHARACTERS = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", @@ -236,7 +239,8 @@ public class BinaryDictIOTests extends AndroidTestCase { String result = " : buffer type = " + ((bufferType == USE_BYTE_BUFFER) ? "byte buffer" : "byte array"); result += " : version = " + formatOptions.mVersion; - return result + ", hasParentAddress = " + formatOptions.mHasParentAddress; + return result + ", hasParentAddress = " + formatOptions.mHasParentAddress + + ", hasLinkedListNode = " + formatOptions.mHasLinkedListNode; } // Tests for readDictionaryBinary and writeDictionaryBinary @@ -305,6 +309,7 @@ public class BinaryDictIOTests extends AndroidTestCase { runReadAndWriteTests(results, USE_BYTE_BUFFER, VERSION2); runReadAndWriteTests(results, USE_BYTE_BUFFER, VERSION3_WITHOUT_PARENTADDRESS); runReadAndWriteTests(results, USE_BYTE_BUFFER, VERSION3_WITH_PARENTADDRESS); + runReadAndWriteTests(results, USE_BYTE_BUFFER, VERSION3_WITH_LINKEDLIST_NODE); for (final String result : results) { Log.d(TAG, result); @@ -317,6 +322,7 @@ public class BinaryDictIOTests extends AndroidTestCase { runReadAndWriteTests(results, USE_BYTE_ARRAY, VERSION2); runReadAndWriteTests(results, USE_BYTE_ARRAY, VERSION3_WITHOUT_PARENTADDRESS); runReadAndWriteTests(results, USE_BYTE_ARRAY, VERSION3_WITH_PARENTADDRESS); + runReadAndWriteTests(results, USE_BYTE_ARRAY, VERSION3_WITH_LINKEDLIST_NODE); for (final String result : results) { Log.d(TAG, result); @@ -385,7 +391,7 @@ public class BinaryDictIOTests extends AndroidTestCase { assertNotNull("Can't get buffer.", buffer); try { now = System.currentTimeMillis(); - BinaryDictInputOutput.readUnigramsAndBigramsBinary(buffer, resultWords, resultFreqs, + BinaryDictIOUtils.readUnigramsAndBigramsBinary(buffer, resultWords, resultFreqs, resultBigrams); diff = System.currentTimeMillis() - now; } catch (IOException e) { @@ -450,6 +456,7 @@ public class BinaryDictIOTests extends AndroidTestCase { runReadUnigramsAndBigramsTests(results, USE_BYTE_BUFFER, VERSION2); runReadUnigramsAndBigramsTests(results, USE_BYTE_BUFFER, VERSION3_WITHOUT_PARENTADDRESS); runReadUnigramsAndBigramsTests(results, USE_BYTE_BUFFER, VERSION3_WITH_PARENTADDRESS); + runReadUnigramsAndBigramsTests(results, USE_BYTE_BUFFER, VERSION3_WITH_LINKEDLIST_NODE); for (final String result : results) { Log.d(TAG, result); @@ -462,9 +469,99 @@ public class BinaryDictIOTests extends AndroidTestCase { runReadUnigramsAndBigramsTests(results, USE_BYTE_ARRAY, VERSION2); runReadUnigramsAndBigramsTests(results, USE_BYTE_ARRAY, VERSION3_WITHOUT_PARENTADDRESS); runReadUnigramsAndBigramsTests(results, USE_BYTE_ARRAY, VERSION3_WITH_PARENTADDRESS); + runReadUnigramsAndBigramsTests(results, USE_BYTE_ARRAY, VERSION3_WITH_LINKEDLIST_NODE); for (final String result : results) { Log.d(TAG, result); } } + + // Tests for getTerminalPosition + private String getWordFromBinary(final FusionDictionaryBufferInterface buffer, + final int address) { + if (buffer.position() != 0) buffer.position(0); + + FileHeader header = null; + try { + header = BinaryDictInputOutput.readHeader(buffer); + } catch (IOException e) { + return null; + } catch (UnsupportedFormatException e) { + return null; + } + if (header == null) return null; + return BinaryDictInputOutput.getWordAtAddress(buffer, header.mHeaderSize, + address - header.mHeaderSize, header.mFormatOptions); + } + + private long runGetTerminalPosition(final FusionDictionaryBufferInterface buffer, + 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); + diff = System.nanoTime() - now; + } catch (IOException e) { + Log.e(TAG, "IOException while getTerminalPosition: " + e); + } catch (UnsupportedFormatException e) { + Log.e(TAG, "UnsupportedFormatException while getTermianlPosition: " + e); + } + + assertEquals(FormatSpec.NOT_VALID_WORD != position, contained); + if (contained) assertEquals(getWordFromBinary(buffer, position), word); + return diff; + } + + public void testGetTerminalPosition() { + File file = null; + try { + file = File.createTempFile("runReadUnigrams", ".dict"); + } catch (IOException e) { + // do nothing + } + assertNotNull(file); + + final FusionDictionary dict = new FusionDictionary(new Node(), + new FusionDictionary.DictionaryOptions( + new HashMap<String, String>(), false, false)); + addUnigrams(sWords.size(), dict, sWords, null /* shortcutMap */); + timeWritingDictToFile(file, dict, VERSION3_WITH_LINKEDLIST_NODE); + + final FusionDictionaryBufferInterface buffer = getBuffer(file, USE_BYTE_ARRAY); + + try { + // too long word + final String longWord = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; + assertEquals(FormatSpec.NOT_VALID_WORD, + BinaryDictIOUtils.getTerminalPosition(buffer, longWord)); + + // null + assertEquals(FormatSpec.NOT_VALID_WORD, + BinaryDictIOUtils.getTerminalPosition(buffer, null)); + + // empty string + assertEquals(FormatSpec.NOT_VALID_WORD, + BinaryDictIOUtils.getTerminalPosition(buffer, "")); + } catch (IOException e) { + } catch (UnsupportedFormatException e) { + } + + // 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); + sum += time == -1 ? 0 : time; + } + Log.d(TAG, "per a search : " + (((double)sum) / sWords.size() / 1000000)); + + // Test a word that isn't contained within the dictionary. + final Random random = new Random((int)System.currentTimeMillis()); + for (int i = 0; i < 1000; ++i) { + final String word = generateWord(random.nextInt()); + if (sWords.indexOf(word) != -1) continue; + runGetTerminalPosition(buffer, word, i, false); + } + } } |