| 1 |
//===-- sanitizer_addrhashmap_test.cpp ------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
#include "sanitizer_common/sanitizer_addrhashmap.h" |
| 9 |
|
| 10 |
#include <unordered_map> |
| 11 |
|
| 12 |
#include "gtest/gtest.h" |
| 13 |
|
| 14 |
namespace __sanitizer { |
| 15 |
|
| 16 |
struct Value { |
| 17 |
int payload; |
| 18 |
inline bool operator==(const Value& rhs) const { |
| 19 |
return payload == rhs.payload; |
| 20 |
} |
| 21 |
}; |
| 22 |
|
| 23 |
using MapTy = AddrHashMap<Value, 11>; |
| 24 |
using HandleTy = MapTy::Handle; |
| 25 |
using RefMapTy = std::unordered_map<uptr, Value>; |
| 26 |
|
| 27 |
static void ExistsInReferenceMap(const uptr key, const Value& val, void* arg) { |
| 28 |
RefMapTy* ref = reinterpret_cast<RefMapTy*>(arg); |
| 29 |
const RefMapTy::iterator iter = ref->find(key); |
| 30 |
ASSERT_NE(iter, ref->end()); |
| 31 |
EXPECT_EQ(iter->second, val); |
| 32 |
ref->erase(iter); |
| 33 |
} |
| 34 |
|
| 35 |
TEST(AddrHashMap, Basic) { |
| 36 |
// Use a reference implementation to compare with. |
| 37 |
RefMapTy reference_map{ |
| 38 |
{0x1000, {1}}, |
| 39 |
{0x2000, {2}}, |
| 40 |
{0x3000, {3}}, |
| 41 |
}; |
| 42 |
|
| 43 |
MapTy m; |
| 44 |
|
| 45 |
for (const auto& key_val : reference_map) { |
| 46 |
const uptr key = key_val.first; |
| 47 |
const Value val = key_val.second; |
| 48 |
|
| 49 |
// Insert all the elements. |
| 50 |
{ |
| 51 |
HandleTy h(&m, key); |
| 52 |
ASSERT_TRUE(h.created()); |
| 53 |
h->payload = val.payload; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// Now check that all the elements are present. |
| 58 |
m.ForEach(ExistsInReferenceMap, &reference_map); |
| 59 |
EXPECT_TRUE(reference_map.empty()); |
| 60 |
} |
| 61 |
|
| 62 |
} // namespace __sanitizer |