fake_file_writer_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/fake_file_writer.h"
00018 
00019 #include <vector>
00020 
00021 #include "glog/logging.h"
00022 #include "gtest/gtest.h"
00023 
00024 namespace cartographer {
00025 namespace io {
00026 namespace {
00027 
00028 std::string toString(const std::vector<char>& data) {
00029   return std::string(data.data(), data.size());
00030 }
00031 
00032 TEST(FakeFileWriter, Filename) {
00033   auto content = std::make_shared<std::vector<char>>();
00034   FakeFileWriter writer("file", content);
00035   EXPECT_EQ("file", writer.GetFilename());
00036 }
00037 
00038 TEST(FakeFileWriter, CloseStream) {
00039   auto content = std::make_shared<std::vector<char>>();
00040   FakeFileWriter writer("file", content);
00041   EXPECT_TRUE(writer.Close());
00042   EXPECT_EQ("", toString(*content));
00043 }
00044 
00045 TEST(FakeFileWriter, WriteHeader) {
00046   auto content = std::make_shared<std::vector<char>>();
00047   const std::string header("dummy header");
00048   const std::string header_2("dummy header 2");
00049   FakeFileWriter writer("file", content);
00050 
00051   EXPECT_TRUE(writer.WriteHeader(header.c_str(), header.size()));
00052   EXPECT_EQ("dummy header", toString(*content));
00053 
00054   EXPECT_TRUE(writer.WriteHeader(header_2.c_str(), header_2.size()));
00055   EXPECT_EQ("dummy header 2", toString(*content));
00056 
00057   EXPECT_TRUE(writer.Close());
00058   EXPECT_EQ("dummy header 2", toString(*content));
00059 }
00060 
00061 TEST(FakeFileWriter, Write) {
00062   auto content = std::make_shared<std::vector<char>>();
00063   const std::vector<std::string> data_stream = {"data 1", "data 2"};
00064   FakeFileWriter writer("file", content);
00065 
00066   for (const auto& data : data_stream) {
00067     EXPECT_TRUE(writer.Write(data.c_str(), data.size()));
00068   }
00069 
00070   EXPECT_EQ("data 1data 2", toString(*content));
00071   EXPECT_TRUE(writer.Close());
00072   EXPECT_EQ("data 1data 2", toString(*content));
00073 }
00074 
00075 TEST(FakeFileWriter, HeaderAndWrite) {
00076   auto content = std::make_shared<std::vector<char>>();
00077   const std::string header("dummy header");
00078   const std::vector<std::string> data_stream = {"data 1", "data 2"};
00079   FakeFileWriter writer("file", content);
00080 
00081   EXPECT_TRUE(writer.WriteHeader(header.c_str(), header.size()));
00082   EXPECT_EQ("dummy header", toString(*content));
00083 
00084   for (const auto& data : data_stream) {
00085     EXPECT_TRUE(writer.Write(data.c_str(), data.size()));
00086   }
00087 
00088   EXPECT_TRUE(writer.Close());
00089   EXPECT_EQ("dummy headerdata 1data 2", toString(*content));
00090 }
00091 
00092 TEST(FakeFileWriter, WriteTerminatedString) {
00093   auto content = std::make_shared<std::vector<char>>();
00094   std::vector<char> data_stream = {'d', 'a', 't', 'a', '\0', ' ', '1'};
00095   FakeFileWriter writer("file", content);
00096   EXPECT_TRUE(writer.Write(data_stream.data(), data_stream.size()));
00097   EXPECT_EQ(data_stream, *content);
00098 }
00099 
00100 TEST(FakeFileWriter, WriteTerminatedHeaderString) {
00101   auto content = std::make_shared<std::vector<char>>();
00102   std::vector<char> header = {'h', 'e', 'a', 'd', '\0', ' ', 'e', 'r'};
00103   FakeFileWriter writer("file", content);
00104   EXPECT_TRUE(writer.WriteHeader(header.data(), header.size()));
00105   EXPECT_EQ(header, *content);
00106 }
00107 
00108 TEST(FakeFileWriter, HeaderAndWriteTerminatedString) {
00109   auto content = std::make_shared<std::vector<char>>();
00110   std::vector<char> header = {'d', 'a', 't', 'a', '\0', ' ', '1'};
00111   std::vector<char> data = {'h', 'e', 'a', 'd', '\0', ' ', 'e', 'r'};
00112 
00113   FakeFileWriter writer("file", content);
00114   EXPECT_TRUE(writer.WriteHeader(header.data(), header.size()));
00115   EXPECT_EQ(header, *content);
00116 
00117   EXPECT_TRUE(writer.Write(data.data(), data.size()));
00118 
00119   std::vector<char> expected_output = header;
00120   expected_output.insert(expected_output.end(), data.begin(), data.end());
00121 
00122   EXPECT_EQ(expected_output, *content);
00123 
00124   EXPECT_TRUE(writer.Close());
00125   EXPECT_EQ(expected_output, *content);
00126 }
00127 
00128 }  // namespace
00129 }  // namespace io
00130 }  // namespace cartographer


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