1 package com.hughes.android.dictionary.engine;
3 import java.io.IOException;
4 import java.io.RandomAccessFile;
5 import java.util.concurrent.atomic.AtomicBoolean;
7 import junit.framework.TestCase;
9 import com.hughes.android.dictionary.engine.Index.IndexEntry;
10 import com.hughes.android.dictionary.engine.PairEntry.Row;
13 public class DictionaryTest extends TestCase {
16 protected void setUp() {
17 while (!TransliteratorManager.init(null)) {
20 } catch (InterruptedException e) {
26 public void testEnItWiktionary() throws IOException {
27 final RandomAccessFile raf = new RandomAccessFile("dictOutputs/EN-IT_enwiktionary.quickdic", "r");
28 final Dictionary dict = new Dictionary(raf);
29 final Index enIndex = dict.indices.get(0);
31 final PairEntry.Row row = (Row) enIndex.rows.get(2);
32 assertEquals("z", row.getRawText(false));
37 public void testGermanMetadata() throws IOException {
38 final RandomAccessFile raf = new RandomAccessFile("testdata/de-en.quickdic", "r");
39 final Dictionary dict = new Dictionary(raf);
40 final Index deIndex = dict.indices.get(0);
42 assertEquals("de", deIndex.shortName);
43 assertEquals("de->en", deIndex.longName);
45 assertEquals(2, dict.sources.size());
46 assertEquals("chemnitz", dict.sources.get(0).name);
47 assertEquals(0, dict.sources.get(0).pairEntryStart);
48 assertEquals("dictcc", dict.sources.get(1).name);
49 assertEquals(113, dict.sources.get(1).pairEntryStart);
54 public void testGermanIndex() throws IOException {
55 final RandomAccessFile raf = new RandomAccessFile("testdata/de-en.quickdic", "r");
56 final Dictionary dict = new Dictionary(raf);
57 final Index deIndex = dict.indices.get(0);
59 for (final Index.IndexEntry indexEntry : deIndex.sortedIndexEntries) {
60 System.out.println("testing: " + indexEntry.token);
61 final IndexEntry searchResult = deIndex.findInsertionPoint(indexEntry.token, new AtomicBoolean(
63 assertEquals("Looked up: " + indexEntry.token, indexEntry.token.toLowerCase(), searchResult.token.toLowerCase());
66 // TODO: maybe if user types capitalization, use it.
67 assertSearchResult("aaac", "aaac", deIndex.findInsertionPoint("aaac", new AtomicBoolean(false)));
68 assertSearchResult("aaac", "aaac", deIndex.findInsertionPoint("AAAC", new AtomicBoolean(false)));
69 assertSearchResult("aaac", "aaac", deIndex.findInsertionPoint("AAAc", new AtomicBoolean(false)));
70 assertSearchResult("aaac", "aaac", deIndex.findInsertionPoint("aAac", new AtomicBoolean(false)));
72 // Before the beginning.
73 assertSearchResult("40", "40" /* special case */, deIndex.findInsertionPoint("", new AtomicBoolean(false)));
74 assertSearchResult("40", "40" /* special case */, deIndex.findInsertionPoint("__", new AtomicBoolean(false)));
77 assertSearchResult("Zweckorientiertheit", "zählen", deIndex.findInsertionPoint("ZZZZZ", new AtomicBoolean(false)));
79 assertSearchResult("ab", "aaac", deIndex.findInsertionPoint("aaaca", new AtomicBoolean(false)));
80 assertSearchResult("machen", "machen", deIndex.findInsertionPoint("m", new AtomicBoolean(false)));
81 assertSearchResult("machen", "machen", deIndex.findInsertionPoint("macdddd", new AtomicBoolean(false)));
84 assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberprüfe", new AtomicBoolean(false)));
85 assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberpruefe", new AtomicBoolean(false)));
87 assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberpBLEH", new AtomicBoolean(false)));
88 assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("überprBLEH", new AtomicBoolean(false)));
90 assertSearchResult("überprüfen", "überprüfe", deIndex.findInsertionPoint("überprüfeBLEH", new AtomicBoolean(false)));
92 // Check that search in lowercase works.
93 assertSearchResult("Alibi", "Alibi", deIndex.findInsertionPoint("alib", new AtomicBoolean(false)));
94 System.out.println(deIndex.findInsertionPoint("alib", new AtomicBoolean(false)).toString());
99 private void assertSearchResult(final String insertionPoint, final String longestPrefix,
100 final IndexEntry actual) {
101 assertEquals(insertionPoint, actual.token);
104 public void testGermanTokenRows() throws IOException {
105 final RandomAccessFile raf = new RandomAccessFile("testdata/de-en.quickdic", "r");
106 final Dictionary dict = new Dictionary(raf);
107 final Index deIndex = dict.indices.get(0);
109 // Pre-cache a few of these, just to make sure that's working.
110 for (int i = 0; i < deIndex.rows.size(); i += 7) {
111 deIndex.rows.get(i).getTokenRow(true);
114 // Do the exhaustive searching.
115 TokenRow lastTokenRow = null;
116 for (final RowBase row : deIndex.rows) {
117 if (row instanceof TokenRow) {
118 lastTokenRow = (TokenRow) row;
120 assertEquals(lastTokenRow, row.getTokenRow(true));
123 // Now they're all cached, we shouldn't have to search.
124 for (final RowBase row : deIndex.rows) {
125 if (row instanceof TokenRow) {
126 lastTokenRow = (TokenRow) row;
128 // This will break if the Row cache isn't big enough.
129 assertEquals(lastTokenRow, row.getTokenRow(false));
135 public void testChemnitz() throws IOException {
136 final RandomAccessFile raf = new RandomAccessFile("dictOutputs/de-en_chemnitz.quickdic", "r");
137 final Dictionary dict = new Dictionary(raf);
138 final Index deIndex = dict.indices.get(0);
140 assertSearchResult("Höschen", "Hos", deIndex.findInsertionPoint("Hos", new AtomicBoolean(false)));
141 assertSearchResult("Höschen", "hos", deIndex.findInsertionPoint("hos", new AtomicBoolean(false)));