From 985b2c2e91485a0cb293bff35a80877b2be71eae Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Tue, 2 Sep 2014 17:10:42 +0900 Subject: [PATCH] Add GeometryUtilsTest. Change-Id: I6ea9ef137c2930ca8e8745fec829b9bcaf1d10b9 --- native/jni/NativeFileList.mk | 1 + .../src/suggest/core/layout/geometry_utils.h | 14 ++-- .../core/layout/geometry_utils_test.cpp | 83 +++++++++++++++++++ 3 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 native/jni/tests/suggest/core/layout/geometry_utils_test.cpp diff --git a/native/jni/NativeFileList.mk b/native/jni/NativeFileList.mk index 082e1e2f9..5a0aacda1 100644 --- a/native/jni/NativeFileList.mk +++ b/native/jni/NativeFileList.mk @@ -123,6 +123,7 @@ LATIN_IME_CORE_TEST_FILES := \ defines_test.cpp \ suggest/core/dicnode/dic_node_pool_test.cpp \ 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/structure/v4/content/language_model_dict_content_test.cpp \ suggest/policyimpl/dictionary/structure/v4/content/probability_entry_test.cpp \ diff --git a/native/jni/src/suggest/core/layout/geometry_utils.h b/native/jni/src/suggest/core/layout/geometry_utils.h index b667df68f..000fcd4a1 100644 --- a/native/jni/src/suggest/core/layout/geometry_utils.h +++ b/native/jni/src/suggest/core/layout/geometry_utils.h @@ -38,13 +38,15 @@ class GeometryUtils { } static AK_FORCE_INLINE float getAngleDiff(const float a1, const float a2) { - const float deltaA = fabsf(a1 - a2); - const float diff = ROUND_FLOAT_10000(deltaA); - if (diff > M_PI_F) { - const float normalizedDiff = 2.0f * M_PI_F - diff; - return ROUND_FLOAT_10000(normalizedDiff); + static const float M_2PI_F = M_PI * 2.0f; + float delta = fabsf(a1 - a2); + if (delta > M_2PI_F) { + delta -= (M_2PI_F * static_cast(delta / M_2PI_F)); } - return diff; + if (delta > M_PI_F) { + delta = M_2PI_F - delta; + } + return ROUND_FLOAT_10000(delta); } static AK_FORCE_INLINE int getDistanceInt(const int x1, const int y1, const int x2, 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 + +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( + 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