Add InlineExecutor.cpp to Makefile.am
[folly.git] / folly / test / MemoryMappingTest.cpp
index 7c62ff5ad82006f76d6fc70e95f66d68c08a685d..958f97c31cb0d3ff77c680e7077d328cc531e677 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * limitations under the License.
  */
 
-#include <sys/mman.h>
 #include <cstdlib>
+
 #include <folly/FileUtil.h>
 #include <folly/MemoryMapping.h>
-#include <gtest/gtest.h>
+#include <folly/Random.h>
+#include <folly/portability/GTest.h>
+#include <folly/portability/SysMman.h>
+
+static constexpr double kSomeDouble = 3.14;
 
 namespace folly {
 
@@ -27,7 +31,7 @@ TEST(MemoryMapping, Basic) {
   {
     MemoryMapping m(File(f.fd()), 0, sizeof(double), MemoryMapping::writable());
     double* d = m.asWritableRange<double>().data();
-    *d = 37 * M_PI;
+    *d = 37 * kSomeDouble;
   }
   {
     MemoryMapping m(File(f.fd()), 0, 3);
@@ -36,7 +40,7 @@ TEST(MemoryMapping, Basic) {
   {
     MemoryMapping m(File(f.fd()), 0, sizeof(double));
     const double* d = m.asRange<double>().data();
-    EXPECT_EQ(*d, 37 * M_PI);
+    EXPECT_EQ(*d, 37 * kSomeDouble);
   }
 }
 
@@ -46,18 +50,18 @@ TEST(MemoryMapping, Move) {
     MemoryMapping m(
         File(f.fd()), 0, sizeof(double) * 2, MemoryMapping::writable());
     double* d = m.asWritableRange<double>().data();
-    d[0] = 37 * M_PI;
+    d[0] = 37 * kSomeDouble;
     MemoryMapping m2(std::move(m));
     double* d2 = m2.asWritableRange<double>().data();
-    d2[1] = 39 * M_PI;
+    d2[1] = 39 * kSomeDouble;
   }
   {
     MemoryMapping m(File(f.fd()), 0, sizeof(double));
     const double* d = m.asRange<double>().data();
-    EXPECT_EQ(d[0], 37 * M_PI);
+    EXPECT_EQ(d[0], 37 * kSomeDouble);
     MemoryMapping m2(std::move(m));
     const double* d2 = m2.asRange<double>().data();
-    EXPECT_EQ(d2[1], 39 * M_PI);
+    EXPECT_EQ(d2[1], 39 * kSomeDouble);
   }
 }
 
@@ -72,10 +76,10 @@ TEST(MemoryMapping, DoublyMapped) {
 
   // Show that it's truly the same value, even though the pointers differ
   EXPECT_NE(dw, dr);
-  *dw = 42 * M_PI;
-  EXPECT_EQ(*dr, 42 * M_PI);
-  *dw = 43 * M_PI;
-  EXPECT_EQ(*dr, 43 * M_PI);
+  *dw = 42 * kSomeDouble;
+  EXPECT_EQ(*dr, 42 * kSomeDouble);
+  *dw = 43 * kSomeDouble;
+  EXPECT_EQ(*dr, 43 * kSomeDouble);
 }
 
 namespace {
@@ -100,7 +104,7 @@ void writeStringToFileOrDie(const std::string& str, int fd) {
   } while (r != 0 && count);
 }
 
-}  // anonymous namespace
+} // namespace
 
 TEST(MemoryMapping, Simple) {
   File f = File::temporary();
@@ -121,7 +125,7 @@ TEST(MemoryMapping, LargeFile) {
   size_t fileSize = sysconf(_SC_PAGESIZE) * 3 + 10;
   fileData.reserve(fileSize);
   for (size_t i = 0; i < fileSize; i++) {
-    fileData.push_back(0xff & random());
+    fileData.push_back(0xff & Random::rand32());
   }
 
   File f = File::temporary();