1 //===-- sanitizer_addrhashmap_test.cpp ------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
8 #include "sanitizer_common/sanitizer_addrhashmap.h"
10 #include <unordered_map>
12 #include "gtest/gtest.h"
14 namespace __sanitizer
{
18 inline bool operator==(const Value
& rhs
) const {
19 return payload
== rhs
.payload
;
23 using MapTy
= AddrHashMap
<Value
, 11>;
24 using HandleTy
= MapTy::Handle
;
25 using RefMapTy
= std::unordered_map
<uptr
, Value
>;
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
);
35 TEST(AddrHashMap
, Basic
) {
36 // Use a reference implementation to compare with.
37 RefMapTy reference_map
{
45 for (const auto& key_val
: reference_map
) {
46 const uptr key
= key_val
.first
;
47 const Value val
= key_val
.second
;
49 // Insert all the elements.
52 ASSERT_TRUE(h
.created());
53 h
->payload
= val
.payload
;
57 // Now check that all the elements are present.
58 m
.ForEach(ExistsInReferenceMap
, &reference_map
);
59 EXPECT_TRUE(reference_map
.empty());
62 } // namespace __sanitizer