Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 93f74baf authored by Jean Chalard's avatar Jean Chalard Committed by Android (Google) Code Review
Browse files

Merge "Add tests to dicttool test."

parents 1db2df08 23d4eb55
Loading
Loading
Loading
Loading
+81 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.inputmethod.latin;

import com.android.inputmethod.latin.makedict.BinaryDictInputOutput.FusionDictionaryBufferInterface;

/**
 * This class provides an implementation for the FusionDictionary buffer interface that is backed
 * by a simpled byte array. It allows to create a binary dictionary in memory.
 */
public final class ByteArrayWrapper implements FusionDictionaryBufferInterface {
    private byte[] mBuffer;
    private int mPosition;

    public ByteArrayWrapper(final byte[] buffer) {
        mBuffer = buffer;
        mPosition = 0;
    }

    @Override
    public int readUnsignedByte() {
        return mBuffer[mPosition++] & 0xFF;
    }

    @Override
    public int readUnsignedShort() {
        final int retval = readUnsignedByte();
        return (retval << 8) + readUnsignedByte();
    }

    @Override
    public int readUnsignedInt24() {
        final int retval = readUnsignedShort();
        return (retval << 8) + readUnsignedByte();
    }

    @Override
    public int readInt() {
        final int retval = readUnsignedShort();
        return (retval << 16) + readUnsignedShort();
    }

    @Override
    public int position() {
        return mPosition;
    }

    @Override
    public void position(int position) {
        mPosition = position;
    }

    @Override
    public void put(final byte b) {
        mBuffer[mPosition++] = b;
    }

    @Override
    public int limit() {
        return mBuffer.length - 1;
    }

    @Override
    public int capacity() {
        return mBuffer.length;
    }
}
+0 −58
Original line number Diff line number Diff line
@@ -53,64 +53,6 @@ public final class UserHistoryDictIOUtils {
        public int getFrequency(final String word1, final String word2);
    }

    public static final class ByteArrayWrapper implements FusionDictionaryBufferInterface {
        private byte[] mBuffer;
        private int mPosition;

        public ByteArrayWrapper(final byte[] buffer) {
            mBuffer = buffer;
            mPosition = 0;
        }

        @Override
        public int readUnsignedByte() {
            return mBuffer[mPosition++] & 0xFF;
        }

        @Override
        public int readUnsignedShort() {
            final int retval = readUnsignedByte();
            return (retval << 8) + readUnsignedByte();
        }

        @Override
        public int readUnsignedInt24() {
            final int retval = readUnsignedShort();
            return (retval << 8) + readUnsignedByte();
        }

        @Override
        public int readInt() {
            final int retval = readUnsignedShort();
            return (retval << 16) + readUnsignedShort();
        }

        @Override
        public int position() {
            return mPosition;
        }

        @Override
        public void position(int position) {
            mPosition = position;
        }

        @Override
        public void put(final byte b) {
            mBuffer[mPosition++] = b;
        }

        @Override
        public int limit() {
            return mBuffer.length - 1;
        }

        @Override
        public int capacity() {
            return mBuffer.length;
        }
    }

    /**
     * Writes dictionary to file.
     */
+1 −1
Original line number Diff line number Diff line
@@ -261,7 +261,7 @@ public final class UserHistoryDictionary extends ExpandableDictionary {
            inStream = new FileInputStream(file);
            inStream.read(buffer);
            UserHistoryDictIOUtils.readDictionaryBinary(
                    new UserHistoryDictIOUtils.ByteArrayWrapper(buffer), listener);
                    new ByteArrayWrapper(buffer), listener);
        } catch (FileNotFoundException e) {
            // This is an expected condition: we don't have a user history dictionary for this
            // language yet. It will be created sometime later.
+1 −1
Original line number Diff line number Diff line
@@ -153,7 +153,7 @@ public class UserHistoryDictIOUtilsTests extends AndroidTestCase
            inStream.read(buffer);

            UserHistoryDictIOUtils.readDictionaryBinary(
                    new UserHistoryDictIOUtils.ByteArrayWrapper(buffer), listener);
                    new ByteArrayWrapper(buffer), listener);
        } catch (FileNotFoundException e) {
            Log.e(TAG, "file not found", e);
        } catch (IOException e) {
+2 −2
Original line number Diff line number Diff line
@@ -22,8 +22,8 @@ import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;
import android.util.SparseArray;

import com.android.inputmethod.latin.ByteArrayWrapper;
import com.android.inputmethod.latin.CollectionUtils;
import com.android.inputmethod.latin.UserHistoryDictIOUtils;
import com.android.inputmethod.latin.makedict.BinaryDictInputOutput.FusionDictionaryBufferInterface;
import com.android.inputmethod.latin.makedict.FormatSpec.FileHeader;
import com.android.inputmethod.latin.makedict.FusionDictionary.CharGroup;
@@ -106,7 +106,7 @@ public class BinaryDictIOTests extends AndroidTestCase {
            if (bufferType == USE_BYTE_ARRAY) {
                final byte[] array = new byte[(int)file.length()];
                inStream.read(array);
                return new UserHistoryDictIOUtils.ByteArrayWrapper(array);
                return new ByteArrayWrapper(array);
            } else if (bufferType == USE_BYTE_BUFFER){
                final ByteBuffer buffer = inStream.getChannel().map(
                        FileChannel.MapMode.READ_ONLY, 0, file.length());
Loading