summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Costan <costan@google.com>2020-12-15 06:20:22 +0000
committerVictor Costan <costan@google.com>2020-12-15 06:21:47 +0000
commite1e91ee464373e0bba4aadfbd3d88a6d84dc5b95 (patch)
treeb7b521e48802ed856da1f23606b734ff7e6b5c29
parent6aa79cb4712e2b0b3afbac9471d222e7c7b5d359 (diff)
downloadsnappy-git-e1e91ee464373e0bba4aadfbd3d88a6d84dc5b95.tar.gz
Rework file:: stubs.
PiperOrigin-RevId: 347541488
-rw-r--r--snappy-test.cc60
-rw-r--r--snappy-test.h81
2 files changed, 92 insertions, 49 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,61 +90,44 @@
#include "lz4.h"
#endif
-namespace {
-
namespace file {
- int Defaults() { return 0; }
-
- class DummyStatus {
- public:
- void CheckSuccess() { }
- };
-
- 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);
- }
-
- 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));
- }
-
- std::fclose(fp);
-
- return DummyStatus();
- }
- 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);
- }
+// 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();
+};
- int ret = std::fwrite(str.data(), str.size(), 1, fp);
- if (ret != 1) {
- std::perror("fwrite");
- std::exit(1);
- }
+const OptionsStub &Defaults();
- std::fclose(fp);
+// 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();
- return DummyStatus();
- }
-} // namespace file
+ bool ok();
+};
-} // namespace
+StatusStub GetContents(const std::string &file_name, std::string *output,
+ const OptionsStub & /* options */);
+
+StatusStub SetContents(const std::string &file_name, const std::string &content,
+ const OptionsStub & /* options */);
+
+} // namespace file
namespace snappy {