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.android.dictionary.engine.PairEntry.Row;
27 public class DictionaryTest extends TestCase {
30 protected void setUp() {
31 while (!TransliteratorManager.init(null)) {
34 } catch (InterruptedException e) {
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);
45 final PairEntry.Row row = (Row) enIndex.rows.get(2);
46 assertEquals("z", row.getRawText(false));
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);
56 assertEquals("de", deIndex.shortName);
57 assertEquals("de->en", deIndex.longName);
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);
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);
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(
77 assertEquals("Looked up: " + indexEntry.token, indexEntry.token.toLowerCase(), searchResult.token.toLowerCase());
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)));
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)));
91 assertSearchResult("Zweckorientiertheit", "zählen", deIndex.findInsertionPoint("ZZZZZ", new AtomicBoolean(false)));
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)));
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)));
101 assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberpBLEH", new AtomicBoolean(false)));
102 assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("überprBLEH", new AtomicBoolean(false)));
104 assertSearchResult("überprüfen", "überprüfe", deIndex.findInsertionPoint("überprüfeBLEH", new AtomicBoolean(false)));
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());
113 private void assertSearchResult(final String insertionPoint, final String longestPrefix,
114 final IndexEntry actual) {
115 assertEquals(insertionPoint, actual.token);
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);
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);
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;
134 assertEquals(lastTokenRow, row.getTokenRow(true));
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;
142 // This will break if the Row cache isn't big enough.
143 assertEquals(lastTokenRow, row.getTokenRow(false));
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);
154 assertSearchResult("Höschen", "Hos", deIndex.findInsertionPoint("Hos", new AtomicBoolean(false)));
155 assertSearchResult("Höschen", "hos", deIndex.findInsertionPoint("hos", new AtomicBoolean(false)));