proto_stream_deserializer_test.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2018 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
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 // This test checks if the serialization works.
00062 TEST_F(ProtoStreamDeserializerTest, WorksOnGoldenTextStream) {
00063   // Load text proto into in_memory_reader.
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   // TODO(sebastianklose): Add matcher for protos in common place and use here.
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 }  // namespace
00142 }  // namespace io
00143 }  // namespace cartographer


cartographer
Author(s): The Cartographer Authors
autogenerated on Thu May 9 2019 02:27:35