MRPT  2.0.1
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-2020, 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  // Note: gtest doesn't support long double, hence the static_cast:
60  EXPECT_NEAR(static_cast<double>(mrpt::DEG2RAD(180.0L)), M_PI, 1e-6);
61 
62  EXPECT_NEAR(mrpt::DEG2RAD(180.0), M_PI, 1e-6);
63  EXPECT_NEAR(mrpt::DEG2RAD(180), M_PI, 1e-6);
64  EXPECT_NEAR(mrpt::DEG2RAD(180.0f), mrpt::d2f(M_PI), 1e-4f);
65  {
66  using namespace mrpt; // _deg
67  EXPECT_NEAR(180.0_deg, M_PI, 1e-6);
68  }
69 
71  static_cast<double>(mrpt::RAD2DEG(static_cast<long double>(M_PI))),
72  180.0, 1e-6);
73  EXPECT_NEAR(mrpt::RAD2DEG(M_PI), 180.0, 1e-6);
74  EXPECT_NEAR(mrpt::RAD2DEG(mrpt::d2f(M_PI)), 180.0f, 1e-4f);
75 }
76 
77 TEST(bits_math, signWithZero)
78 {
80  EXPECT_EQ(mrpt::signWithZero(89.0), 1);
83  EXPECT_EQ(mrpt::signWithZero(-89), -1);
84  EXPECT_EQ(mrpt::signWithZero(-89.0), -1);
85 }
86 
87 TEST(bits_math, abs_diff)
88 {
89  EXPECT_EQ(mrpt::abs_diff(10, 20), 10);
90  EXPECT_EQ(mrpt::abs_diff(20, 10), 10);
91  EXPECT_DOUBLE_EQ(mrpt::abs_diff(20.0, 10.0), 10.0);
92  EXPECT_DOUBLE_EQ(mrpt::abs_diff(10.0, 20.0), 10.0);
93 }
94 
95 TEST(bits_math, min3)
96 {
97  EXPECT_EQ(mrpt::min3(1, 2, 3), 1);
98  EXPECT_EQ(mrpt::min3(-1, -2, -3), -3);
99 
100  EXPECT_DOUBLE_EQ(mrpt::min3(1., 2., 3.), 1.);
101  EXPECT_DOUBLE_EQ(mrpt::min3(-1., -2., -3.), -3.);
102 }
103 
104 TEST(bits_math, max3)
105 {
106  EXPECT_EQ(mrpt::max3(1, 2, 3), 3);
107  EXPECT_EQ(mrpt::max3(-1, -2, -3), -1);
108 
109  EXPECT_DOUBLE_EQ(mrpt::max3(1., 2., 3.), 3.);
110  EXPECT_DOUBLE_EQ(mrpt::max3(-1., -2., -3.), -1.);
111 }
112 
113 TEST(bits_math, fix)
114 {
115  EXPECT_EQ(mrpt::fix(1.2), 1);
116  EXPECT_EQ(mrpt::fix(2.9), 2);
117  EXPECT_EQ(mrpt::fix(-1.2), -1);
118  EXPECT_EQ(mrpt::fix(-2.9), -2);
119 }
120 
121 TEST(bits_math, saturate_val)
122 {
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  EXPECT_DOUBLE_EQ(mrpt::saturate_val(-11.2, -10.0, 10.0), -10.0);
126  EXPECT_DOUBLE_EQ(mrpt::saturate_val(-1.2, -10.0, 10.0), -1.2);
127 }
128 
129 TEST(bits_math, saturate)
130 {
131  double v = 11.2;
132  mrpt::saturate(v, -10.0, 10.0);
133  EXPECT_DOUBLE_EQ(v, 10.0);
134  mrpt::saturate(v, -20.0, 20.0);
135  EXPECT_DOUBLE_EQ(v, 10.0);
136  mrpt::saturate(v, -1.0, 1.0);
137  EXPECT_DOUBLE_EQ(v, 1.0);
138 }
139 
140 TEST(bits_math, lowestPositive)
141 {
144  EXPECT_EQ(mrpt::lowestPositive(-10, 3), 3);
145  EXPECT_EQ(mrpt::lowestPositive(3, -1), 3);
146 }
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].
float d2f(const double d)
shortcut for static_cast<float>(double)
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 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020