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

View File

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