1 package com.hughes.android.dictionary.engine;
3 import java.io.IOException;
4 import java.io.RandomAccessFile;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Collections;
9 import java.util.concurrent.atomic.AtomicBoolean;
11 import junit.framework.TestCase;
13 import com.hughes.android.dictionary.engine.Index.SearchResult;
16 public class DictionaryTest extends TestCase {
25 raf = new RandomAccessFile("testdata/de_en.dict", "r");
26 dict = new Dictionary(raf);
27 } catch (IOException e) {
28 throw new RuntimeException(e);
31 deIndex = dict.indices.get(0);
35 public void tearDown() {
38 } catch (IOException e) {
39 throw new RuntimeException(e);
44 public void testGermanMetadata() throws IOException {
45 assertEquals("de", deIndex.shortName);
46 assertEquals("de->en", deIndex.longName);
49 public void testGermanIndex() throws IOException {
50 for (final Index.IndexEntry indexEntry : deIndex.sortedIndexEntries) {
51 System.out.println("testing: " + indexEntry.token);
52 final Index.SearchResult searchResult = deIndex.findLongestSubstring(indexEntry.token, new AtomicBoolean(
54 assertEquals(indexEntry.token.toLowerCase(), searchResult.insertionPoint.token.toLowerCase());
55 assertEquals(indexEntry.token.toLowerCase(), searchResult.longestPrefix.token.toLowerCase());
58 // TODO: maybe if user types capitalization, use it.
59 assertSearchResult("aaac", "aaac", deIndex.findLongestSubstring("aaac", new AtomicBoolean(false)));
60 assertSearchResult("aaac", "aaac", deIndex.findLongestSubstring("AAAC", new AtomicBoolean(false)));
61 assertSearchResult("aaac", "aaac", deIndex.findLongestSubstring("AAAc", new AtomicBoolean(false)));
62 assertSearchResult("aaac", "aaac", deIndex.findLongestSubstring("aAac", new AtomicBoolean(false)));
64 // Before the beginning.
65 assertSearchResult("40", "40" /* special case */, deIndex.findLongestSubstring("__", new AtomicBoolean(false)));
68 assertSearchResult("Zweckorientiertheit", "zählen", deIndex.findLongestSubstring("ZZZZZ", new AtomicBoolean(false)));
70 assertSearchResult("ab", "aaac", deIndex.findLongestSubstring("aaaca", new AtomicBoolean(false)));
71 assertSearchResult("machen", "machen", deIndex.findLongestSubstring("m", new AtomicBoolean(false)));
74 assertSearchResult("überprüfe", "überprüfe", deIndex.findLongestSubstring("ueberprüfe", new AtomicBoolean(false)));
75 assertSearchResult("überprüfe", "überprüfe", deIndex.findLongestSubstring("ueberpruefe", new AtomicBoolean(false)));
77 assertSearchResult("überprüfe", "überprüfe", deIndex.findLongestSubstring("ueberpBLEH", new AtomicBoolean(false)));
78 assertSearchResult("überprüfe", "überprüfe", deIndex.findLongestSubstring("überprBLEH", new AtomicBoolean(false)));
80 assertSearchResult("überprüfen", "überprüfe", deIndex.findLongestSubstring("überprüfeBLEH", new AtomicBoolean(false)));
84 private void assertSearchResult(final String insertionPoint, final String longestPrefix,
85 final SearchResult actual) {
86 assertEquals(insertionPoint, actual.insertionPoint.token);
87 assertEquals(longestPrefix, actual.longestPrefix.token);
90 public void testGermanTokenRows() {
91 // Pre-cache a few of these, just to make sure that's working.
92 for (int i = 0; i < deIndex.rows.size(); i += 7) {
93 deIndex.rows.get(i).getTokenRow(true);
96 // Do the exhaustive searching.
97 TokenRow lastTokenRow = null;
98 for (final RowBase row : deIndex.rows) {
99 if (row instanceof TokenRow) {
100 lastTokenRow = (TokenRow) row;
102 assertEquals(lastTokenRow, row.getTokenRow(true));
105 // Now they're all cached, we shouldn't have to search.
106 for (final RowBase row : deIndex.rows) {
107 if (row instanceof TokenRow) {
108 lastTokenRow = (TokenRow) row;
110 // This will break if the Row cache isn't big enough.
111 assertEquals(lastTokenRow, row.getTokenRow(false));
115 public void testGermanSort() {
116 assertEquals("aüÄÄ", Language.de.textNorm("aueAeAE", false));
117 final List<String> words = Arrays.asList(
141 assertEquals(0, Language.de.sortComparator.compare("hülle", "huelle"));
142 assertEquals(0, Language.de.sortComparator.compare("huelle", "hülle"));
144 assertEquals(-1, Language.de.sortComparator.compare("hülle", "Hülle"));
145 assertEquals(0, Language.de.findComparator.compare("hülle", "Hülle"));
146 assertEquals(-1, Language.de.findComparator.compare("hulle", "Hülle"));
149 for (final String s : words) {
150 System.out.println(s + "\t" + Language.de.textNorm(s, false));
152 final List<String> sorted = new ArrayList<String>(words);
153 // Collections.shuffle(shuffled, new Random(0));
154 Collections.sort(sorted, Language.de.sortComparator);
155 System.out.println(sorted.toString());
156 for (int i = 0; i < words.size(); ++i) {
157 System.out.println(words.get(i) + "\t" + sorted.get(i));
158 assertEquals(words.get(i), sorted.get(i));
162 @SuppressWarnings("unchecked")
163 public void testEnglishSort() {
165 final List<String> words = Arrays.asList(
171 final List<String> sorted = new ArrayList<String>(words);
172 Collections.sort(sorted, Language.en.getSortCollator());
173 for (int i = 0; i < words.size(); ++i) {
175 assertTrue(Language.en.getSortCollator().compare(words.get(i-1), words.get(i)) < 0);
177 System.out.println(words.get(i) + "\t" + sorted.get(i));
178 assertEquals(words.get(i), sorted.get(i));
181 assertTrue(Language.en.getSortCollator().compare("pre-print", "preppy") < 0);
185 public void testLanguage() {
186 assertEquals(Language.de, Language.lookup("de"));
187 assertEquals(Language.en, Language.lookup("en"));
188 assertEquals("es", Language.lookup("es").getSymbol());