aboutsummaryrefslogtreecommitdiffstats
path: root/native/jni/tests
diff options
context:
space:
mode:
Diffstat (limited to 'native/jni/tests')
-rw-r--r--native/jni/tests/suggest/core/layout/geometry_utils_test.cpp83
-rw-r--r--native/jni/tests/suggest/policyimpl/utils/damerau_levenshtein_edit_distance_policy_test.cpp65
-rw-r--r--native/jni/tests/utils/char_utils_test.cpp122
3 files changed, 270 insertions, 0 deletions
diff --git a/native/jni/tests/suggest/core/layout/geometry_utils_test.cpp b/native/jni/tests/suggest/core/layout/geometry_utils_test.cpp
new file mode 100644
index 000000000..f5f89ede1
--- /dev/null
+++ b/native/jni/tests/suggest/core/layout/geometry_utils_test.cpp
@@ -0,0 +1,83 @@
+/*
+ * 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/core/layout/geometry_utils.h"
+
+#include <gtest/gtest.h>
+
+namespace latinime {
+namespace {
+
+::testing::AssertionResult ExpectAngleDiffEq(const char* expectedExpression,
+ const char* actualExpression, float expected, float actual) {
+ if (actual < 0.0f || M_PI_F < actual) {
+ return ::testing::AssertionFailure()
+ << "Must be in the range of [0.0f, M_PI_F]."
+ << " expected: " << expected
+ << " actual: " << actual;
+ }
+ return ::testing::internal::CmpHelperFloatingPointEQ<float>(
+ expectedExpression, actualExpression, expected, actual);
+}
+
+#define EXPECT_ANGLE_DIFF_EQ(expected, actual) \
+ EXPECT_PRED_FORMAT2(ExpectAngleDiffEq, expected, actual);
+
+TEST(GeometryUtilsTest, testSquareFloat) {
+ const float test_data[] = { 0.0f, 1.0f, 123.456f, -1.0f, -9876.54321f };
+ for (const float value : test_data) {
+ EXPECT_FLOAT_EQ(value * value, GeometryUtils::SQUARE_FLOAT(value));
+ }
+}
+
+TEST(GeometryUtilsTest, testGetAngle) {
+ EXPECT_FLOAT_EQ(0.0f, GeometryUtils::getAngle(0, 0, 0, 0));
+ EXPECT_FLOAT_EQ(0.0f, GeometryUtils::getAngle(100, -10, 100, -10));
+
+ EXPECT_FLOAT_EQ(M_PI_F / 4.0f, GeometryUtils::getAngle(1, 1, 0, 0));
+ EXPECT_FLOAT_EQ(M_PI_F, GeometryUtils::getAngle(-1, 0, 0, 0));
+
+ EXPECT_FLOAT_EQ(GeometryUtils::getAngle(0, 0, -1, 0), GeometryUtils::getAngle(1, 0, 0, 0));
+ EXPECT_FLOAT_EQ(GeometryUtils::getAngle(1, 2, 3, 4),
+ GeometryUtils::getAngle(100, 200, 300, 400));
+}
+
+TEST(GeometryUtilsTest, testGetAngleDiff) {
+ EXPECT_ANGLE_DIFF_EQ(0.0f, GeometryUtils::getAngleDiff(0.0f, 0.0f));
+ EXPECT_ANGLE_DIFF_EQ(0.0f, GeometryUtils::getAngleDiff(10000.0f, 10000.0f));
+ EXPECT_ANGLE_DIFF_EQ(ROUND_FLOAT_10000(M_PI_F),
+ GeometryUtils::getAngleDiff(0.0f, M_PI_F));
+ EXPECT_ANGLE_DIFF_EQ(ROUND_FLOAT_10000(M_PI_F / 6.0f),
+ GeometryUtils::getAngleDiff(M_PI_F / 3.0f, M_PI_F / 2.0f));
+ EXPECT_ANGLE_DIFF_EQ(ROUND_FLOAT_10000(M_PI_F / 2.0f),
+ GeometryUtils::getAngleDiff(0.0f, M_PI_F * 1.5f));
+ EXPECT_ANGLE_DIFF_EQ(0.0f, GeometryUtils::getAngleDiff(0.0f, M_PI_F * 1024.0f));
+ EXPECT_ANGLE_DIFF_EQ(0.0f, GeometryUtils::getAngleDiff(-M_PI_F, M_PI_F));
+}
+
+TEST(GeometryUtilsTest, testGetDistanceInt) {
+ EXPECT_EQ(0, GeometryUtils::getDistanceInt(0, 0, 0, 0));
+ EXPECT_EQ(0, GeometryUtils::getAngle(100, -10, 100, -10));
+
+ EXPECT_EQ(5, GeometryUtils::getDistanceInt(0, 0, 5, 0));
+ EXPECT_EQ(5, GeometryUtils::getDistanceInt(0, 0, 3, 4));
+ EXPECT_EQ(5, GeometryUtils::getDistanceInt(0, -4, 3, 0));
+ EXPECT_EQ(5, GeometryUtils::getDistanceInt(0, 0, -3, -4));
+ EXPECT_EQ(500, GeometryUtils::getDistanceInt(0, 0, 300, -400));
+}
+
+} // namespace
+} // namespace latinime
diff --git a/native/jni/tests/suggest/policyimpl/utils/damerau_levenshtein_edit_distance_policy_test.cpp b/native/jni/tests/suggest/policyimpl/utils/damerau_levenshtein_edit_distance_policy_test.cpp
new file mode 100644
index 000000000..d13417964
--- /dev/null
+++ b/native/jni/tests/suggest/policyimpl/utils/damerau_levenshtein_edit_distance_policy_test.cpp
@@ -0,0 +1,65 @@
+/*
+ * 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/utils/damerau_levenshtein_edit_distance_policy.h"
+
+#include <gtest/gtest.h>
+
+#include <vector>
+
+#include "suggest/policyimpl/utils/edit_distance.h"
+#include "utils/int_array_view.h"
+
+namespace latinime {
+namespace {
+
+TEST(DamerauLevenshteinEditDistancePolicyTest, TestConstructPolicy) {
+ const std::vector<int> codePoints0 = { 0x20, 0x40, 0x60 };
+ const std::vector<int> codePoints1 = { 0x10, 0x20, 0x30, 0x40, 0x50, 0x60 };
+ DamerauLevenshteinEditDistancePolicy policy(codePoints0.data(), codePoints0.size(),
+ codePoints1.data(), codePoints1.size());
+
+ EXPECT_EQ(static_cast<int>(codePoints0.size()), policy.getString0Length());
+ EXPECT_EQ(static_cast<int>(codePoints1.size()), policy.getString1Length());
+}
+
+float getEditDistance(const std::vector<int> &codePoints0, const std::vector<int> &codePoints1) {
+ DamerauLevenshteinEditDistancePolicy policy(codePoints0.data(), codePoints0.size(),
+ codePoints1.data(), codePoints1.size());
+ return EditDistance::getEditDistance(&policy);
+}
+
+TEST(DamerauLevenshteinEditDistancePolicyTest, TestEditDistance) {
+ EXPECT_FLOAT_EQ(0.0f, getEditDistance({}, {}));
+ EXPECT_FLOAT_EQ(0.0f, getEditDistance({ 1 }, { 1 }));
+ EXPECT_FLOAT_EQ(0.0f, getEditDistance({ 1, 2, 3 }, { 1, 2, 3 }));
+
+ EXPECT_FLOAT_EQ(1.0f, getEditDistance({ 1 }, { }));
+ EXPECT_FLOAT_EQ(1.0f, getEditDistance({}, { 100 }));
+ EXPECT_FLOAT_EQ(5.0f, getEditDistance({}, { 1, 2, 3, 4, 5 }));
+
+ EXPECT_FLOAT_EQ(1.0f, getEditDistance({ 0 }, { 100 }));
+ EXPECT_FLOAT_EQ(5.0f, getEditDistance({ 1, 2, 3, 4, 5 }, { 11, 12, 13, 14, 15 }));
+
+ EXPECT_FLOAT_EQ(1.0f, getEditDistance({ 1 }, { 1, 2 }));
+ EXPECT_FLOAT_EQ(2.0f, getEditDistance({ 1, 2 }, { 0, 1, 2, 3 }));
+ EXPECT_FLOAT_EQ(2.0f, getEditDistance({ 0, 1, 2, 3 }, { 1, 2 }));
+
+ EXPECT_FLOAT_EQ(1.0f, getEditDistance({ 1, 2 }, { 2, 1 }));
+ EXPECT_FLOAT_EQ(2.0f, getEditDistance({ 1, 2, 3, 4 }, { 2, 1, 4, 3 }));
+}
+} // namespace
+} // namespace latinime
diff --git a/native/jni/tests/utils/char_utils_test.cpp b/native/jni/tests/utils/char_utils_test.cpp
new file mode 100644
index 000000000..01d534043
--- /dev/null
+++ b/native/jni/tests/utils/char_utils_test.cpp
@@ -0,0 +1,122 @@
+/*
+ * 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 "utils/char_utils.h"
+
+#include <gtest/gtest.h>
+
+#include "defines.h"
+
+namespace latinime {
+namespace {
+
+TEST(CharUtilsTest, TestIsAsciiUpper) {
+ EXPECT_TRUE(CharUtils::isAsciiUpper('A'));
+ EXPECT_TRUE(CharUtils::isAsciiUpper('Z'));
+ EXPECT_FALSE(CharUtils::isAsciiUpper('a'));
+ EXPECT_FALSE(CharUtils::isAsciiUpper('z'));
+ EXPECT_FALSE(CharUtils::isAsciiUpper('@'));
+ EXPECT_FALSE(CharUtils::isAsciiUpper(' '));
+ EXPECT_FALSE(CharUtils::isAsciiUpper(0x00C0 /* LATIN CAPITAL LETTER A WITH GRAVE */));
+ EXPECT_FALSE(CharUtils::isAsciiUpper(0x00E0 /* LATIN SMALL LETTER A WITH GRAVE */));
+ EXPECT_FALSE(CharUtils::isAsciiUpper(0x03C2 /* GREEK SMALL LETTER FINAL SIGMA */));
+ EXPECT_FALSE(CharUtils::isAsciiUpper(0x0410 /* CYRILLIC CAPITAL LETTER A */));
+ EXPECT_FALSE(CharUtils::isAsciiUpper(0x0430 /* CYRILLIC SMALL LETTER A */));
+ EXPECT_FALSE(CharUtils::isAsciiUpper(0x3042 /* HIRAGANA LETTER A */));
+ EXPECT_FALSE(CharUtils::isAsciiUpper(0x1F36A /* COOKIE */));
+}
+
+TEST(CharUtilsTest, TestToLowerCase) {
+ EXPECT_EQ('a', CharUtils::toLowerCase('A'));
+ EXPECT_EQ('z', CharUtils::toLowerCase('Z'));
+ EXPECT_EQ('a', CharUtils::toLowerCase('a'));
+ EXPECT_EQ('z', CharUtils::toLowerCase('z'));
+ EXPECT_EQ('@', CharUtils::toLowerCase('@'));
+ EXPECT_EQ(' ', CharUtils::toLowerCase(' '));
+ EXPECT_EQ(0x00E0 /* LATIN SMALL LETTER A WITH GRAVE */,
+ CharUtils::toLowerCase(0x00C0 /* LATIN CAPITAL LETTER A WITH GRAVE */));
+ EXPECT_EQ(0x00E0 /* LATIN SMALL LETTER A WITH GRAVE */,
+ CharUtils::toLowerCase(0x00E0 /* LATIN SMALL LETTER A WITH GRAVE */));
+ EXPECT_EQ(0x03C2 /* GREEK SMALL LETTER FINAL SIGMA */,
+ CharUtils::toLowerCase(0x03C2 /* GREEK SMALL LETTER FINAL SIGMA */));
+ EXPECT_EQ(0x0430 /* CYRILLIC SMALL LETTER A */,
+ CharUtils::toLowerCase(0x0410 /* CYRILLIC CAPITAL LETTER A */));
+ EXPECT_EQ(0x0430 /* CYRILLIC SMALL LETTER A */,
+ CharUtils::toLowerCase(0x0430 /* CYRILLIC SMALL LETTER A */));
+ EXPECT_EQ(0x3042 /* HIRAGANA LETTER A */,
+ CharUtils::toLowerCase(0x3042 /* HIRAGANA LETTER A */));
+ EXPECT_EQ(0x1F36A /* COOKIE */, CharUtils::toLowerCase(0x1F36A /* COOKIE */));
+}
+
+TEST(CharUtilsTest, TestToBaseLowerCase) {
+ EXPECT_EQ('a', CharUtils::toBaseLowerCase('A'));
+ EXPECT_EQ('z', CharUtils::toBaseLowerCase('Z'));
+ EXPECT_EQ('a', CharUtils::toBaseLowerCase('a'));
+ EXPECT_EQ('z', CharUtils::toBaseLowerCase('z'));
+ EXPECT_EQ('@', CharUtils::toBaseLowerCase('@'));
+ EXPECT_EQ(' ', CharUtils::toBaseLowerCase(' '));
+ EXPECT_EQ('a', CharUtils::toBaseLowerCase(0x00C0 /* LATIN CAPITAL LETTER A WITH GRAVE */));
+ EXPECT_EQ('a', CharUtils::toBaseLowerCase(0x00E0 /* LATIN SMALL LETTER A WITH GRAVE */));
+ EXPECT_EQ(0x03C2 /* GREEK SMALL LETTER FINAL SIGMA */,
+ CharUtils::toBaseLowerCase(0x03C2 /* GREEK SMALL LETTER FINAL SIGMA */));
+ EXPECT_EQ(0x0430 /* CYRILLIC SMALL LETTER A */,
+ CharUtils::toBaseLowerCase(0x0410 /* CYRILLIC CAPITAL LETTER A */));
+ EXPECT_EQ(0x0430 /* CYRILLIC SMALL LETTER A */,
+ CharUtils::toBaseLowerCase(0x0430 /* CYRILLIC SMALL LETTER A */));
+ EXPECT_EQ(0x3042 /* HIRAGANA LETTER A */,
+ CharUtils::toBaseLowerCase(0x3042 /* HIRAGANA LETTER A */));
+ EXPECT_EQ(0x1F36A /* COOKIE */, CharUtils::toBaseLowerCase(0x1F36A /* COOKIE */));
+}
+
+TEST(CharUtilsTest, TestToBaseCodePoint) {
+ EXPECT_EQ('A', CharUtils::toBaseCodePoint('A'));
+ EXPECT_EQ('Z', CharUtils::toBaseCodePoint('Z'));
+ EXPECT_EQ('a', CharUtils::toBaseCodePoint('a'));
+ EXPECT_EQ('z', CharUtils::toBaseCodePoint('z'));
+ EXPECT_EQ('@', CharUtils::toBaseCodePoint('@'));
+ EXPECT_EQ(' ', CharUtils::toBaseCodePoint(' '));
+ EXPECT_EQ('A', CharUtils::toBaseCodePoint(0x00C0 /* LATIN CAPITAL LETTER A WITH GRAVE */));
+ EXPECT_EQ('a', CharUtils::toBaseCodePoint(0x00E0 /* LATIN SMALL LETTER A WITH GRAVE */));
+ EXPECT_EQ(0x03C2 /* GREEK SMALL LETTER FINAL SIGMA */,
+ CharUtils::toBaseLowerCase(0x03C2 /* GREEK SMALL LETTER FINAL SIGMA */));
+ EXPECT_EQ(0x0410 /* CYRILLIC CAPITAL LETTER A */,
+ CharUtils::toBaseCodePoint(0x0410 /* CYRILLIC CAPITAL LETTER A */));
+ EXPECT_EQ(0x0430 /* CYRILLIC SMALL LETTER A */,
+ CharUtils::toBaseCodePoint(0x0430 /* CYRILLIC SMALL LETTER A */));
+ EXPECT_EQ(0x3042 /* HIRAGANA LETTER A */,
+ CharUtils::toBaseCodePoint(0x3042 /* HIRAGANA LETTER A */));
+ EXPECT_EQ(0x1F36A /* COOKIE */, CharUtils::toBaseCodePoint(0x1F36A /* COOKIE */));
+}
+
+TEST(CharUtilsTest, TestIsIntentionalOmissionCodePoint) {
+ EXPECT_TRUE(CharUtils::isIntentionalOmissionCodePoint('\''));
+ EXPECT_TRUE(CharUtils::isIntentionalOmissionCodePoint('-'));
+ EXPECT_FALSE(CharUtils::isIntentionalOmissionCodePoint('a'));
+ EXPECT_FALSE(CharUtils::isIntentionalOmissionCodePoint('?'));
+ EXPECT_FALSE(CharUtils::isIntentionalOmissionCodePoint('/'));
+}
+
+TEST(CharUtilsTest, TestIsInUnicodeSpace) {
+ EXPECT_FALSE(CharUtils::isInUnicodeSpace(NOT_A_CODE_POINT));
+ EXPECT_FALSE(CharUtils::isInUnicodeSpace(CODE_POINT_BEGINNING_OF_SENTENCE));
+ EXPECT_TRUE(CharUtils::isInUnicodeSpace('a'));
+ EXPECT_TRUE(CharUtils::isInUnicodeSpace(0x0410 /* CYRILLIC CAPITAL LETTER A */));
+ EXPECT_TRUE(CharUtils::isInUnicodeSpace(0x3042 /* HIRAGANA LETTER A */));
+ EXPECT_TRUE(CharUtils::isInUnicodeSpace(0x1F36A /* COOKIE */));
+}
+
+} // namespace
+} // namespace latinime