1 /**
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "util/addr_map.h"
17 #include "util/range.h"
18
19 #include "util/tests/verifier_test.h"
20
21 #include <gtest/gtest.h>
22
23 namespace ark::verifier::test {
24
25 // NOLINTBEGIN(readability-magic-numbers)
26
TEST_F(VerifierTest,AddrMap)27 TEST_F(VerifierTest, AddrMap)
28 {
29 // NOLINTNEXTLINE(modernize-avoid-c-arrays)
30 char mem[123U] = {};
31 AddrMap amap1 {&mem[0], &mem[122U]};
32 AddrMap amap2 {&mem[0], &mem[122U]};
33 amap1.Mark(&mem[50U], &mem[60U]);
34 EXPECT_TRUE(amap1.HasMark(&mem[50U]));
35 EXPECT_TRUE(amap1.HasMark(&mem[60U]));
36 EXPECT_FALSE(amap1.HasMark(&mem[49U]));
37 EXPECT_FALSE(amap1.HasMark(&mem[61U]));
38 amap2.Mark(&mem[70U], &mem[90U]);
39 EXPECT_FALSE(amap1.HasCommonMarks(amap2));
40 amap2.Mark(&mem[60U]);
41 char *ptr;
42 EXPECT_TRUE(amap1.GetFirstCommonMark(amap2, &ptr));
43 EXPECT_EQ(ptr, &mem[60U]);
44
45 PandaVector<const char *> ptrs;
46 amap1.Clear();
47 amap1.Mark(&mem[48U]);
48 amap1.Mark(&mem[61U]);
49 amap1.Mark(&mem[50U]);
50 amap1.Mark(&mem[60U]);
51 amap1.EnumerateMarksInScope<const char *>(&mem[49U], &mem[60U], [&ptrs](const char *addr) {
52 ptrs.push_back(addr);
53 return true;
54 });
55
56 EXPECT_EQ(ptrs.size(), 1);
57 EXPECT_EQ(ptrs[0], reinterpret_cast<const char *>(&mem[50U]));
58 }
59
60 // NOLINTEND(readability-magic-numbers)
61
62 } // namespace ark::verifier::test
63