serialization_format_migration_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 
18 
19 #include <functional>
20 #include <memory>
21 
23 #include "cartographer/mapping/proto/internal/legacy_serialized_data.pb.h"
24 #include "cartographer/mapping/proto/pose_graph.pb.h"
25 #include "cartographer/mapping/proto/serialization.pb.h"
26 #include "cartographer/mapping/proto/trajectory_builder_options.pb.h"
27 #include "gmock/gmock.h"
28 #include "google/protobuf/text_format.h"
29 #include "gtest/gtest.h"
30 
31 namespace cartographer {
32 namespace io {
33 namespace {
34 
35 using ::google::protobuf::TextFormat;
36 using ::testing::Eq;
37 using ::testing::SizeIs;
38 
39 class MigrationTest : public ::testing::Test {
40  protected:
41  void SetUp() {
42  writer_.reset(new ForwardingProtoStreamWriter(
43  [this](const google::protobuf::Message* proto) -> bool {
44  std::string msg_string;
45  TextFormat::PrintToString(*proto, &msg_string);
46  this->output_messages_.push_back(msg_string);
47  return true;
48  }));
49 
50  mapping::proto::PoseGraph pose_graph;
51  mapping::proto::AllTrajectoryBuilderOptions all_options;
52  mapping::proto::LegacySerializedData submap;
53  submap.mutable_submap();
54  mapping::proto::LegacySerializedData node;
55  node.mutable_node();
56  mapping::proto::LegacySerializedData imu_data;
57  imu_data.mutable_imu_data();
58  mapping::proto::LegacySerializedData odometry_data;
59  odometry_data.mutable_odometry_data();
60  mapping::proto::LegacySerializedData fixed_frame_pose;
61  fixed_frame_pose.mutable_fixed_frame_pose_data();
62  mapping::proto::LegacySerializedData trajectory_data;
63  trajectory_data.mutable_trajectory_data();
64  mapping::proto::LegacySerializedData landmark_data;
65  landmark_data.mutable_landmark_data();
66 
67  reader_.AddProto(pose_graph);
68  reader_.AddProto(all_options);
69  reader_.AddProto(submap);
70  reader_.AddProto(node);
71  reader_.AddProto(imu_data);
72  reader_.AddProto(odometry_data);
73  reader_.AddProto(fixed_frame_pose);
74  reader_.AddProto(trajectory_data);
75  reader_.AddProto(landmark_data);
76  }
77 
78  InMemoryProtoStreamReader reader_;
79  std::unique_ptr<ForwardingProtoStreamWriter> writer_;
80  std::vector<std::string> output_messages_;
81 
82  static constexpr int kNumOriginalMessages = 9;
83 };
84 
85 TEST_F(MigrationTest, MigrationAddsHeaderAsFirstMessage) {
87  // We expect one message more than the original number of messages, because of
88  // the added header.
89  EXPECT_THAT(output_messages_, SizeIs(kNumOriginalMessages + 1));
90 
91  mapping::proto::SerializationHeader header;
92  EXPECT_TRUE(TextFormat::ParseFromString(output_messages_[0], &header));
93  EXPECT_THAT(header.format_version(), Eq(1));
94 }
95 
96 TEST_F(MigrationTest, SerializedDataOrderIsCorrect) {
98  EXPECT_THAT(output_messages_, SizeIs(kNumOriginalMessages + 1));
99 
100  std::vector<mapping::proto::SerializedData> serialized(
101  output_messages_.size() - 1);
102  for (size_t i = 1; i < output_messages_.size(); ++i) {
103  EXPECT_TRUE(
104  TextFormat::ParseFromString(output_messages_[i], &serialized[i - 1]));
105  }
106 
107  EXPECT_TRUE(serialized[0].has_pose_graph());
108  EXPECT_TRUE(serialized[1].has_all_trajectory_builder_options());
109  EXPECT_TRUE(serialized[2].has_submap());
110  EXPECT_TRUE(serialized[3].has_node());
111  EXPECT_TRUE(serialized[4].has_trajectory_data());
112  EXPECT_TRUE(serialized[5].has_imu_data());
113  EXPECT_TRUE(serialized[6].has_odometry_data());
114  EXPECT_TRUE(serialized[7].has_fixed_frame_pose_data());
115  EXPECT_TRUE(serialized[8].has_landmark_data());
116 }
117 
118 } // namespace
119 } // namespace io
120 } // namespace cartographer
void MigrateStreamFormatToVersion1(cartographer::io::ProtoStreamReaderInterface *const input, cartographer::io::ProtoStreamWriterInterface *const output)
InMemoryProtoStreamReader reader_
std::vector< std::string > output_messages_
std::unique_ptr< ForwardingProtoStreamWriter > writer_
static constexpr int kNumOriginalMessages


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