54c3a2bf0d9fcb2e382e485cee136fa43f516eef
[folly.git] / folly / test / MemoryMappingTest.cpp
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <cstdlib>
18 #include <gtest/gtest.h>
19 #include "folly/MemoryMapping.h"
20
21 namespace folly {
22
23 TEST(MemoryMapping, Basic) {
24   File f = File::temporary();
25   {
26     MemoryMapping m(File(f.fd()), 0, sizeof(double),
27                     MemoryMapping::writable());
28     double volatile* d = m.asWritableRange<double>().data();
29     *d = 37 * M_PI;
30   }
31   {
32     MemoryMapping m(File(f.fd()), 0, 3);
33     EXPECT_EQ(0, m.asRange<int>().size()); //not big enough
34   }
35   {
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);
39   }
40 }
41
42 TEST(MemoryMapping, Move) {
43   File f = File::temporary();
44   {
45     MemoryMapping m(File(f.fd()), 0, sizeof(double) * 2,
46                     MemoryMapping::writable());
47     double volatile* d = m.asWritableRange<double>().data();
48     d[0] = 37 * M_PI;
49     MemoryMapping m2(std::move(m));
50     double volatile* d2 = m2.asWritableRange<double>().data();
51     d2[1] = 39 * M_PI;
52   }
53   {
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);
60   }
61 }
62
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));
69
70   double volatile* dw = mw.asWritableRange<double>().data();
71   const double volatile* dr = mr.asRange<double>().data();
72
73   // Show that it's truly the same value, even though the pointers differ
74   EXPECT_NE(dw, dr);
75   *dw = 42 * M_PI;
76   EXPECT_EQ(*dr, 42 * M_PI);
77   *dw = 43 * M_PI;
78   EXPECT_EQ(*dr, 43 * M_PI);
79 }
80
81 namespace {
82
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;
87   ssize_t r;
88   do {
89     r = write(fd, b, count);
90     if (r == -1) {
91       if (errno == EINTR) {
92         continue;
93       }
94       PCHECK(r) << "write";
95     }
96
97     total_bytes += r;
98     b += r;
99     count -= r;
100   } while (r != 0 && count);
101 }
102
103 }  // anonymous namespace
104
105 TEST(MemoryMapping, Simple) {
106   File f = File::temporary();
107   writeStringToFileOrDie("hello", f.fd());
108
109   {
110     MemoryMapping m(File(f.fd()));
111     EXPECT_EQ("hello", m.data());
112   }
113   {
114     MemoryMapping m(File(f.fd()), 1, 2);
115     EXPECT_EQ("el", m.data());
116   }
117 }
118
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());
125   }
126
127   File f = File::temporary();
128   writeStringToFileOrDie(fileData, f.fd());
129
130   {
131     MemoryMapping m(File(f.fd()));
132     EXPECT_EQ(fileData, m.data());
133   }
134   {
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());
139   }
140 }
141
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());
148 }
149
150 } // namespace folly