From c04ae7758358eac29899fa43cfa91bb32f9c0cf1 Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Mon, 25 Aug 2014 16:52:12 +0900 Subject: [PATCH] Add ByteArrayUtilsTest. Change-Id: I4695a2573b4d2d8331fc9ce1e9ff83397791a586 --- native/jni/NativeFileList.mk | 1 + .../utils/buffer_with_extendable_buffer.cpp | 2 +- .../dictionary/utils/byte_array_utils.h | 2 +- .../utils/byte_array_utils_test.cpp | 92 +++++++++++++++++++ 4 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 native/jni/tests/suggest/policyimpl/dictionary/utils/byte_array_utils_test.cpp diff --git a/native/jni/NativeFileList.mk b/native/jni/NativeFileList.mk index 7a732a588..4a89b2b63 100644 --- a/native/jni/NativeFileList.mk +++ b/native/jni/NativeFileList.mk @@ -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 diff --git a/native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.cpp b/native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.cpp index 833063c17..ecbe7922c 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.cpp +++ b/native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.cpp @@ -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; } diff --git a/native/jni/src/suggest/policyimpl/dictionary/utils/byte_array_utils.h b/native/jni/src/suggest/policyimpl/dictionary/utils/byte_array_utils.h index c0a9fcb1d..4b3c98988 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/utils/byte_array_utils.h +++ b/native/jni/src/suggest/policyimpl/dictionary/utils/byte_array_utils.h @@ -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); diff --git a/native/jni/tests/suggest/policyimpl/dictionary/utils/byte_array_utils_test.cpp b/native/jni/tests/suggest/policyimpl/dictionary/utils/byte_array_utils_test.cpp new file mode 100644 index 000000000..a1c310d8a --- /dev/null +++ b/native/jni/tests/suggest/policyimpl/dictionary/utils/byte_array_utils_test.cpp @@ -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 + +#include + +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