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,
"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) {

View File

@ -90,62 +90,45 @@
#include "lz4.h"
#endif
namespace {
namespace file {
int Defaults() { return 0; }
class DummyStatus {
// 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:
void CheckSuccess() { }
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