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;
26 public class DictionaryTest extends TestCase {
28 static final String TEST_OUTPUTS = com.hughes.android.dictionary.engine.DictionaryBuilderTest.TEST_OUTPUTS;
29 public static final String OUTPUTS = "../DictionaryData/outputs/";
32 protected void setUp() {
33 while (!TransliteratorManager.init(null)) {
36 } catch (InterruptedException e) {
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);
47 final RowBase row = enIndex.rows.get(4);
48 assertEquals("carbonyl chloride (the compound COCl<sub>2</sub>) (noun)\tossicloruro di carbonio", row.getRawText(false));
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);
58 assertEquals("de", deIndex.shortName);
59 assertEquals("de->en", deIndex.longName);
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
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);
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(
79 assertEquals("Looked up: " + indexEntry.token, indexEntry.token.toLowerCase(), searchResult.token.toLowerCase());
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)));
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)));
93 assertSearchResult("Zweckorientiertheit", "zählen", deIndex.findInsertionPoint("ZZZZZ", new AtomicBoolean(false)));
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)));
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)));
103 assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberpBLEH", new AtomicBoolean(false)));
104 assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("überprBLEH", new AtomicBoolean(false)));
106 assertSearchResult("überprüfen", "überprüfe", deIndex.findInsertionPoint("überprüfeBLEH", new AtomicBoolean(false)));
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());
115 private void assertSearchResult(final String insertionPoint, final String longestPrefix,
116 final IndexEntry actual) {
117 assertEquals(insertionPoint, actual.token);
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);
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);
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;
136 assertEquals(lastTokenRow, row.getTokenRow(true));
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;
144 // This will break if the Row cache isn't big enough.
145 assertEquals(lastTokenRow, row.getTokenRow(false));
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);
156 assertSearchResult("Höschen", "Hos", deIndex.findInsertionPoint("Hos", new AtomicBoolean(false)));
157 assertSearchResult("Höschen", "hos", deIndex.findInsertionPoint("hos", new AtomicBoolean(false)));