]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/IndexTest.java
addingList
[DictionaryPC.git] / src / com / hughes / android / dictionary / IndexTest.java
1 package com.hughes.android.dictionary;\r
2 \r
3 import java.io.IOException;\r
4 import java.io.RandomAccessFile;\r
5 import java.util.LinkedHashSet;\r
6 import java.util.Set;\r
7 \r
8 import junit.framework.TestCase;\r
9 \r
10 import com.hughes.android.dictionary.Index.Node;\r
11 import com.hughes.util.FileUtil;\r
12 \r
13 public class IndexTest extends TestCase {\r
14 \r
15   static final String file = "c:\\dict-de-en.txt";\r
16   static final String file_index = file + "_index_0";\r
17   \r
18   public void testRoot() throws IOException {\r
19     System.out.println("  testRoot");\r
20     final Index index = new Index(file_index);\r
21     final Node node = index.lookup("");\r
22     assertNotNull(node);\r
23     \r
24     assertEquals(312220, node.descendantTokenCount);\r
25     assertEquals(1087063, node.descendantEntryCount);\r
26     \r
27     for (final String token : node.tokenToOffsets.keySet()) {\r
28       System.out.println(token);\r
29       assertTrue(token.toLowerCase().contains("handhubwagen"));\r
30     }\r
31   }\r
32   \r
33   public void testLookup() throws IOException {\r
34     System.out.println("  testLookup");\r
35     final Index index = new Index(file_index);\r
36     final Node node = index.lookup("handhubwagen");\r
37     assertNotNull(node);\r
38     \r
39     assertEquals(1, node.descendantTokenCount);\r
40     assertEquals(2, node.descendantEntryCount);\r
41     \r
42     for (final String token : node.tokenToOffsets.keySet()) {\r
43       System.out.println(token);\r
44       assertTrue(token.toLowerCase().contains("handhubwagen"));\r
45     }\r
46   }\r
47 \r
48   public void testGetDescendantOffsets() throws IOException {\r
49     System.out.println("  testGetDescendantOffsets");\r
50     final Index index = new Index(file_index);\r
51     \r
52     final Node node = index.lookup("handhebe");\r
53     assertNotNull(node);\r
54     assertEquals("handhebel", node.nodeHandle.normalizedToken);\r
55     final Set<Integer> offsets = new LinkedHashSet<Integer>();\r
56     node.getDescendantEntryOffsets(offsets, 10);\r
57     final RandomAccessFile raf = new RandomAccessFile(file, "r");\r
58     for (final Integer offset : offsets) {\r
59       final String entry = FileUtil.readLine(raf, offset);\r
60       System.out.println(entry);\r
61       assertTrue(entry.toLowerCase().contains(node.nodeHandle.normalizedToken));\r
62     }\r
63   }\r
64 \r
65   public void testGetDescendants() throws IOException {\r
66     System.out.println("  testGetDescendant");\r
67     final Index index = new Index(file_index);\r
68     final RandomAccessFile raf = new RandomAccessFile(file, "r");\r
69     for (int i = 1000000; i < 1000050; ++i) {\r
70       final Object o = index.root.getDescendant(i);\r
71       if (o instanceof Integer) {\r
72         System.out.println("  " + FileUtil.readLine(raf, (Integer)o));\r
73       } else {\r
74         System.out.println(o);\r
75       }\r
76     }\r
77     raf.close();\r
78   }\r
79 \r
80 }\r