Add ByteArrayUtilsTest.

Change-Id: I4695a2573b4d2d8331fc9ce1e9ff83397791a586
main
Keisuke Kuroyanagi 2014-08-25 16:52:12 +09:00
parent e38f79182d
commit c04ae77583
4 changed files with 95 additions and 2 deletions

View File

@ -128,6 +128,7 @@ LATIN_IME_CORE_TEST_FILES := \
suggest/policyimpl/dictionary/structure/v4/content/language_model_dict_content_test.cpp \
suggest/policyimpl/dictionary/structure/v4/content/probability_entry_test.cpp \
suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer_test.cpp \
suggest/policyimpl/dictionary/utils/byte_array_utils_test.cpp \
suggest/policyimpl/dictionary/utils/trie_map_test.cpp \
utils/autocorrection_threshold_utils_test.cpp \
utils/int_array_view_test.cpp

View File

@ -31,7 +31,7 @@ uint32_t BufferWithExtendableBuffer::readUint(const int size, const int pos) con
uint32_t BufferWithExtendableBuffer::readUintAndAdvancePosition(const int size,
int *const pos) const {
const int value = readUint(size, *pos);
const uint32_t value = readUint(size, *pos);
*pos += size;
return value;
}

View File

@ -114,7 +114,7 @@ class ByteArrayUtils {
return buffer[(*pos)++];
}
static AK_FORCE_INLINE int readUint(const uint8_t *const buffer,
static AK_FORCE_INLINE uint32_t readUint(const uint8_t *const buffer,
const int size, const int pos) {
// size must be in 1 to 4.
ASSERT(size >= 1 && size <= 4);

View File

@ -0,0 +1,92 @@
/*
* Copyright (C) 2014 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.
*/
#include "suggest/policyimpl/dictionary/utils/byte_array_utils.h"
#include <gtest/gtest.h>
#include <cstdint>
namespace latinime {
namespace {
TEST(ByteArrayUtilsTest, TestReadInt) {
const uint8_t buffer[] = { 0x1u, 0x8Au, 0x0u, 0xAAu };
EXPECT_EQ(0x01u, ByteArrayUtils::readUint8(buffer, 0));
EXPECT_EQ(0x8Au, ByteArrayUtils::readUint8(buffer, 1));
EXPECT_EQ(0x0u, ByteArrayUtils::readUint8(buffer, 2));
EXPECT_EQ(0xAAu, ByteArrayUtils::readUint8(buffer, 3));
EXPECT_EQ(0x018Au, ByteArrayUtils::readUint16(buffer, 0));
EXPECT_EQ(0x8A00u, ByteArrayUtils::readUint16(buffer, 1));
EXPECT_EQ(0xAAu, ByteArrayUtils::readUint16(buffer, 2));
EXPECT_EQ(0x18A00AAu, ByteArrayUtils::readUint32(buffer, 0));
int pos = 0;
EXPECT_EQ(0x18A00, ByteArrayUtils::readSint24AndAdvancePosition(buffer, &pos));
pos = 1;
EXPECT_EQ(-0xA00AA, ByteArrayUtils::readSint24AndAdvancePosition(buffer, &pos));
}
TEST(ByteArrayUtilsTest, TestWriteAndReadInt) {
uint8_t buffer[4];
int pos = 0;
const uint8_t data_1B = 0xC8;
ByteArrayUtils::writeUintAndAdvancePosition(buffer, data_1B, 1, &pos);
EXPECT_EQ(data_1B, ByteArrayUtils::readUint(buffer, 1, 0));
pos = 0;
const uint32_t data_4B = 0xABCD1234;
ByteArrayUtils::writeUintAndAdvancePosition(buffer, data_4B, 4, &pos);
EXPECT_EQ(data_4B, ByteArrayUtils::readUint(buffer, 4, 0));
}
TEST(ByteArrayUtilsTest, TestReadCodePoint) {
const uint8_t buffer[] = { 0x10, 0xFF, 0x00u, 0x20u, 0x41u, 0x1Fu, 0x60 };
EXPECT_EQ(0x10FF00, ByteArrayUtils::readCodePoint(buffer, 0));
EXPECT_EQ(0x20, ByteArrayUtils::readCodePoint(buffer, 3));
EXPECT_EQ(0x41, ByteArrayUtils::readCodePoint(buffer, 4));
EXPECT_EQ(NOT_A_CODE_POINT, ByteArrayUtils::readCodePoint(buffer, 5));
int pos = 0;
int codePointArray[3];
EXPECT_EQ(3, ByteArrayUtils::readStringAndAdvancePosition(buffer, MAX_WORD_LENGTH,
codePointArray, &pos));
EXPECT_EQ(0x10FF00, codePointArray[0]);
EXPECT_EQ(0x20, codePointArray[1]);
EXPECT_EQ(0x41, codePointArray[2]);
EXPECT_EQ(0x60, ByteArrayUtils::readCodePoint(buffer, pos));
}
TEST(ByteArrayUtilsTest, TestWriteAndReadCodePoint) {
uint8_t buffer[10];
const int codePointArray[] = { 0x10FF00, 0x20, 0x41 };
int pos = 0;
ByteArrayUtils::writeCodePointsAndAdvancePosition(buffer, codePointArray, 3,
true /* writesTerminator */, &pos);
EXPECT_EQ(0x10FF00, ByteArrayUtils::readCodePoint(buffer, 0));
EXPECT_EQ(0x20, ByteArrayUtils::readCodePoint(buffer, 3));
EXPECT_EQ(0x41, ByteArrayUtils::readCodePoint(buffer, 4));
EXPECT_EQ(NOT_A_CODE_POINT, ByteArrayUtils::readCodePoint(buffer, 5));
}
} // namespace
} // namespace latinime