Fix undefined behaviour in 128-bit integer-to-string conversion
[folly.git] / folly / test / AtomicHashArrayTest.cpp
index b2a367cae969c3348fbfbc643ca0afeb3684a85d..92be30da9ae2fcfb9dfb17e53e308a644a2c348d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2016 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 <stdexcept>
 
 #include <folly/AtomicHashArray.h>
-#include <folly/Hash.h>
 #include <folly/Conv.h>
+#include <folly/Hash.h>
 #include <folly/Memory.h>
+#include <folly/portability/SysMman.h>
 #include <gtest/gtest.h>
 
-#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
-#define MAP_ANONYMOUS MAP_ANON
-#endif
-
 using namespace std;
 using namespace folly;
 
@@ -304,7 +299,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 +329,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);
 }