Fix tests.

This commit is contained in:
Victor Zverovich 2014-05-03 14:40:18 -07:00
parent 7b2568a3a9
commit f516fb9cba

View File

@ -152,20 +152,24 @@ TEST(FileTest, DefaultCtor) {
} }
// Checks if the file is open by reading one character from it. // Checks if the file is open by reading one character from it.
// Note that IsOpen is not equivalent to !IsClosed.
bool IsOpen(int fd) { bool IsOpen(int fd) {
char buffer; char buffer;
return read(fd, &buffer, 1) == 1; return read(fd, &buffer, 1) == 1;
} }
// Checks if the file is closed. bool IsClosedInternal(int fd) {
// Note that IsClosed is not equivalent to !IsOpen.
bool IsClosed(int fd) {
char buffer; char buffer;
std::streamsize result = read(fd, &buffer, 1); std::streamsize result = read(fd, &buffer, 1);
return result == -1 && errno == EBADF; return result == -1 && errno == EBADF;
} }
#ifndef _WIN32
// Checks if the file is closed.
# define EXPECT_CLOSED(fd) EXPECT_TRUE(IsClosedInternal(fd))
#else
# define EXPECT_CLOSED(fd) EXPECT_DEATH(IsClosedInternal(fd))
#endif
TEST(FileTest, OpenFileInCtor) { TEST(FileTest, OpenFileInCtor) {
int fd = 0; int fd = 0;
{ {
@ -173,7 +177,7 @@ TEST(FileTest, OpenFileInCtor) {
fd = f.get(); fd = f.get();
ASSERT_TRUE(IsOpen(fd)); ASSERT_TRUE(IsOpen(fd));
} }
ASSERT_TRUE(IsClosed(fd)); EXPECT_CLOSED(fd);
} }
TEST(FileTest, OpenFileError) { TEST(FileTest, OpenFileError) {
@ -205,7 +209,7 @@ TEST(FileTest, MoveAssignmentClosesFile) {
File f2("CMakeLists.txt", File::RDONLY); File f2("CMakeLists.txt", File::RDONLY);
int old_fd = f2.get(); int old_fd = f2.get();
f2 = std::move(f); f2 = std::move(f);
EXPECT_TRUE(IsClosed(old_fd)); EXPECT_CLOSED(old_fd);
} }
File OpenFile(int &fd) { File OpenFile(int &fd) {
@ -232,7 +236,7 @@ TEST(FileTest, MoveFromTemporaryInAssignmentClosesFile) {
File f(".travis.yml", File::RDONLY); File f(".travis.yml", File::RDONLY);
int old_fd = f.get(); int old_fd = f.get();
f = OpenFile(fd); f = OpenFile(fd);
EXPECT_TRUE(IsClosed(old_fd)); EXPECT_CLOSED(old_fd);
} }
TEST(FileTest, CloseFileInDtor) { TEST(FileTest, CloseFileInDtor) {
@ -297,6 +301,11 @@ TEST(FileTest, Write) {
EXPECT_READ(read_end, MESSAGE); EXPECT_READ(read_end, MESSAGE);
} }
TEST(FileTest, WriteError) {
File f;
EXPECT_SYSTEM_ERROR(f.write(" ", 1), EBADF, "cannot write to file");
}
TEST(FileTest, Dup) { TEST(FileTest, Dup) {
File f(".travis.yml", File::RDONLY); File f(".travis.yml", File::RDONLY);
File dup = File::dup(f.get()); File dup = File::dup(f.get());
@ -351,8 +360,6 @@ TEST(FileTest, Pipe) {
// TODO: test pipe // TODO: test pipe
// TODO: test File::read
// TODO: compile both with C++11 & C++98 mode // TODO: compile both with C++11 & C++98 mode
#endif #endif