BinaryDictOffdeviceUtilsTests.java revision 8ffc631826b108423f98e3ff4d987f067cbc4e0c
1/* 2 * Copyright (C) 2012 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.dicttool; 18 19import com.android.inputmethod.latin.makedict.DictDecoder; 20import com.android.inputmethod.latin.makedict.DictEncoder; 21import com.android.inputmethod.latin.makedict.DictionaryHeader; 22import com.android.inputmethod.latin.makedict.FormatSpec; 23import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions; 24import com.android.inputmethod.latin.makedict.FusionDictionary; 25import com.android.inputmethod.latin.makedict.FusionDictionary.DictionaryOptions; 26import com.android.inputmethod.latin.makedict.FusionDictionary.PtNodeArray; 27import com.android.inputmethod.latin.makedict.ProbabilityInfo; 28import com.android.inputmethod.latin.makedict.UnsupportedFormatException; 29import com.android.inputmethod.latin.makedict.Ver2DictEncoder; 30 31import junit.framework.TestCase; 32 33import java.io.File; 34import java.io.BufferedOutputStream; 35import java.io.FileOutputStream; 36import java.io.IOException; 37import java.io.OutputStream; 38import java.util.HashMap; 39 40/** 41 * Unit tests for BinaryDictOffdeviceUtils 42 */ 43public class BinaryDictOffdeviceUtilsTests extends TestCase { 44 private static final int TEST_FREQ = 37; // Some arbitrary value unlikely to happen by chance 45 46 public void testGetRawDictWorks() throws IOException, UnsupportedFormatException { 47 final String VERSION = "1"; 48 final String LOCALE = "test"; 49 final String ID = "main:test"; 50 51 // Create a thrice-compressed dictionary file. 52 final DictionaryOptions testOptions = new DictionaryOptions(new HashMap<String, String>()); 53 testOptions.mAttributes.put(DictionaryHeader.DICTIONARY_VERSION_KEY, VERSION); 54 testOptions.mAttributes.put(DictionaryHeader.DICTIONARY_LOCALE_KEY, LOCALE); 55 testOptions.mAttributes.put(DictionaryHeader.DICTIONARY_ID_KEY, ID); 56 final FusionDictionary dict = new FusionDictionary(new PtNodeArray(), testOptions); 57 dict.add("foo", new ProbabilityInfo(TEST_FREQ), null, false /* isNotAWord */); 58 dict.add("fta", new ProbabilityInfo(1), null, false /* isNotAWord */); 59 dict.add("ftb", new ProbabilityInfo(1), null, false /* isNotAWord */); 60 dict.add("bar", new ProbabilityInfo(1), null, false /* isNotAWord */); 61 dict.add("fool", new ProbabilityInfo(1), null, false /* isNotAWord */); 62 63 final File dst = File.createTempFile("testGetRawDict", ".tmp"); 64 dst.deleteOnExit(); 65 66 final OutputStream out = Compress.getCompressedStream( 67 Compress.getCompressedStream( 68 Compress.getCompressedStream( 69 new BufferedOutputStream(new FileOutputStream(dst))))); 70 final DictEncoder dictEncoder = new Ver2DictEncoder(out); 71 dictEncoder.writeDictionary(dict, new FormatOptions(2, false)); 72 73 // Test for an actually compressed dictionary and its contents 74 final BinaryDictOffdeviceUtils.DecoderChainSpec decodeSpec = 75 BinaryDictOffdeviceUtils.getRawDictionaryOrNull(dst); 76 for (final String step : decodeSpec.mDecoderSpec) { 77 assertEquals("Wrong decode spec", BinaryDictOffdeviceUtils.COMPRESSION, step); 78 } 79 assertEquals("Wrong decode spec", 3, decodeSpec.mDecoderSpec.size()); 80 final DictDecoder dictDecoder = FormatSpec.getDictDecoder(decodeSpec.mFile); 81 final FusionDictionary resultDict = dictDecoder.readDictionaryBinary( 82 null /* dict : an optional dictionary to add words to, or null */, 83 false /* deleteDictIfBroken */); 84 assertEquals("Wrong version attribute", VERSION, resultDict.mOptions.mAttributes.get( 85 DictionaryHeader.DICTIONARY_VERSION_KEY)); 86 assertEquals("Wrong locale attribute", LOCALE, resultDict.mOptions.mAttributes.get( 87 DictionaryHeader.DICTIONARY_LOCALE_KEY)); 88 assertEquals("Wrong id attribute", ID, resultDict.mOptions.mAttributes.get( 89 DictionaryHeader.DICTIONARY_ID_KEY)); 90 assertEquals("Dictionary can't be read back correctly", 91 FusionDictionary.findWordInTree(resultDict.mRootNodeArray, "foo").getProbability(), 92 TEST_FREQ); 93 } 94 95 public void testGetRawDictFails() throws IOException { 96 // Randomly create some 4k file containing garbage 97 final File dst = File.createTempFile("testGetRawDict", ".tmp"); 98 dst.deleteOnExit(); 99 final OutputStream out = new BufferedOutputStream(new FileOutputStream(dst)); 100 for (int i = 0; i < 1024; ++i) { 101 out.write(0x12345678); 102 } 103 out.close(); 104 105 // Test that a random data file actually fails 106 assertNull("Wrongly identified data file", 107 BinaryDictOffdeviceUtils.getRawDictionaryOrNull(dst)); 108 109 final File gzDst = File.createTempFile("testGetRawDict", ".tmp"); 110 gzDst.deleteOnExit(); 111 final OutputStream gzOut = 112 Compress.getCompressedStream(new BufferedOutputStream(new FileOutputStream(gzDst))); 113 for (int i = 0; i < 1024; ++i) { 114 gzOut.write(0x12345678); 115 } 116 gzOut.close(); 117 118 // Test that a compressed random data file actually fails 119 assertNull("Wrongly identified data file", 120 BinaryDictOffdeviceUtils.getRawDictionaryOrNull(gzDst)); 121 } 122} 123