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 {
18 public void testGermanMetadata() throws IOException {
19 final RandomAccessFile raf = new RandomAccessFile("testdata/de-en.dict", "r");
20 final Dictionary dict = new Dictionary(raf);
21 final Index deIndex = dict.indices.get(0);
23 assertEquals("de", deIndex.shortName);
24 assertEquals("de->en", deIndex.longName);
29 public void testGermanIndex() throws IOException {
30 final RandomAccessFile raf = new RandomAccessFile("testdata/de-en.dict", "r");
31 final Dictionary dict = new Dictionary(raf);
32 final Index deIndex = dict.indices.get(0);
34 for (final Index.IndexEntry indexEntry : deIndex.sortedIndexEntries) {
35 System.out.println("testing: " + indexEntry.token);
36 final Index.SearchResult searchResult = deIndex.findLongestSubstring(indexEntry.token, new AtomicBoolean(
38 assertEquals(indexEntry.token.toLowerCase(), searchResult.insertionPoint.token.toLowerCase());
39 assertEquals(indexEntry.token.toLowerCase(), searchResult.longestPrefix.token.toLowerCase());
42 // TODO: maybe if user types capitalization, use it.
43 assertSearchResult("aaac", "aaac", deIndex.findLongestSubstring("aaac", new AtomicBoolean(false)));
44 assertSearchResult("aaac", "aaac", deIndex.findLongestSubstring("AAAC", new AtomicBoolean(false)));
45 assertSearchResult("aaac", "aaac", deIndex.findLongestSubstring("AAAc", new AtomicBoolean(false)));
46 assertSearchResult("aaac", "aaac", deIndex.findLongestSubstring("aAac", new AtomicBoolean(false)));
48 // Before the beginning.
49 assertSearchResult("40", "40" /* special case */, deIndex.findLongestSubstring("", new AtomicBoolean(false)));
50 assertSearchResult("40", "40" /* special case */, deIndex.findLongestSubstring("__", new AtomicBoolean(false)));
53 assertSearchResult("Zweckorientiertheit", "zählen", deIndex.findLongestSubstring("ZZZZZ", new AtomicBoolean(false)));
55 assertSearchResult("ab", "aaac", deIndex.findLongestSubstring("aaaca", new AtomicBoolean(false)));
56 assertSearchResult("machen", "machen", deIndex.findLongestSubstring("m", new AtomicBoolean(false)));
58 assertFalse(deIndex.findLongestSubstring("macdddd", new AtomicBoolean(false)).success);
61 assertSearchResult("überprüfe", "überprüfe", deIndex.findLongestSubstring("ueberprüfe", new AtomicBoolean(false)));
62 assertSearchResult("überprüfe", "überprüfe", deIndex.findLongestSubstring("ueberpruefe", new AtomicBoolean(false)));
64 assertSearchResult("überprüfe", "überprüfe", deIndex.findLongestSubstring("ueberpBLEH", new AtomicBoolean(false)));
65 assertSearchResult("überprüfe", "überprüfe", deIndex.findLongestSubstring("überprBLEH", new AtomicBoolean(false)));
67 assertSearchResult("überprüfen", "überprüfe", deIndex.findLongestSubstring("überprüfeBLEH", new AtomicBoolean(false)));
69 // Check that search in lowercase works.
70 assertSearchResult("Alibi", "Alibi", deIndex.findLongestSubstring("alib", new AtomicBoolean(false)));
71 assertTrue(deIndex.findLongestSubstring("alib", new AtomicBoolean(false)).success);
72 System.out.println(deIndex.findLongestSubstring("alib", new AtomicBoolean(false)).toString());
77 private void assertSearchResult(final String insertionPoint, final String longestPrefix,
78 final SearchResult actual) {
79 assertEquals(insertionPoint, actual.insertionPoint.token);
80 assertEquals(longestPrefix, actual.longestPrefix.token);
83 public void testGermanTokenRows() throws IOException {
84 final RandomAccessFile raf = new RandomAccessFile("testdata/de-en.dict", "r");
85 final Dictionary dict = new Dictionary(raf);
86 final Index deIndex = dict.indices.get(0);
88 // Pre-cache a few of these, just to make sure that's working.
89 for (int i = 0; i < deIndex.rows.size(); i += 7) {
90 deIndex.rows.get(i).getTokenRow(true);
93 // Do the exhaustive searching.
94 TokenRow lastTokenRow = null;
95 for (final RowBase row : deIndex.rows) {
96 if (row instanceof TokenRow) {
97 lastTokenRow = (TokenRow) row;
99 assertEquals(lastTokenRow, row.getTokenRow(true));
102 // Now they're all cached, we shouldn't have to search.
103 for (final RowBase row : deIndex.rows) {
104 if (row instanceof TokenRow) {
105 lastTokenRow = (TokenRow) row;
107 // This will break if the Row cache isn't big enough.
108 assertEquals(lastTokenRow, row.getTokenRow(false));
114 public void testGermanSort() {
115 assertEquals("aüÄÄ", Language.de.textNorm("aueAeAE", false));
116 final List<String> words = Arrays.asList(
144 assertEquals(0, Language.de.sortComparator.compare("hülle", "huelle"));
145 assertEquals(0, Language.de.sortComparator.compare("huelle", "hülle"));
147 assertEquals(-1, Language.de.sortComparator.compare("hülle", "Hülle"));
148 assertEquals(0, Language.de.findComparator.compare("hülle", "Hülle"));
149 assertEquals(-1, Language.de.findComparator.compare("hulle", "Hülle"));
152 for (final String s : words) {
153 System.out.println(s + "\t" + Language.de.textNorm(s, false));
155 final List<String> sorted = new ArrayList<String>(words);
156 // Collections.shuffle(shuffled, new Random(0));
157 Collections.sort(sorted, Language.de.sortComparator);
158 System.out.println(sorted.toString());
159 for (int i = 0; i < words.size(); ++i) {
160 System.out.println(words.get(i) + "\t" + sorted.get(i));
161 assertEquals(words.get(i), sorted.get(i));
165 @SuppressWarnings("unchecked")
166 public void testEnglishSort() {
168 final List<String> words = Arrays.asList(
174 final List<String> sorted = new ArrayList<String>(words);
175 Collections.sort(sorted, Language.en.getSortCollator());
176 for (int i = 0; i < words.size(); ++i) {
178 assertTrue(Language.en.getSortCollator().compare(words.get(i-1), words.get(i)) < 0);
180 System.out.println(words.get(i) + "\t" + sorted.get(i));
181 assertEquals(words.get(i), sorted.get(i));
184 assertTrue(Language.en.getSortCollator().compare("pre-print", "preppy") < 0);
188 public void testLanguage() {
189 assertEquals(Language.de, Language.lookup("de"));
190 assertEquals(Language.en, Language.lookup("en"));
191 assertEquals("es", Language.lookup("es").getSymbol());
194 public void testTextNorm() {
195 assertEquals("hoschen", "Höschen".toLowerCase(Language.de.locale));
198 public void testChemnitz() throws IOException {
199 final RandomAccessFile raf = new RandomAccessFile("testdata/de-en_chemnitz.dict", "r");
200 final Dictionary dict = new Dictionary(raf);
201 final Index deIndex = dict.indices.get(0);
203 //assertSearchResult("Höschen", "Hos", deIndex.findLongestSubstring("Hos", new AtomicBoolean(false)));
204 //assertSearchResult("Höschen", "hos", deIndex.findLongestSubstring("hos", new AtomicBoolean(false)));