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

Side by Side Diff: base/platform_file_unittest.cc

Issue 7821013: Base: Change ReadPlatformFile to perform a best (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 3 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/platform_file_posix.cc ('k') | base/platform_file_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_util.h" 5 #include "base/file_util.h"
6 #include "base/platform_file.h" 6 #include "base/platform_file.h"
7 #include "base/scoped_temp_dir.h" 7 #include "base/scoped_temp_dir.h"
8 #include "base/time.h" 8 #include "base/time.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
11 namespace { 11 namespace {
12 12
13 // Reads from a file the given number of bytes, or until EOF is reached. 13 // Reads from a file the given number of bytes, or until EOF is reached.
14 // Returns the number of bytes read. 14 // Returns the number of bytes read.
15 int ReadFully(base::PlatformFile file, int64 offset, char* data, int size) { 15 int ReadFully(base::PlatformFile file, int64 offset, char* data, int size) {
16 int total_bytes_read = 0; 16 return base::ReadPlatformFile(file, offset, data, size);
17 int bytes_read;
18 while (total_bytes_read < size) {
19 bytes_read = base::ReadPlatformFile(
20 file, offset + total_bytes_read, &data[total_bytes_read],
21 size - total_bytes_read);
22
23 // If we reached EOF, bytes_read will be 0.
24 if (bytes_read == 0)
25 return total_bytes_read;
26
27 if ((bytes_read < 0) || (bytes_read > size - total_bytes_read))
28 return -1;
29
30 total_bytes_read += bytes_read;
31 }
32
33 return total_bytes_read;
34 } 17 }
35 18
36 // Writes the given number of bytes to a file. 19 // Writes the given number of bytes to a file.
37 // Returns the number of bytes written. 20 // Returns the number of bytes written.
38 int WriteFully(base::PlatformFile file, int64 offset, 21 int WriteFully(base::PlatformFile file, int64 offset,
39 const char* data, int size) { 22 const char* data, int size) {
40 return base::WritePlatformFile(file, offset, data, size); 23 return base::WritePlatformFile(file, offset, data, size);
41 } 24 }
42 25
43 } // namespace 26 } // namespace
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 // Read 0 bytes. 168 // Read 0 bytes.
186 bytes_read = ReadFully(file, 0, data_read_1, 0); 169 bytes_read = ReadFully(file, 0, data_read_1, 0);
187 EXPECT_EQ(0, bytes_read); 170 EXPECT_EQ(0, bytes_read);
188 171
189 // Read the entire file. 172 // Read the entire file.
190 bytes_read = ReadFully(file, 0, data_read_1, kTestDataSize); 173 bytes_read = ReadFully(file, 0, data_read_1, kTestDataSize);
191 EXPECT_EQ(kTestDataSize, bytes_read); 174 EXPECT_EQ(kTestDataSize, bytes_read);
192 for (int i = 0; i < bytes_read; i++) 175 for (int i = 0; i < bytes_read; i++)
193 EXPECT_EQ(data_to_write[i], data_read_1[i]); 176 EXPECT_EQ(data_to_write[i], data_read_1[i]);
194 177
178 // Read again, but using the trivial native wrapper.
179 bytes_read = base::ReadPlatformFileNoBestEffort(file, 0, data_read_1,
180 kTestDataSize);
181 EXPECT_LE(bytes_read, kTestDataSize);
182 for (int i = 0; i < bytes_read; i++)
183 EXPECT_EQ(data_to_write[i], data_read_1[i]);
184
195 // Write past the end of the file. 185 // Write past the end of the file.
196 const int kOffsetBeyondEndOfFile = 10; 186 const int kOffsetBeyondEndOfFile = 10;
197 const int kPartialWriteLength = 2; 187 const int kPartialWriteLength = 2;
198 bytes_written = WriteFully(file, kOffsetBeyondEndOfFile, 188 bytes_written = WriteFully(file, kOffsetBeyondEndOfFile,
199 data_to_write, kPartialWriteLength); 189 data_to_write, kPartialWriteLength);
200 EXPECT_EQ(kPartialWriteLength, bytes_written); 190 EXPECT_EQ(kPartialWriteLength, bytes_written);
201 191
202 // Make sure the file was extended. 192 // Make sure the file was extended.
203 int64 file_size = 0; 193 int64 file_size = 0;
204 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size)); 194 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size));
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 EXPECT_EQ(info.last_modified.ToInternalValue(), 327 EXPECT_EQ(info.last_modified.ToInternalValue(),
338 new_last_modified.ToInternalValue()); 328 new_last_modified.ToInternalValue());
339 #endif 329 #endif
340 330
341 EXPECT_EQ(info.creation_time.ToInternalValue(), 331 EXPECT_EQ(info.creation_time.ToInternalValue(),
342 creation_time.ToInternalValue()); 332 creation_time.ToInternalValue());
343 333
344 // Close the file handle to allow the temp directory to be deleted. 334 // Close the file handle to allow the temp directory to be deleted.
345 base::ClosePlatformFile(file); 335 base::ClosePlatformFile(file);
346 } 336 }
OLDNEW
« no previous file with comments | « base/platform_file_posix.cc ('k') | base/platform_file_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698