]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/DictionaryTest.java
a44b5203cb786ffcc35058928f52a4d091dd116d
[DictionaryPC.git] / src / com / hughes / android / dictionary / engine / DictionaryTest.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package com.hughes.android.dictionary.engine;
16
17 import java.io.IOException;
18 import java.io.RandomAccessFile;
19 import java.util.concurrent.atomic.AtomicBoolean;
20
21 import junit.framework.TestCase;
22
23 import com.hughes.android.dictionary.engine.Index.IndexEntry;
24
25
26 public class DictionaryTest extends TestCase {
27   
28   static final String TEST_OUTPUTS = com.hughes.android.dictionary.engine.DictionaryBuilderTest.TEST_OUTPUTS;
29   public static final String OUTPUTS = "../DictionaryData/outputs/";
30
31   @Override
32   protected void setUp() {
33     while (!TransliteratorManager.init(null)) {
34       try {
35         Thread.sleep(10);
36       } catch (InterruptedException e) {
37         e.printStackTrace();
38       }
39     }
40   }
41
42   public void testEnItWiktionary() throws IOException {
43     final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "EN-IT_enwiktionary.quickdic", "r");
44     final Dictionary dict = new Dictionary(raf);
45     final Index enIndex = dict.indices.get(0);
46     
47     final RowBase row = enIndex.rows.get(4);
48     assertEquals("eagle (A gold coin with a face value of $10.00) (noun)\tmoneta di dieci dollari", row.getRawText(false));
49
50     raf.close();
51   }
52
53   public void testGermanMetadata() throws IOException {
54     final RandomAccessFile raf = new RandomAccessFile(TEST_OUTPUTS + "de-en.quickdic", "r");
55     final Dictionary dict = new Dictionary(raf);
56     final Index deIndex = dict.indices.get(0);
57     
58     assertEquals("de", deIndex.shortName);
59     assertEquals("de->en", deIndex.longName);
60     
61     assertEquals(2, dict.sources.size());
62     assertEquals("chemnitz", dict.sources.get(0).name);
63     assertEquals(0, dict.sources.get(0).pairEntryStart);
64     assertEquals("dictcc", dict.sources.get(1).name);
65     assertEquals(0, dict.sources.get(1).pairEntryStart);  // TODO: rethink this
66     
67     raf.close();
68   }
69   
70   public void testGermanIndex() throws IOException {
71     final RandomAccessFile raf = new RandomAccessFile(TEST_OUTPUTS + "de-en.quickdic", "r");
72     final Dictionary dict = new Dictionary(raf);
73     final Index deIndex = dict.indices.get(0);
74     
75     for (final Index.IndexEntry indexEntry : deIndex.sortedIndexEntries) {
76       System.out.println("testing: " + indexEntry.token);
77       final IndexEntry searchResult = deIndex.findInsertionPoint(indexEntry.token, new AtomicBoolean(
78           false));
79       assertEquals("Looked up: " + indexEntry.token, indexEntry.token.toLowerCase(), searchResult.token.toLowerCase());
80     }
81
82     // TODO: maybe if user types capitalization, use it.
83     assertSearchResult("aaac", "aaac", deIndex.findInsertionPoint("aaac", new AtomicBoolean(false)));
84     assertSearchResult("aaac", "aaac", deIndex.findInsertionPoint("AAAC", new AtomicBoolean(false)));
85     assertSearchResult("aaac", "aaac", deIndex.findInsertionPoint("AAAc", new AtomicBoolean(false)));
86     assertSearchResult("aaac", "aaac", deIndex.findInsertionPoint("aAac", new AtomicBoolean(false)));
87
88     // Before the beginning.
89     assertSearchResult("40", "40" /* special case */, deIndex.findInsertionPoint("", new AtomicBoolean(false)));
90     assertSearchResult("40", "40" /* special case */, deIndex.findInsertionPoint("__", new AtomicBoolean(false)));
91     
92     // After the end.
93     assertSearchResult("Zweckorientiertheit", "zählen", deIndex.findInsertionPoint("ZZZZZ", new AtomicBoolean(false)));
94
95     assertSearchResult("ab", "aaac", deIndex.findInsertionPoint("aaaca", new AtomicBoolean(false)));
96     assertSearchResult("machen", "machen", deIndex.findInsertionPoint("m", new AtomicBoolean(false)));
97     assertSearchResult("machen", "machen", deIndex.findInsertionPoint("macdddd", new AtomicBoolean(false)));
98
99
100     assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberprüfe", new AtomicBoolean(false)));
101     assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberpruefe", new AtomicBoolean(false)));
102
103     assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberpBLEH", new AtomicBoolean(false)));
104     assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("überprBLEH", new AtomicBoolean(false)));
105
106     assertSearchResult("überprüfen", "überprüfe", deIndex.findInsertionPoint("überprüfeBLEH", new AtomicBoolean(false)));
107
108     // Check that search in lowercase works.
109     assertSearchResult("Alibi", "Alibi", deIndex.findInsertionPoint("alib", new AtomicBoolean(false)));
110     System.out.println(deIndex.findInsertionPoint("alib", new AtomicBoolean(false)).toString());
111     
112     raf.close();
113   }
114   
115   private void assertSearchResult(final String insertionPoint, final String longestPrefix,
116       final IndexEntry actual) {
117     assertEquals(insertionPoint, actual.token);
118   }
119
120   public void testGermanTokenRows() throws IOException {
121     final RandomAccessFile raf = new RandomAccessFile(TEST_OUTPUTS + "de-en.quickdic", "r");
122     final Dictionary dict = new Dictionary(raf);
123     final Index deIndex = dict.indices.get(0);
124     
125     // Pre-cache a few of these, just to make sure that's working.
126     for (int i = 0; i < deIndex.rows.size(); i += 7) {
127       deIndex.rows.get(i).getTokenRow(true);
128     }
129     
130     // Do the exhaustive searching.
131     TokenRow lastTokenRow = null;
132     for (final RowBase row : deIndex.rows) {
133       if (row instanceof TokenRow) {
134         lastTokenRow = (TokenRow) row;
135       }
136       assertEquals(lastTokenRow, row.getTokenRow(true));
137     }
138
139     // Now they're all cached, we shouldn't have to search.
140     for (final RowBase row : deIndex.rows) {
141       if (row instanceof TokenRow) {
142         lastTokenRow = (TokenRow) row;
143       }
144       // This will break if the Row cache isn't big enough.
145       assertEquals(lastTokenRow, row.getTokenRow(false));
146     }
147     
148     raf.close();
149   }
150   
151   public void testChemnitz() throws IOException {
152     final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "/de-en_chemnitz.quickdic", "r");
153     final Dictionary dict = new Dictionary(raf);
154     final Index deIndex = dict.indices.get(0);
155     
156     assertSearchResult("Höschen", "Hos", deIndex.findInsertionPoint("Hos", new AtomicBoolean(false)));
157     assertSearchResult("Höschen", "hos", deIndex.findInsertionPoint("hos", new AtomicBoolean(false)));
158
159     raf.close();
160   }
161
162
163 }