1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com.hughes.android.dictionary.engine;
17 import java.io.IOException;
18 import java.io.RandomAccessFile;
19 import java.util.concurrent.atomic.AtomicBoolean;
21 import junit.framework.TestCase;
23 import com.hughes.android.dictionary.engine.Index.IndexEntry;
24 import com.hughes.util.ListUtil;
27 public class DictionaryTest extends TestCase {
29 static final String TEST_OUTPUTS = com.hughes.android.dictionary.engine.DictionaryBuilderTest.TEST_OUTPUTS;
30 public static final String OUTPUTS = "../DictionaryData/outputs/";
33 protected void setUp() {
34 while (!TransliteratorManager.init(null)) {
37 } catch (InterruptedException e) {
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);
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));
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);
59 assertEquals("de", deIndex.shortName);
60 assertEquals("de->en", deIndex.longName);
62 assertEquals(2, dict.sources.size());
63 assertEquals("chemnitz", dict.sources.get(0).name);
64 assertEquals("dictcc", dict.sources.get(1).name);
66 assertEquals("chemnitz", dict.pairEntries.get(0).entrySource.name);
67 assertEquals("dictcc", ListUtil.getLast(dict.pairEntries).entrySource.name);
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);
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(
81 assertEquals("Looked up: " + indexEntry.token, indexEntry.token.toLowerCase(), searchResult.token.toLowerCase());
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)));
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)));
95 assertSearchResult("Zweckorientiertheit", "zählen", deIndex.findInsertionPoint("ZZZZZ", new AtomicBoolean(false)));
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)));
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)));
105 assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberpBLEH", new AtomicBoolean(false)));
106 assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("überprBLEH", new AtomicBoolean(false)));
108 assertSearchResult("überprüfen", "überprüfe", deIndex.findInsertionPoint("überprüfeBLEH", new AtomicBoolean(false)));
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());
117 private void assertSearchResult(final String insertionPoint, final String longestPrefix,
118 final IndexEntry actual) {
119 assertEquals(insertionPoint, actual.token);
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);
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);
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;
138 assertEquals(lastTokenRow, row.getTokenRow(true));
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;
146 // This will break if the Row cache isn't big enough.
147 assertEquals(lastTokenRow, row.getTokenRow(false));
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);
158 assertSearchResult("Höschen", "Hos", deIndex.findInsertionPoint("Hos", new AtomicBoolean(false)));
159 assertSearchResult("Höschen", "hos", deIndex.findInsertionPoint("hos", new AtomicBoolean(false)));