[GenericPoseGraph] Move ParseProto and Near to test_helpers.h (#1307)

master
Alexander Belyaev 2018-07-20 09:30:03 +02:00 committed by GitHub
parent 7fa11dcde6
commit 42d7133a2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 40 deletions

View File

@ -18,8 +18,7 @@
#include "cartographer/common/make_unique.h" #include "cartographer/common/make_unique.h"
#include "cartographer/mapping/internal/optimization/cost_functions/cost_helpers.h" #include "cartographer/mapping/internal/optimization/cost_functions/cost_helpers.h"
#include "gmock/gmock.h" #include "cartographer/pose_graph/internal/testing/test_helpers.h"
#include "google/protobuf/text_format.h"
namespace cartographer { namespace cartographer {
namespace pose_graph { namespace pose_graph {
@ -30,8 +29,10 @@ constexpr int kResidualsCount = 3;
constexpr int kParameterBlocksCount = 2; constexpr int kParameterBlocksCount = 2;
constexpr int kJacobianColDimension = kResidualsCount * kPoseDimension; constexpr int kJacobianColDimension = kResidualsCount * kPoseDimension;
using ::google::protobuf::TextFormat;
using ::testing::ElementsAre; using ::testing::ElementsAre;
using testing::Near;
using testing::ParseProto;
using ResidualType = std::array<double, kResidualsCount>; using ResidualType = std::array<double, kResidualsCount>;
using JacobianType = std::array<std::array<double, kJacobianColDimension>, using JacobianType = std::array<std::array<double, kJacobianColDimension>,
kParameterBlocksCount>; kParameterBlocksCount>;
@ -67,7 +68,6 @@ class AutoDiffRelativePoseCost {
class RelativePoseCost2DTest : public ::testing::Test { class RelativePoseCost2DTest : public ::testing::Test {
public: public:
RelativePoseCost2DTest() { RelativePoseCost2DTest() {
proto::RelativePose2D::Parameters parameters;
constexpr char kParameters[] = R"PROTO( constexpr char kParameters[] = R"PROTO(
first_t_second { first_t_second {
translation: { x: 1 y: 1 } translation: { x: 1 y: 1 }
@ -76,8 +76,9 @@ class RelativePoseCost2DTest : public ::testing::Test {
translation_weight: 1 translation_weight: 1
rotation_weight: 10 rotation_weight: 10
)PROTO"; )PROTO";
EXPECT_TRUE(TextFormat::ParseFromString(kParameters, &parameters));
auto parameters =
ParseProto<proto::RelativePose2D::Parameters>(kParameters);
auto_diff_cost_ = common::make_unique<RelativePoseCost2D>(parameters); auto_diff_cost_ = common::make_unique<RelativePoseCost2D>(parameters);
analytical_cost_ = common::make_unique< analytical_cost_ = common::make_unique<
ceres::AutoDiffCostFunction<AutoDiffRelativePoseCost, kResidualsCount, ceres::AutoDiffCostFunction<AutoDiffRelativePoseCost, kResidualsCount,
@ -115,11 +116,6 @@ class RelativePoseCost2DTest : public ::testing::Test {
std::unique_ptr<ceres::CostFunction> analytical_cost_; std::unique_ptr<ceres::CostFunction> analytical_cost_;
}; };
::testing::Matcher<double> Near(double expected) {
constexpr double kPrecision = 1e-05;
return ::testing::DoubleNear(expected, kPrecision);
}
TEST_F(RelativePoseCost2DTest, CompareAutoDiffAndAnalytical) { TEST_F(RelativePoseCost2DTest, CompareAutoDiffAndAnalytical) {
std::array<double, kPoseDimension> start_pose{{1., 1., 1.}}; std::array<double, kPoseDimension> start_pose{{1., 1., 1.}};
std::array<double, kPoseDimension> end_pose{{10., 1., 100.}}; std::array<double, kPoseDimension> end_pose{{10., 1., 100.}};

View File

@ -17,26 +17,20 @@
#include "cartographer/pose_graph/constraint/cost_function/relative_pose_cost_3d.h" #include "cartographer/pose_graph/constraint/cost_function/relative_pose_cost_3d.h"
#include "cartographer/pose_graph/internal/testing/test_helpers.h" #include "cartographer/pose_graph/internal/testing/test_helpers.h"
#include "gmock/gmock.h"
#include "google/protobuf/text_format.h"
namespace cartographer { namespace cartographer {
namespace pose_graph { namespace pose_graph {
namespace { namespace {
using ::google::protobuf::TextFormat;
using ::testing::ElementsAre; using ::testing::ElementsAre;
using testing::EqualsProto; using testing::EqualsProto;
using testing::Near;
using testing::ParseProto;
using PositionType = std::array<double, 3>; using PositionType = std::array<double, 3>;
using RotationType = std::array<double, 4>; using RotationType = std::array<double, 4>;
using ResidualType = std::array<double, 6>; using ResidualType = std::array<double, 6>;
::testing::Matcher<double> Near(double expected) {
constexpr double kPrecision = 1e-05;
return ::testing::DoubleNear(expected, kPrecision);
}
constexpr char kParameters[] = R"PROTO( constexpr char kParameters[] = R"PROTO(
first_t_second { first_t_second {
translation: { x: 1 y: 2 z: 3 } translation: { x: 1 y: 2 z: 3 }
@ -47,18 +41,16 @@ constexpr char kParameters[] = R"PROTO(
)PROTO"; )PROTO";
TEST(RelativePoseCost3DTest, SerializesCorrectly) { TEST(RelativePoseCost3DTest, SerializesCorrectly) {
proto::RelativePose3D::Parameters relative_pose_parameters; auto relative_pose_parameters =
ASSERT_TRUE( ParseProto<proto::RelativePose3D::Parameters>(kParameters);
TextFormat::ParseFromString(kParameters, &relative_pose_parameters));
RelativePoseCost3D relative_pose_cost_3d(relative_pose_parameters); RelativePoseCost3D relative_pose_cost_3d(relative_pose_parameters);
const auto actual_proto = relative_pose_cost_3d.ToProto(); const auto actual_proto = relative_pose_cost_3d.ToProto();
EXPECT_THAT(actual_proto, EqualsProto(kParameters)); EXPECT_THAT(actual_proto, EqualsProto(kParameters));
} }
TEST(RelativePoseCost3DTest, EvaluatesCorrectly) { TEST(RelativePoseCost3DTest, EvaluatesCorrectly) {
proto::RelativePose3D::Parameters relative_pose_parameters; auto relative_pose_parameters =
ASSERT_TRUE( ParseProto<proto::RelativePose3D::Parameters>(kParameters);
TextFormat::ParseFromString(kParameters, &relative_pose_parameters));
RelativePoseCost3D relative_pose_cost_3d(relative_pose_parameters); RelativePoseCost3D relative_pose_cost_3d(relative_pose_parameters);
const PositionType kPosition1{{1., 1., 1.}}; const PositionType kPosition1{{1., 1., 1.}};

View File

@ -24,14 +24,25 @@
namespace cartographer { namespace cartographer {
namespace testing { namespace testing {
template <typename ProtoType>
ProtoType ParseProto(const std::string& proto_string) {
ProtoType proto;
EXPECT_TRUE(
::google::protobuf::TextFormat::ParseFromString(proto_string, &proto));
return proto;
}
MATCHER_P(EqualsProto, expected_proto_string, "") { MATCHER_P(EqualsProto, expected_proto_string, "") {
using ConstProtoType = typename std::remove_reference<decltype(arg)>::type; using ConstProtoType = typename std::remove_reference<decltype(arg)>::type;
using ProtoType = typename std::remove_cv<ConstProtoType>::type;
typename std::remove_cv<ConstProtoType>::type expected_proto; return google::protobuf::util::MessageDifferencer::Equals(
EXPECT_TRUE(google::protobuf::TextFormat::ParseFromString( arg, ParseProto<ProtoType>(expected_proto_string));
expected_proto_string, &expected_proto)); }
return google::protobuf::util::MessageDifferencer::Equals(arg,
expected_proto); ::testing::Matcher<double> Near(double expected) {
constexpr double kPrecision = 1e-05;
return ::testing::DoubleNear(expected, kPrecision);
} }
} // namespace testing } // namespace testing

View File

@ -18,14 +18,14 @@
#include "cartographer/common/make_unique.h" #include "cartographer/common/make_unique.h"
#include "cartographer/pose_graph/constraint/relative_pose_constraint_2d.h" #include "cartographer/pose_graph/constraint/relative_pose_constraint_2d.h"
#include "google/protobuf/text_format.h" #include "cartographer/pose_graph/internal/testing/test_helpers.h"
#include "gtest/gtest.h"
namespace cartographer { namespace cartographer {
namespace pose_graph { namespace pose_graph {
namespace { namespace {
using testing::ParseProto;
// TODO(pifon): Use the factory function, when the factory is done. // TODO(pifon): Use the factory function, when the factory is done.
Pose2D GetPose2D(const proto::Node& proto) { Pose2D GetPose2D(const proto::Node& proto) {
return {NodeId(proto.id()), proto.constant(), return {NodeId(proto.id()), proto.constant(),
@ -33,14 +33,6 @@ Pose2D GetPose2D(const proto::Node& proto) {
proto.parameters().pose_2d().rotation()}; proto.parameters().pose_2d().rotation()};
} }
template <typename ProtoType>
ProtoType ParseProto(const std::string& proto_string) {
ProtoType proto;
EXPECT_TRUE(
::google::protobuf::TextFormat::ParseFromString(proto_string, &proto));
return proto;
}
constexpr char kStartNode[] = R"PROTO( constexpr char kStartNode[] = R"PROTO(
id { object_id: "start_node" timestamp: 1 } id { object_id: "start_node" timestamp: 1 }
constant: false constant: false