FusionDictionaryTest.java revision a411595b169c1f136d09d114a458def1f99f91d9
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.inputmethod.latin.makedict;
18
19import com.android.inputmethod.latin.makedict.FusionDictionary;
20import com.android.inputmethod.latin.makedict.FusionDictionary.CharGroup;
21import com.android.inputmethod.latin.makedict.FusionDictionary.DictionaryOptions;
22import com.android.inputmethod.latin.makedict.FusionDictionary.Node;
23import com.android.inputmethod.latin.makedict.Word;
24
25import junit.framework.TestCase;
26
27import java.util.ArrayList;
28import java.util.HashMap;
29import java.util.Random;
30
31/**
32 * Unit tests for BinaryDictInputOutput.
33 */
34public class FusionDictionaryTest extends TestCase {
35    private static final ArrayList<String> sWords = new ArrayList<String>();
36    private static final int MAX_UNIGRAMS = 1000;
37
38    private void prepare(final long seed) {
39        System.out.println("Seed is " + seed);
40        final Random random = new Random(seed);
41        sWords.clear();
42        generateWords(MAX_UNIGRAMS, random);
43    }
44
45    /**
46     * Generates a random word.
47     */
48    private String generateWord(final Random random) {
49        StringBuilder builder = new StringBuilder("a");
50        int count = random.nextInt() % 30;
51        while (count > 0) {
52            final long r = Math.abs(random.nextInt());
53            if (r < 0) continue;
54            // Don't insert 0~20, but insert any other code point.
55            // Code points are in the range 0~0x10FFFF.
56            if (builder.length() < 7)
57                builder.appendCodePoint((int)(20 +r % (0x10FFFF - 20)));
58            --count;
59        }
60        if (builder.length() == 1) return generateWord(random);
61        return builder.toString();
62    }
63
64    private void generateWords(final int number, final Random random) {
65        while (sWords.size() < number) {
66            sWords.add(generateWord(random));
67        }
68    }
69
70    private void checkDictionary(final FusionDictionary dict, final ArrayList<String> words,
71            int limit) {
72        assertNotNull(dict);
73        for (final String word : words) {
74            if (--limit < 0) return;
75            final CharGroup cg = FusionDictionary.findWordInTree(dict.mRoot, word);
76            if (null == cg) {
77                System.out.println("word " + dumpWord(word));
78                dumpDict(dict);
79            }
80            assertNotNull(cg);
81        }
82    }
83
84    private String dumpWord(final String word) {
85        final StringBuilder sb = new StringBuilder("");
86        for (int i = 0; i < word.length(); i = word.offsetByCodePoints(i, 1)) {
87            sb.append(word.codePointAt(i));
88            sb.append(" ");
89        }
90        return sb.toString();
91    }
92
93    private void dumpDict(final FusionDictionary dict) {
94        for (Word w : dict) {
95            System.out.println("Word " + dumpWord(w.mWord));
96        }
97    }
98
99    // Test the flattened array contains the expected number of nodes, and
100    // that it does not contain any duplicates.
101    public void testFusion() {
102        final FusionDictionary dict = new FusionDictionary(new Node(),
103                new DictionaryOptions(new HashMap<String, String>(),
104                        false /* germanUmlautProcessing */, false /* frenchLigatureProcessing */));
105        final long time = System.currentTimeMillis();
106        prepare(time);
107        for (int i = 0; i < sWords.size(); ++i) {
108            System.out.println("Adding in pos " + i + " : " + dumpWord(sWords.get(i)));
109            dict.add(sWords.get(i), 180, null, false);
110            dumpDict(dict);
111            checkDictionary(dict, sWords, i);
112        }
113    }
114}
115