]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/DictionaryTest.java
go
[DictionaryPC.git] / src / com / hughes / android / dictionary / engine / DictionaryTest.java
1 package com.hughes.android.dictionary.engine;
2
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;
8 import java.util.List;
9 import java.util.concurrent.atomic.AtomicBoolean;
10
11 import junit.framework.TestCase;
12
13 import com.hughes.android.dictionary.engine.Index.SearchResult;
14
15
16 public class DictionaryTest extends TestCase {
17   
18   RandomAccessFile raf;
19   Dictionary dict;
20   Index deIndex; 
21   
22   @Override
23   public void setUp() {
24     try {
25       raf = new RandomAccessFile("testdata/de_en.dict", "r");
26       dict = new Dictionary(raf);
27     } catch (IOException e) {
28       throw new RuntimeException(e);
29     }
30
31     deIndex = dict.indices.get(0);
32 }
33   
34   @Override
35   public void tearDown() {
36     try {
37       raf.close();
38     } catch (IOException e) {
39       throw new RuntimeException(e);
40     }
41   }
42   
43
44   public void testGermanMetadata() throws IOException {
45     assertEquals("de", deIndex.shortName);
46     assertEquals("de->en", deIndex.longName);
47   }
48   
49   public void testGermanIndex() throws IOException {
50     for (final Index.IndexEntry indexEntry : deIndex.sortedIndexEntries) {
51       System.out.println("testing: " + indexEntry.token);
52       final Index.SearchResult searchResult = deIndex.findLongestSubstring(indexEntry.token, new AtomicBoolean(
53           false));
54       assertEquals(indexEntry.token.toLowerCase(), searchResult.insertionPoint.token.toLowerCase());
55       assertEquals(indexEntry.token.toLowerCase(), searchResult.longestPrefix.token.toLowerCase());
56     }
57
58     // TODO: maybe if user types capitalization, use it.
59     assertSearchResult("aaac", "aaac", deIndex.findLongestSubstring("aaac", new AtomicBoolean(false)));
60     assertSearchResult("aaac", "aaac", deIndex.findLongestSubstring("AAAC", new AtomicBoolean(false)));
61     assertSearchResult("aaac", "aaac", deIndex.findLongestSubstring("AAAc", new AtomicBoolean(false)));
62     assertSearchResult("aaac", "aaac", deIndex.findLongestSubstring("aAac", new AtomicBoolean(false)));
63
64     // Before the beginning.
65     assertSearchResult("40", "40" /* special case */, deIndex.findLongestSubstring("__", new AtomicBoolean(false)));
66     
67     // After the end.
68     assertSearchResult("Zweckorientiertheit", "zählen", deIndex.findLongestSubstring("ZZZZZ", new AtomicBoolean(false)));
69
70     assertSearchResult("ab", "aaac", deIndex.findLongestSubstring("aaaca", new AtomicBoolean(false)));
71     assertSearchResult("machen", "machen", deIndex.findLongestSubstring("m", new AtomicBoolean(false)));
72
73
74     assertSearchResult("überprüfe", "überprüfe", deIndex.findLongestSubstring("ueberprüfe", new AtomicBoolean(false)));
75     assertSearchResult("überprüfe", "überprüfe", deIndex.findLongestSubstring("ueberpruefe", new AtomicBoolean(false)));
76
77     assertSearchResult("überprüfe", "überprüfe", deIndex.findLongestSubstring("ueberpBLEH", new AtomicBoolean(false)));
78     assertSearchResult("überprüfe", "überprüfe", deIndex.findLongestSubstring("überprBLEH", new AtomicBoolean(false)));
79
80     assertSearchResult("überprüfen", "überprüfe", deIndex.findLongestSubstring("überprüfeBLEH", new AtomicBoolean(false)));
81
82   }
83   
84   private void assertSearchResult(final String insertionPoint, final String longestPrefix,
85       final SearchResult actual) {
86     assertEquals(insertionPoint, actual.insertionPoint.token);
87     assertEquals(longestPrefix, actual.longestPrefix.token);
88   }
89
90   public void testGermanTokenRows() {
91     // Pre-cache a few of these, just to make sure that's working.
92     for (int i = 0; i < deIndex.rows.size(); i += 7) {
93       deIndex.rows.get(i).getTokenRow(true);
94     }
95     
96     // Do the exhaustive searching.
97     TokenRow lastTokenRow = null;
98     for (final RowBase row : deIndex.rows) {
99       if (row instanceof TokenRow) {
100         lastTokenRow = (TokenRow) row;
101       }
102       assertEquals(lastTokenRow, row.getTokenRow(true));
103     }
104
105     // Now they're all cached, we shouldn't have to search.
106     for (final RowBase row : deIndex.rows) {
107       if (row instanceof TokenRow) {
108         lastTokenRow = (TokenRow) row;
109       }
110       // This will break if the Row cache isn't big enough.
111       assertEquals(lastTokenRow, row.getTokenRow(false));
112     }
113   }
114   
115   public void testGermanSort() {
116     assertEquals("aüÄÄ", Language.de.textNorm("aueAeAE", false));
117     final List<String> words = Arrays.asList(
118         "er-ben",
119         "erben",
120         "Erben",
121         "Erbse",
122         "Erbsen",
123         "essen",
124         "Essen",
125         "Grosformat",
126         "Grosformats",
127         "Grossformat",
128         "Großformat",
129         "Grossformats",
130         "Großformats",
131         "Großpoo",
132         "Großpoos",
133         "hulle",
134         "Hulle",
135         "hülle",
136         "huelle",
137         "Hülle",
138         "Huelle",
139         "Hum"
140         );
141     assertEquals(0, Language.de.sortComparator.compare("hülle", "huelle"));
142     assertEquals(0, Language.de.sortComparator.compare("huelle", "hülle"));
143     
144     assertEquals(-1, Language.de.sortComparator.compare("hülle", "Hülle"));
145     assertEquals(0, Language.de.findComparator.compare("hülle", "Hülle"));
146     assertEquals(-1, Language.de.findComparator.compare("hulle", "Hülle"));
147
148     
149     for (final String s : words) {
150       System.out.println(s + "\t" + Language.de.textNorm(s, false));
151     }
152     final List<String> sorted = new ArrayList<String>(words);
153 //    Collections.shuffle(shuffled, new Random(0));
154     Collections.sort(sorted, Language.de.sortComparator);
155     System.out.println(sorted.toString());
156     for (int i = 0; i < words.size(); ++i) {
157       System.out.println(words.get(i) + "\t" + sorted.get(i));
158       assertEquals(words.get(i), sorted.get(i));
159     }
160   }
161
162   @SuppressWarnings("unchecked")
163   public void testEnglishSort() {
164
165     final List<String> words = Arrays.asList(
166         "pre-print", 
167         "preppie", 
168         "preppy",
169         "preprocess");
170     
171     final List<String> sorted = new ArrayList<String>(words);
172     Collections.sort(sorted, Language.en.getSortCollator());
173     for (int i = 0; i < words.size(); ++i) {
174       if (i > 0) {
175         assertTrue(Language.en.getSortCollator().compare(words.get(i-1), words.get(i)) < 0);
176       }
177       System.out.println(words.get(i) + "\t" + sorted.get(i));
178       assertEquals(words.get(i), sorted.get(i));
179     }
180     
181     assertTrue(Language.en.getSortCollator().compare("pre-print", "preppy") < 0);
182
183   }
184   
185   public void testLanguage() {
186     assertEquals(Language.de, Language.lookup("de"));
187     assertEquals(Language.en, Language.lookup("en"));
188     assertEquals("es", Language.lookup("es").getSymbol());
189   }
190
191
192 }