From e3ba8d9530e84a14b3c61da6f062b8bd3762da66 Mon Sep 17 00:00:00 2001 From: carterl Date: Fri, 13 Feb 2015 08:00:01 +0100 Subject: [PATCH] UT stub for extra methods in BufferedFile --- posix.cc | 29 +++++++++++++++++++++++------ posix.h | 14 ++------------ test/posix-test.cc | 12 ++++++++++++ test/posix-test.h | 4 ++++ 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/posix.cc b/posix.cc index 5415039b..68fe8e1d 100644 --- a/posix.cc +++ b/posix.cc @@ -105,12 +105,29 @@ int fmt::BufferedFile::fileno() const { } void fmt::BufferedFile::flush() { - if (!file_) - return; - // FIXME: int result = FMT_SYSTEM(fflush(file_)); - int result = ::fflush(file_); - if (result != 0) - throw SystemError(errno, "cannot flush file"); + if (!file_) + return; + int result = FMT_SYSTEM(fflush(file_)); + if (result != 0) + throw SystemError(errno, "cannot flush file"); +} + +void fmt::BufferedFile::write_raw(fmt::StringRef str) { + if (!file_) + return; + std::size_t result = FMT_SYSTEM(fwrite(str.c_str(), str.size(), 1, file_)); + if (result < str.size()) { + // FIXME: According to ```man fwrite```, fwrite won't set errno at all + throw SystemError(errno, "failed to write raw string, only {} character(s) written", result); + } +} + +void fmt::BufferedFile::write_raw(char c) { + if (!file_) + return; + int result = FMT_SYSTEM(fputc(c, file_)); + if (result == EOF) + throw SystemError(errno, "failed to write '{}'", result); } diff --git a/posix.h b/posix.h index b1b3a245..672fe09d 100644 --- a/posix.h +++ b/posix.h @@ -197,19 +197,9 @@ public: void flush(); // FIXME: Should be const? // Writes raw string without formatting - std::size_t write_raw(fmt::StringRef str) { - // TODO: throw exception when failing - if (!is_open()) - return 0; - return ::fwrite(str.c_str(), str.size(), 1, file_); - } + void write_raw(fmt::StringRef str); // Writes a single charactor without formatting - void write_raw(char c) { - // TODO: throw exception when failing - if (!is_open()) - return; - ::fputc(c, file_); - } + void write_raw(char c); void print(fmt::StringRef format_str, const ArgList &args) { fmt::print(file_, format_str, args); diff --git a/test/posix-test.cc b/test/posix-test.cc index c9529688..fe18cd62 100644 --- a/test/posix-test.cc +++ b/test/posix-test.cc @@ -178,6 +178,18 @@ int test::fileno(FILE *stream) { return ::FMT_POSIX(fileno(stream)); } +int test::fflush(FILE *stream) { + return ::fflush(stream); +} + +int test::fwrite(const void *str, size_t size, size_t count, FILE *stream) { + return ::fwrite(str, size, count, stream); +} + +int test::fputc(int ch, FILE *stream) { + return ::fputc(ch, stream); +} + #ifndef _WIN32 # define EXPECT_RETRY(statement, func, message) \ func##_count = 1; \ diff --git a/test/posix-test.h b/test/posix-test.h index 4768cb94..48012e4b 100644 --- a/test/posix-test.h +++ b/test/posix-test.h @@ -73,6 +73,10 @@ int pipe(int *pfds, unsigned psize, int textmode); FILE *fopen(const char *filename, const char *mode); int fclose(FILE *stream); int fileno(FILE *stream); +int fflush(FILE *stream); + +int fwrite(const void *str, size_t size, size_t count, FILE *stream); +int fputc(int ch, FILE *stream); } // namespace test