File: RangeMapTest.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (217 lines) | stat: -rw-r--r-- 6,625 bytes parent folder | download | duplicates (9)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//===-- 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;

TEST(RangeVector, CombineConsecutiveRanges) {
  using RangeVector = RangeVector<uint32_t, uint32_t>;
  using Entry = RangeVector::Entry;

  RangeVector V;
  V.Append(0, 1);
  V.Append(5, 1);
  V.Append(6, 1);
  V.Append(10, 9);
  V.Append(15, 1);
  V.Append(20, 9);
  V.Append(21, 9);
  V.Sort();
  V.CombineConsecutiveRanges();
  EXPECT_THAT(V, testing::ElementsAre(Entry(0, 1), Entry(5, 2), Entry(10, 9),
                                      Entry(20, 10)));

  V.Clear();
  V.Append(0, 20);
  V.Append(5, 1);
  V.Append(10, 1);
  V.Sort();
  V.CombineConsecutiveRanges();
  EXPECT_THAT(V, testing::ElementsAre(Entry(0, 20)));
}

TEST(RangeVector, GetOverlaps) {
  using RangeVector = RangeVector<uint32_t, uint32_t>;

  RangeVector V1;
  RangeVector V2;
  RangeVector Expected;
  // same range
  V1.Append(0, 1);
  V2.Append(0, 1);
  Expected.Append(0, 1);

  // no overlap
  V1.Append(2, 2);
  V2.Append(4, 1);

  // same base overlap
  V1.Append(10, 5);
  V2.Append(10, 3);
  Expected.Append(10, 3);

  // same end overlap
  V1.Append(27, 1);
  V2.Append(20, 8);
  Expected.Append(27, 1);

  // smaller base overlap
  V1.Append(33, 4);
  V2.Append(30, 5);
  Expected.Append(33, 2);

  // larger base overlap
  V1.Append(46, 3);
  V2.Append(40, 7);
  Expected.Append(46, 1);

  // encompass 1 range
  V1.Append(50, 9);
  V2.Append(51, 7);
  Expected.Append(51, 7);

  // encompass 2 ranges
  V1.Append(60, 9);
  V2.Append(60, 3);
  V2.Append(65, 3);
  Expected.Append(60, 3);
  Expected.Append(65, 3);

  V1.Sort();
  V2.Sort();
  Expected.Sort();

  EXPECT_EQ(RangeVector::GetOverlaps(V1, V2), Expected);
  EXPECT_EQ(RangeVector::GetOverlaps(V2, V1), Expected);
}

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());
}