]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/DictionaryTest.java
go
[DictionaryPC.git] / src / com / hughes / android / dictionary / engine / DictionaryTest.java
1 package com.hughes.android.dictionary.engine;
2
3 import java.io.IOException;
4 import java.io.RandomAccessFile;
5 import java.util.concurrent.atomic.AtomicBoolean;
6
7 import junit.framework.TestCase;
8
9 import com.hughes.android.dictionary.engine.Index.IndexEntry;
10 import com.hughes.android.dictionary.engine.PairEntry.Row;
11
12
13 public class DictionaryTest extends TestCase {
14
15   @Override
16   protected void setUp() {
17     while (!TransliteratorManager.init(null)) {
18       try {
19         Thread.sleep(10);
20       } catch (InterruptedException e) {
21         e.printStackTrace();
22       }
23     }
24   }
25
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);
30     
31     final PairEntry.Row row = (Row) enIndex.rows.get(2);
32     assertEquals("z", row.getRawText(false));
33
34     raf.close();
35   }
36
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);
41     
42     assertEquals("de", deIndex.shortName);
43     assertEquals("de->en", deIndex.longName);
44     
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);
50     
51     raf.close();
52   }
53   
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);
58     
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(
62           false));
63       assertEquals("Looked up: " + indexEntry.token, indexEntry.token.toLowerCase(), searchResult.token.toLowerCase());
64     }
65
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)));
71
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)));
75     
76     // After the end.
77     assertSearchResult("Zweckorientiertheit", "zählen", deIndex.findInsertionPoint("ZZZZZ", new AtomicBoolean(false)));
78
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)));
82
83
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)));
86
87     assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberpBLEH", new AtomicBoolean(false)));
88     assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("überprBLEH", new AtomicBoolean(false)));
89
90     assertSearchResult("überprüfen", "überprüfe", deIndex.findInsertionPoint("überprüfeBLEH", new AtomicBoolean(false)));
91
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());
95     
96     raf.close();
97   }
98   
99   private void assertSearchResult(final String insertionPoint, final String longestPrefix,
100       final IndexEntry actual) {
101     assertEquals(insertionPoint, actual.token);
102   }
103
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);
108     
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);
112     }
113     
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;
119       }
120       assertEquals(lastTokenRow, row.getTokenRow(true));
121     }
122
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;
127       }
128       // This will break if the Row cache isn't big enough.
129       assertEquals(lastTokenRow, row.getTokenRow(false));
130     }
131     
132     raf.close();
133   }
134   
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);
139     
140     assertSearchResult("Höschen", "Hos", deIndex.findInsertionPoint("Hos", new AtomicBoolean(false)));
141     assertSearchResult("Höschen", "hos", deIndex.findInsertionPoint("hos", new AtomicBoolean(false)));
142
143     raf.close();
144   }
145
146
147 }