Fix copyright lines for Bits.h and move BitsBenchmark.cpp
[folly.git] / folly / test / AtomicHashArrayTest.cpp
index 87abd4862e9d72e7521c8ef500339b81595c4e6b..2ffb701b32482c095260742898d5cf77b0e2cefe 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 <cstddef>
 #include <map>
+#include <memory>
 #include <stdexcept>
 
 #include <folly/AtomicHashArray.h>
-#include <folly/Hash.h>
 #include <folly/Conv.h>
 #include <folly/Memory.h>
-#include <gtest/gtest.h>
-
-#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
-#define MAP_ANONYMOUS MAP_ANON
-#endif
+#include <folly/hash/Hash.h>
+#include <folly/portability/GTest.h>
+#include <folly/portability/SysMman.h>
 
 using namespace std;
 using namespace folly;
@@ -78,8 +74,10 @@ class MmapAllocator {
 
   T *allocate(size_t n) {
     void *p = mmap(nullptr, n * sizeof(T), PROT_READ | PROT_WRITE,
-        MAP_SHARED | MAP_ANONYMOUS, -1, 0);
-    if (p == MAP_FAILED) throw std::bad_alloc();
+        MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+    if (p == MAP_FAILED) {
+      throw std::bad_alloc();
+    }
     return (T *)p;
   }
 
@@ -88,16 +86,17 @@ class MmapAllocator {
   }
 };
 
-template<class KeyT, class ValueT>
+template <class KeyT, class ValueT>
 pair<KeyT,ValueT> createEntry(int i) {
   return pair<KeyT,ValueT>(to<KeyT>(folly::hash::jenkins_rev_mix32(i) % 1000),
                            to<ValueT>(i + 3));
 }
 
-template <class KeyT,
-          class ValueT,
-          class Allocator = std::allocator<char>,
-          class ProbeFcn = AtomicHashArrayLinearProbeFcn>
+template <
+    class KeyT,
+    class ValueT,
+    class Allocator = std::allocator<char>,
+    class ProbeFcn = AtomicHashArrayLinearProbeFcn>
 void testMap() {
   typedef AtomicHashArray<KeyT, ValueT, std::hash<KeyT>,
                           std::equal_to<KeyT>, Allocator, ProbeFcn> MyArr;
@@ -145,7 +144,9 @@ void testMap() {
   }
 }
 
-template<class KeyT, class ValueT,
+template <
+    class KeyT,
+    class ValueT,
     class Allocator = std::allocator<char>,
     class ProbeFcn = AtomicHashArrayLinearProbeFcn>
 void testNoncopyableMap() {
@@ -154,7 +155,7 @@ void testNoncopyableMap() {
 
   auto arr = MyArr::create(250);
   for (int i = 0; i < 100; i++) {
-    arr->insert(make_pair(i,std::unique_ptr<ValueT>(new ValueT(i))));
+    arr->insert(make_pair(i, std::make_unique<ValueT>(i)));
   }
   for (int i = 100; i < 150; i++) {
     arr->emplace(i,new ValueT(i));
@@ -266,7 +267,9 @@ struct EqTraits {
 struct HashTraits {
   size_t operator()(char* a) {
     size_t result = 0;
-    while (a[0] != 0) result += static_cast<size_t>(*(a++));
+    while (a[0] != 0) {
+      result += static_cast<size_t>(*(a++));
+    }
     return result;
   }
   size_t operator()(const char& a) {
@@ -274,7 +277,9 @@ struct HashTraits {
   }
   size_t operator()(const StringPiece a) {
     size_t result = 0;
-    for (const auto& ch : a) result += static_cast<size_t>(ch);
+    for (const auto& ch : a) {
+      result += static_cast<size_t>(ch);
+    }
     return result;
   }
 };
@@ -304,7 +309,10 @@ static bool legalKey(char* a) {
 TEST(Aha, LookupAny) {
   auto arr = AHACstrInt::create(12);
 
-  arr->insert(std::make_pair(strdup("f"), 42));
+  char* f_char = strdup("f");
+  SCOPE_EXIT { free(f_char); };
+  arr->insert(std::make_pair(f_char, 42));
+
   EXPECT_EQ(42, arr->find("f")->second);
   {
     // Look up a single char, successfully.
@@ -331,5 +339,14 @@ TEST(Aha, LookupAny) {
     EXPECT_TRUE(res.first != arr->end());
   }
 
-  for (auto it : *arr) free(it.first);
+  for (auto it : *arr) {
+    free(it.first);
+  }
+}
+
+using AHAIntCInt = AtomicHashArray<int64_t, const int32_t>;
+
+TEST(Aha, ConstValue) {
+  auto aha = AHAIntCInt::create(10);
+  aha->emplace(1, 2);
 }