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