2 * Copyright 2014 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <gtest/gtest.h>
19 #include "folly/MemoryMapping.h"
23 TEST(MemoryMapping, Basic) {
24 File f = File::temporary();
26 MemoryMapping m(File(f.fd()), 0, sizeof(double),
27 MemoryMapping::writable());
28 double volatile* d = m.asWritableRange<double>().data();
32 MemoryMapping m(File(f.fd()), 0, 3);
33 EXPECT_EQ(0, m.asRange<int>().size()); //not big enough
36 MemoryMapping m(File(f.fd()), 0, sizeof(double));
37 const double volatile* d = m.asRange<double>().data();
38 EXPECT_EQ(*d, 37 * M_PI);
42 TEST(MemoryMapping, Move) {
43 File f = File::temporary();
45 MemoryMapping m(File(f.fd()), 0, sizeof(double) * 2,
46 MemoryMapping::writable());
47 double volatile* d = m.asWritableRange<double>().data();
49 MemoryMapping m2(std::move(m));
50 double volatile* d2 = m2.asWritableRange<double>().data();
54 MemoryMapping m(File(f.fd()), 0, sizeof(double));
55 const double volatile* d = m.asRange<double>().data();
56 EXPECT_EQ(d[0], 37 * M_PI);
57 MemoryMapping m2(std::move(m));
58 const double volatile* d2 = m2.asRange<double>().data();
59 EXPECT_EQ(d2[1], 39 * M_PI);
63 TEST(MemoryMapping, DoublyMapped) {
64 File f = File::temporary();
65 // two mappings of the same memory, different addresses.
66 MemoryMapping mw(File(f.fd()), 0, sizeof(double),
67 MemoryMapping::writable());
68 MemoryMapping mr(File(f.fd()), 0, sizeof(double));
70 double volatile* dw = mw.asWritableRange<double>().data();
71 const double volatile* dr = mr.asRange<double>().data();
73 // Show that it's truly the same value, even though the pointers differ
76 EXPECT_EQ(*dr, 42 * M_PI);
78 EXPECT_EQ(*dr, 43 * M_PI);
83 void writeStringToFileOrDie(const std::string& str, int fd) {
84 const char* b = str.c_str();
85 size_t count = str.size();
86 ssize_t total_bytes = 0;
89 r = write(fd, b, count);
100 } while (r != 0 && count);
103 } // anonymous namespace
105 TEST(MemoryMapping, Simple) {
106 File f = File::temporary();
107 writeStringToFileOrDie("hello", f.fd());
110 MemoryMapping m(File(f.fd()));
111 EXPECT_EQ("hello", m.data());
114 MemoryMapping m(File(f.fd()), 1, 2);
115 EXPECT_EQ("el", m.data());
119 TEST(MemoryMapping, LargeFile) {
120 std::string fileData;
121 size_t fileSize = sysconf(_SC_PAGESIZE) * 3 + 10;
122 fileData.reserve(fileSize);
123 for (size_t i = 0; i < fileSize; i++) {
124 fileData.push_back(0xff & random());
127 File f = File::temporary();
128 writeStringToFileOrDie(fileData, f.fd());
131 MemoryMapping m(File(f.fd()));
132 EXPECT_EQ(fileData, m.data());
135 size_t size = sysconf(_SC_PAGESIZE) * 2;
136 StringPiece s(fileData.data() + 9, size - 9);
137 MemoryMapping m(File(f.fd()), 9, size - 9);
138 EXPECT_EQ(s.toString(), m.data());
142 TEST(MemoryMapping, ZeroLength) {
143 File f = File::temporary();
144 MemoryMapping m(File(f.fd()));
145 EXPECT_TRUE(m.mlock(MemoryMapping::LockMode::MUST_LOCK));
146 EXPECT_TRUE(m.mlocked());
147 EXPECT_EQ(0, m.data().size());