mirror of
https://github.com/fmtlib/fmt.git
synced 2025-02-04 15:40:07 +00:00
More tests.
This commit is contained in:
parent
c6f40e339f
commit
823ce5fc3d
@ -204,7 +204,7 @@ TEST(FileTest, MoveAssignmentClosesFile) {
|
|||||||
File f2("CMakeLists.txt", File::RDONLY);
|
File f2("CMakeLists.txt", File::RDONLY);
|
||||||
int old_fd = f2.descriptor();
|
int old_fd = f2.descriptor();
|
||||||
f2 = std::move(f);
|
f2 = std::move(f);
|
||||||
EXPECT_CLOSED(old_fd);
|
EXPECT_TRUE(IsClosedInternal(old_fd));
|
||||||
}
|
}
|
||||||
|
|
||||||
File OpenFile(int &fd) {
|
File OpenFile(int &fd) {
|
||||||
@ -272,7 +272,7 @@ TEST(FileTest, CloseError) {
|
|||||||
EXPECT_EQ(message, error.what());
|
EXPECT_EQ(message, error.what());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempts to read count characters from the file.
|
// Attempts to read count characters from a file.
|
||||||
std::string Read(File &f, std::size_t count) {
|
std::string Read(File &f, std::size_t count) {
|
||||||
std::string buffer(count, '\0');
|
std::string buffer(count, '\0');
|
||||||
std::streamsize offset = 0, n = 0;
|
std::streamsize offset = 0, n = 0;
|
||||||
@ -284,6 +284,17 @@ std::string Read(File &f, std::size_t count) {
|
|||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Attempts to write a string to a file.
|
||||||
|
void Write(File &f, fmt::StringRef s) {
|
||||||
|
std::size_t num_chars_left = s.size();
|
||||||
|
const char *ptr = s.c_str();
|
||||||
|
do {
|
||||||
|
std::streamsize count = f.write(ptr, num_chars_left);
|
||||||
|
ptr += count;
|
||||||
|
num_chars_left -= count;
|
||||||
|
} while (num_chars_left != 0);
|
||||||
|
}
|
||||||
|
|
||||||
#define EXPECT_READ(file, expected_content) \
|
#define EXPECT_READ(file, expected_content) \
|
||||||
EXPECT_EQ(expected_content, Read(file, std::strlen(expected_content)))
|
EXPECT_EQ(expected_content, Read(file, std::strlen(expected_content)))
|
||||||
|
|
||||||
@ -299,17 +310,11 @@ TEST(FileTest, ReadError) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(FileTest, Write) {
|
TEST(FileTest, Write) {
|
||||||
const char MESSAGE[] = "test";
|
|
||||||
File read_end, write_end;
|
File read_end, write_end;
|
||||||
File::pipe(read_end, write_end);
|
File::pipe(read_end, write_end);
|
||||||
enum { SIZE = sizeof(MESSAGE) - 1 };
|
Write(write_end, "test");
|
||||||
std::streamsize offset = 0, count = 0;
|
write_end.close();
|
||||||
do {
|
EXPECT_READ(read_end, "test");
|
||||||
count = write_end.write(MESSAGE + offset, SIZE - offset);
|
|
||||||
offset += count;
|
|
||||||
} while (offset < SIZE && count != 0);
|
|
||||||
write_end = File(); // Close file.
|
|
||||||
EXPECT_READ(read_end, MESSAGE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(FileTest, WriteError) {
|
TEST(FileTest, WriteError) {
|
||||||
@ -341,7 +346,7 @@ TEST(FileTest, Dup2) {
|
|||||||
TEST(FileTest, Dup2Error) {
|
TEST(FileTest, Dup2Error) {
|
||||||
File f(".travis.yml", File::RDONLY);
|
File f(".travis.yml", File::RDONLY);
|
||||||
EXPECT_SYSTEM_ERROR(f.dup2(-1), EBADF,
|
EXPECT_SYSTEM_ERROR(f.dup2(-1), EBADF,
|
||||||
fmt::Format("cannot duplicate file descriptor {} to -1") << f.descriptor());
|
fmt::Format("cannot duplicate file descriptor {} to -1") << f.descriptor());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(FileTest, Dup2NoExcept) {
|
TEST(FileTest, Dup2NoExcept) {
|
||||||
@ -366,7 +371,8 @@ TEST(FileTest, Pipe) {
|
|||||||
File::pipe(read_end, write_end);
|
File::pipe(read_end, write_end);
|
||||||
EXPECT_NE(-1, read_end.descriptor());
|
EXPECT_NE(-1, read_end.descriptor());
|
||||||
EXPECT_NE(-1, write_end.descriptor());
|
EXPECT_NE(-1, write_end.descriptor());
|
||||||
// TODO: try writing to write_end and reading from read_end
|
Write(write_end, "test");
|
||||||
|
EXPECT_READ(read_end, "test");
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: test pipe
|
// TODO: test pipe
|
||||||
|
@ -82,7 +82,7 @@ void File::close() {
|
|||||||
|
|
||||||
std::streamsize File::read(void *buffer, std::size_t count) {
|
std::streamsize File::read(void *buffer, std::size_t count) {
|
||||||
std::streamsize result = 0;
|
std::streamsize result = 0;
|
||||||
FMT_RETRY(result, ::read(fd_, buffer, count));
|
FMT_RETRY(result, ::FMT_POSIX(read(fd_, buffer, count)));
|
||||||
if (result == -1)
|
if (result == -1)
|
||||||
fmt::ThrowSystemError(errno, "cannot read from file");
|
fmt::ThrowSystemError(errno, "cannot read from file");
|
||||||
return result;
|
return result;
|
||||||
@ -90,7 +90,7 @@ std::streamsize File::read(void *buffer, std::size_t count) {
|
|||||||
|
|
||||||
std::streamsize File::write(const void *buffer, std::size_t count) {
|
std::streamsize File::write(const void *buffer, std::size_t count) {
|
||||||
std::streamsize result = 0;
|
std::streamsize result = 0;
|
||||||
FMT_RETRY(result, ::write(fd_, buffer, count));
|
FMT_RETRY(result, ::FMT_POSIX(write(fd_, buffer, count)));
|
||||||
if (result == -1)
|
if (result == -1)
|
||||||
fmt::ThrowSystemError(errno, "cannot write to file");
|
fmt::ThrowSystemError(errno, "cannot write to file");
|
||||||
return result;
|
return result;
|
||||||
@ -145,7 +145,7 @@ void File::pipe(File &read_end, File &write_end) {
|
|||||||
OutputRedirector::OutputRedirector(FILE *file) : file_(file) {
|
OutputRedirector::OutputRedirector(FILE *file) : file_(file) {
|
||||||
if (std::fflush(file) != 0)
|
if (std::fflush(file) != 0)
|
||||||
fmt::ThrowSystemError(errno, "cannot flush stream");
|
fmt::ThrowSystemError(errno, "cannot flush stream");
|
||||||
int fd = fileno(file);
|
int fd = FMT_POSIX(fileno(file));
|
||||||
saved_ = File::dup(fd);
|
saved_ = File::dup(fd);
|
||||||
File write_end;
|
File write_end;
|
||||||
File::pipe(read_end_, write_end);
|
File::pipe(read_end_, write_end);
|
||||||
@ -156,7 +156,7 @@ OutputRedirector::~OutputRedirector() {
|
|||||||
if (std::fflush(file_) != 0)
|
if (std::fflush(file_) != 0)
|
||||||
fmt::ReportSystemError(errno, "cannot flush stream");
|
fmt::ReportSystemError(errno, "cannot flush stream");
|
||||||
ErrorCode ec;
|
ErrorCode ec;
|
||||||
saved_.dup2(fileno(file_), ec);
|
saved_.dup2(FMT_POSIX(fileno(file_)), ec);
|
||||||
if (ec.get())
|
if (ec.get())
|
||||||
fmt::ReportSystemError(errno, "cannot restore output");
|
fmt::ReportSystemError(errno, "cannot restore output");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user