RangeMapTest.cpp
4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//===-- RangeTest.cpp -----------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Utility/RangeMap.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using namespace lldb_private;
using RangeDataVectorT = RangeDataVector<uint32_t, uint32_t, uint32_t>;
using EntryT = RangeDataVectorT::Entry;
static testing::Matcher<const EntryT *> EntryIs(uint32_t ID) {
return testing::Pointee(testing::Field(&EntryT::data, ID));
}
std::vector<uint32_t> FindEntryIndexes(uint32_t address, RangeDataVectorT map) {
std::vector<uint32_t> result;
map.FindEntryIndexesThatContain(address, result);
return result;
}
TEST(RangeDataVector, FindEntryThatContains) {
RangeDataVectorT Map;
uint32_t NextID = 0;
Map.Append(EntryT(0, 10, NextID++));
Map.Append(EntryT(10, 10, NextID++));
Map.Append(EntryT(20, 10, NextID++));
Map.Sort();
EXPECT_THAT(Map.FindEntryThatContains(0), EntryIs(0));
EXPECT_THAT(Map.FindEntryThatContains(9), EntryIs(0));
EXPECT_THAT(Map.FindEntryThatContains(10), EntryIs(1));
EXPECT_THAT(Map.FindEntryThatContains(19), EntryIs(1));
EXPECT_THAT(Map.FindEntryThatContains(20), EntryIs(2));
EXPECT_THAT(Map.FindEntryThatContains(29), EntryIs(2));
EXPECT_THAT(Map.FindEntryThatContains(30), nullptr);
}
TEST(RangeDataVector, FindEntryThatContains_Overlap) {
RangeDataVectorT Map;
uint32_t NextID = 0;
Map.Append(EntryT(0, 40, NextID++));
Map.Append(EntryT(10, 20, NextID++));
Map.Append(EntryT(20, 10, NextID++));
Map.Sort();
// With overlapping intervals, the intention seems to be to return the first
// interval which contains the address.
EXPECT_THAT(Map.FindEntryThatContains(25), EntryIs(0));
// However, this does not always succeed.
// TODO: This should probably return the range (0, 40) as well.
EXPECT_THAT(Map.FindEntryThatContains(35), nullptr);
}
TEST(RangeDataVector, CustomSort) {
// First the default ascending order sorting of the data field.
auto Map = RangeDataVectorT();
Map.Append(EntryT(0, 10, 50));
Map.Append(EntryT(0, 10, 52));
Map.Append(EntryT(0, 10, 53));
Map.Append(EntryT(0, 10, 51));
Map.Sort();
EXPECT_THAT(Map.GetSize(), 4);
EXPECT_THAT(Map.GetEntryRef(0).data, 50);
EXPECT_THAT(Map.GetEntryRef(1).data, 51);
EXPECT_THAT(Map.GetEntryRef(2).data, 52);
EXPECT_THAT(Map.GetEntryRef(3).data, 53);
// And then a custom descending order sorting of the data field.
class CtorParam {};
class CustomSort {
public:
CustomSort(CtorParam) {}
bool operator()(const uint32_t a_data, const uint32_t b_data) {
return a_data > b_data;
}
};
using RangeDataVectorCustomSortT =
RangeDataVector<uint32_t, uint32_t, uint32_t, 0, CustomSort>;
using EntryT = RangeDataVectorT::Entry;
auto MapC = RangeDataVectorCustomSortT(CtorParam());
MapC.Append(EntryT(0, 10, 50));
MapC.Append(EntryT(0, 10, 52));
MapC.Append(EntryT(0, 10, 53));
MapC.Append(EntryT(0, 10, 51));
MapC.Sort();
EXPECT_THAT(MapC.GetSize(), 4);
EXPECT_THAT(MapC.GetEntryRef(0).data, 53);
EXPECT_THAT(MapC.GetEntryRef(1).data, 52);
EXPECT_THAT(MapC.GetEntryRef(2).data, 51);
EXPECT_THAT(MapC.GetEntryRef(3).data, 50);
}
TEST(RangeDataVector, FindEntryIndexesThatContain) {
RangeDataVectorT Map;
Map.Append(EntryT(0, 10, 10));
Map.Append(EntryT(10, 10, 11));
Map.Append(EntryT(20, 10, 12));
Map.Sort();
EXPECT_THAT(FindEntryIndexes(0, Map), testing::ElementsAre(10));
EXPECT_THAT(FindEntryIndexes(9, Map), testing::ElementsAre(10));
EXPECT_THAT(FindEntryIndexes(10, Map), testing::ElementsAre(11));
EXPECT_THAT(FindEntryIndexes(19, Map), testing::ElementsAre(11));
EXPECT_THAT(FindEntryIndexes(20, Map), testing::ElementsAre(12));
EXPECT_THAT(FindEntryIndexes(29, Map), testing::ElementsAre(12));
EXPECT_THAT(FindEntryIndexes(30, Map), testing::ElementsAre());
}
TEST(RangeDataVector, FindEntryIndexesThatContain_Overlap) {
RangeDataVectorT Map;
Map.Append(EntryT(0, 40, 10));
Map.Append(EntryT(10, 20, 11));
Map.Append(EntryT(20, 10, 12));
Map.Sort();
EXPECT_THAT(FindEntryIndexes(0, Map), testing::ElementsAre(10));
EXPECT_THAT(FindEntryIndexes(9, Map), testing::ElementsAre(10));
EXPECT_THAT(FindEntryIndexes(10, Map), testing::ElementsAre(10, 11));
EXPECT_THAT(FindEntryIndexes(19, Map), testing::ElementsAre(10, 11));
EXPECT_THAT(FindEntryIndexes(20, Map), testing::ElementsAre(10, 11, 12));
EXPECT_THAT(FindEntryIndexes(29, Map), testing::ElementsAre(10, 11, 12));
EXPECT_THAT(FindEntryIndexes(30, Map), testing::ElementsAre(10));
EXPECT_THAT(FindEntryIndexes(39, Map), testing::ElementsAre(10));
EXPECT_THAT(FindEntryIndexes(40, Map), testing::ElementsAre());
}