More tests.

This commit is contained in:
Victor Zverovich 2014-05-03 15:26:44 -07:00
parent c6f40e339f
commit 823ce5fc3d
2 changed files with 23 additions and 17 deletions

View File

@ -204,7 +204,7 @@ TEST(FileTest, MoveAssignmentClosesFile) {
File f2("CMakeLists.txt", File::RDONLY);
int old_fd = f2.descriptor();
f2 = std::move(f);
EXPECT_CLOSED(old_fd);
EXPECT_TRUE(IsClosedInternal(old_fd));
}
File OpenFile(int &fd) {
@ -272,7 +272,7 @@ TEST(FileTest, CloseError) {
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 buffer(count, '\0');
std::streamsize offset = 0, n = 0;
@ -284,6 +284,17 @@ std::string Read(File &f, std::size_t count) {
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) \
EXPECT_EQ(expected_content, Read(file, std::strlen(expected_content)))
@ -299,17 +310,11 @@ TEST(FileTest, ReadError) {
}
TEST(FileTest, Write) {
const char MESSAGE[] = "test";
File read_end, write_end;
File::pipe(read_end, write_end);
enum { SIZE = sizeof(MESSAGE) - 1 };
std::streamsize offset = 0, count = 0;
do {
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);
Write(write_end, "test");
write_end.close();
EXPECT_READ(read_end, "test");
}
TEST(FileTest, WriteError) {
@ -341,7 +346,7 @@ TEST(FileTest, Dup2) {
TEST(FileTest, Dup2Error) {
File f(".travis.yml", File::RDONLY);
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) {
@ -366,7 +371,8 @@ TEST(FileTest, Pipe) {
File::pipe(read_end, write_end);
EXPECT_NE(-1, read_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

View File

@ -82,7 +82,7 @@ void File::close() {
std::streamsize File::read(void *buffer, std::size_t count) {
std::streamsize result = 0;
FMT_RETRY(result, ::read(fd_, buffer, count));
FMT_RETRY(result, ::FMT_POSIX(read(fd_, buffer, count)));
if (result == -1)
fmt::ThrowSystemError(errno, "cannot read from file");
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 result = 0;
FMT_RETRY(result, ::write(fd_, buffer, count));
FMT_RETRY(result, ::FMT_POSIX(write(fd_, buffer, count)));
if (result == -1)
fmt::ThrowSystemError(errno, "cannot write to file");
return result;
@ -145,7 +145,7 @@ void File::pipe(File &read_end, File &write_end) {
OutputRedirector::OutputRedirector(FILE *file) : file_(file) {
if (std::fflush(file) != 0)
fmt::ThrowSystemError(errno, "cannot flush stream");
int fd = fileno(file);
int fd = FMT_POSIX(fileno(file));
saved_ = File::dup(fd);
File write_end;
File::pipe(read_end_, write_end);
@ -156,7 +156,7 @@ OutputRedirector::~OutputRedirector() {
if (std::fflush(file_) != 0)
fmt::ReportSystemError(errno, "cannot flush stream");
ErrorCode ec;
saved_.dup2(fileno(file_), ec);
saved_.dup2(FMT_POSIX(fileno(file_)), ec);
if (ec.get())
fmt::ReportSystemError(errno, "cannot restore output");
}