aboutsummaryrefslogtreecommitdiffstats
path: root/native/jni/tests
diff options
context:
space:
mode:
Diffstat (limited to 'native/jni/tests')
-rw-r--r--native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/language_model_dict_content_test.cpp16
-rw-r--r--native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/probability_entry_test.cpp2
-rw-r--r--native/jni/tests/suggest/policyimpl/dictionary/utils/byte_array_utils_test.cpp92
-rw-r--r--native/jni/tests/suggest/policyimpl/dictionary/utils/trie_map_test.cpp28
-rw-r--r--native/jni/tests/utils/int_array_view_test.cpp17
5 files changed, 152 insertions, 3 deletions
diff --git a/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/language_model_dict_content_test.cpp b/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/language_model_dict_content_test.cpp
index 6eef2040b..3cacba1c3 100644
--- a/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/language_model_dict_content_test.cpp
+++ b/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/language_model_dict_content_test.cpp
@@ -35,6 +35,13 @@ TEST(LanguageModelDictContentTest, TestUnigramProbability) {
LanguageModelDictContent.getProbabilityEntry(wordId);
EXPECT_EQ(flag, entry.getFlags());
EXPECT_EQ(probability, entry.getProbability());
+
+ // Remove
+ EXPECT_TRUE(LanguageModelDictContent.removeProbabilityEntry(wordId));
+ EXPECT_FALSE(LanguageModelDictContent.getProbabilityEntry(wordId).isValid());
+ EXPECT_FALSE(LanguageModelDictContent.removeProbabilityEntry(wordId));
+ EXPECT_TRUE(LanguageModelDictContent.setProbabilityEntry(wordId, &probabilityEntry));
+ EXPECT_TRUE(LanguageModelDictContent.getProbabilityEntry(wordId).isValid());
}
TEST(LanguageModelDictContentTest, TestUnigramProbabilityWithHistoricalInfo) {
@@ -46,13 +53,20 @@ TEST(LanguageModelDictContentTest, TestUnigramProbabilityWithHistoricalInfo) {
const int count = 10;
const int wordId = 100;
const HistoricalInfo historicalInfo(timestamp, level, count);
- const ProbabilityEntry probabilityEntry(flag, NOT_A_PROBABILITY, &historicalInfo);
+ const ProbabilityEntry probabilityEntry(flag, &historicalInfo);
LanguageModelDictContent.setProbabilityEntry(wordId, &probabilityEntry);
const ProbabilityEntry entry = LanguageModelDictContent.getProbabilityEntry(wordId);
EXPECT_EQ(flag, entry.getFlags());
EXPECT_EQ(timestamp, entry.getHistoricalInfo()->getTimeStamp());
EXPECT_EQ(level, entry.getHistoricalInfo()->getLevel());
EXPECT_EQ(count, entry.getHistoricalInfo()->getCount());
+
+ // Remove
+ EXPECT_TRUE(LanguageModelDictContent.removeProbabilityEntry(wordId));
+ EXPECT_FALSE(LanguageModelDictContent.getProbabilityEntry(wordId).isValid());
+ EXPECT_FALSE(LanguageModelDictContent.removeProbabilityEntry(wordId));
+ EXPECT_TRUE(LanguageModelDictContent.setProbabilityEntry(wordId, &probabilityEntry));
+ EXPECT_TRUE(LanguageModelDictContent.removeProbabilityEntry(wordId));
}
} // namespace
diff --git a/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/probability_entry_test.cpp b/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/probability_entry_test.cpp
index db94550ef..f0494f355 100644
--- a/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/probability_entry_test.cpp
+++ b/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/probability_entry_test.cpp
@@ -43,7 +43,7 @@ TEST(ProbabilityEntryTest, TestEncodeDecodeWithHistoricalInfo) {
const int count = 10;
const HistoricalInfo historicalInfo(timestamp, level, count);
- const ProbabilityEntry entry(flag, NOT_A_PROBABILITY, &historicalInfo);
+ const ProbabilityEntry entry(flag, &historicalInfo);
const uint64_t encodedEntry = entry.encode(true /* hasHistoricalInfo */);
EXPECT_EQ(0xF03FFFFFFF030Aull, encodedEntry);
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 <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
diff --git a/native/jni/tests/suggest/policyimpl/dictionary/utils/trie_map_test.cpp b/native/jni/tests/suggest/policyimpl/dictionary/utils/trie_map_test.cpp
index df778b6cf..56b5aa985 100644
--- a/native/jni/tests/suggest/policyimpl/dictionary/utils/trie_map_test.cpp
+++ b/native/jni/tests/suggest/policyimpl/dictionary/utils/trie_map_test.cpp
@@ -40,6 +40,7 @@ TEST(TrieMapTest, TestSetAndGet) {
trieMap.putRoot(11, 1000);
EXPECT_EQ(1000ull, trieMap.getRoot(11).mValue);
const int next = trieMap.getNextLevelBitmapEntryIndex(10);
+ EXPECT_EQ(1000ull, trieMap.getRoot(10).mValue);
trieMap.put(9, 9, next);
EXPECT_EQ(9ull, trieMap.get(9, next).mValue);
EXPECT_FALSE(trieMap.get(11, next).mIsValid);
@@ -47,6 +48,33 @@ TEST(TrieMapTest, TestSetAndGet) {
EXPECT_EQ(0xFFFFFFFFFull, trieMap.getRoot(0).mValue);
}
+TEST(TrieMapTest, TestRemove) {
+ TrieMap trieMap;
+ trieMap.putRoot(10, 10);
+ EXPECT_EQ(10ull, trieMap.getRoot(10).mValue);
+ EXPECT_TRUE(trieMap.remove(10, trieMap.getRootBitmapEntryIndex()));
+ EXPECT_FALSE(trieMap.getRoot(10).mIsValid);
+ for (const auto &element : trieMap.getEntriesInRootLevel()) {
+ EXPECT_TRUE(false);
+ }
+ EXPECT_TRUE(trieMap.putRoot(10, 0x3FFFFF));
+ EXPECT_FALSE(trieMap.remove(11, trieMap.getRootBitmapEntryIndex()))
+ << "Should fail if the key does not exist.";
+ EXPECT_EQ(0x3FFFFFull, trieMap.getRoot(10).mValue);
+ trieMap.putRoot(12, 11);
+ const int nextLevel = trieMap.getNextLevelBitmapEntryIndex(10);
+ trieMap.put(10, 10, nextLevel);
+ EXPECT_EQ(0x3FFFFFull, trieMap.getRoot(10).mValue);
+ EXPECT_EQ(10ull, trieMap.get(10, nextLevel).mValue);
+ EXPECT_TRUE(trieMap.remove(10, trieMap.getRootBitmapEntryIndex()));
+ const TrieMap::Result result = trieMap.getRoot(10);
+ EXPECT_FALSE(result.mIsValid);
+ EXPECT_EQ(TrieMap::INVALID_INDEX, result.mNextLevelBitmapEntryIndex);
+ EXPECT_EQ(11ull, trieMap.getRoot(12).mValue);
+ EXPECT_TRUE(trieMap.putRoot(S_INT_MAX, 0xFFFFFFFFFull));
+ EXPECT_TRUE(trieMap.remove(S_INT_MAX, trieMap.getRootBitmapEntryIndex()));
+}
+
TEST(TrieMapTest, TestSetAndGetLarge) {
static const int ELEMENT_COUNT = 200000;
TrieMap trieMap;
diff --git a/native/jni/tests/utils/int_array_view_test.cpp b/native/jni/tests/utils/int_array_view_test.cpp
index bd843ab02..ecc451af0 100644
--- a/native/jni/tests/utils/int_array_view_test.cpp
+++ b/native/jni/tests/utils/int_array_view_test.cpp
@@ -53,9 +53,24 @@ TEST(IntArrayViewTest, TestConstructFromArray) {
TEST(IntArrayViewTest, TestConstructFromObject) {
const int object = 10;
const auto intArrayView = IntArrayView::fromObject(&object);
- EXPECT_EQ(1, intArrayView.size());
+ EXPECT_EQ(1u, intArrayView.size());
EXPECT_EQ(object, intArrayView[0]);
}
+TEST(IntArrayViewTest, TestLimit) {
+ const std::vector<int> intVector = {3, 2, 1, 0, -1, -2};
+ IntArrayView intArrayView(intVector);
+
+ EXPECT_TRUE(intArrayView.limit(0).empty());
+ EXPECT_EQ(intArrayView.size(), intArrayView.limit(intArrayView.size()).size());
+ EXPECT_EQ(intArrayView.size(), intArrayView.limit(1000).size());
+
+ IntArrayView subView = intArrayView.limit(4);
+ EXPECT_EQ(4u, subView.size());
+ for (size_t i = 0; i < subView.size(); ++i) {
+ EXPECT_EQ(intVector[i], subView[i]);
+ }
+}
+
} // namespace
} // namespace latinime