]> 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
14 public class DictionaryTest extends TestCase {
15   
16   RandomAccessFile raf;
17   Dictionary dict;
18   Index deIndex; 
19   
20   @Override
21   public void setUp() {
22     try {
23       raf = new RandomAccessFile("testdata/de_en.dict", "r");
24       dict = new Dictionary(raf);
25     } catch (IOException e) {
26       throw new RuntimeException(e);
27     }
28
29     deIndex = dict.indices.get(0);
30 }
31   
32   @Override
33   public void tearDown() {
34     try {
35       raf.close();
36     } catch (IOException e) {
37       throw new RuntimeException(e);
38     }
39   }
40   
41
42   public void testGermanMetadata() throws IOException {
43     assertEquals("de", deIndex.shortName);
44     assertEquals("de->en", deIndex.longName);
45   }
46   
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(
51           false));
52       assertEquals(indexEntry.token.toLowerCase(), row.getToken().toLowerCase());
53     }
54
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());
60     
61     // Before the beginning.
62     assertEquals("40", deIndex.find("__", new AtomicBoolean(false)).getToken());
63     
64     // After the end.
65     assertEquals("Zweckorientiertheit", deIndex.find("ZZZZZ", new AtomicBoolean(false)).getToken());
66
67     assertEquals("aaac", deIndex.find("aaaca", new AtomicBoolean(false)).getToken());
68     
69     assertEquals("überprüfe", deIndex.find("ueberprüfe", new AtomicBoolean(false)).getToken());
70     assertEquals("überprüfe", deIndex.find("ueberpruefe", new AtomicBoolean(false)).getToken());
71
72   }
73   
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);
78     }
79     
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;
85       }
86       assertEquals(lastTokenRow, row.getTokenRow(true));
87     }
88
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;
93       }
94       // This will break if the Row cache isn't big enough.
95       assertEquals(lastTokenRow, row.getTokenRow(false));
96     }
97   }
98   
99   @SuppressWarnings("unchecked")
100   public void testGermanSort() {
101     assertEquals("aüÄÄ", Language.de.textNorm("aueAeAE", false));
102     final List<String> words = Arrays.asList(
103         "er-ben",
104         "erben",
105         "Erben",
106         "Erbse",
107         "Erbsen",
108         "essen",
109         "Essen",
110         "Grosformat",
111         "Grosformats",
112         "Grossformat",
113         "Großformat",
114         "Grossformats",
115         "Großformats",
116         "Großpoo",
117         "Großpoos",
118         "hulle",
119         "Hulle",
120         "hülle",
121         "huelle",
122         "Hülle",
123         "Huelle",
124         "Hum"
125         );
126     assertEquals(0, Language.de.sortComparator.compare("hülle", "huelle"));
127     assertEquals(0, Language.de.sortComparator.compare("huelle", "hülle"));
128     
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"));
132
133     
134     for (final String s : words) {
135       System.out.println(s + "\t" + Language.de.textNorm(s, false));
136     }
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));
144     }
145   }
146
147   @SuppressWarnings("unchecked")
148   public void testEnglishSort() {
149
150     final List<String> words = Arrays.asList(
151         "pre-print", 
152         "preppie", 
153         "preppy",
154         "preprocess");
155     
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) {
159       if (i > 0) {
160         assertTrue(Language.en.getSortCollator().compare(words.get(i-1), words.get(i)) < 0);
161       }
162       System.out.println(words.get(i) + "\t" + sorted.get(i));
163       assertEquals(words.get(i), sorted.get(i));
164     }
165     
166     assertTrue(Language.en.getSortCollator().compare("pre-print", "preppy") < 0);
167
168   }
169   
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());
174   }
175
176
177 }