• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "suggest/core/layout/geometry_utils.h"
18 
19 #include <gtest/gtest.h>
20 
21 namespace latinime {
22 namespace {
23 
ExpectAngleDiffEq(const char * expectedExpression,const char * actualExpression,float expected,float actual)24 ::testing::AssertionResult ExpectAngleDiffEq(const char* expectedExpression,
25       const char* actualExpression, float expected, float actual) {
26     if (actual < 0.0f || M_PI_F < actual) {
27         return ::testing::AssertionFailure()
28               << "Must be in the range of [0.0f, M_PI_F]."
29               << " expected: " << expected
30               << " actual: " << actual;
31     }
32     return ::testing::internal::CmpHelperFloatingPointEQ<float>(
33             expectedExpression, actualExpression, expected, actual);
34 }
35 
36 #define EXPECT_ANGLE_DIFF_EQ(expected, actual) \
37         EXPECT_PRED_FORMAT2(ExpectAngleDiffEq, expected, actual);
38 
TEST(GeometryUtilsTest,testSquareFloat)39 TEST(GeometryUtilsTest, testSquareFloat) {
40     const float test_data[] = { 0.0f, 1.0f, 123.456f, -1.0f, -9876.54321f };
41     for (const float value : test_data) {
42         EXPECT_FLOAT_EQ(value * value, GeometryUtils::SQUARE_FLOAT(value));
43     }
44 }
45 
TEST(GeometryUtilsTest,testGetAngle)46 TEST(GeometryUtilsTest, testGetAngle) {
47     EXPECT_FLOAT_EQ(0.0f, GeometryUtils::getAngle(0, 0, 0, 0));
48     EXPECT_FLOAT_EQ(0.0f, GeometryUtils::getAngle(100, -10, 100, -10));
49 
50     EXPECT_FLOAT_EQ(M_PI_F / 4.0f, GeometryUtils::getAngle(1, 1, 0, 0));
51     EXPECT_FLOAT_EQ(M_PI_F, GeometryUtils::getAngle(-1, 0, 0, 0));
52 
53     EXPECT_FLOAT_EQ(GeometryUtils::getAngle(0, 0, -1, 0), GeometryUtils::getAngle(1, 0, 0, 0));
54     EXPECT_FLOAT_EQ(GeometryUtils::getAngle(1, 2, 3, 4),
55             GeometryUtils::getAngle(100, 200, 300, 400));
56 }
57 
TEST(GeometryUtilsTest,testGetAngleDiff)58 TEST(GeometryUtilsTest, testGetAngleDiff) {
59     EXPECT_ANGLE_DIFF_EQ(0.0f, GeometryUtils::getAngleDiff(0.0f, 0.0f));
60     EXPECT_ANGLE_DIFF_EQ(0.0f, GeometryUtils::getAngleDiff(10000.0f, 10000.0f));
61     EXPECT_ANGLE_DIFF_EQ(ROUND_FLOAT_10000(M_PI_F),
62             GeometryUtils::getAngleDiff(0.0f, M_PI_F));
63     EXPECT_ANGLE_DIFF_EQ(ROUND_FLOAT_10000(M_PI_F / 6.0f),
64             GeometryUtils::getAngleDiff(M_PI_F / 3.0f, M_PI_F / 2.0f));
65     EXPECT_ANGLE_DIFF_EQ(ROUND_FLOAT_10000(M_PI_F / 2.0f),
66             GeometryUtils::getAngleDiff(0.0f, M_PI_F * 1.5f));
67     EXPECT_ANGLE_DIFF_EQ(0.0f, GeometryUtils::getAngleDiff(0.0f, M_PI_F * 1024.0f));
68     EXPECT_ANGLE_DIFF_EQ(0.0f, GeometryUtils::getAngleDiff(-M_PI_F, M_PI_F));
69 }
70 
TEST(GeometryUtilsTest,testGetDistanceInt)71 TEST(GeometryUtilsTest, testGetDistanceInt) {
72     EXPECT_EQ(0, GeometryUtils::getDistanceInt(0, 0, 0, 0));
73     EXPECT_EQ(0, GeometryUtils::getAngle(100, -10, 100, -10));
74 
75     EXPECT_EQ(5, GeometryUtils::getDistanceInt(0, 0, 5, 0));
76     EXPECT_EQ(5, GeometryUtils::getDistanceInt(0, 0, 3, 4));
77     EXPECT_EQ(5, GeometryUtils::getDistanceInt(0, -4, 3, 0));
78     EXPECT_EQ(5, GeometryUtils::getDistanceInt(0, 0, -3, -4));
79     EXPECT_EQ(500, GeometryUtils::getDistanceInt(0, 0, 300, -400));
80 }
81 
82 }  // namespace
83 }  // namespace latinime
84