proxygen
AtomicHashArrayTest.cpp File Reference
#include <cstddef>
#include <map>
#include <memory>
#include <stdexcept>
#include <folly/AtomicHashArray.h>
#include <folly/Conv.h>
#include <folly/Memory.h>
#include <folly/hash/Hash.h>
#include <folly/portability/GTest.h>
#include <folly/portability/SysMman.h>

Go to the source code of this file.

Classes

class  MmapAllocator< T >
 
struct  MmapAllocator< T >::rebind< U >
 
struct  EqTraits
 
struct  HashTraits
 
struct  KeyConvertTraits
 

Typedefs

typedef AtomicHashArray< char *, int64_t, HashTraits, EqTraits, MmapAllocator< char >, AtomicHashArrayQuadraticProbeFcn, KeyConvertTraitsAHACstrInt
 
using AHAIntCInt = AtomicHashArray< int64_t, const int32_t >
 

Functions

template<class KeyT , class ValueT >
pair< KeyT, ValueTcreateEntry (int i)
 
template<class KeyT , class ValueT , class Allocator = std::allocator<char>, class ProbeFcn = AtomicHashArrayLinearProbeFcn>
void testMap ()
 
template<class KeyT , class ValueT , class Allocator = std::allocator<char>, class ProbeFcn = AtomicHashArrayLinearProbeFcn>
void testNoncopyableMap ()
 
 TEST (Aha, InsertErase_i32_i32)
 
 TEST (Aha, InsertErase_i64_i32)
 
 TEST (Aha, InsertErase_i64_i64)
 
 TEST (Aha, InsertErase_i32_i64)
 
 TEST (Aha, InsertErase_i32_str)
 
 TEST (Aha, InsertErase_i64_str)
 
 TEST (Aha, Create_cstr_i64)
 
static bool legalKey (char *a)
 
 TEST (Aha, LookupAny)
 
 TEST (Aha, ConstValue)
 

Variables

AHACstrInt::Config cstrIntCfg
 

Typedef Documentation

Definition at line 432 of file AtomicHashArrayTest.cpp.

Function Documentation

template<class KeyT , class ValueT >
pair<KeyT, ValueT> createEntry ( int  i)

Definition at line 98 of file AtomicHashArrayTest.cpp.

References folly::hash::jenkins_rev_mix32().

98  {
99  return pair<KeyT, ValueT>(
100  to<KeyT>(folly::hash::jenkins_rev_mix32(i) % 1000), to<ValueT>(i + 3));
101 }
uint32_t jenkins_rev_mix32(uint32_t key) noexcept
Definition: Hash.h:97
TEST ( Aha  ,
InsertErase_i32_i32   
)

Definition at line 192 of file AtomicHashArrayTest.cpp.

References int32_t, testMap(), and testNoncopyableMap().

192  {
193  testMap<int32_t, int32_t>();
194  testMap<int32_t, int32_t, MmapAllocator<char>>();
195  testMap<
196  int32_t,
197  int32_t,
198  std::allocator<char>,
200  testMap<
201  int32_t,
202  int32_t,
205  testNoncopyableMap<int32_t, int32_t>();
206  testNoncopyableMap<int32_t, int32_t, MmapAllocator<char>>();
208  int32_t,
209  int32_t,
210  std::allocator<char>,
213  int32_t,
214  int32_t,
215  MmapAllocator<char>,
217 }
void testMap()
void testNoncopyableMap()
TEST ( Aha  ,
InsertErase_i64_i32   
)

Definition at line 218 of file AtomicHashArrayTest.cpp.

References int32_t, int64_t, testMap(), and testNoncopyableMap().

218  {
219  testMap<int64_t, int32_t>();
220  testMap<int64_t, int32_t, MmapAllocator<char>>();
221  testMap<
222  int64_t,
223  int32_t,
224  std::allocator<char>,
226  testMap<
227  int64_t,
228  int32_t,
231  testNoncopyableMap<int64_t, int32_t>();
232  testNoncopyableMap<int64_t, int32_t, MmapAllocator<char>>();
234  int64_t,
235  int32_t,
236  std::allocator<char>,
239  int64_t,
240  int32_t,
241  MmapAllocator<char>,
243 }
void testMap()
void testNoncopyableMap()
TEST ( Aha  ,
InsertErase_i64_i64   
)

Definition at line 244 of file AtomicHashArrayTest.cpp.

