From 1c339b880c85a768b1aef935330011cea0879bb8 Mon Sep 17 00:00:00 2001 From: Rainer Kuemmerle Date: Sat, 21 Oct 2023 17:58:39 +0200 Subject: [PATCH] Improve print in case of failing test Relates #722 Origin: https://github.com/RainerKuemmerle/g2o/issues/722 Applied-Upstream: yes --- unit_test/slam3d/optimization_slam3d.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/unit_test/slam3d/optimization_slam3d.cpp b/unit_test/slam3d/optimization_slam3d.cpp index de3845ff..d94c1890 100644 --- a/unit_test/slam3d/optimization_slam3d.cpp +++ b/unit_test/slam3d/optimization_slam3d.cpp @@ -30,7 +30,8 @@ #include "g2o/core/optimization_algorithm_levenberg.h" #include "g2o/solvers/eigen/linear_solver_eigen.h" #include "g2o/types/slam3d/edge_se3.h" -#include "gtest/gtest.h" +#include "gmock/gmock.h" +#include "unit_test/test_helper/eigen_matcher.h" using namespace std; using namespace g2o; @@ -91,10 +92,10 @@ TYPED_TEST_P(Slam3DOptimization, Translation) { g2o::VertexSE3* v2AfterOpti = dynamic_cast(this->optimizer.vertex(1)); - ASSERT_TRUE( - v2AfterOpti->estimate().translation().isApprox(g2o::Vector3::Zero())); - ASSERT_TRUE(v2AfterOpti->estimate().rotation().diagonal().isApprox( - g2o::Vector3::Ones())); + EXPECT_THAT(print_wrap(v2AfterOpti->estimate().translation()), + EigenMaxDiff(print_wrap(g2o::Vector3::Zero()), 1e-3)); + EXPECT_THAT(print_wrap(v2AfterOpti->estimate().rotation().diagonal()), + EigenMaxDiff(print_wrap(g2o::Vector3::Ones()), 1e-3)); } TYPED_TEST_P(Slam3DOptimization, Rotation) { @@ -130,10 +131,10 @@ TYPED_TEST_P(Slam3DOptimization, Rotation) { g2o::VertexSE3* v2AfterOpti = dynamic_cast(this->optimizer.vertex(1)); - ASSERT_TRUE( - v2AfterOpti->estimate().translation().isApprox(g2o::Vector3::Zero())); - ASSERT_TRUE(v2AfterOpti->estimate().rotation().diagonal().isApprox( - g2o::Vector3::Ones())); + EXPECT_THAT(print_wrap(v2AfterOpti->estimate().translation()), + EigenMaxDiff(print_wrap(g2o::Vector3::Zero()), 1e-3)); + EXPECT_THAT(print_wrap(v2AfterOpti->estimate().rotation().diagonal()), + EigenMaxDiff(print_wrap(g2o::Vector3::Ones()), 1e-3)); } // registering the test suite and all the types to be tested diff --git a/unit_test/test_helper/eigen_matcher.h b/unit_test/test_helper/eigen_matcher.h new file mode 100644 index 00000000..2e8fd0a5 --- /dev/null +++ b/unit_test/test_helper/eigen_matcher.h @@ -0,0 +1,59 @@ +// g2o - General Graph Optimization +// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include + +#include + +MATCHER_P(EigenEqual, expect, + std::string(negation ? "isn't" : "is") + " equal to" + + ::testing::PrintToString(expect)) { + return arg == expect; +} + +MATCHER_P2(EigenApproxEqual, expect, prec, + std::string(negation ? "isn't" : "is") + " approx equal to" + + ::testing::PrintToString(expect) + "\nwith precision " + + ::testing::PrintToString(prec)) { + return arg.isApprox(expect, prec); +} + +MATCHER_P2(EigenMaxDiff, expect, max_diff, + std::string(negation ? "doesn't" : "does") + " differ from " + + ::testing::PrintToString(expect) + "\nmore than " + + ::testing::PrintToString(max_diff)) { + return (arg - expect).array().abs().maxCoeff() <= max_diff; +} + +template +class EigenPrintWrap : public Base { + void PrintTo(const EigenPrintWrap& m, ::std::ostream* o) { *o << "\n" << m; } +}; + +template +const EigenPrintWrap& print_wrap(const Base& base) { + return static_cast&>(base); +} -- 2.37.2