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