00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <gtest/gtest.h>
00031
00032 #include <swri_math_util/constants.h>
00033 #include <swri_math_util/math_util.h>
00034
00035 TEST(MathUtilTests, Round)
00036 {
00037 EXPECT_EQ(0.0, swri_math_util::Round(0.1));
00038 EXPECT_EQ(0.0, swri_math_util::Round(-0.1));
00039 EXPECT_EQ(0.0, swri_math_util::Round(0.45));
00040 EXPECT_EQ(90.0, swri_math_util::Round(90.1));
00041 EXPECT_EQ(90.0, swri_math_util::Round(89.7));
00042 EXPECT_EQ(-10.0, swri_math_util::Round(-9.9));
00043 EXPECT_EQ(-10.0, swri_math_util::Round(-10.1));
00044 }
00045
00046 TEST(MathUtilTests, ToNearest)
00047 {
00048 EXPECT_EQ(0.0, swri_math_util::ToNearest(0.1, 1));
00049 EXPECT_EQ(0.0, swri_math_util::ToNearest(-0.1, 1));
00050 EXPECT_EQ(0.0, swri_math_util::ToNearest(0.45, 1));
00051 EXPECT_EQ(90.0, swri_math_util::ToNearest(90.1, 1));
00052 EXPECT_EQ(90.0, swri_math_util::ToNearest(89.7, 1));
00053 EXPECT_EQ(-10.0, swri_math_util::ToNearest(-9.9, 1));
00054 EXPECT_EQ(-10.0, swri_math_util::ToNearest(-10.1, 1));
00055 EXPECT_EQ(4, swri_math_util::ToNearest(4, 2));
00056 EXPECT_EQ(-4, swri_math_util::ToNearest(-4, 2));
00057
00058 EXPECT_EQ(90.0, swri_math_util::ToNearest(85.4, 90));
00059 EXPECT_EQ(270.0, swri_math_util::ToNearest(301.4, 90));
00060 EXPECT_EQ(0.0, swri_math_util::ToNearest(35.4, 90));
00061 EXPECT_EQ(0.0, swri_math_util::ToNearest(-35.3, 90));
00062 EXPECT_EQ(-270.0, swri_math_util::ToNearest(-301.4, 90));
00063
00064 EXPECT_EQ(0.0, swri_math_util::ToNearest(85.4, 0));
00065 EXPECT_EQ(0.0, swri_math_util::ToNearest(301.4, 0));
00066 EXPECT_EQ(0.0, swri_math_util::ToNearest(35.4, 0));
00067 EXPECT_EQ(0.0, swri_math_util::ToNearest(-35.3, 0));
00068 EXPECT_EQ(0.0, swri_math_util::ToNearest(-301.4, 0));
00069
00070 EXPECT_EQ(0, swri_math_util::ToNearest(-0.45, swri_math_util::_half_pi));
00071 EXPECT_EQ(0, swri_math_util::ToNearest(0.45, swri_math_util::_half_pi));
00072 EXPECT_FLOAT_EQ(swri_math_util::_half_pi, swri_math_util::ToNearest(1.2, swri_math_util::_half_pi));
00073 EXPECT_FLOAT_EQ(swri_math_util::_half_pi, swri_math_util::ToNearest(1.6, swri_math_util::_half_pi));
00074 EXPECT_FLOAT_EQ(swri_math_util::_pi, swri_math_util::ToNearest(2.4, swri_math_util::_half_pi));
00075 EXPECT_FLOAT_EQ(swri_math_util::_pi, swri_math_util::ToNearest(swri_math_util::_pi, swri_math_util::_half_pi));
00076 EXPECT_FLOAT_EQ(swri_math_util::_2pi, swri_math_util::ToNearest(6.1, swri_math_util::_half_pi));
00077 }
00078
00079 TEST(MathUtilTests, UpToNearest)
00080 {
00081 EXPECT_EQ(1.0, swri_math_util::UpToNearest(0.1, 1));
00082 EXPECT_EQ(0.0, swri_math_util::UpToNearest(-0.1, 1));
00083 EXPECT_EQ(1.0, swri_math_util::UpToNearest(0.45, 1));
00084 EXPECT_EQ(91.0, swri_math_util::UpToNearest(90.1, 1));
00085 EXPECT_EQ(90.0, swri_math_util::UpToNearest(89.7, 1));
00086 EXPECT_EQ(-9.0, swri_math_util::UpToNearest(-9.9, 1));
00087 EXPECT_EQ(-10.0, swri_math_util::UpToNearest(-10.1, 1));
00088 EXPECT_EQ(4, swri_math_util::UpToNearest(4, 2));
00089 EXPECT_EQ(-4, swri_math_util::UpToNearest(-4, 2));
00090
00091 EXPECT_EQ(90.0, swri_math_util::UpToNearest(85.4, 90));
00092 EXPECT_EQ(360.0, swri_math_util::UpToNearest(301.4, 90));
00093 EXPECT_EQ(90.0, swri_math_util::UpToNearest(35.4, 90));
00094 EXPECT_EQ(0.0, swri_math_util::UpToNearest(-35.3, 90));
00095 EXPECT_EQ(-270.0, swri_math_util::UpToNearest(-301.4, 90));
00096
00097 EXPECT_EQ(0.0, swri_math_util::UpToNearest(85.4, 0));
00098 EXPECT_EQ(0.0, swri_math_util::UpToNearest(301.4, 0));
00099 EXPECT_EQ(0.0, swri_math_util::UpToNearest(35.4, 0));
00100 EXPECT_EQ(0.0, swri_math_util::UpToNearest(-35.3, 0));
00101 EXPECT_EQ(0.0, swri_math_util::UpToNearest(-301.4, 0));
00102
00103 EXPECT_EQ(0, swri_math_util::UpToNearest(-0.45, swri_math_util::_half_pi));
00104 EXPECT_FLOAT_EQ(swri_math_util::_half_pi, swri_math_util::UpToNearest(0.45, swri_math_util::_half_pi));
00105 EXPECT_FLOAT_EQ(swri_math_util::_half_pi, swri_math_util::UpToNearest(1.2, swri_math_util::_half_pi));
00106 EXPECT_FLOAT_EQ(swri_math_util::_pi, swri_math_util::UpToNearest(1.6, swri_math_util::_half_pi));
00107 EXPECT_FLOAT_EQ(swri_math_util::_pi, swri_math_util::UpToNearest(2.4, swri_math_util::_half_pi));
00108 EXPECT_FLOAT_EQ(swri_math_util::_pi, swri_math_util::UpToNearest(swri_math_util::_pi, swri_math_util::_half_pi));
00109 EXPECT_FLOAT_EQ(swri_math_util::_2pi, swri_math_util::UpToNearest(6.1, swri_math_util::_half_pi));
00110 }
00111
00112 TEST(MathUtilTests, IsNear)
00113 {
00114 EXPECT_TRUE(swri_math_util::IsNear(1, 1, 0));
00115 EXPECT_TRUE(swri_math_util::IsNear(3535.353, 3535.353, 0));
00116 EXPECT_TRUE(swri_math_util::IsNear(10.1, 10, 0.1));
00117 EXPECT_TRUE(swri_math_util::IsNear(10, 10.1, 0.1));
00118 EXPECT_TRUE(swri_math_util::IsNear(-10.1, -10, 0.1));
00119 EXPECT_TRUE(swri_math_util::IsNear(-10, -10.1, 0.1));
00120 }
00121
00122
00123 int main(int argc, char **argv)
00124 {
00125 testing::InitGoogleTest(&argc, argv);
00126
00127 return RUN_ALL_TESTS();
00128 }