proto_stream_deserializer_test.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2018 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <memory>
18 
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"
26 
27 namespace cartographer {
28 namespace io {
29 namespace {
30 
32 using ::cartographer::mapping::proto::SerializationHeader;
33 using ::cartographer::mapping::proto::SerializedData;
34 using ::google::protobuf::Message;
35 using ::google::protobuf::util::MessageDifferencer;
36 using ::testing::Eq;
37 using ::testing::Not;
38 
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 {}";
52 
53 template <typename T>
54 T ProtoFromStringOrDie(const std::string& proto_string) {
55  T msg;
56  CHECK(google::protobuf::TextFormat::ParseFromString(proto_string, &msg));
57  return msg;
58 }
59 
60 class ProtoStreamDeserializerTest : public ::testing::Test {
61  protected:
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)));
71  }
72  reader_ = make_unique<InMemoryProtoStreamReader>(std::move(proto_queue));
73  }
74 
75  std::unique_ptr<InMemoryProtoStreamReader> reader_;
76 };
77 
78 // This test checks if the serialization works.
79 TEST_F(ProtoStreamDeserializerTest, WorksOnGoldenTextStream) {
80  // Load text proto into in_memory_reader.
81  InitializeProtoReader(kSerializationHeaderProtoString,
82  {
83  kPoseGraphProtoString,
84  kAllTrajectoryBuilderOptionsProtoString,
85  kSubmapProtoString,
86  kNodeProtoString,
87  kTrajectoryDataProtoString,
88  kImuDataProtoString,
89  kOdometryDataProtoString,
90  kFixedFramePoseDataProtoString,
91  kLandmarkDataProtoString,
92  });
93 
94  io::ProtoStreamDeserializer deserializer(reader_.get());
95 
96  EXPECT_TRUE(MessageDifferencer::Equals(
97  deserializer.header(), ProtoFromStringOrDie<SerializationHeader>(
98  kSerializationHeaderProtoString)));
99 
100  EXPECT_TRUE(MessageDifferencer::Equals(
101  deserializer.pose_graph(),
102  ProtoFromStringOrDie<SerializedData>(kPoseGraphProtoString)
103  .pose_graph()));
104 
105  EXPECT_TRUE(
106  MessageDifferencer::Equals(deserializer.all_trajectory_builder_options(),
107  ProtoFromStringOrDie<SerializedData>(
108  kAllTrajectoryBuilderOptionsProtoString)
109  .all_trajectory_builder_options()));
110 
111  SerializedData serialized_data;
112  EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
113  // TODO(sebastianklose): Add matcher for protos in common place and use here.
114  EXPECT_TRUE(MessageDifferencer::Equals(
115  serialized_data,
116  ProtoFromStringOrDie<SerializedData>(kSubmapProtoString)));
117 
118  EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
119  EXPECT_TRUE(MessageDifferencer::Equals(
120  serialized_data, ProtoFromStringOrDie<SerializedData>(kNodeProtoString)));
121 
122  EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
123  EXPECT_TRUE(MessageDifferencer::Equals(
124  serialized_data,
125  ProtoFromStringOrDie<SerializedData>(kTrajectoryDataProtoString)));
126 
127  EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
128  EXPECT_TRUE(MessageDifferencer::Equals(
129  serialized_data,
130  ProtoFromStringOrDie<SerializedData>(kImuDataProtoString)));
131 
132  EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
133  EXPECT_TRUE(MessageDifferencer::Equals(
134  serialized_data,
135  ProtoFromStringOrDie<SerializedData>(kOdometryDataProtoString)));
136 
137  EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
138  EXPECT_TRUE(MessageDifferencer::Equals(
139  serialized_data,
140  ProtoFromStringOrDie<SerializedData>(kFixedFramePoseDataProtoString)));
141 
142  EXPECT_TRUE(deserializer.ReadNextSerializedData(&serialized_data));
143  EXPECT_TRUE(MessageDifferencer::Equals(
144  serialized_data,
145  ProtoFromStringOrDie<SerializedData>(kLandmarkDataProtoString)));
146 
147  EXPECT_FALSE(deserializer.ReadNextSerializedData(&serialized_data));
148  EXPECT_TRUE(reader_->eof());
149 }
150 
151 TEST_F(ProtoStreamDeserializerTest, FailsIfVersionNotSupported) {
152  InitializeProtoReader(kUnsupportedSerializationHeaderProtoString, {});
153  EXPECT_DEATH(common::make_unique<ProtoStreamDeserializer>(reader_.get()),
154  "Unsupported serialization format");
155 }
156 
157 } // namespace
158 } // namespace io
159 } // namespace cartographer
_Unique_if< T >::_Single_object make_unique(Args &&... args)
Definition: make_unique.h:46
std::unique_ptr< InMemoryProtoStreamReader > reader_


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58