]> 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.concurrent.atomic.AtomicBoolean;
6
7 import junit.framework.TestCase;
8
9 public class DictionaryTest extends TestCase {
10   
11   RandomAccessFile raf;
12   Dictionary dict;
13   Index deIndex; 
14   
15   @Override
16   public void setUp() {
17     try {
18       raf = new RandomAccessFile("testdata/de_en.dict", "r");
19       dict = new Dictionary(raf);
20     } catch (IOException e) {
21       throw new RuntimeException(e);
22     }
23
24     deIndex = dict.indices.get(0);
25 }
26   
27   @Override
28   public void tearDown() {
29     try {
30       raf.close();
31     } catch (IOException e) {
32       throw new RuntimeException(e);
33     }
34   }
35   
36
37   public void testGermanMetadata() throws IOException {
38     assertEquals("de", deIndex.shortName);
39     assertEquals("de->en", deIndex.longName);
40   }
41   
42   public void testGermanIndex() throws IOException {
43     for (final Index.IndexEntry indexEntry : deIndex.sortedIndexEntries) {
44       System.out.println("testing: " + indexEntry.token);
45       final TokenRow row = deIndex.find(indexEntry.token, new AtomicBoolean(
46           false));
47       assertEquals(indexEntry.token.toLowerCase(), row.getToken().toLowerCase());
48     }
49
50     assertEquals("aaac", deIndex.find("AAAC", new AtomicBoolean(false)).getToken());
51     assertEquals("aaac", deIndex.find("aaac", new AtomicBoolean(false)).getToken());
52     assertEquals("aaac", deIndex.find("AAAc", new AtomicBoolean(false)).getToken());
53     assertEquals("aaac", deIndex.find("aaac", new AtomicBoolean(false)).getToken());
54   }
55   
56   public void testGermanTokenRows() {
57     // Pre-cache a few of these, just to make sure that's working.
58     for (int i = 0; i < deIndex.rows.size(); i += 7) {
59       deIndex.rows.get(i).getTokenRow(true);
60     }
61     
62     // Do the exhaustive searching.
63     TokenRow lastTokenRow = null;
64     for (final RowBase row : deIndex.rows) {
65       if (row instanceof TokenRow) {
66         lastTokenRow = (TokenRow) row;
67       }
68       assertEquals(lastTokenRow, row.getTokenRow(true));
69     }
70
71     // Now they're all cached, we shouldn't have to search.
72     for (final RowBase row : deIndex.rows) {
73       if (row instanceof TokenRow) {
74         lastTokenRow = (TokenRow) row;
75       }
76       // This will break if the Row cache isn't big enough.
77       assertEquals(lastTokenRow, row.getTokenRow(false));
78     }
79
80   }
81
82
83 }