Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "cartographer/io/proto_stream_deserializer.h"
00018
00019 #include <memory>
00020
00021 #include "cartographer/io/internal/in_memory_proto_stream.h"
00022 #include "cartographer/io/testing/test_helpers.h"
00023 #include "glog/logging.h"
00024 #include "gmock/gmock.h"
00025 #include "google/protobuf/text_format.h"
00026 #include "google/protobuf/util/message_differencer.h"
00027 #include "gtest/gtest.h"
00028
00029 namespace cartographer {
00030 namespace io {
00031 namespace {
00032
00033 using ::cartographer::io::testing::ProtoFromStringOrDie;
00034 using ::cartographer::io::testing::ProtoReaderFromStrings;
00035 using ::cartographer::mapping::proto::SerializationHeader;
00036 using ::cartographer::mapping::proto::SerializedData;
00037 using ::google::protobuf::Message;
00038 using ::google::protobuf::util::MessageDifferencer;
00039 using ::testing::Eq;
00040 using ::testing::Not;
00041
00042 constexpr char kSerializationHeaderProtoString[] = "format_version: 1";
00043 constexpr char kUnsupportedSerializationHeaderProtoString[] =
00044 "format_version: 123";
00045 constexpr char kPoseGraphProtoString[] = "pose_graph {}";
00046 constexpr char kAllTrajectoryBuilderOptionsProtoString[] =
00047 "all_trajectory_builder_options {}";
00048 constexpr char kSubmapProtoString[] = "submap {}";
00049 constexpr char kNodeProtoString[] = "node {}";
00050 constexpr char kTrajectoryDataProtoString[] = "trajectory_data {}";
00051 constexpr char kImuDataProtoString[] = "imu_data {}";
00052 constexpr char kOdometryDataProtoString[] = "odometry_data {}";
00053 constexpr char kFixedFramePoseDataProtoString[] = "fixed_frame_pose_data {}";
00054 constexpr char kLandmarkDataProtoString[] = "landmark_data {}";
00055
00056 class ProtoStreamDeserializerTest : public ::testing::Test {
00057 protected:
00058 std::unique_ptr<InMemoryProtoStreamReader> reader_;
00059 };
00060
00061
00062 TEST_F(ProtoStreamDeserializerTest, WorksOnGoldenTextStream) {
00063
00064 reader_ = ProtoReaderFromStrings(kSerializationHeaderProtoString,
00065 {
00066 kPoseGraphProtoString,
00067 kAllTrajectoryBuilderOptionsProtoString,
00068 kSubmapProtoString,
00069 kNodeProtoString,
00070 kTrajectoryDataProtoString,
00071 kImuDataProtoString,
00072 kOdometryDataProtoString,
00073 kFixedFramePoseDataProtoString,
00074 kLandmarkDataProtoString,
00075 });
00076
00077 io::ProtoStreamDeserializer deserializer(reader_.get());
00078
00079 EXPECT_TRUE(MessageDifferencer::Equals(
00080 deserializer.header(), ProtoFromStringOrDie<SerializationHeader>(
00081 kSerializationHeaderProtoString)));
00082
00083 EXPECT_TRUE(MessageDifferencer::Equals(
00084 deserializer.pose_graph(),
00085 ProtoFromStringOrDie<SerializedData>(kPoseGraphProtoString)
00086 .pose_graph()));
00087
00088 EXPECT_TRUE(
00089 MessageDifferencer::Equals(deserializer.all_trajectory_builder_options(),
00090 ProtoFromStringOrDie<SerializedData>(
00091 kAllTrajectoryBuilderOptionsProtoString)
00092 .all_trajectory_builder_options()));
00093
00094 SerializedData serialized_data;
00095 EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
00096
00097 EXPECT_TRUE(MessageDifferencer::Equals(
00098 serialized_data,
00099 ProtoFromStringOrDie<SerializedData>(kSubmapProtoString)));
00100
00101 EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
00102 EXPECT_TRUE(MessageDifferencer::Equals(
00103 serialized_data, ProtoFromStringOrDie<SerializedData>(kNodeProtoString)));
00104
00105 EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
00106 EXPECT_TRUE(MessageDifferencer::Equals(
00107 serialized_data,
00108 ProtoFromStringOrDie<SerializedData>(kTrajectoryDataProtoString)));
00109
00110 EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
00111 EXPECT_TRUE(MessageDifferencer::Equals(
00112 serialized_data,
00113 ProtoFromStringOrDie<SerializedData>(kImuDataProtoString)));
00114
00115 EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
00116 EXPECT_TRUE(MessageDifferencer::Equals(
00117 serialized_data,
00118 ProtoFromStringOrDie<SerializedData>(kOdometryDataProtoString)));
00119
00120 EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
00121 EXPECT_TRUE(MessageDifferencer::Equals(
00122 serialized_data,
00123 ProtoFromStringOrDie<SerializedData>(kFixedFramePoseDataProtoString)));
00124
00125 EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
00126 EXPECT_TRUE(MessageDifferencer::Equals(
00127 serialized_data,
00128 ProtoFromStringOrDie<SerializedData>(kLandmarkDataProtoString)));
00129
00130 EXPECT_FALSE(deserializer.ReadNextSerializedData(&serialized_data));
00131 EXPECT_TRUE(reader_->eof());
00132 }
00133
00134 TEST_F(ProtoStreamDeserializerTest, FailsIfVersionNotSupported) {
00135 reader_ =
00136 ProtoReaderFromStrings(kUnsupportedSerializationHeaderProtoString, {});
00137 EXPECT_DEATH(absl::make_unique<ProtoStreamDeserializer>(reader_.get()),
00138 "Unsupported serialization format");
00139 }
00140
00141 }
00142 }
00143 }