test_math_util.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 
30 #include <gtest/gtest.h>
31 
34 
35 TEST(MathUtilTests, Round)
36 {
37  EXPECT_EQ(0.0, swri_math_util::Round(0.1));
38  EXPECT_EQ(0.0, swri_math_util::Round(-0.1));
39  EXPECT_EQ(0.0, swri_math_util::Round(0.45));
40  EXPECT_EQ(90.0, swri_math_util::Round(90.1));
41  EXPECT_EQ(90.0, swri_math_util::Round(89.7));
42  EXPECT_EQ(-10.0, swri_math_util::Round(-9.9));
43  EXPECT_EQ(-10.0, swri_math_util::Round(-10.1));
44 }
45 
46 TEST(MathUtilTests, ToNearest)
47 {
48  EXPECT_EQ(0.0, swri_math_util::ToNearest(0.1, 1));
49  EXPECT_EQ(0.0, swri_math_util::ToNearest(-0.1, 1));
50  EXPECT_EQ(0.0, swri_math_util::ToNearest(0.45, 1));
51  EXPECT_EQ(90.0, swri_math_util::ToNearest(90.1, 1));
52  EXPECT_EQ(90.0, swri_math_util::ToNearest(89.7, 1));
53  EXPECT_EQ(-10.0, swri_math_util::ToNearest(-9.9, 1));
54  EXPECT_EQ(-10.0, swri_math_util::ToNearest(-10.1, 1));
55  EXPECT_EQ(4, swri_math_util::ToNearest(4, 2));
56  EXPECT_EQ(-4, swri_math_util::ToNearest(-4, 2));
57 
58  EXPECT_EQ(90.0, swri_math_util::ToNearest(85.4, 90));
59  EXPECT_EQ(270.0, swri_math_util::ToNearest(301.4, 90));
60  EXPECT_EQ(0.0, swri_math_util::ToNearest(35.4, 90));
61  EXPECT_EQ(0.0, swri_math_util::ToNearest(-35.3, 90));
62  EXPECT_EQ(-270.0, swri_math_util::ToNearest(-301.4, 90));
63 
64  EXPECT_EQ(0.0, swri_math_util::ToNearest(85.4, 0));
65  EXPECT_EQ(0.0, swri_math_util::ToNearest(301.4, 0));
66  EXPECT_EQ(0.0, swri_math_util::ToNearest(35.4, 0));
67  EXPECT_EQ(0.0, swri_math_util::ToNearest(-35.3, 0));
68  EXPECT_EQ(0.0, swri_math_util::ToNearest(-301.4, 0));
69 
77 }
78 
79 TEST(MathUtilTests, UpToNearest)
80 {
81  EXPECT_EQ(1.0, swri_math_util::UpToNearest(0.1, 1));
82  EXPECT_EQ(0.0, swri_math_util::UpToNearest(-0.1, 1));
83  EXPECT_EQ(1.0, swri_math_util::UpToNearest(0.45, 1));
84  EXPECT_EQ(91.0, swri_math_util::UpToNearest(90.1, 1));
85  EXPECT_EQ(90.0, swri_math_util::UpToNearest(89.7, 1));
86  EXPECT_EQ(-9.0, swri_math_util::UpToNearest(-9.9, 1));
87  EXPECT_EQ(-10.0, swri_math_util::UpToNearest(-10.1, 1));
88  EXPECT_EQ(4, swri_math_util::UpToNearest(4, 2));
89  EXPECT_EQ(-4, swri_math_util::UpToNearest(-4, 2));
90 
91  EXPECT_EQ(90.0, swri_math_util::UpToNearest(85.4, 90));
92  EXPECT_EQ(360.0, swri_math_util::UpToNearest(301.4, 90));
93  EXPECT_EQ(90.0, swri_math_util::UpToNearest(35.4, 90));
94  EXPECT_EQ(0.0, swri_math_util::UpToNearest(-35.3, 90));
95  EXPECT_EQ(-270.0, swri_math_util::UpToNearest(-301.4, 90));
96 
97  EXPECT_EQ(0.0, swri_math_util::UpToNearest(85.4, 0));
98  EXPECT_EQ(0.0, swri_math_util::UpToNearest(301.4, 0));
99  EXPECT_EQ(0.0, swri_math_util::UpToNearest(35.4, 0));
100  EXPECT_EQ(0.0, swri_math_util::UpToNearest(-35.3, 0));
101  EXPECT_EQ(0.0, swri_math_util::UpToNearest(-301.4, 0));
102 
110 }
111 
112 TEST(MathUtilTests, IsNear)
113 {
114  EXPECT_TRUE(swri_math_util::IsNear(1, 1, 0));
115  EXPECT_TRUE(swri_math_util::IsNear(3535.353, 3535.353, 0));
116  EXPECT_TRUE(swri_math_util::IsNear(10.1, 10, 0.1));
117  EXPECT_TRUE(swri_math_util::IsNear(10, 10.1, 0.1));
118  EXPECT_TRUE(swri_math_util::IsNear(-10.1, -10, 0.1));
119  EXPECT_TRUE(swri_math_util::IsNear(-10, -10.1, 0.1));
120 }
121 
122 // Run all the tests that were declared with TEST()
123 int main(int argc, char **argv)
124 {
125  testing::InitGoogleTest(&argc, argv);
126 
127  return RUN_ALL_TESTS();
128 }
static const long double _half_pi
Definition: constants.h:39
double UpToNearest(double value, double multiple)
Definition: math_util.cpp:51
double ToNearest(double value, double multiple)
Definition: math_util.cpp:41
TEST(MathUtilTests, Round)
bool IsNear(double v1, double v2, double epsilon)
Definition: math_util.cpp:61
int main(int argc, char **argv)
double Round(double value)
Definition: math_util.cpp:36
static const long double _2pi
Definition: constants.h:37
static const long double _pi
Definition: constants.h:35


swri_math_util
Author(s): Marc Alban
autogenerated on Fri Jun 7 2019 22:05:41