From d15e0d3e06f2b62ca5a6c56278401d5398e5abed Mon Sep 17 00:00:00 2001 From: vitaut Date: Fri, 31 Jul 2015 08:23:39 -0700 Subject: [PATCH] Use scoped_ptr instead of raw pointers in tests --- test/posix-mock-test.cc | 18 +++++++++--------- test/posix-test.cc | 9 +++++---- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/test/posix-mock-test.cc b/test/posix-mock-test.cc index c41f5956..a6080fdb 100644 --- a/test/posix-mock-test.cc +++ b/test/posix-mock-test.cc @@ -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 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 f(new File(std::move(read_end))); + scoped_ptr 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 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 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"); diff --git a/test/posix-test.cc b/test/posix-test.cc index 4a2e678c..e4f75257 100644 --- a/test/posix-test.cc +++ b/test/posix-test.cc @@ -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 f( - new BufferedFile(open_buffered_file())); + scoped_ptr 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 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"); }