• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.inputmethod.latin.makedict;
18 
19 import com.android.inputmethod.latin.makedict.FormatSpec.DictionaryOptions;
20 import com.android.inputmethod.latin.makedict.FusionDictionary;
21 import com.android.inputmethod.latin.makedict.FusionDictionary.PtNode;
22 import com.android.inputmethod.latin.makedict.FusionDictionary.PtNodeArray;
23 import com.android.inputmethod.latin.makedict.WordProperty;
24 
25 import junit.framework.TestCase;
26 
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.Random;
30 
31 /**
32  * Unit tests for FusionDictionary.
33  */
34 public class FusionDictionaryTest extends TestCase {
35     private static final ArrayList<String> sWords = new ArrayList<>();
36     private static final int MAX_UNIGRAMS = 1000;
37 
prepare(final long seed)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      */
generateWord(final Random random)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 
generateWords(final int number, final Random random)64     private void generateWords(final int number, final Random random) {
65         while (sWords.size() < number) {
66             sWords.add(generateWord(random));
67         }
68     }
69 
checkDictionary(final FusionDictionary dict, final ArrayList<String> words, final int limit)70     private static void checkDictionary(final FusionDictionary dict, final ArrayList<String> words,
71             final int limit) {
72         assertNotNull(dict);
73         int count = limit;
74         for (final String word : words) {
75             if (--count < 0) return;
76             final PtNode ptNode = FusionDictionary.findWordInTree(dict.mRootNodeArray, word);
77             assertNotNull(ptNode);
78         }
79     }
80 
dumpWord(final String word)81     private static String dumpWord(final String word) {
82         final StringBuilder sb = new StringBuilder("");
83         for (int i = 0; i < word.length(); i = word.offsetByCodePoints(i, 1)) {
84             sb.append(word.codePointAt(i));
85             sb.append(" ");
86         }
87         return sb.toString();
88     }
89 
dumpDict(final FusionDictionary dict)90     private static void dumpDict(final FusionDictionary dict) {
91         for (WordProperty wordProperty : dict) {
92             System.out.println("Word " + dumpWord(wordProperty.mWord));
93         }
94     }
95 
96     // Test the flattened array contains the expected number of nodes, and
97     // that it does not contain any duplicates.
testFusion()98     public void testFusion() {
99         final FusionDictionary dict = new FusionDictionary(new PtNodeArray(),
100                 new DictionaryOptions(new HashMap<String, String>()));
101         final long time = System.currentTimeMillis();
102         prepare(time);
103         for (int i = 0; i < sWords.size(); ++i) {
104             System.out.println("Adding in pos " + i + " : " + dumpWord(sWords.get(i)));
105             dict.add(sWords.get(i), new ProbabilityInfo(180), false,
106                     false /* isPossiblyOffensive */);
107             dumpDict(dict);
108             checkDictionary(dict, sWords, i);
109         }
110     }
111 }
112