References int64_t, testMap(), and testNoncopyableMap().

244  {
245  testMap<int64_t, int64_t>();
246  testMap<int64_t, int64_t, MmapAllocator<char>>();
247  testMap<
248  int64_t,
249  int64_t,
250  std::allocator<char>,
252  testMap<
253  int64_t,
254  int64_t,
257  testNoncopyableMap<int64_t, int64_t>();
258  testNoncopyableMap<int64_t, int64_t, MmapAllocator<char>>();
260  int64_t,
261  int64_t,
262  std::allocator<char>,
265  int64_t,
266  int64_t,
267  MmapAllocator<char>,
269 }
void testMap()
void testNoncopyableMap()
TEST ( Aha  ,
InsertErase_i32_i64   
)

Definition at line 270 of file AtomicHashArrayTest.cpp.

References int32_t, int64_t, testMap(), and testNoncopyableMap().

270  {
271  testMap<int32_t, int64_t>();
272  testMap<int32_t, int64_t, MmapAllocator<char>>();
273  testMap<
274  int32_t,
275  int64_t,
276  std::allocator<char>,
278  testMap<
279  int32_t,
280  int64_t,
283  testNoncopyableMap<int32_t, int64_t>();
284  testNoncopyableMap<int32_t, int64_t, MmapAllocator<char>>();
286  int32_t,
287  int64_t,
288  std::allocator<char>,
291  int32_t,
292  int64_t,
293  MmapAllocator<char>,
295 }
void testMap()
void testNoncopyableMap()
TEST ( Aha  ,
InsertErase_i32_str   
)

Definition at line 296 of file AtomicHashArrayTest.cpp.

References int32_t, string, and testMap().

296  {
297  testMap<int32_t, string>();
298  testMap<int32_t, string, MmapAllocator<char>>();
299  testMap<
300  int32_t,
301  string,
302  std::allocator<char>,
304  testMap<
305  int32_t,
306  string,
309 }
void testMap()
const char * string
Definition: Conv.cpp:212
TEST ( Aha  ,
InsertErase_i64_str   
)

Definition at line 310 of file AtomicHashArrayTest.cpp.

References int64_t, string, and testMap().

310  {
311  testMap<int64_t, string>();
312  testMap<int64_t, string, MmapAllocator<char>>();
313  testMap<
314  int64_t,
315  string,
316  std::allocator<char>,
318  testMap<
319  int64_t,
320  string,
323 }
void testMap()
const char * string
Definition: Conv.cpp:212
TEST ( Aha  ,
Create_cstr_i64   
)

Definition at line 325 of file AtomicHashArrayTest.cpp.

References a, and legalKey().

TEST ( Aha  ,
LookupAny   
)

Definition at line 392 of file AtomicHashArrayTest.cpp.

References folly::AtomicHashArray< KeyT, ValueT, HashFcn, EqualFcn, Allocator, ProbeFcn, KeyConvertFcn >::create(), EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, bm::free(), and SCOPE_EXIT.

