Debian Codegraph @

unit_test/stuff/misc_tests.cpp

raw symbols SCIP
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 #include "g2o/stuff/misc.h"
28 #include "gtest/gtest.h"
29
30 namespace {
31 double simple_normalize_theta(double th) {
32 while (th >= g2o::const_pi()) th -= 2. * g2o::const_pi();
33 while (th < -g2o::const_pi()) th += 2. * g2o::const_pi();
34 return th;
35 }
36 } // namespace
37
38 TEST(Stuff, NormalizeTheta) {
39 constexpr double epsilon = 1e-9;
40 EXPECT_NEAR(0, g2o::normalize_theta(0), epsilon);
41 EXPECT_NEAR(M_PI, g2o::normalize_theta(M_PI), epsilon);
42 EXPECT_NEAR(0, g2o::normalize_theta(2 * M_PI), epsilon);
43 EXPECT_NEAR(M_PI, g2o::normalize_theta(3 * M_PI), epsilon);
44 EXPECT_NEAR(0, g2o::normalize_theta(4 * M_PI), epsilon);
45
46 EXPECT_NEAR(0, g2o::normalize_theta(-0), epsilon);
47 EXPECT_NEAR(M_PI, g2o::normalize_theta(-M_PI), epsilon);
48 EXPECT_NEAR(0, g2o::normalize_theta(-2 * M_PI), epsilon);
49 EXPECT_NEAR(M_PI, g2o::normalize_theta(-3 * M_PI), epsilon);
50 EXPECT_NEAR(0, g2o::normalize_theta(-4 * M_PI), epsilon);
51
52 EXPECT_NEAR(0, g2o::normalize_theta(-0), epsilon);
53 EXPECT_NEAR(-M_PI / 2, g2o::normalize_theta(-M_PI / 2), epsilon);
54 EXPECT_NEAR(M_PI, g2o::normalize_theta(-M_PI), epsilon);
55 EXPECT_NEAR(M_PI / 2, g2o::normalize_theta(-3 * M_PI / 2), epsilon);
56 EXPECT_NEAR(0, g2o::normalize_theta(-4 * M_PI / 2), epsilon);
57
58 EXPECT_NEAR(0, g2o::normalize_theta(0), epsilon);
59 EXPECT_NEAR(M_PI / 2, g2o::normalize_theta(M_PI / 2), epsilon);
60 EXPECT_NEAR(M_PI / 2, g2o::normalize_theta(5 * M_PI / 2), epsilon);
61 EXPECT_NEAR(M_PI / 2, g2o::normalize_theta(9 * M_PI / 2), epsilon);
62 EXPECT_NEAR(M_PI / 2, g2o::normalize_theta(-3 * M_PI / 2), epsilon);
63 }
64
65 TEST(Stuff, NormalizeThetaCmpBruteForce) {
66 constexpr double epsilon = 1e-9;
67 for (double d = -10.; d <= 10.; d += 0.1) {
68 EXPECT_NEAR(simple_normalize_theta(d), g2o::normalize_theta(d), epsilon);
69 }
70 }
71
72 TEST(Stuff, Deg2Rad) {
73 constexpr double epsilon = 1e-9;
74 EXPECT_NEAR(0, g2o::deg2rad(0), epsilon);
75 EXPECT_NEAR(M_PI / 2, g2o::deg2rad(90), epsilon);
76 EXPECT_NEAR(M_PI, g2o::deg2rad(180), epsilon);
77 EXPECT_NEAR(M_PI * 3 / 2, g2o::deg2rad(270), epsilon);
78 EXPECT_NEAR(2 * M_PI, g2o::deg2rad(360), epsilon);
79 EXPECT_NEAR(M_PI / 3, g2o::deg2rad(60), epsilon);
80 EXPECT_NEAR(M_PI * 2 / 3, g2o::deg2rad(120), epsilon);
81 EXPECT_NEAR(M_PI / 4, g2o::deg2rad(45), epsilon);
82 EXPECT_NEAR(M_PI * 3 / 4, g2o::deg2rad(135), epsilon);
83 EXPECT_NEAR(M_PI / 6, g2o::deg2rad(30), epsilon);
84 }
85
86 TEST(Stuff, Rad2Deg) {
87 constexpr double epsilon = 1e-9;
88 EXPECT_NEAR(g2o::rad2deg(0), 0, epsilon);
89 EXPECT_NEAR(g2o::rad2deg(M_PI / 2), 90, epsilon);
90 EXPECT_NEAR(g2o::rad2deg(M_PI), 180, epsilon);
91 EXPECT_NEAR(g2o::rad2deg(M_PI * 3 / 2), 270, epsilon);
92 EXPECT_NEAR(g2o::rad2deg(2 * M_PI), 360, epsilon);
93 EXPECT_NEAR(g2o::rad2deg(M_PI / 3), 60, epsilon);
94 EXPECT_NEAR(g2o::rad2deg(M_PI * 2 / 3), 120, epsilon);
95 EXPECT_NEAR(g2o::rad2deg(M_PI / 4), 45, epsilon);
96 EXPECT_NEAR(g2o::rad2deg(M_PI * 3 / 4), 135, epsilon);
97 EXPECT_NEAR(g2o::rad2deg(M_PI / 6), 30, epsilon);
98 }
99
100 TEST(Stuff, ArrayHasNaN) {
101 constexpr int size = 10;
102 double data[size];
103 std::fill_n(data, size, 0);
104
105 auto aux = [](const double* data, int size) {
106 int nanIndex = -1;
107 bool hasNan = g2o::arrayHasNaN(data, size, &nanIndex);
108 return std::make_pair(hasNan, nanIndex);
109 };
110
111 EXPECT_EQ(std::make_pair(false, -1), aux(data, size));
112
113 for (int i = 0; i < size; ++i) {
114 std::fill_n(data, size, 0);
115 data[i] = std::numeric_limits<double>::quiet_NaN();
116 EXPECT_EQ(std::make_pair(true, i), aux(data, size));
117 }
118 }
119
120 TEST(Stuff, Square) {
121 EXPECT_DOUBLE_EQ(4, g2o::square(2));
122 EXPECT_DOUBLE_EQ(4, g2o::square(-2));
123 }
124
125 TEST(Stuff, Hypot) {
126 EXPECT_DOUBLE_EQ(hypot(2, 0), 2);
127 EXPECT_DOUBLE_EQ(hypot(-2, 0), 2);
128 EXPECT_DOUBLE_EQ(hypot(0, 2), 2);
129 EXPECT_DOUBLE_EQ(hypot(0, -2), 2);
130 EXPECT_DOUBLE_EQ(hypot(3, 3), sqrt(18));
131 EXPECT_DOUBLE_EQ(hypot(-3, 3), sqrt(18));
132 EXPECT_DOUBLE_EQ(hypot(-3, 3), sqrt(18));
133 EXPECT_DOUBLE_EQ(hypot(3, -3), sqrt(18));
134 }