]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/DictionaryTest.java
43fb7ab407d324d4eb2653a16fa748c265f0a1fd
[DictionaryPC.git] / src / com / hughes / android / dictionary / DictionaryTest.java
1 package com.hughes.android.dictionary;\r
2 \r
3 import java.io.File;\r
4 import java.io.IOException;\r
5 import java.io.RandomAccessFile;\r
6 import java.util.Arrays;\r
7 import java.util.List;\r
8 \r
9 import junit.framework.TestCase;\r
10 \r
11 import com.hughes.android.dictionary.Dictionary.IndexEntry;\r
12 import com.hughes.android.dictionary.Dictionary.Row;\r
13 \r
14 public class DictionaryTest extends TestCase {\r
15 \r
16   public void testDictionary() throws IOException {\r
17     final File file = File.createTempFile("asdf", "asdf");\r
18     file.deleteOnExit();\r
19 \r
20     final Dictionary goldenDict;\r
21     final List<Entry> entries = Arrays.asList(\r
22         new Entry("der Hund", "the dog"),\r
23         new Entry("Die grosse Katze", "The big cat"), \r
24         new Entry("die Katze", "the cat"),\r
25         new Entry("gross", "big"),\r
26         new Entry("Dieb", "thief"),\r
27         new Entry("rennen", "run"));\r
28 \r
29     {\r
30       final Dictionary dict = new Dictionary("de", "en");\r
31       for (final Entry entry : entries) {\r
32         dict.entries.add(entry);\r
33       }\r
34       DictionaryBuilder.createIndex(dict, Entry.LANG1);\r
35       DictionaryBuilder.createIndex(dict, Entry.LANG2);\r
36       final RandomAccessFile raf = new RandomAccessFile(file, "rw");\r
37       dict.write(raf);\r
38       raf.close();\r
39       \r
40       goldenDict = dict;\r
41     }\r
42 \r
43     final RandomAccessFile raf = new RandomAccessFile(file, "r");\r
44     final Dictionary dict = new Dictionary(raf);\r
45     \r
46     assertEquals(entries, dict.entries);\r
47     \r
48     assertEquals("der", dict.languages[0].sortedIndex.get(0).word);\r
49     assertEquals("Die", dict.languages[0].sortedIndex.get(1).word);\r
50     \r
51     for (final IndexEntry indexEntry : dict.languages[0].sortedIndex) {\r
52       System.out.println(indexEntry);\r
53     }\r
54 \r
55     int rowCount = 0;\r
56     for (final Row row : dict.languages[0].rows) {\r
57       if (row.index >= 0) {\r
58         System.out.println("  " + rowCount + ":" + dict.entries.get(row.index));\r
59       } else {\r
60         System.out.println(rowCount + ":" + dict.languages[0].sortedIndex.get(-row.index - 1));\r
61       }\r
62       ++rowCount;\r
63     }\r
64 \r
65 \r
66   }\r
67 \r
68 }\r