]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/DictionaryTest.java
Fixing tests after moving out data.
[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 import com.hughes.android.dictionary.engine.PairEntry.Row;
25
26
27 public class DictionaryTest extends TestCase {
28   
29   static final String TEST_OUTPUTS = com.hughes.android.dictionary.engine.DictionaryBuilderTest.TEST_OUTPUTS;
30   static final String OUTPUTS = com.hughes.android.dictionary.engine.DictionaryBuilderTest.OUTPUTS;
31
32   @Override
33   protected void setUp() {
34     while (!TransliteratorManager.init(null)) {
35       try {
36         Thread.sleep(10);
37       } catch (InterruptedException e) {
38         e.printStackTrace();
39       }
40     }
41   }
42
43   public void testEnItWiktionary() throws IOException {
44     final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "EN-IT_enwiktionary.quickdic", "r");
45     final Dictionary dict = new Dictionary(raf);
46     final Index enIndex = dict.indices.get(0);
47     
48     final RowBase row = enIndex.rows.get(4);
49     assertEquals("carbonyl chloride (the compound COCl<sub>2</sub>) (noun)\tossicloruro di carbonio", row.getRawText(false));
50
51     raf.close();
52   }
53
54   public void testGermanMetadata() throws IOException {
55     final RandomAccessFile raf = new RandomAccessFile(TEST_OUTPUTS + "de-en.quickdic", "r");
56     final Dictionary dict = new Dictionary(raf);
57     final Index deIndex = dict.indices.get(0);
58     
59     assertEquals("de", deIndex.shortName);
60     assertEquals("de->en", deIndex.longName);
61     
62     assertEquals(2, dict.sources.size());
63     assertEquals("chemnitz", dict.sources.get(0).name);
64     assertEquals(0, dict.sources.get(0).pairEntryStart);
65     assertEquals("dictcc", dict.sources.get(1).name);
66     assertEquals(0, dict.sources.get(1).pairEntryStart);  // TODO: rethink this
67     
68     raf.close();
69   }
70   
71   public void testGermanIndex() throws IOException {
72     final RandomAccessFile raf = new RandomAccessFile(TEST_OUTPUTS + "de-en.quickdic", "r");
73     final Dictionary dict = new Dictionary(raf);
74     final Index deIndex = dict.indices.get(0);
75     
76     for (final Index.IndexEntry indexEntry : deIndex.sortedIndexEntries) {
77       System.out.println("testing: " + indexEntry.token);
78       final IndexEntry searchResult = deIndex.findInsertionPoint(indexEntry.token, new AtomicBoolean(
79           false));
80       assertEquals("Looked up: " + indexEntry.token, indexEntry.token.toLowerCase(), searchResult.token.toLowerCase());
81     }
82
83     // TODO: maybe if user types capitalization, use it.
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     assertSearchResult("aaac", "aaac", deIndex.findInsertionPoint("aAac", new AtomicBoolean(false)));
88
89     // Before the beginning.
90     assertSearchResult("40", "40" /* special case */, deIndex.findInsertionPoint("", new AtomicBoolean(false)));
91     assertSearchResult("40", "40" /* special case */, deIndex.findInsertionPoint("__", new AtomicBoolean(false)));
92     
93     // After the end.
94     assertSearchResult("Zweckorientiertheit", "zählen", deIndex.findInsertionPoint("ZZZZZ", new AtomicBoolean(false)));
95
96     assertSearchResult("ab", "aaac", deIndex.findInsertionPoint("aaaca", new AtomicBoolean(false)));
97     assertSearchResult("machen", "machen", deIndex.findInsertionPoint("m", new AtomicBoolean(false)));
98     assertSearchResult("machen", "machen", deIndex.findInsertionPoint("macdddd", new AtomicBoolean(false)));
99
100
101     assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberprüfe", new AtomicBoolean(false)));
102     assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberpruefe", new AtomicBoolean(false)));
103
104     assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberpBLEH", new AtomicBoolean(false)));
105     assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("überprBLEH", new AtomicBoolean(false)));
106
107     assertSearchResult("überprüfen", "überprüfe", deIndex.findInsertionPoint("überprüfeBLEH", new AtomicBoolean(false)));
108
109     // Check that search in lowercase works.
110     assertSearchResult("Alibi", "Alibi", deIndex.findInsertionPoint("alib", new AtomicBoolean(false)));
111     System.out.println(deIndex.findInsertionPoint("alib", new AtomicBoolean(false)).toString());
112     
113     raf.close();
114   }
115   
116   private void assertSearchResult(final String insertionPoint, final String longestPrefix,
117       final IndexEntry actual) {
118     assertEquals(insertionPoint, actual.token);
119   }
120
121   public void testGermanTokenRows() throws IOException {
122     final RandomAccessFile raf = new RandomAccessFile(TEST_OUTPUTS + "de-en.quickdic", "r");
123     final Dictionary dict = new Dictionary(raf);
124     final Index deIndex = dict.indices.get(0);
125     
126     // Pre-cache a few of these, just to make sure that's working.
127     for (int i = 0; i < deIndex.rows.size(); i += 7) {
128       deIndex.rows.get(i).getTokenRow(true);
129     }
130     
131     // Do the exhaustive searching.
132     TokenRow lastTokenRow = null;
133     for (final RowBase row : deIndex.rows) {
134       if (row instanceof TokenRow) {
135         lastTokenRow = (TokenRow) row;
136       }
137       assertEquals(lastTokenRow, row.getTokenRow(true));
138     }
139
140     // Now they're all cached, we shouldn't have to search.
141     for (final RowBase row : deIndex.rows) {
142       if (row instanceof TokenRow) {
143         lastTokenRow = (TokenRow) row;
144       }
145       // This will break if the Row cache isn't big enough.
146       assertEquals(lastTokenRow, row.getTokenRow(false));
147     }
148     
149     raf.close();
150   }
151   
152   public void testChemnitz() throws IOException {
153     final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "/de-en_chemnitz.quickdic", "r");
154     final Dictionary dict = new Dictionary(raf);
155     final Index deIndex = dict.indices.get(0);
156     
157     assertSearchResult("Höschen", "Hos", deIndex.findInsertionPoint("Hos", new AtomicBoolean(false)));
158     assertSearchResult("Höschen", "hos", deIndex.findInsertionPoint("hos", new AtomicBoolean(false)));
159
160     raf.close();
161   }
162
163
164 }