21 #include "glog/logging.h" 22 #include "gmock/gmock.h" 23 #include "google/protobuf/text_format.h" 24 #include "google/protobuf/util/message_differencer.h" 25 #include "gtest/gtest.h" 32 using ::cartographer::mapping::proto::SerializationHeader;
33 using ::cartographer::mapping::proto::SerializedData;
34 using ::google::protobuf::Message;
35 using ::google::protobuf::util::MessageDifferencer;
39 constexpr
char kSerializationHeaderProtoString[] =
"format_version: 1";
40 constexpr
char kUnsupportedSerializationHeaderProtoString[] =
41 "format_version: 123";
42 constexpr
char kPoseGraphProtoString[] =
"pose_graph {}";
43 constexpr
char kAllTrajectoryBuilderOptionsProtoString[] =
44 "all_trajectory_builder_options {}";
45 constexpr
char kSubmapProtoString[] =
"submap {}";
46 constexpr
char kNodeProtoString[] =
"node {}";
47 constexpr
char kTrajectoryDataProtoString[] =
"trajectory_data {}";
48 constexpr
char kImuDataProtoString[] =
"imu_data {}";
49 constexpr
char kOdometryDataProtoString[] =
"odometry_data {}";
50 constexpr
char kFixedFramePoseDataProtoString[] =
"fixed_frame_pose_data {}";
51 constexpr
char kLandmarkDataProtoString[] =
"landmark_data {}";
54 T ProtoFromStringOrDie(
const std::string& proto_string) {
56 CHECK(google::protobuf::TextFormat::ParseFromString(proto_string, &msg));
60 class ProtoStreamDeserializerTest :
public ::testing::Test {
62 void InitializeProtoReader(
63 const std::string& header_textpb,
64 const std::initializer_list<std::string>& data_textpbs) {
65 std::queue<std::unique_ptr<Message>> proto_queue;
66 proto_queue.emplace(make_unique<SerializationHeader>(
67 ProtoFromStringOrDie<SerializationHeader>(header_textpb)));
68 for (
const std::string& data_textpb : data_textpbs) {
69 proto_queue.emplace(make_unique<SerializedData>(
70 ProtoFromStringOrDie<SerializedData>(data_textpb)));
72 reader_ = make_unique<InMemoryProtoStreamReader>(std::move(proto_queue));
75 std::unique_ptr<InMemoryProtoStreamReader>
reader_;
79 TEST_F(ProtoStreamDeserializerTest, WorksOnGoldenTextStream) {
81 InitializeProtoReader(kSerializationHeaderProtoString,
83 kPoseGraphProtoString,
84 kAllTrajectoryBuilderOptionsProtoString,
87 kTrajectoryDataProtoString,
89 kOdometryDataProtoString,
90 kFixedFramePoseDataProtoString,
91 kLandmarkDataProtoString,
94 io::ProtoStreamDeserializer deserializer(
reader_.get());
96 EXPECT_TRUE(MessageDifferencer::Equals(
97 deserializer.header(), ProtoFromStringOrDie<SerializationHeader>(
98 kSerializationHeaderProtoString)));
100 EXPECT_TRUE(MessageDifferencer::Equals(
101 deserializer.pose_graph(),
102 ProtoFromStringOrDie<SerializedData>(kPoseGraphProtoString)
106 MessageDifferencer::Equals(deserializer.all_trajectory_builder_options(),
107 ProtoFromStringOrDie<SerializedData>(
108 kAllTrajectoryBuilderOptionsProtoString)
109 .all_trajectory_builder_options()));
111 SerializedData serialized_data;
112 EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
114 EXPECT_TRUE(MessageDifferencer::Equals(
116 ProtoFromStringOrDie<SerializedData>(kSubmapProtoString)));
118 EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
119 EXPECT_TRUE(MessageDifferencer::Equals(
120 serialized_data, ProtoFromStringOrDie<SerializedData>(kNodeProtoString)));
122 EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
123 EXPECT_TRUE(MessageDifferencer::Equals(
125 ProtoFromStringOrDie<SerializedData>(kTrajectoryDataProtoString)));
127 EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
128 EXPECT_TRUE(MessageDifferencer::Equals(
130 ProtoFromStringOrDie<SerializedData>(kImuDataProtoString)));
132 EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
133 EXPECT_TRUE(MessageDifferencer::Equals(
135 ProtoFromStringOrDie<SerializedData>(kOdometryDataProtoString)));
137 EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
138 EXPECT_TRUE(MessageDifferencer::Equals(
140 ProtoFromStringOrDie<SerializedData>(kFixedFramePoseDataProtoString)));
142 EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
143 EXPECT_TRUE(MessageDifferencer::Equals(
145 ProtoFromStringOrDie<SerializedData>(kLandmarkDataProtoString)));
147 EXPECT_FALSE(deserializer.ReadNextSerializedData(&serialized_data));
151 TEST_F(ProtoStreamDeserializerTest, FailsIfVersionNotSupported) {
152 InitializeProtoReader(kUnsupportedSerializationHeaderProtoString, {});
153 EXPECT_DEATH(common::make_unique<ProtoStreamDeserializer>(
reader_.get()),
154 "Unsupported serialization format");
_Unique_if< T >::_Single_object make_unique(Args &&... args)
std::unique_ptr< InMemoryProtoStreamReader > reader_