Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(414)

Unified Diff: base/file_util_unittest.cc

Issue 240893002: base::ReadFile() should return the number of read bytes on Windows. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Correct comments and how to return value on error cases. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/file_util_posix.cc ('k') | base/file_util_win.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/file_util_unittest.cc
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc
index 642c9d5a21b7cf4e09b82322512b4440406f1a94..0d793918d681fafeaa2e52a289cd4ebbb8a7e94d 100644
--- a/base/file_util_unittest.cc
+++ b/base/file_util_unittest.cc
@@ -21,6 +21,7 @@
#include <algorithm>
#include <fstream>
#include <set>
+#include <vector>
#include "base/base_paths.h"
#include "base/file_util.h"
@@ -1960,6 +1961,51 @@ TEST_F(FileUtilTest, AppendToFile) {
EXPECT_EQ(L"hellohello", read_content);
}
+TEST_F(FileUtilTest, ReadFile) {
+ // Create a test file to be read.
+ const std::string kTestData("The quick brown fox jumps over the lazy dog.");
+ FilePath file_path =
+ temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileTest"));
+
+ ASSERT_EQ(static_cast<int>(kTestData.size()),
+ WriteFile(file_path, kTestData.data(), kTestData.size()));
+
+ // Make buffers with various size.
+ std::vector<char> small_buffer(kTestData.size() / 2);
+ std::vector<char> exact_buffer(kTestData.size());
+ std::vector<char> large_buffer(kTestData.size() * 2);
+
+ // Read the file with smaller buffer.
+ int bytes_read_small = ReadFile(
+ file_path, &small_buffer[0], static_cast<int>(small_buffer.size()));
+ EXPECT_EQ(bytes_read_small, static_cast<int>(small_buffer.size()));
+ EXPECT_EQ(
+ std::string(small_buffer.begin(), small_buffer.end()),
+ std::string(kTestData.begin(), kTestData.begin() + small_buffer.size()));
+
+ // Read the file with buffer which have exactly same size.
+ int bytes_read_exact = ReadFile(
+ file_path, &exact_buffer[0], static_cast<int>(exact_buffer.size()));
+ EXPECT_EQ(bytes_read_exact, static_cast<int>(kTestData.size()));
+ EXPECT_EQ(std::string(exact_buffer.begin(), exact_buffer.end()), kTestData);
+
+ // Read the file with larger buffer.
+ int bytes_read_large = ReadFile(
+ file_path, &large_buffer[0], static_cast<int>(large_buffer.size()));
+ EXPECT_EQ(bytes_read_large, static_cast<int>(kTestData.size()));
+ EXPECT_EQ(std::string(large_buffer.begin(),
+ large_buffer.begin() + kTestData.size()),
+ kTestData);
+
+ // Make sure the return value is -1 if the file doesn't exist.
+ FilePath file_path_not_exist =
+ temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileNotExistTest"));
+ EXPECT_EQ(-1,
+ ReadFile(file_path_not_exist,
+ &exact_buffer[0],
+ static_cast<int>(exact_buffer.size())));
+}
+
TEST_F(FileUtilTest, ReadFileToString) {
const char kTestData[] = "0123";
std::string data;
« no previous file with comments | « base/file_util_posix.cc ('k') | base/file_util_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698