From 1d477ea6d5221c6654db4c0a7cebc68bb70aa080 Mon Sep 17 00:00:00 2001 From: Viktor Palmkvist Date: Mon, 3 Oct 2016 13:16:02 +0200 Subject: [PATCH] Default constructor for file BUG=webrtc:6177 R=perkj@webrtc.org, sprang@webrtc.org Review URL: https://codereview.webrtc.org/2360303004 . Cr-Commit-Position: refs/heads/master@{#14473} --- webrtc/base/file.cc | 2 ++ webrtc/base/file.h | 2 ++ webrtc/base/file_unittest.cc | 28 ++++++++++++++++++++-------- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/webrtc/base/file.cc b/webrtc/base/file.cc index c97504f9c7..74d48177e4 100644 --- a/webrtc/base/file.cc +++ b/webrtc/base/file.cc @@ -14,6 +14,8 @@ namespace rtc { File::File(PlatformFile file) : file_(file) {} +File::File() : file_(kInvalidPlatformFileValue) {} + File::~File() { Close(); } diff --git a/webrtc/base/file.h b/webrtc/base/file.h index 6ca4825ed0..10fb69834b 100644 --- a/webrtc/base/file.h +++ b/webrtc/base/file.h @@ -30,6 +30,8 @@ class File { // Wraps the given PlatformFile. This class is then responsible for closing // the file, which will be done in the destructor if Close is never called. explicit File(PlatformFile); + // The default constructor produces a closed file. + File(); ~File(); File(File&& other); diff --git a/webrtc/base/file_unittest.cc b/webrtc/base/file_unittest.cc index edf2b29127..aa787c14c5 100644 --- a/webrtc/base/file_unittest.cc +++ b/webrtc/base/file_unittest.cc @@ -56,12 +56,24 @@ class FileTest : public ::testing::Test { path_ = webrtc::test::TempFilename(webrtc::test::OutputPath(), "test_file"); ASSERT_FALSE(path_.empty()); } - rtc::File OpenTempFile() { return rtc::File::Open(path_); } - void TearDown() { rtc::RemoveFile(path_); } + void TearDown() { RemoveFile(path_); } }; +TEST_F(FileTest, DefaultConstructor) { + File file; + uint8_t buffer[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + + EXPECT_FALSE(file.IsOpen()); + EXPECT_EQ(0u, file.Write(buffer, 10)); + EXPECT_FALSE(file.Seek(0)); + EXPECT_EQ(0u, file.Read(buffer, 10)); + EXPECT_EQ(0u, file.WriteAt(buffer, 10, 0)); + EXPECT_EQ(0u, file.ReadAt(buffer, 10, 0)); + EXPECT_FALSE(file.Close()); +} + TEST_F(FileTest, DoubleClose) { - File file = OpenTempFile(); + File file = File::Open(path_); ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError(); EXPECT_TRUE(file.Close()); @@ -69,7 +81,7 @@ TEST_F(FileTest, DoubleClose) { } TEST_F(FileTest, SimpleReadWrite) { - File file = OpenTempFile(); + File file = File::Open(path_); ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError(); uint8_t data[100] = {0}; @@ -101,7 +113,7 @@ TEST_F(FileTest, SimpleReadWrite) { } TEST_F(FileTest, ReadWriteClose) { - File file = OpenTempFile(); + File file = File::Open(path_); ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError(); uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; @@ -109,14 +121,14 @@ TEST_F(FileTest, ReadWriteClose) { EXPECT_EQ(10u, file.Write(data, 10)); EXPECT_TRUE(file.Close()); - File file2 = OpenTempFile(); + File file2 = File::Open(path_); ASSERT_TRUE(file2.IsOpen()) << "Error: " << LastError(); EXPECT_EQ(10u, file2.Read(out, 10)); EXPECT_TRUE(VerifyBuffer(out, 10, 0)); } TEST_F(FileTest, RandomAccessRead) { - File file = OpenTempFile(); + File file = File::Open(path_); ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError(); uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; @@ -134,7 +146,7 @@ TEST_F(FileTest, RandomAccessRead) { } TEST_F(FileTest, RandomAccessReadWrite) { - File file = OpenTempFile(); + File file = File::Open(path_); ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError(); uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};