test_math_util.cpp
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 // Run all the tests that were declared with TEST()
00123 int main(int argc, char **argv)
00124 {
00125   testing::InitGoogleTest(&argc, argv);
00126 
00127   return RUN_ALL_TESTS();
00128 }


swri_math_util
Author(s): Marc Alban
autogenerated on Thu Jun 6 2019 20:34:41