From e1e91ee464373e0bba4aadfbd3d88a6d84dc5b95 Mon Sep 17 00:00:00 2001 From: Victor Costan Date: Tue, 15 Dec 2020 06:20:22 +0000 Subject: [PATCH] Rework file:: stubs. PiperOrigin-RevId: 347541488 --- snappy-test.cc | 60 ++++++++++++++++++++++++++++++++++++++++ snappy-test.h | 75 +++++++++++++++++++------------------------------- 2 files changed, 89 insertions(+), 46 deletions(-) diff --git a/snappy-test.cc b/snappy-test.cc index 4f6a2bf..315228a 100644 --- a/snappy-test.cc +++ b/snappy-test.cc @@ -46,6 +46,66 @@ DEFINE_bool(run_microbenchmarks, true, "Run microbenchmarks before doing anything else."); +namespace file { + +OptionsStub::OptionsStub() = default; +OptionsStub::~OptionsStub() = default; + +const OptionsStub &Defaults() { + static OptionsStub defaults; + return defaults; +} + +StatusStub::StatusStub() = default; +StatusStub::StatusStub(const StatusStub &) = default; +StatusStub &StatusStub::operator=(const StatusStub &) = default; +StatusStub::~StatusStub() = default; + +bool StatusStub::ok() { return true; } + +StatusStub GetContents(const std::string &filename, std::string *output, + const OptionsStub & /* options */) { + FILE *fp = std::fopen(filename.c_str(), "rb"); + if (fp == nullptr) { + std::perror(filename.c_str()); + std::exit(1); + } + + output->clear(); + while (!std::feof(fp)) { + char buffer[4096]; + size_t bytes_read = std::fread(buffer, 1, sizeof(buffer), fp); + if (bytes_read == 0 && std::ferror(fp)) { + std::perror("fread"); + std::exit(1); + } + output->append(buffer, bytes_read); + } + + std::fclose(fp); + return StatusStub(); +} + +StatusStub SetContents(const std::string &file_name, const std::string &content, + const OptionsStub & /* options */) { + FILE *fp = std::fopen(file_name.c_str(), "wb"); + if (fp == nullptr) { + std::perror(file_name.c_str()); + std::exit(1); + } + + size_t bytes_written = std::fwrite(content.data(), 1, content.size(), fp); + if (bytes_written != content.size()) { + std::perror("fwrite"); + std::exit(1); + } + + std::fclose(fp); + return StatusStub(); +} + +} // namespace file + namespace snappy { std::string ReadTestDataFile(const std::string& base, size_t size_limit) { diff --git a/snappy-test.h b/snappy-test.h index 54c4ae2..243480a 100644 --- a/snappy-test.h +++ b/snappy-test.h @@ -90,62 +90,45 @@ #include "lz4.h" #endif -namespace { - namespace file { - int Defaults() { return 0; } - class DummyStatus { - public: - void CheckSuccess() { } - }; +// Stubs the class file::Options. +// +// This class should not be instantiated explicitly. It should only be used by +// passing file::Defaults() to file::GetContents() / file::SetContents(). +class OptionsStub { + public: + OptionsStub(); + OptionsStub(const OptionsStub &) = delete; + OptionsStub &operator=(const OptionsStub &) = delete; + ~OptionsStub(); +}; - DummyStatus GetContents( - const std::string& filename, std::string* data, int /*unused*/) { - FILE* fp = std::fopen(filename.c_str(), "rb"); - if (fp == NULL) { - std::perror(filename.c_str()); - std::exit(1); - } +const OptionsStub &Defaults(); - data->clear(); - while (!feof(fp)) { - char buf[4096]; - size_t ret = fread(buf, 1, 4096, fp); - if (ret == 0 && ferror(fp)) { - std::perror("fread"); - std::exit(1); - } - data->append(std::string(buf, ret)); - } +// Stubs the class absl::Status. +// +// This class should not be instantiated explicitly. It should only be used by +// passing the result of file::GetContents() / file::SetContents() to +// CHECK_OK(). +class StatusStub { + public: + StatusStub(); + StatusStub(const StatusStub &); + StatusStub &operator=(const StatusStub &); + ~StatusStub(); - std::fclose(fp); + bool ok(); +}; - return DummyStatus(); - } +StatusStub GetContents(const std::string &file_name, std::string *output, + const OptionsStub & /* options */); - inline DummyStatus SetContents( - const std::string& filename, const std::string& str, int /*unused*/) { - FILE* fp = std::fopen(filename.c_str(), "wb"); - if (fp == NULL) { - std::perror(filename.c_str()); - std::exit(1); - } +StatusStub SetContents(const std::string &file_name, const std::string &content, + const OptionsStub & /* options */); - int ret = std::fwrite(str.data(), str.size(), 1, fp); - if (ret != 1) { - std::perror("fwrite"); - std::exit(1); - } - - std::fclose(fp); - - return DummyStatus(); - } } // namespace file -} // namespace - namespace snappy { #define FLAGS_test_random_seed 301