| 1 |
From 1c339b880c85a768b1aef935330011cea0879bb8 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Rainer Kuemmerle <[email protected]> |
| 3 |
Date: Sat, 21 Oct 2023 17:58:39 +0200 |
| 4 |
Subject: [PATCH] Improve print in case of failing test |
| 5 |
Relates #722 |
| 6 |
Origin: https://github.com/RainerKuemmerle/g2o/issues/722 |
| 7 |
Applied-Upstream: yes |
| 8 |
--- |
| 9 |
unit_test/slam3d/optimization_slam3d.cpp | 19 ++++++++++--------- |
| 10 |
1 file changed, 10 insertions(+), 9 deletions(-) |
| 11 |
|
| 12 |
diff --git a/unit_test/slam3d/optimization_slam3d.cpp b/unit_test/slam3d/optimization_slam3d.cpp |
| 13 |
index de3845ff..d94c1890 100644 |
| 14 |
--- a/unit_test/slam3d/optimization_slam3d.cpp |
| 15 |
+++ b/unit_test/slam3d/optimization_slam3d.cpp |
| 16 |
@@ -30,7 +30,8 @@ |
| 17 |
#include "g2o/core/optimization_algorithm_levenberg.h" |
| 18 |
#include "g2o/solvers/eigen/linear_solver_eigen.h" |
| 19 |
#include "g2o/types/slam3d/edge_se3.h" |
| 20 |
-#include "gtest/gtest.h" |
| 21 |
+#include "gmock/gmock.h" |
| 22 |
+#include "unit_test/test_helper/eigen_matcher.h" |
| 23 |
|
| 24 |
using namespace std; |
| 25 |
using namespace g2o; |
| 26 |
@@ -91,10 +92,10 @@ TYPED_TEST_P(Slam3DOptimization, Translation) { |
| 27 |
|
| 28 |
g2o::VertexSE3* v2AfterOpti = |
| 29 |
dynamic_cast<g2o::VertexSE3*>(this->optimizer.vertex(1)); |
| 30 |
- ASSERT_TRUE( |
| 31 |
- v2AfterOpti->estimate().translation().isApprox(g2o::Vector3::Zero())); |
| 32 |
- ASSERT_TRUE(v2AfterOpti->estimate().rotation().diagonal().isApprox( |
| 33 |
- g2o::Vector3::Ones())); |
| 34 |
+ EXPECT_THAT(print_wrap(v2AfterOpti->estimate().translation()), |
| 35 |
+ EigenMaxDiff(print_wrap(g2o::Vector3::Zero()), 1e-3)); |
| 36 |
+ EXPECT_THAT(print_wrap(v2AfterOpti->estimate().rotation().diagonal()), |
| 37 |
+ EigenMaxDiff(print_wrap(g2o::Vector3::Ones()), 1e-3)); |
| 38 |
} |
| 39 |
|
| 40 |
TYPED_TEST_P(Slam3DOptimization, Rotation) { |
| 41 |
@@ -130,10 +131,10 @@ TYPED_TEST_P(Slam3DOptimization, Rotation) { |
| 42 |
|
| 43 |
g2o::VertexSE3* v2AfterOpti = |
| 44 |
dynamic_cast<g2o::VertexSE3*>(this->optimizer.vertex(1)); |
| 45 |
- ASSERT_TRUE( |
| 46 |
- v2AfterOpti->estimate().translation().isApprox(g2o::Vector3::Zero())); |
| 47 |
- ASSERT_TRUE(v2AfterOpti->estimate().rotation().diagonal().isApprox( |
| 48 |
- g2o::Vector3::Ones())); |
| 49 |
+ EXPECT_THAT(print_wrap(v2AfterOpti->estimate().translation()), |
| 50 |
+ EigenMaxDiff(print_wrap(g2o::Vector3::Zero()), 1e-3)); |
| 51 |
+ EXPECT_THAT(print_wrap(v2AfterOpti->estimate().rotation().diagonal()), |
| 52 |
+ EigenMaxDiff(print_wrap(g2o::Vector3::Ones()), 1e-3)); |
| 53 |
} |
| 54 |
|
| 55 |
// registering the test suite and all the types to be tested |
| 56 |
diff --git a/unit_test/test_helper/eigen_matcher.h b/unit_test/test_helper/eigen_matcher.h |
| 57 |
new file mode 100644 |
| 58 |
index 00000000..2e8fd0a5 |
| 59 |
--- /dev/null |
| 60 |
+++ b/unit_test/test_helper/eigen_matcher.h |
| 61 |
@@ -0,0 +1,59 @@ |
| 62 |
+// g2o - General Graph Optimization |
| 63 |
+// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard |
| 64 |
+// All rights reserved. |
| 65 |
+// |
| 66 |
+// Redistribution and use in source and binary forms, with or without |
| 67 |
+// modification, are permitted provided that the following conditions are |
| 68 |
+// met: |
| 69 |
+// |
| 70 |
+// * Redistributions of source code must retain the above copyright notice, |
| 71 |
+// this list of conditions and the following disclaimer. |
| 72 |
+// * Redistributions in binary form must reproduce the above copyright |
| 73 |
+// notice, this list of conditions and the following disclaimer in the |
| 74 |
+// documentation and/or other materials provided with the distribution. |
| 75 |
+// |
| 76 |
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 77 |
+// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 78 |
+// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 79 |
+// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 80 |
+// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 81 |
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 82 |
+// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 83 |
+// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 84 |
+// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 85 |
+// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 86 |
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 87 |
+ |
| 88 |
+#include <gmock/gmock.h> |
| 89 |
+ |
| 90 |
+#include <Eigen/Core> |
| 91 |
+ |
| 92 |
+MATCHER_P(EigenEqual, expect, |
| 93 |
+ std::string(negation ? "isn't" : "is") + " equal to" + |
| 94 |
+ ::testing::PrintToString(expect)) { |
| 95 |
+ return arg == expect; |
| 96 |
+} |
| 97 |
+ |
| 98 |
+MATCHER_P2(EigenApproxEqual, expect, prec, |
| 99 |
+ std::string(negation ? "isn't" : "is") + " approx equal to" + |
| 100 |
+ ::testing::PrintToString(expect) + "\nwith precision " + |
| 101 |
+ ::testing::PrintToString(prec)) { |
| 102 |
+ return arg.isApprox(expect, prec); |
| 103 |
+} |
| 104 |
+ |
| 105 |
+MATCHER_P2(EigenMaxDiff, expect, max_diff, |
| 106 |
+ std::string(negation ? "doesn't" : "does") + " differ from " + |
| 107 |
+ ::testing::PrintToString(expect) + "\nmore than " + |
| 108 |
+ ::testing::PrintToString(max_diff)) { |
| 109 |
+ return (arg - expect).array().abs().maxCoeff() <= max_diff; |
| 110 |
+} |
| 111 |
+ |
| 112 |
+template <class Base> |
| 113 |
+class EigenPrintWrap : public Base { |
| 114 |
+ void PrintTo(const EigenPrintWrap& m, ::std::ostream* o) { *o << "\n" << m; } |
| 115 |
+}; |
| 116 |
+ |
| 117 |
+template <class Base> |
| 118 |
+const EigenPrintWrap<Base>& print_wrap(const Base& base) { |
| 119 |
+ return static_cast<const EigenPrintWrap<Base>&>(base); |
| 120 |
+} |
| 121 |
-- |
| 122 |
2.37.2 |