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.FusionDictionary; 20 import com.android.inputmethod.latin.makedict.FusionDictionary.PtNode; 21 import com.android.inputmethod.latin.makedict.FusionDictionary.DictionaryOptions; 22 import com.android.inputmethod.latin.makedict.FusionDictionary.PtNodeArray; 23 import com.android.inputmethod.latin.makedict.Word; 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<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 PtNode ptNode = FusionDictionary.findWordInTree(dict.mRootNodeArray, word); 76 assertNotNull(ptNode); 77 } 78 } 79 80 private String dumpWord(final String word) { 81 final StringBuilder sb = new StringBuilder(""); 82 for (int i = 0; i < word.length(); i = word.offsetByCodePoints(i, 1)) { 83 sb.append(word.codePointAt(i)); 84 sb.append(" "); 85 } 86 return sb.toString(); 87 } 88 89 private void dumpDict(final FusionDictionary dict) { 90 for (Word w : dict) { 91 System.out.println("Word " + dumpWord(w.mWord)); 92 } 93 } 94 95 // Test the flattened array contains the expected number of nodes, and 96 // that it does not contain any duplicates. 97 public void testFusion() { 98 final FusionDictionary dict = new FusionDictionary(new PtNodeArray(), 99 new DictionaryOptions(new HashMap<String, String>(), 100 false /* germanUmlautProcessing */, false /* frenchLigatureProcessing */)); 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), 180, null, false); 106 dumpDict(dict); 107 checkDictionary(dict, sWords, i); 108 } 109 } 110 } 111