1 package com.hughes.android.dictionary.engine;
3 import java.io.IOException;
4 import java.io.RandomAccessFile;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Collections;
9 import java.util.concurrent.atomic.AtomicBoolean;
11 import junit.framework.TestCase;
14 public class DictionaryTest extends TestCase {
23 raf = new RandomAccessFile("testdata/de_en.dict", "r");
24 dict = new Dictionary(raf);
25 } catch (IOException e) {
26 throw new RuntimeException(e);
29 deIndex = dict.indices.get(0);
33 public void tearDown() {
36 } catch (IOException e) {
37 throw new RuntimeException(e);
42 public void testGermanMetadata() throws IOException {
43 assertEquals("de", deIndex.shortName);
44 assertEquals("de->en", deIndex.longName);
47 public void testGermanIndex() throws IOException {
48 for (final Index.IndexEntry indexEntry : deIndex.sortedIndexEntries) {
49 System.out.println("testing: " + indexEntry.token);
50 final TokenRow row = deIndex.find(indexEntry.token, new AtomicBoolean(
52 assertEquals(indexEntry.token.toLowerCase(), row.getToken().toLowerCase());
55 // TODO: maybe if user types capitalization, use it.
56 assertEquals("aaac", deIndex.find("AAAC", new AtomicBoolean(false)).getToken());
57 assertEquals("aaac", deIndex.find("aaac", new AtomicBoolean(false)).getToken());
58 assertEquals("aaac", deIndex.find("AAAc", new AtomicBoolean(false)).getToken());
59 assertEquals("aaac", deIndex.find("aaac", new AtomicBoolean(false)).getToken());
61 // Before the beginning.
62 assertEquals("40", deIndex.find("__", new AtomicBoolean(false)).getToken());
65 assertEquals("Zweckorientiertheit", deIndex.find("ZZZZZ", new AtomicBoolean(false)).getToken());
67 assertEquals("aaac", deIndex.find("aaaca", new AtomicBoolean(false)).getToken());
69 assertEquals("überprüfe", deIndex.find("ueberprüfe", new AtomicBoolean(false)).getToken());
70 assertEquals("überprüfe", deIndex.find("ueberpruefe", new AtomicBoolean(false)).getToken());
74 public void testGermanTokenRows() {
75 // Pre-cache a few of these, just to make sure that's working.
76 for (int i = 0; i < deIndex.rows.size(); i += 7) {
77 deIndex.rows.get(i).getTokenRow(true);
80 // Do the exhaustive searching.
81 TokenRow lastTokenRow = null;
82 for (final RowBase row : deIndex.rows) {
83 if (row instanceof TokenRow) {
84 lastTokenRow = (TokenRow) row;
86 assertEquals(lastTokenRow, row.getTokenRow(true));
89 // Now they're all cached, we shouldn't have to search.
90 for (final RowBase row : deIndex.rows) {
91 if (row instanceof TokenRow) {
92 lastTokenRow = (TokenRow) row;
94 // This will break if the Row cache isn't big enough.
95 assertEquals(lastTokenRow, row.getTokenRow(false));
99 @SuppressWarnings("unchecked")
100 public void testGermanSort() {
101 assertEquals("aüÄÄ", Language.de.textNorm("aueAeAE", false));
102 final List<String> words = Arrays.asList(
126 assertEquals(0, Language.de.sortComparator.compare("hülle", "huelle"));
127 assertEquals(0, Language.de.sortComparator.compare("huelle", "hülle"));
129 assertEquals(-1, Language.de.sortComparator.compare("hülle", "Hülle"));
130 assertEquals(0, Language.de.findComparator.compare("hülle", "Hülle"));
131 assertEquals(-1, Language.de.findComparator.compare("hulle", "Hülle"));
134 for (final String s : words) {
135 System.out.println(s + "\t" + Language.de.textNorm(s, false));
137 final List<String> sorted = new ArrayList<String>(words);
138 // Collections.shuffle(shuffled, new Random(0));
139 Collections.sort(sorted, Language.de.sortComparator);
140 System.out.println(sorted.toString());
141 for (int i = 0; i < words.size(); ++i) {
142 System.out.println(words.get(i) + "\t" + sorted.get(i));
143 assertEquals(words.get(i), sorted.get(i));
147 @SuppressWarnings("unchecked")
148 public void testEnglishSort() {
150 final List<String> words = Arrays.asList(
156 final List<String> sorted = new ArrayList<String>(words);
157 Collections.sort(sorted, Language.en.getSortCollator());
158 for (int i = 0; i < words.size(); ++i) {
160 assertTrue(Language.en.getSortCollator().compare(words.get(i-1), words.get(i)) < 0);
162 System.out.println(words.get(i) + "\t" + sorted.get(i));
163 assertEquals(words.get(i), sorted.get(i));
166 assertTrue(Language.en.getSortCollator().compare("pre-print", "preppy") < 0);
170 public void testLanguage() {
171 assertEquals(Language.de, Language.lookup("de"));
172 assertEquals(Language.en, Language.lookup("en"));
173 assertEquals("es", Language.lookup("es").getSymbol());