MRPT  1.9.9
bits_math_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <gtest/gtest.h>
11 #include <mrpt/core/bits_math.h>
12 
13 TEST(bits_math, sign)
14 {
15  EXPECT_EQ(mrpt::sign(-8), -1);
16  EXPECT_EQ(mrpt::sign(8), 1);
17  EXPECT_EQ(mrpt::sign(0), 1);
18  EXPECT_EQ(mrpt::sign(8.0), 1);
19  EXPECT_EQ(mrpt::sign(-8.0), -1);
20  EXPECT_EQ(mrpt::sign(0.0), 1);
21 }
22 
23 TEST(bits_math, keep_min)
24 {
25  int min = 40;
26  mrpt::keep_min(min, 30);
27  EXPECT_EQ(min, 30);
28  mrpt::keep_min(min, 80);
29  EXPECT_EQ(min, 30);
30  mrpt::keep_min(min, -80);
31  EXPECT_EQ(min, -80);
32 }
33 TEST(bits_math, keep_max)
34 {
35  int max = 40;
36  mrpt::keep_max(max, 30);
37  EXPECT_EQ(max, 40);
38  mrpt::keep_max(max, 80);
39  EXPECT_EQ(max, 80);
40  mrpt::keep_max(max, -80);
41  EXPECT_EQ(max, 80);
42 }
43 
44 TEST(bits_math, round2up)
45 {
48  EXPECT_EQ(mrpt::round2up(200), 256);
49 }
50 
51 TEST(bits_math, hypot_fast)
52 {
53  EXPECT_NEAR(mrpt::hypot_fast(3.0, 4.0), 5.0, 1e-6);
54  EXPECT_NEAR(mrpt::hypot_fast(3.0f, 4.0f), 5.0f, 1e-6f);
55 }
56 
57 TEST(bits_math, deg_rad)
58 {
59  EXPECT_NEAR(mrpt::DEG2RAD(180.0L), static_cast<long double>(M_PI), 1e-6L);
60  EXPECT_NEAR(mrpt::DEG2RAD(180.0), M_PI, 1e-6);
61  EXPECT_NEAR(mrpt::DEG2RAD(180), M_PI, 1e-6);
62  EXPECT_NEAR(mrpt::DEG2RAD(180.0f), static_cast<float>(M_PI), 1e-4f);
63  {
64  using namespace mrpt; // _deg
65  EXPECT_NEAR(180.0_deg, M_PI, 1e-6);
66  }
67 
69  mrpt::RAD2DEG(static_cast<long double>(M_PI)), 180.0L,
70  static_cast<long double>(1e-6));
71  EXPECT_NEAR(mrpt::RAD2DEG(M_PI), 180.0, 1e-6);
72  EXPECT_NEAR(mrpt::RAD2DEG(static_cast<float>(M_PI)), 180.0f, 1e-4f);
73 }
74 
75 TEST(bits_math, signWithZero)
76 {
78  EXPECT_EQ(mrpt::signWithZero(89.0), 1);
81  EXPECT_EQ(mrpt::signWithZero(-89), -1);
82  EXPECT_EQ(mrpt::signWithZero(-89.0), -1);
83 }
84 
85 TEST(bits_math, abs_diff)
86 {
87  EXPECT_EQ(mrpt::abs_diff(10, 20), 10);
88  EXPECT_EQ(mrpt::abs_diff(20, 10), 10);
89  EXPECT_DOUBLE_EQ(mrpt::abs_diff(20.0, 10.0), 10.0);
90  EXPECT_DOUBLE_EQ(mrpt::abs_diff(10.0, 20.0), 10.0);
91 }
92 
93 TEST(bits_math, min3)
94 {
95  EXPECT_EQ(mrpt::min3(1, 2, 3), 1);
96  EXPECT_EQ(mrpt::min3(-1, -2, -3), -3);
97 
98  EXPECT_DOUBLE_EQ(mrpt::min3(1., 2., 3.), 1.);
99  EXPECT_DOUBLE_EQ(mrpt::min3(-1., -2., -3.), -3.);
100 }
101 
102 TEST(bits_math, max3)
103 {
104  EXPECT_EQ(mrpt::max3(1, 2, 3), 3);
105  EXPECT_EQ(mrpt::max3(-1, -2, -3), -1);
106 
107  EXPECT_DOUBLE_EQ(mrpt::max3(1., 2., 3.), 3.);
108  EXPECT_DOUBLE_EQ(mrpt::max3(-1., -2., -3.), -1.);
109 }
110 
111 TEST(bits_math, fix)
112 {
113  EXPECT_EQ(mrpt::fix(1.2), 1);
114  EXPECT_EQ(mrpt::fix(2.9), 2);
115  EXPECT_EQ(mrpt::fix(-1.2), -1);
116  EXPECT_EQ(mrpt::fix(-2.9), -2);
117 }
118 
119 TEST(bits_math, saturate_val)
120 {
121  EXPECT_DOUBLE_EQ(mrpt::saturate_val(11.2, -10.0, 10.0), 10.0);
122  EXPECT_DOUBLE_EQ(mrpt::saturate_val(1.2, -10.0, 10.0), 1.2);
123  EXPECT_DOUBLE_EQ(mrpt::saturate_val(-11.2, -10.0, 10.0), -10.0);
124  EXPECT_DOUBLE_EQ(mrpt::saturate_val(-1.2, -10.0, 10.0), -1.2);
125 }
126 
127 TEST(bits_math, saturate)
128 {
129  double v = 11.2;
130  mrpt::saturate(v, -10.0, 10.0);
131  EXPECT_DOUBLE_EQ(v, 10.0);
132  mrpt::saturate(v, -20.0, 20.0);
133  EXPECT_DOUBLE_EQ(v, 10.0);
134  mrpt::saturate(v, -1.0, 1.0);
135  EXPECT_DOUBLE_EQ(v, 1.0);
136 }
137 
138 TEST(bits_math, lowestPositive)
139 {
142  EXPECT_EQ(mrpt::lowestPositive(-10, 3), 3);
143  EXPECT_EQ(mrpt::lowestPositive(3, -1), 3);
144 }
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
int fix(T x)
Rounds toward zero.
const T min3(const T &A, const T &B, const T &C)
void saturate(T &var, const T sat_min, const T sat_max)
Saturate the value of var (the variable gets modified) so it does not get out of [min,max].
TEST(bits_math, sign)
constexpr double DEG2RAD(const double x)
Degrees to radians.
int sign(T x)
Returns the sign of X as "1" or "-1".
T round2up(T val)
Round up to the nearest power of two of a given number.
T hypot_fast(const T x, const T y)
Faster version of std::hypot(), to use when overflow is not an issue and we prefer fast code...
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
T lowestPositive(const T a, const T b)
Returns the smallest positive number among a and b.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
constexpr double RAD2DEG(const double x)
Radians to degrees.
EXPECT_EQ(out.image_pair_was_used.size(), NUM_IMGS)
EXPECT_NEAR(out.cam_params.rightCameraPose.x, 0.1194, 0.005)
T saturate_val(const T &value, const T sat_min, const T sat_max)
Like saturate() but it returns the value instead of modifying the variable.
const T max3(const T &A, const T &B, const T &C)
T abs_diff(const T a, const T b)
Efficient and portable evaluation of the absolute difference of two unsigned integer values (but will...
int signWithZero(T x)
Returns the sign of X as "0", "1" or "-1".



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 338471620 Mon Feb 17 00:12:39 2020 +0100 at lun feb 17 00:15:10 CET 2020