Add HeaderReadWriteUtilsTest

Change-Id: I08aeaa3e5852008874e38ed4799ab8989759e861
diff --git a/native/jni/NativeFileList.mk b/native/jni/NativeFileList.mk
index 4f77388..68d2bbd 100644
--- a/native/jni/NativeFileList.mk
+++ b/native/jni/NativeFileList.mk
@@ -125,6 +125,7 @@
     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 a8f8f28..d2c3d2f 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 @@
 }
 
 /* static */ void HeaderReadWriteUtils::setCodePointVectorAttribute(
-        AttributeMap *const headerAttributes, const char *const key, const std::vector<int> value) {
+        AttributeMap *const headerAttributes, const char *const key,
+        const std::vector<int> &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 9b90488..1ab2eec 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 @@
      */
     static void setCodePointVectorAttribute(
             DictionaryHeaderStructurePolicy::AttributeMap *const headerAttributes,
-            const char *const key, const std::vector<int> value);
+            const char *const key, const std::vector<int> &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 0000000..da6a2af
--- /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 <gtest/gtest.h>
+
+#include <cstring>
+#include <vector>
+
+#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<int> codePoints = { 0x0, 0x20, 0x1F, 0x100000 };
+    HeaderReadWriteUtils::setCodePointVectorAttribute(&attributeMap, "abc", codePoints);
+    EXPECT_EQ(codePoints, HeaderReadWriteUtils::readCodePointVectorAttributeValue(
+            &attributeMap, "abc"));
+}
+
+}  // namespace
+}  // namespace latinime