1//===-- VMRangeTest.cpp ---------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "gtest/gtest.h"
10
11#include <limits>
12
13#include "lldb/Utility/VMRange.h"
14
15using namespace lldb_private;
16
17namespace lldb_private {
18void PrintTo(const VMRange &v, std::ostream *os) {
19 (*os) << "VMRange(" << v.GetBaseAddress() << ", " << v.GetEndAddress() << ")";
20}
21} // namespace lldb_private
22
23TEST(VMRange, IsValid) {
24 VMRange range;
25 EXPECT_FALSE(range.IsValid());
26
27 range.Reset(start_addr: 0x1, end_addr: 0x100);
28 EXPECT_TRUE(range.IsValid());
29
30 range.Reset(start_addr: 0x1, end_addr: 0x1);
31 EXPECT_FALSE(range.IsValid());
32}
33
34TEST(VMRange, Clear) {
35 VMRange range(0x100, 0x200);
36 EXPECT_NE(VMRange(), range);
37 range.Clear();
38 EXPECT_EQ(VMRange(), range);
39}
40
41TEST(VMRange, Comparison) {
42 VMRange range1(0x100, 0x200);
43 VMRange range2(0x100, 0x200);
44 EXPECT_EQ(range1, range2);
45
46 EXPECT_NE(VMRange(0x100, 0x1ff), range1);
47 EXPECT_NE(VMRange(0x100, 0x201), range1);
48 EXPECT_NE(VMRange(0x0ff, 0x200), range1);
49 EXPECT_NE(VMRange(0x101, 0x200), range1);
50
51 range2.Clear();
52 EXPECT_NE(range1, range2);
53}
54
55TEST(VMRange, Reset) {
56 VMRange range(0x100, 0x200);
57 EXPECT_FALSE(VMRange(0x200, 0x200) == range);
58 range.Reset(start_addr: 0x200, end_addr: 0x200);
59 EXPECT_TRUE(VMRange(0x200, 0x200) == range);
60}
61
62TEST(VMRange, SetEndAddress) {
63 VMRange range(0x100, 0x200);
64
65 range.SetEndAddress(0xFF);
66 EXPECT_EQ(0U, range.GetByteSize());
67 EXPECT_FALSE(range.IsValid());
68
69 range.SetEndAddress(0x101);
70 EXPECT_EQ(1U, range.GetByteSize());
71 EXPECT_TRUE(range.IsValid());
72}
73
74TEST(VMRange, ContainsAddr) {
75 VMRange range(0x100, 0x200);
76
77 EXPECT_FALSE(range.Contains(0x00));
78 EXPECT_FALSE(range.Contains(0xFF));
79 EXPECT_TRUE(range.Contains(0x100));
80 EXPECT_TRUE(range.Contains(0x101));
81 EXPECT_TRUE(range.Contains(0x1FF));
82 EXPECT_FALSE(range.Contains(0x200));
83 EXPECT_FALSE(range.Contains(0x201));
84 EXPECT_FALSE(range.Contains(0xFFF));
85 EXPECT_FALSE(range.Contains(std::numeric_limits<lldb::addr_t>::max()));
86}
87
88TEST(VMRange, ContainsRange) {
89 VMRange range(0x100, 0x200);
90
91 EXPECT_FALSE(range.Contains(VMRange(0x0, 0x0)));
92
93 EXPECT_FALSE(range.Contains(VMRange(0x0, 0x100)));
94 EXPECT_FALSE(range.Contains(VMRange(0x0, 0x101)));
95 EXPECT_TRUE(range.Contains(VMRange(0x100, 0x105)));
96 EXPECT_TRUE(range.Contains(VMRange(0x101, 0x105)));
97 EXPECT_TRUE(range.Contains(VMRange(0x100, 0x1FF)));
98 EXPECT_TRUE(range.Contains(VMRange(0x105, 0x200)));
99 EXPECT_FALSE(range.Contains(VMRange(0x105, 0x201)));
100 EXPECT_FALSE(range.Contains(VMRange(0x200, 0x201)));
101 EXPECT_TRUE(range.Contains(VMRange(0x100, 0x200)));
102 EXPECT_FALSE(
103 range.Contains(VMRange(0x105, std::numeric_limits<lldb::addr_t>::max())));
104
105 // Empty range.
106 EXPECT_TRUE(range.Contains(VMRange(0x100, 0x100)));
107
108 range.Clear();
109 EXPECT_FALSE(range.Contains(VMRange(0x0, 0x0)));
110}
111
112TEST(VMRange, Ordering) {
113 VMRange range1(0x44, 0x200);
114 VMRange range2(0x100, 0x1FF);
115 VMRange range3(0x100, 0x200);
116
117 EXPECT_LE(range1, range1);
118 EXPECT_GE(range1, range1);
119
120 EXPECT_LT(range1, range2);
121 EXPECT_LT(range2, range3);
122
123 EXPECT_GT(range2, range1);
124 EXPECT_GT(range3, range2);
125
126 // Ensure that < and > are always false when comparing ranges with themselves.
127 EXPECT_FALSE(range1 < range1);
128 EXPECT_FALSE(range2 < range2);
129 EXPECT_FALSE(range3 < range3);
130
131 EXPECT_FALSE(range1 > range1);
132 EXPECT_FALSE(range2 > range2);
133 EXPECT_FALSE(range3 > range3);
134}
135
136TEST(VMRange, CollectionContains) {
137 VMRange::collection collection = {VMRange(0x100, 0x105),
138 VMRange(0x108, 0x110)};
139
140 EXPECT_FALSE(VMRange::ContainsValue(collection, 0xFF));
141 EXPECT_TRUE(VMRange::ContainsValue(collection, 0x100));
142 EXPECT_FALSE(VMRange::ContainsValue(collection, 0x105));
143 EXPECT_TRUE(VMRange::ContainsValue(collection, 0x109));
144
145 EXPECT_TRUE(VMRange::ContainsRange(collection, VMRange(0x100, 0x104)));
146 EXPECT_TRUE(VMRange::ContainsRange(collection, VMRange(0x108, 0x100)));
147 EXPECT_FALSE(VMRange::ContainsRange(collection, VMRange(0xFF, 0x100)));
148
149 // TODO: Implement and test ContainsRange with values that span multiple
150 // ranges in the collection.
151}
152

source code of lldb/unittests/Utility/VMRangeTest.cpp