]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/DictionaryTest.java
Wiktionary upgrade!
[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.util.ListUtil;
25
26
27 public class DictionaryTest extends TestCase {
28   
29   static final String TEST_OUTPUTS = com.hughes.android.dictionary.engine.DictionaryBuilderTest.TEST_OUTPUTS;
30   public static final String OUTPUTS = "../DictionaryData/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("eagle (A gold coin with a face value of $10.00) (noun)\tmoneta di dieci dollari", 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("dictcc", dict.sources.get(1).name);
65     
66     assertEquals("chemnitz", dict.pairEntries.get(0).entrySource.name);
67     assertEquals("dictcc", ListUtil.getLast(dict.pairEntries).entrySource.name);
68     
69     raf.close();
70   }
71   
72   public void testGermanIndex() throws IOException {
73     final RandomAccessFile raf = new RandomAccessFile(TEST_OUTPUTS + "de-en.quickdic", "r");
74     final Dictionary dict = new Dictionary(raf);
75     final Index deIndex = dict.indices.get(0);
76     
77     for (final Index.IndexEntry indexEntry : deIndex.sortedIndexEntries) {
78       System.out.println("testing: " + indexEntry.token);
79       final IndexEntry searchResult = deIndex.findInsertionPoint(indexEntry.token, new AtomicBoolean(
80           false));
81       assertEquals("Looked up: " + indexEntry.token, indexEntry.token.toLowerCase(), searchResult.token.toLowerCase());
82     }
83
84     // TODO: maybe if user types capitalization, use it.
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     assertSearchResult("aaac", "aaac", deIndex.findInsertionPoint("aAac", new AtomicBoolean(false)));
89
90     // Before the beginning.
91     assertSearchResult("40", "40" /* special case */, deIndex.findInsertionPoint("", new AtomicBoolean(false)));
92     assertSearchResult("40", "40" /* special case */, deIndex.findInsertionPoint("__", new AtomicBoolean(false)));
93     
94     // After the end.
95     assertSearchResult("Zweckorientiertheit", "zählen", deIndex.findInsertionPoint("ZZZZZ", new AtomicBoolean(false)));
96
97     assertSearchResult("ab", "aaac", deIndex.findInsertionPoint("aaaca", new AtomicBoolean(false)));
98     assertSearchResult("machen", "machen", deIndex.findInsertionPoint("m", new AtomicBoolean(false)));
99     assertSearchResult("machen", "machen", deIndex.findInsertionPoint("macdddd", new AtomicBoolean(false)));
100
101
102     assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberprüfe", new AtomicBoolean(false)));
103     assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberpruefe", new AtomicBoolean(false)));
104
105     assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberpBLEH", new AtomicBoolean(false)));
106     assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("überprBLEH", new AtomicBoolean(false)));
107
108     assertSearchResult("überprüfen", "überprüfe", deIndex.findInsertionPoint("überprüfeBLEH", new AtomicBoolean(false)));
109
110     // Check that search in lowercase works.
111     assertSearchResult("Alibi", "Alibi", deIndex.findInsertionPoint("alib", new AtomicBoolean(false)));
112     System.out.println(deIndex.findInsertionPoint("alib", new AtomicBoolean(false)).toString());
113     
114     raf.close();
115   }
116   
117   private void assertSearchResult(final String insertionPoint, final String longestPrefix,
118       final IndexEntry actual) {
119     assertEquals(insertionPoint, actual.token);
120   }
121
122   public void testGermanTokenRows() throws IOException {
123     final RandomAccessFile raf = new RandomAccessFile(TEST_OUTPUTS + "de-en.quickdic", "r");
124     final Dictionary dict = new Dictionary(raf);
125     final Index deIndex = dict.indices.get(0);
126     
127     // Pre-cache a few of these, just to make sure that's working.
128     for (int i = 0; i < deIndex.rows.size(); i += 7) {
129       deIndex.rows.get(i).getTokenRow(true);
130     }
131     
132     // Do the exhaustive searching.
133     TokenRow lastTokenRow = null;
134     for (final RowBase row : deIndex.rows) {
135       if (row instanceof TokenRow) {
136         lastTokenRow = (TokenRow) row;
137       }
138       assertEquals(lastTokenRow, row.getTokenRow(true));
139     }
140
141     // Now they're all cached, we shouldn't have to search.
142     for (final RowBase row : deIndex.rows) {
143       if (row instanceof TokenRow) {
144         lastTokenRow = (TokenRow) row;
145       }
146       // This will break if the Row cache isn't big enough.
147       assertEquals(lastTokenRow, row.getTokenRow(false));
148     }
149     
150     raf.close();
151   }
152   
153   public void testChemnitz() throws IOException {
154     final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "/de-en_chemnitz.quickdic", "r");
155     final Dictionary dict = new Dictionary(raf);
156     final Index deIndex = dict.indices.get(0);
157     
158     assertSearchResult("Höschen", "Hos", deIndex.findInsertionPoint("Hos", new AtomicBoolean(false)));
159     assertSearchResult("Höschen", "hos", deIndex.findInsertionPoint("hos", new AtomicBoolean(false)));
160
161     raf.close();
162   }
163
164
165 }