Rework file:: stubs.

PiperOrigin-RevId: 347541488
This commit is contained in:
Victor Costan
2020-12-15 06:20:22 +00:00
parent 6aa79cb471
commit e1e91ee464
2 changed files with 89 additions and 46 deletions

View File

@@ -46,6 +46,66 @@
DEFINE_bool(run_microbenchmarks, true, DEFINE_bool(run_microbenchmarks, true,
"Run microbenchmarks before doing anything else."); "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 { namespace snappy {
std::string ReadTestDataFile(const std::string& base, size_t size_limit) { std::string ReadTestDataFile(const std::string& base, size_t size_limit) {

View File

@@ -90,62 +90,45 @@
#include "lz4.h" #include "lz4.h"
#endif #endif
namespace {
namespace file { namespace file {
int Defaults() { return 0; }
class DummyStatus { // Stubs the class file::Options.
public: //
void CheckSuccess() { } // 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 OptionsStub &Defaults();
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(); // Stubs the class absl::Status.
while (!feof(fp)) { //
char buf[4096]; // This class should not be instantiated explicitly. It should only be used by
size_t ret = fread(buf, 1, 4096, fp); // passing the result of file::GetContents() / file::SetContents() to
if (ret == 0 && ferror(fp)) { // CHECK_OK().
std::perror("fread"); class StatusStub {
std::exit(1); public:
} StatusStub();
data->append(std::string(buf, ret)); 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( StatusStub SetContents(const std::string &file_name, const std::string &content,
const std::string& filename, const std::string& str, int /*unused*/) { const OptionsStub & /* options */);
FILE* fp = std::fopen(filename.c_str(), "wb");
if (fp == NULL) {
std::perror(filename.c_str());
std::exit(1);
}
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 file
} // namespace
namespace snappy { namespace snappy {
#define FLAGS_test_random_seed 301 #define FLAGS_test_random_seed 301