From fbddeedff617d101b7617995bdf7c9c7ce7eaf24 Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Thu, 4 Sep 2014 15:32:30 +0900 Subject: [PATCH] Add HeaderReadWriteUtilsTest Change-Id: I08aeaa3e5852008874e38ed4799ab8989759e861 --- native/jni/NativeFileList.mk | 1 + .../header/header_read_write_utils.cpp | 3 +- .../header/header_read_write_utils.h | 2 +- .../header/header_read_write_utils_test.cpp | 78 +++++++++++++++++++ 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 native/jni/tests/suggest/policyimpl/dictionary/header/header_read_write_utils_test.cpp diff --git a/native/jni/NativeFileList.mk b/native/jni/NativeFileList.mk index 4f77388fd..68d2bbd83 100644 --- a/native/jni/NativeFileList.mk +++ b/native/jni/NativeFileList.mk @@ -125,6 +125,7 @@ LATIN_IME_CORE_TEST_FILES := \ suggest/core/dictionary/bloom_filter_test.cpp \ suggest/core/layout/geometry_utils_test.cpp \ suggest/core/layout/normal_distribution_2d_test.cpp \ + suggest/policyimpl/dictionary/header/header_read_write_utils_test.cpp \ 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/structure/v4/content/terminal_position_lookup_table_test.cpp \ diff --git a/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.cpp b/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.cpp index a8f8f284b..d2c3d2fe0 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.cpp +++ b/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.cpp @@ -142,7 +142,8 @@ typedef DictionaryHeaderStructurePolicy::AttributeMap AttributeMap; } /* static */ void HeaderReadWriteUtils::setCodePointVectorAttribute( - AttributeMap *const headerAttributes, const char *const key, const std::vector value) { + AttributeMap *const headerAttributes, const char *const key, + const std::vector &value) { AttributeMap::key_type keyVector; insertCharactersIntoVector(key, &keyVector); (*headerAttributes)[keyVector] = value; diff --git a/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.h b/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.h index 9b90488fc..1ab2eec69 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.h +++ b/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.h @@ -64,7 +64,7 @@ class HeaderReadWriteUtils { */ static void setCodePointVectorAttribute( DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, - const char *const key, const std::vector value); + const char *const key, const std::vector &value); static void setBoolAttribute( DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes, diff --git a/native/jni/tests/suggest/policyimpl/dictionary/header/header_read_write_utils_test.cpp b/native/jni/tests/suggest/policyimpl/dictionary/header/header_read_write_utils_test.cpp new file mode 100644 index 000000000..da6a2af27 --- /dev/null +++ b/native/jni/tests/suggest/policyimpl/dictionary/header/header_read_write_utils_test.cpp @@ -0,0 +1,78 @@ +/* + * 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/header/header_read_write_utils.h" + +#include + +#include +#include + +#include "suggest/core/policy/dictionary_header_structure_policy.h" + +namespace latinime { +namespace { + +TEST(HeaderReadWriteUtilsTest, TestInsertCharactersIntoVector) { + DictionaryHeaderStructurePolicy::AttributeMap::key_type vector; + + HeaderReadWriteUtils::insertCharactersIntoVector("", &vector); + EXPECT_TRUE(vector.empty()); + + static const char *str = "abc-xyz!?"; + HeaderReadWriteUtils::insertCharactersIntoVector(str, &vector); + EXPECT_EQ(strlen(str) , vector.size()); + for (size_t i = 0; i < vector.size(); ++i) { + EXPECT_EQ(str[i], vector[i]); + } +} + +TEST(HeaderReadWriteUtilsTest, TestAttributeMapForInt) { + DictionaryHeaderStructurePolicy::AttributeMap attributeMap; + + // Returns default value if not exists. + EXPECT_EQ(-1, HeaderReadWriteUtils::readIntAttributeValue(&attributeMap, "", -1)); + EXPECT_EQ(100, HeaderReadWriteUtils::readIntAttributeValue(&attributeMap, "abc", 100)); + + HeaderReadWriteUtils::setIntAttribute(&attributeMap, "abc", 10); + EXPECT_EQ(10, HeaderReadWriteUtils::readIntAttributeValue(&attributeMap, "abc", 100)); + HeaderReadWriteUtils::setIntAttribute(&attributeMap, "abc", 20); + EXPECT_EQ(20, HeaderReadWriteUtils::readIntAttributeValue(&attributeMap, "abc", 100)); + HeaderReadWriteUtils::setIntAttribute(&attributeMap, "abcd", 30); + EXPECT_EQ(30, HeaderReadWriteUtils::readIntAttributeValue(&attributeMap, "abcd", 100)); + EXPECT_EQ(20, HeaderReadWriteUtils::readIntAttributeValue(&attributeMap, "abc", 100)); +} + +TEST(HeaderReadWriteUtilsTest, TestAttributeMapCodeForPoints) { + DictionaryHeaderStructurePolicy::AttributeMap attributeMap; + + // Returns empty vector if not exists. + EXPECT_TRUE(HeaderReadWriteUtils::readCodePointVectorAttributeValue(&attributeMap, "").empty()); + EXPECT_TRUE(HeaderReadWriteUtils::readCodePointVectorAttributeValue( + &attributeMap, "abc").empty()); + + HeaderReadWriteUtils::setCodePointVectorAttribute(&attributeMap, "abc", {}); + EXPECT_TRUE(HeaderReadWriteUtils::readCodePointVectorAttributeValue( + &attributeMap, "abc").empty()); + + const std::vector codePoints = { 0x0, 0x20, 0x1F, 0x100000 }; + HeaderReadWriteUtils::setCodePointVectorAttribute(&attributeMap, "abc", codePoints); + EXPECT_EQ(codePoints, HeaderReadWriteUtils::readCodePointVectorAttributeValue( + &attributeMap, "abc")); +} + +} // namespace +} // namespace latinime