Roll forward to cl/314549634

Change-Id: I4d1e7eacd5fa4078a094571ad6d5f51e422535ff
This commit is contained in:
Himanshu Jaju
2020-06-03 11:18:32 -07:00
committed by Alexey Polyudov
parent ae1c427b99
commit cffbc04508
92 changed files with 3804 additions and 697 deletions
+26 -5
View File
@@ -9,10 +9,7 @@ cc_library(
visibility = [
"//platform_v2/impl:__subpackages__",
],
deps = [
"//platform_v2/api",
"//platform_v2/base",
],
deps = ["//platform_v2/api:types"],
)
cc_library(
@@ -28,7 +25,31 @@ cc_library(
],
deps = [
":posix_mutex",
"//platform_v2/api",
"//platform_v2/api:types",
],
)
cc_library(
name = "file",
srcs = ["file.cc"],
hdrs = ["file.h"],
visibility = [
"//platform_v2/impl:__subpackages__",
],
deps = [
"//platform_v2/api:types",
"//platform_v2/base",
"//absl/strings",
],
)
cc_test(
name = "file_test",
srcs = ["file_test.cc"],
deps = [
":file",
"//file/util:temp_path",
"//platform_v2/base",
"//testing/base/public:gunit_main",
],
)
+81
View File
@@ -0,0 +1,81 @@
#include "platform_v2/impl/shared/file.h"
#include <cstddef>
#include <memory>
#include "platform_v2/base/exception.h"
#include "absl/strings/string_view.h"
namespace location {
namespace nearby {
namespace shared {
// InputFile
InputFile::InputFile(const std::string& path, std::int64_t size)
: file_(path), path_(path), total_size_(size) {}
ExceptionOr<ByteArray> InputFile::Read(std::int64_t size) {
if (!file_.is_open()) {
return ExceptionOr<ByteArray>{Exception::kIo};
}
if (file_.peek() == EOF) {
return ExceptionOr<ByteArray>{ByteArray{}};
}
if (!file_.good()) {
return ExceptionOr<ByteArray>{Exception::kIo};
}
ByteArray bytes(size);
std::unique_ptr<char[]> read_bytes{new char[size]};
file_.read(read_bytes.get(), static_cast<ptrdiff_t>(size));
auto num_bytes_read = file_.gcount();
if (num_bytes_read == 0) {
return ExceptionOr<ByteArray>{Exception::kIo};
}
return ExceptionOr<ByteArray>(ByteArray(read_bytes.get(), num_bytes_read));
}
Exception InputFile::Close() {
if (file_.is_open()) {
file_.close();
}
return {Exception::kSuccess};
}
// OutputFile
OutputFile::OutputFile(absl::string_view path) : file_(std::string(path)) {}
Exception OutputFile::Write(const ByteArray& data) {
if (!file_.is_open()) {
return {Exception::kIo};
}
if (!file_.good()) {
return {Exception::kIo};
}
file_.write(data.data(), data.size());
file_.flush();
return {file_.good() ? Exception::kSuccess : Exception::kIo};
}
Exception OutputFile::Flush() {
file_.flush();
return {file_.good() ? Exception::kSuccess : Exception::kIo};
}
Exception OutputFile::Close() {
if (file_.is_open()) {
file_.close();
}
return {Exception::kSuccess};
}
} // namespace shared
} // namespace nearby
} // namespace location
+53
View File
@@ -0,0 +1,53 @@
#ifndef PLATFORM_V2_IMPL_SHARED_FILE_H_
#define PLATFORM_V2_IMPL_SHARED_FILE_H_
#include <cstdint>
#include <fstream>
#include "platform_v2/api/input_file.h"
#include "platform_v2/api/output_file.h"
#include "platform_v2/base/exception.h"
#include "absl/strings/string_view.h"
namespace location {
namespace nearby {
namespace shared {
class InputFile final : public api::InputFile {
public:
explicit InputFile(const std::string& path, std::int64_t size);
~InputFile() override = default;
InputFile(InputFile&&) = default;
InputFile& operator=(InputFile&&) = default;
ExceptionOr<ByteArray> Read(std::int64_t size) override;
std::string GetFilePath() const override { return path_; }
std::int64_t GetTotalSize() const override { return total_size_; }
Exception Close() override;
private:
std::ifstream file_;
std::string path_;
std::int64_t total_size_;
};
class OutputFile final : public api::OutputFile {
public:
explicit OutputFile(absl::string_view path);
~OutputFile() override = default;
OutputFile(OutputFile&&) = default;
OutputFile& operator=(OutputFile&&) = default;
Exception Write(const ByteArray& data) override;
Exception Flush() override;
Exception Close() override;
private:
std::ofstream file_;
};
} // namespace shared
} // namespace nearby
} // namespace location
#endif // PLATFORM_V2_IMPL_SHARED_FILE_H_
+133
View File
@@ -0,0 +1,133 @@
#include "platform_v2/impl/shared/file.h"
#include <cstring>
#include <fstream>
#include <memory>
#include <ostream>
#include "file/util/temp_path.h"
#include "platform_v2/base/byte_array.h"
#include "gtest/gtest.h"
namespace location {
namespace nearby {
namespace shared {
class FileTest : public ::testing::Test {
protected:
void SetUp() override {
temp_path_ = std::make_unique<TempPath>(TempPath::Local);
path_ = temp_path_->path() + "/file.txt";
std::ofstream output_file(path_);
file_ = std::fstream(path_, std::fstream::in | std::fstream::out);
}
void WriteToFile(const std::string& text) {
file_ << text;
file_.flush();
size_ += text.size();
}
size_t GetSize() const { return size_; }
void AssertEquals(const ExceptionOr<ByteArray>& bytes,
const std::string& expected) {
EXPECT_TRUE(bytes.ok());
EXPECT_EQ(std::string(bytes.result()), expected);
}
void AssertEmpty(const ExceptionOr<ByteArray>& bytes) {
EXPECT_TRUE(bytes.ok());
EXPECT_TRUE(bytes.result().Empty());
}
static constexpr int64_t kMaxSize = 3;
std::unique_ptr<TempPath> temp_path_;
std::string path_;
std::fstream file_;
size_t size_ = 0;
};
TEST_F(FileTest, InputFile_NonExistentPath) {
InputFile input_file("/not/a/valid/path.txt", GetSize());
ExceptionOr<ByteArray> read_result = input_file.Read(kMaxSize);
EXPECT_FALSE(read_result.ok());
EXPECT_TRUE(read_result.GetException().Raised(Exception::kIo));
}
TEST_F(FileTest, InputFile_GetFilePath) {
InputFile input_file(path_, GetSize());
EXPECT_EQ(input_file.GetFilePath(), path_);
}
TEST_F(FileTest, InputFile_EmptyFileEOF) {
InputFile input_file(path_, GetSize());
AssertEmpty(input_file.Read(kMaxSize));
}
TEST_F(FileTest, InputFile_ReadWorks) {
WriteToFile("abc");
InputFile input_file(path_, GetSize());
input_file.Read(kMaxSize);
SUCCEED();
}
TEST_F(FileTest, InputFile_ReadUntilEOF) {
WriteToFile("abc");
InputFile input_file(path_, GetSize());
AssertEquals(input_file.Read(kMaxSize), "abc");
AssertEmpty(input_file.Read(kMaxSize));
}
TEST_F(FileTest, InputFile_ReadWithSize) {
WriteToFile("abc");
InputFile input_file(path_, GetSize());
AssertEquals(input_file.Read(2), "ab");
AssertEquals(input_file.Read(1), "c");
AssertEmpty(input_file.Read(kMaxSize));
}
TEST_F(FileTest, InputFile_GetTotalSize) {
WriteToFile("abc");
InputFile input_file(path_, GetSize());
EXPECT_EQ(input_file.GetTotalSize(), 3);
AssertEquals(input_file.Read(1), "a");
EXPECT_EQ(input_file.GetTotalSize(), 3);
}
TEST_F(FileTest, InputFile_Close) {
WriteToFile("abc");
InputFile input_file(path_, GetSize());
input_file.Close();
ExceptionOr<ByteArray> read_result = input_file.Read(kMaxSize);
EXPECT_FALSE(read_result.ok());
EXPECT_TRUE(read_result.GetException().Raised(Exception::kIo));
}
TEST_F(FileTest, OutputFile_NonExistentPath) {
OutputFile output_file("/not/a/valid/path.txt");
ByteArray bytes("a", 1);
EXPECT_TRUE(output_file.Write(bytes).Raised(Exception::kIo));
}
TEST_F(FileTest, OutputFile_Write) {
OutputFile output_file(path_);
ByteArray bytes1("a");
ByteArray bytes2("bc");
EXPECT_EQ(output_file.Write(bytes1), Exception{Exception::kSuccess});
EXPECT_EQ(output_file.Write(bytes2), Exception{Exception::kSuccess});
InputFile input_file(path_, GetSize());
AssertEquals(input_file.Read(kMaxSize), "abc");
}
TEST_F(FileTest, OutputFile_Close) {
OutputFile output_file(path_);
output_file.Close();
ByteArray bytes("a");
EXPECT_EQ(output_file.Write(bytes), Exception{Exception::kIo});
}
} // namespace shared
} // namespace nearby
} // namespace location