2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <folly/AtomicHashArray.h>
24 #include <folly/Hash.h>
25 #include <folly/Conv.h>
26 #include <folly/Memory.h>
27 #include <gtest/gtest.h>
29 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
30 #define MAP_ANONYMOUS MAP_ANON
34 using namespace folly;
41 typedef const T* const_pointer;
43 typedef const T& const_reference;
45 typedef ptrdiff_t difference_type;
46 typedef size_t size_type;
48 T* address(T& x) const {
49 return std::addressof(x);
52 const T* address(const T& x) const {
53 return std::addressof(x);
56 size_t max_size() const {
57 return std::numeric_limits<size_t>::max();
60 template <class U> struct rebind {
61 typedef MmapAllocator<U> other;
64 bool operator!=(const MmapAllocator<T>& other) const {
65 return !(*this == other);
68 bool operator==(const MmapAllocator<T>& other) const {
72 template <class... Args>
73 void construct(T* p, Args&&... args) {
74 new (p) T(std::forward<Args>(args)...);
81 T *allocate(size_t n) {
82 void *p = mmap(nullptr, n * sizeof(T), PROT_READ | PROT_WRITE,
83 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
84 if (p == MAP_FAILED) throw std::bad_alloc();
88 void deallocate(T *p, size_t n) {
89 munmap(p, n * sizeof(T));
93 template<class KeyT, class ValueT>
94 pair<KeyT,ValueT> createEntry(int i) {
95 return pair<KeyT,ValueT>(to<KeyT>(folly::hash::jenkins_rev_mix32(i) % 1000),
99 template<class KeyT, class ValueT, class Allocator = std::allocator<char>>
101 typedef AtomicHashArray<KeyT, ValueT, std::hash<KeyT>,
102 std::equal_to<KeyT>, Allocator> MyArr;
103 auto arr = MyArr::create(150);
104 map<KeyT, ValueT> ref;
105 for (int i = 0; i < 100; ++i) {
106 auto e = createEntry<KeyT, ValueT>(i);
107 auto ret = arr->insert(e);
108 EXPECT_EQ(!ref.count(e.first), ret.second); // succeed iff not in ref
110 EXPECT_EQ(ref.size(), arr->size());
111 if (ret.first == arr->end()) {
112 EXPECT_FALSE("AHA should not have run out of space.");
115 EXPECT_EQ(e.first, ret.first->first);
116 EXPECT_EQ(ref.find(e.first)->second, ret.first->second);
119 for (int i = 125; i > 0; i -= 10) {
120 auto e = createEntry<KeyT, ValueT>(i);
121 auto ret = arr->erase(e.first);
122 auto refRet = ref.erase(e.first);
123 EXPECT_EQ(ref.size(), arr->size());
124 EXPECT_EQ(refRet, ret);
127 for (int i = 155; i > 0; i -= 10) {
128 auto e = createEntry<KeyT, ValueT>(i);
129 auto ret = arr->insert(e);
130 auto refRet = ref.insert(e);
131 EXPECT_EQ(ref.size(), arr->size());
132 EXPECT_EQ(*refRet.first, *ret.first);
133 EXPECT_EQ(refRet.second, ret.second);
136 for (const auto& e : ref) {
137 auto ret = arr->find(e.first);
138 if (ret == arr->end()) {
139 EXPECT_FALSE("Key was not in AHA");
142 EXPECT_EQ(e.first, ret->first);
143 EXPECT_EQ(e.second, ret->second);
147 template<class KeyT, class ValueT, class Allocator = std::allocator<char>>
148 void testNoncopyableMap() {
149 typedef AtomicHashArray<KeyT, std::unique_ptr<ValueT>, std::hash<KeyT>,
150 std::equal_to<KeyT>, Allocator> MyArr;
151 auto arr = MyArr::create(150);
152 for (int i = 0; i < 100; i++) {
153 arr->insert(make_pair(i,std::unique_ptr<ValueT>(new ValueT(i))));
155 for (int i = 0; i < 100; i++) {
156 auto ret = arr->find(i);
157 EXPECT_EQ(*(ret->second), i);
162 TEST(Aha, InsertErase_i32_i32) {
163 testMap<int32_t, int32_t>();
164 testMap<int32_t, int32_t, MmapAllocator<char>>();
165 testNoncopyableMap<int32_t, int32_t>();
166 testNoncopyableMap<int32_t, int32_t, MmapAllocator<char>>();
168 TEST(Aha, InsertErase_i64_i32) {
169 testMap<int64_t, int32_t>();
170 testMap<int64_t, int32_t, MmapAllocator<char>>();
171 testNoncopyableMap<int64_t, int32_t>();
172 testNoncopyableMap<int64_t, int32_t, MmapAllocator<char>>();
174 TEST(Aha, InsertErase_i64_i64) {
175 testMap<int64_t, int64_t>();
176 testMap<int64_t, int64_t, MmapAllocator<char>>();
177 testNoncopyableMap<int64_t, int64_t>();
178 testNoncopyableMap<int64_t, int64_t, MmapAllocator<char>>();
180 TEST(Aha, InsertErase_i32_i64) {
181 testMap<int32_t, int64_t>();
182 testMap<int32_t, int64_t, MmapAllocator<char>>();
183 testNoncopyableMap<int32_t, int64_t>();
184 testNoncopyableMap<int32_t, int64_t, MmapAllocator<char>>();
186 TEST(Aha, InsertErase_i32_str) {
187 testMap<int32_t, string>();
188 testMap<int32_t, string, MmapAllocator<char>>();
190 TEST(Aha, InsertErase_i64_str) {
191 testMap<int64_t, string>();
192 testMap<int64_t, string, MmapAllocator<char>>();