Remove an old PangoFontDescription forward declaration.
[chromium-blink-merge.git] / base / files / memory_mapped_file_unittest.cc
blobd0833b5cc9a24459857a356b5a7f013ca1ba8c49
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/files/memory_mapped_file.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h"
12 namespace base {
14 namespace {
16 // Create a temporary buffer and fill it with a watermark sequence.
17 scoped_ptr<uint8[]> CreateTestBuffer(size_t size, size_t offset) {
18 scoped_ptr<uint8[]> buf(new uint8[size]);
19 for (size_t i = 0; i < size; ++i)
20 buf.get()[i] = static_cast<uint8>((offset + i) % 253);
21 return buf.Pass();
24 // Check that the watermark sequence is consistent with the |offset| provided.
25 bool CheckBufferContents(const uint8* data, size_t size, size_t offset) {
26 scoped_ptr<uint8[]> test_data(CreateTestBuffer(size, offset));
27 return memcmp(test_data.get(), data, size) == 0;
30 class MemoryMappedFileTest : public PlatformTest {
31 protected:
32 void SetUp() override {
33 PlatformTest::SetUp();
34 CreateTemporaryFile(&temp_file_path_);
37 void TearDown() override { EXPECT_TRUE(DeleteFile(temp_file_path_, false)); }
39 void CreateTemporaryTestFile(size_t size) {
40 File file(temp_file_path_,
41 File::FLAG_CREATE_ALWAYS | File::FLAG_READ | File::FLAG_WRITE);
42 EXPECT_TRUE(file.IsValid());
44 scoped_ptr<uint8[]> test_data(CreateTestBuffer(size, 0));
45 size_t bytes_written =
46 file.Write(0, reinterpret_cast<char*>(test_data.get()), size);
47 EXPECT_EQ(size, bytes_written);
48 file.Close();
51 const FilePath temp_file_path() const { return temp_file_path_; }
53 private:
54 FilePath temp_file_path_;
57 TEST_F(MemoryMappedFileTest, MapWholeFileByPath) {
58 const size_t kFileSize = 68 * 1024;
59 CreateTemporaryTestFile(kFileSize);
60 MemoryMappedFile map;
61 map.Initialize(temp_file_path());
62 ASSERT_EQ(kFileSize, map.length());
63 ASSERT_TRUE(map.data() != NULL);
64 EXPECT_TRUE(map.IsValid());
65 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
68 TEST_F(MemoryMappedFileTest, MapWholeFileByFD) {
69 const size_t kFileSize = 68 * 1024;
70 CreateTemporaryTestFile(kFileSize);
71 MemoryMappedFile map;
72 map.Initialize(File(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ));
73 ASSERT_EQ(kFileSize, map.length());
74 ASSERT_TRUE(map.data() != NULL);
75 EXPECT_TRUE(map.IsValid());
76 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
79 TEST_F(MemoryMappedFileTest, MapSmallFile) {
80 const size_t kFileSize = 127;
81 CreateTemporaryTestFile(kFileSize);
82 MemoryMappedFile map;
83 map.Initialize(temp_file_path());
84 ASSERT_EQ(kFileSize, map.length());
85 ASSERT_TRUE(map.data() != NULL);
86 EXPECT_TRUE(map.IsValid());
87 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
90 TEST_F(MemoryMappedFileTest, MapWholeFileUsingRegion) {
91 const size_t kFileSize = 157 * 1024;
92 CreateTemporaryTestFile(kFileSize);
93 MemoryMappedFile map;
95 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ);
96 map.Initialize(file.Pass(), MemoryMappedFile::Region::kWholeFile);
97 ASSERT_EQ(kFileSize, map.length());
98 ASSERT_TRUE(map.data() != NULL);
99 EXPECT_TRUE(map.IsValid());
100 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
103 TEST_F(MemoryMappedFileTest, MapPartialRegionAtBeginning) {
104 const size_t kFileSize = 157 * 1024;
105 const size_t kPartialSize = 4 * 1024 + 32;
106 CreateTemporaryTestFile(kFileSize);
107 MemoryMappedFile map;
109 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ);
110 map.Initialize(file.Pass(), MemoryMappedFile::Region(0, kPartialSize));
111 ASSERT_EQ(kPartialSize, map.length());
112 ASSERT_TRUE(map.data() != NULL);
113 EXPECT_TRUE(map.IsValid());
114 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, 0));
117 TEST_F(MemoryMappedFileTest, MapPartialRegionAtEnd) {
118 const size_t kFileSize = 157 * 1024;
119 const size_t kPartialSize = 5 * 1024 - 32;
120 const size_t kOffset = kFileSize - kPartialSize;
121 CreateTemporaryTestFile(kFileSize);
122 MemoryMappedFile map;
124 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ);
125 map.Initialize(file.Pass(), MemoryMappedFile::Region(kOffset, kPartialSize));
126 ASSERT_EQ(kPartialSize, map.length());
127 ASSERT_TRUE(map.data() != NULL);
128 EXPECT_TRUE(map.IsValid());
129 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, kOffset));
132 TEST_F(MemoryMappedFileTest, MapSmallPartialRegionInTheMiddle) {
133 const size_t kFileSize = 157 * 1024;
134 const size_t kOffset = 1024 * 5 + 32;
135 const size_t kPartialSize = 8;
137 CreateTemporaryTestFile(kFileSize);
138 MemoryMappedFile map;
140 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ);
141 map.Initialize(file.Pass(), MemoryMappedFile::Region(kOffset, kPartialSize));
142 ASSERT_EQ(kPartialSize, map.length());
143 ASSERT_TRUE(map.data() != NULL);
144 EXPECT_TRUE(map.IsValid());
145 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, kOffset));
148 TEST_F(MemoryMappedFileTest, MapLargePartialRegionInTheMiddle) {
149 const size_t kFileSize = 157 * 1024;
150 const size_t kOffset = 1024 * 5 + 32;
151 const size_t kPartialSize = 16 * 1024 - 32;
153 CreateTemporaryTestFile(kFileSize);
154 MemoryMappedFile map;
156 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ);
157 map.Initialize(file.Pass(), MemoryMappedFile::Region(kOffset, kPartialSize));
158 ASSERT_EQ(kPartialSize, map.length());
159 ASSERT_TRUE(map.data() != NULL);
160 EXPECT_TRUE(map.IsValid());
161 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, kOffset));
164 } // namespace
166 } // namespace base