fake_file_writer_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 <vector>
18 
20 #include "glog/logging.h"
21 #include "gtest/gtest.h"
22 
23 namespace cartographer {
24 namespace io {
25 namespace {
26 
27 std::string toString(const std::vector<char>& data) {
28  return std::string(data.data(), data.size());
29 }
30 
31 TEST(FakeFileWriter, Filename) {
32  auto content = std::make_shared<std::vector<char>>();
33  FakeFileWriter writer("file", content);
34  EXPECT_EQ("file", writer.GetFilename());
35 }
36 
37 TEST(FakeFileWriter, CloseStream) {
38  auto content = std::make_shared<std::vector<char>>();
39  FakeFileWriter writer("file", content);
40  EXPECT_TRUE(writer.Close());
41  EXPECT_EQ("", toString(*content));
42 }
43 
44 TEST(FakeFileWriter, WriteHeader) {
45  auto content = std::make_shared<std::vector<char>>();
46  const std::string header("dummy header");
47  const std::string header_2("dummy header 2");
48  FakeFileWriter writer("file", content);
49 
50  EXPECT_TRUE(writer.WriteHeader(header.c_str(), header.size()));
51  EXPECT_EQ("dummy header", toString(*content));
52 
53  EXPECT_TRUE(writer.WriteHeader(header_2.c_str(), header_2.size()));
54  EXPECT_EQ("dummy header 2", toString(*content));
55 
56  EXPECT_TRUE(writer.Close());
57  EXPECT_EQ("dummy header 2", toString(*content));
58 }
59 
60 TEST(FakeFileWriter, Write) {
61  auto content = std::make_shared<std::vector<char>>();
62  const std::vector<std::string> data_stream = {"data 1", "data 2"};
63  FakeFileWriter writer("file", content);
64 
65  for (const auto& data : data_stream) {
66  EXPECT_TRUE(writer.Write(data.c_str(), data.size()));
67  }
68 
69  EXPECT_EQ("data 1data 2", toString(*content));
70  EXPECT_TRUE(writer.Close());
71  EXPECT_EQ("data 1data 2", toString(*content));
72 }
73 
74 TEST(FakeFileWriter, HeaderAndWrite) {
75  auto content = std::make_shared<std::vector<char>>();
76  const std::string header("dummy header");
77  const std::vector<std::string> data_stream = {"data 1", "data 2"};
78  FakeFileWriter writer("file", content);
79 
80  EXPECT_TRUE(writer.WriteHeader(header.c_str(), header.size()));
81  EXPECT_EQ("dummy header", toString(*content));
82 
83  for (const auto& data : data_stream) {
84  EXPECT_TRUE(writer.Write(data.c_str(), data.size()));
85  }
86 
87  EXPECT_TRUE(writer.Close());
88  EXPECT_EQ("dummy headerdata 1data 2", toString(*content));
89 }
90 
91 TEST(FakeFileWriter, WriteTerminatedString) {
92  auto content = std::make_shared<std::vector<char>>();
93  std::vector<char> data_stream = {'d', 'a', 't', 'a', '\0', ' ', '1'};
94  FakeFileWriter writer("file", content);
95  EXPECT_TRUE(writer.Write(data_stream.data(), data_stream.size()));
96  EXPECT_EQ(data_stream, *content);
97 }
98 
99 TEST(FakeFileWriter, WriteTerminatedHeaderString) {
100  auto content = std::make_shared<std::vector<char>>();
101  std::vector<char> header = {'h', 'e', 'a', 'd', '\0', ' ', 'e', 'r'};
102  FakeFileWriter writer("file", content);
103  EXPECT_TRUE(writer.WriteHeader(header.data(), header.size()));
104  EXPECT_EQ(header, *content);
105 }
106 
107 TEST(FakeFileWriter, HeaderAndWriteTerminatedString) {
108  auto content = std::make_shared<std::vector<char>>();
109  std::vector<char> header = {'d', 'a', 't', 'a', '\0', ' ', '1'};
110  std::vector<char> data = {'h', 'e', 'a', 'd', '\0', ' ', 'e', 'r'};
111 
112  FakeFileWriter writer("file", content);
113  EXPECT_TRUE(writer.WriteHeader(header.data(), header.size()));
114  EXPECT_EQ(header, *content);
115 
116  EXPECT_TRUE(writer.Write(data.data(), data.size()));
117 
118  std::vector<char> expected_output = header;
119  expected_output.insert(expected_output.end(), data.begin(), data.end());
120 
121  EXPECT_EQ(expected_output, *content);
122 
123  EXPECT_TRUE(writer.Close());
124  EXPECT_EQ(expected_output, *content);
125 }
126 
127 } // namespace
128 } // namespace io
129 } // namespace cartographer
TEST(TrajectoryConnectivityStateTest, UnknownTrajectory)


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