1 /**
2 * Copyright (c) 2021-2022 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 panda::verifier::test {
24
TEST_F(VerifierTest,AddrMap)25 TEST_F(VerifierTest, AddrMap)
26 {
27 char mem[123];
28 AddrMap amap1 {&mem[0], &mem[122]};
29 AddrMap amap2 {&mem[0], &mem[122]};
30 amap1.Mark(&mem[50], &mem[60]);
31 EXPECT_TRUE(amap1.HasMark(&mem[50]));
32 EXPECT_TRUE(amap1.HasMark(&mem[60]));
33 EXPECT_FALSE(amap1.HasMark(&mem[49]));
34 EXPECT_FALSE(amap1.HasMark(&mem[61]));
35 amap2.Mark(&mem[70], &mem[90]);
36 EXPECT_FALSE(amap1.HasCommonMarks(amap2));
37 amap2.Mark(&mem[60]);
38 char *ptr;
39 EXPECT_TRUE(amap1.GetFirstCommonMark(amap2, &ptr));
40 EXPECT_EQ(ptr, &mem[60]);
41
42 PandaVector<const char *> ptrs;
43 amap1.Clear();
44 amap1.Mark(&mem[48]);
45 amap1.Mark(&mem[61]);
46 amap1.Mark(&mem[50]);
47 amap1.Mark(&mem[60]);
48 amap1.EnumerateMarksInScope<const char *>(&mem[49], &mem[60], [&ptrs](const char *addr) {
49 ptrs.push_back(addr);
50 return true;
51 });
52
53 EXPECT_EQ(ptrs.size(), 2);
54 EXPECT_EQ(ptrs[0], reinterpret_cast<const char *>(&mem[50]));
55 EXPECT_EQ(ptrs[1], reinterpret_cast<const char *>(&mem[60]));
56 }
57
58 } // namespace panda::verifier::test
59