Use scoped_ptr instead of raw pointers in tests

This commit is contained in:
vitaut 2015-07-31 08:23:39 -07:00
parent 922fe90ac3
commit d15e0d3e06
2 changed files with 14 additions and 13 deletions

View File

@ -48,6 +48,8 @@ using fmt::BufferedFile;
using fmt::ErrorCode;
using fmt::File;
using testing::internal::scoped_ptr;
namespace {
int open_count;
int close_count;
@ -234,20 +236,19 @@ TEST(UtilTest, GetPageSize) {
TEST(FileTest, OpenRetry) {
write_file("test", "there must be something here");
File *f = 0;
EXPECT_RETRY(f = new File("test", File::RDONLY),
scoped_ptr<File> f;
EXPECT_RETRY(f.reset(new File("test", File::RDONLY)),
open, "cannot open file test");
#ifndef _WIN32
char c = 0;
f->read(&c, 1);
#endif
delete f;
}
TEST(FileTest, CloseNoRetryInDtor) {
File read_end, write_end;
File::pipe(read_end, write_end);
testing::internal::scoped_ptr<File> f(new File(std::move(read_end)));
scoped_ptr<File> f(new File(std::move(read_end)));
int saved_close_count = 0;
EXPECT_WRITE(stderr, {
close_count = 1;
@ -405,25 +406,24 @@ TEST(FileTest, FdopenNoRetry) {
TEST(BufferedFileTest, OpenRetry) {
write_file("test", "there must be something here");
BufferedFile *f = 0;
EXPECT_RETRY(f = new BufferedFile("test", "r"),
scoped_ptr<BufferedFile> f;
EXPECT_RETRY(f.reset(new BufferedFile("test", "r")),
fopen, "cannot open file test");
#ifndef _WIN32
char c = 0;
if (fread(&c, 1, 1, f->get()) < 1)
throw fmt::SystemError(errno, "fread failed");
#endif
delete f;
}
TEST(BufferedFileTest, CloseNoRetryInDtor) {
File read_end, write_end;
File::pipe(read_end, write_end);
BufferedFile *f = new BufferedFile(read_end.fdopen("r"));
scoped_ptr<BufferedFile> f(new BufferedFile(read_end.fdopen("r")));
int saved_fclose_count = 0;
EXPECT_WRITE(stderr, {
fclose_count = 1;
delete f;
f.reset();
saved_fclose_count = fclose_count;
fclose_count = 0;
}, format_system_error(EINTR, "cannot close file") + "\n");

View File

@ -39,6 +39,8 @@ using fmt::BufferedFile;
using fmt::ErrorCode;
using fmt::File;
using testing::internal::scoped_ptr;
// Checks if the file is open by reading one character from it.
bool isopen(int fd) {
char buffer;
@ -136,8 +138,7 @@ TEST(BufferedFileTest, CloseFileInDtor) {
}
TEST(BufferedFileTest, CloseErrorInDtor) {
testing::internal::scoped_ptr<BufferedFile> f(
new BufferedFile(open_buffered_file()));
scoped_ptr<BufferedFile> f(new BufferedFile(open_buffered_file()));
EXPECT_WRITE(stderr, {
// The close function must be called inside EXPECT_WRITE, otherwise
// the system may recycle closed file descriptor when redirecting the
@ -261,14 +262,14 @@ TEST(FileTest, CloseFileInDtor) {
}
TEST(FileTest, CloseErrorInDtor) {
File *f = new File(open_file());
scoped_ptr<File> f(new File(open_file()));
EXPECT_WRITE(stderr, {
// The close function must be called inside EXPECT_WRITE, otherwise
// the system may recycle closed file descriptor when redirecting the
// output in EXPECT_STDERR and the second close will break output
// redirection.
FMT_POSIX(close(f->descriptor()));
SUPPRESS_ASSERT(delete f);
SUPPRESS_ASSERT(f.reset());
}, format_system_error(EBADF, "cannot close file") + "\n");
}