392  {
393  auto arr = AHACstrInt::create(12);
394 
395  char* f_char = strdup("f");
396  SCOPE_EXIT {
397  free(f_char);
398  };
399  arr->insert(std::make_pair(f_char, 42));
400 
401  EXPECT_EQ(42, arr->find("f")->second);
402  {
403  // Look up a single char, successfully.
404  auto it = arr->find('f');
405  EXPECT_EQ(42, it->second);
406  }
407  {
408  // Look up a single char, unsuccessfully.
409  auto it = arr->find('g');
410  EXPECT_TRUE(it == arr->end());
411  }
412  {
413  // Insert a new char key.
414  auto res = arr->emplace('h', static_cast<int64_t>(123));
415  EXPECT_TRUE(res.second);
416  EXPECT_TRUE(res.first != arr->end());
417  // Look up the string version.
418  EXPECT_EQ(123, arr->find("h")->second);
419  }
420  {
421  // Fail to emplace an existing key.
422  auto res = arr->emplace('f', static_cast<int64_t>(123));
423  EXPECT_FALSE(res.second);
424  EXPECT_TRUE(res.first != arr->end());
425  }
426 
427  for (auto it : *arr) {
428  free(it.first);
429  }
430 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define SCOPE_EXIT
Definition: ScopeGuard.h:274
void free()
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
static SmartPtr create(size_t maxSize, const Config &c=Config())
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( Aha  ,
ConstValue   
)

Definition at line 434 of file AtomicHashArrayTest.cpp.

References folly::AtomicHashArray< KeyT, ValueT, HashFcn, EqualFcn, Allocator, ProbeFcn, KeyConvertFcn >::create().

434  {
435  auto aha = AHAIntCInt::create(10);
436  aha->emplace(1, 2);
437 }
static SmartPtr create(size_t maxSize, const Config &c=Config())
template<class KeyT , class ValueT , class Allocator = std::allocator<char>, class ProbeFcn = AtomicHashArrayLinearProbeFcn>
void testMap ( )

Definition at line 108 of file AtomicHashArrayTest.cpp.

References EXPECT_EQ, EXPECT_FALSE, and i.

Referenced by getBenchPerfectIndexMapUniqueGetsCodeTestMap(), getBenchPerfectIndexMapUniqueGetsOtherStringTestMap(), getBenchUnorderedMapUniqueGetsTestMap(), and TEST().

108  {
109  typedef AtomicHashArray<
110  KeyT,
111  ValueT,
112  std::hash<KeyT>,
113  std::equal_to<KeyT>,
114  Allocator,
115  ProbeFcn>
116  MyArr;
117  auto arr = MyArr::create(150);
118  map<KeyT, ValueT> ref;
119  for (int i = 0; i < 100; ++i) {
120  auto e = createEntry<KeyT, ValueT>(i);
121  auto ret = arr->insert(e);
122  EXPECT_EQ(!ref.count(e.first), ret.second); // succeed iff not in ref
123  ref.insert(e);
124  EXPECT_EQ(ref.size(), arr->size());
125  if (ret.first == arr->end()) {
126  EXPECT_FALSE("AHA should not have run out of space.");
127  continue;
128  }
129  EXPECT_EQ(e.first, ret.first->first);
130  EXPECT_EQ(ref.find(e.first)->second, ret.first->second);
131  }
132 
133  for (int i = 125; i > 0; i -= 10) {
134  auto e = createEntry<KeyT, ValueT>(i);
135  auto ret = arr->erase(e.first);
136  auto refRet = ref.erase(e.first);
137  EXPECT_EQ(ref.size(), arr->size());
138  EXPECT_EQ(refRet, ret);
139  }
140 
141  for (int i = 155; i > 0; i -= 10) {
142  auto e = createEntry<KeyT, ValueT>(i);
143  auto ret = arr->insert(e);
144  auto refRet = ref.insert(e);
145  EXPECT_EQ(ref.size(), arr->size());
146  EXPECT_EQ(*refRet.first, *ret.first);
147  EXPECT_EQ(refRet.second, ret.second);
148  }
149 
150  for (const auto& e : ref) {
151  auto ret = arr->find(e.first);
152  if (ret == arr->end()) {
153  EXPECT_FALSE("Key was not in AHA");
154  continue;
155  }
156  EXPECT_EQ(e.first, ret->first);
157  EXPECT_EQ(e.second, ret->second);
158  }
159 }
int32_t KeyT
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
int32_t ValueT
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
template<class KeyT , class ValueT , class Allocator = std::allocator<char>, class ProbeFcn = AtomicHashArrayLinearProbeFcn>
void testNoncopyableMap ( )

Definition at line 166 of file AtomicHashArrayTest.cpp.

References EXPECT_EQ, and i.

Referenced by TEST().

166  {
167  typedef AtomicHashArray<
168  KeyT,
169  std::unique_ptr<ValueT>,
170  std::hash<KeyT>,
171  std::equal_to<KeyT>,
172  Allocator,
173  ProbeFcn>
174  MyArr;
175 
176  auto arr = MyArr::create(250);
177  for (int i = 0; i < 100; i++) {
178  arr->insert(make_pair(i, std::make_unique<ValueT>(i)));
179  }
180  for (int i = 100; i < 150; i++) {
181  arr->emplace(i, new ValueT(i));
182  }
183  for (int i = 150; i < 200; i++) {
184  arr->emplace(i, new ValueT(i), std::default_delete<ValueT>());
185  }
186  for (int i = 0; i < 200; i++) {
187  auto ret = arr->find(i);
188  EXPECT_EQ(*(ret->second), i);
189  }
190 }
int32_t KeyT
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
int32_t ValueT

Variable Documentation

AHACstrInt::Config cstrIntCfg

Definition at line 385 of file AtomicHashArrayTest.cpp.