1 //===-- VMRangeTest.cpp ---------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "gtest/gtest.h"
13 #include "lldb/Utility/VMRange.h"
15 using namespace lldb_private
;
17 namespace lldb_private
{
18 void PrintTo(const VMRange
&v
, std::ostream
*os
) {
19 (*os
) << "VMRange(" << v
.GetBaseAddress() << ", " << v
.GetEndAddress() << ")";
21 } // namespace lldb_private
23 TEST(VMRange
, IsValid
) {
25 EXPECT_FALSE(range
.IsValid());
27 range
.Reset(0x1, 0x100);
28 EXPECT_TRUE(range
.IsValid());
30 range
.Reset(0x1, 0x1);
31 EXPECT_FALSE(range
.IsValid());
34 TEST(VMRange
, Clear
) {
35 VMRange
range(0x100, 0x200);
36 EXPECT_NE(VMRange(), range
);
38 EXPECT_EQ(VMRange(), range
);
41 TEST(VMRange
, Comparison
) {
42 VMRange
range1(0x100, 0x200);
43 VMRange
range2(0x100, 0x200);
44 EXPECT_EQ(range1
, range2
);
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
);
52 EXPECT_NE(range1
, range2
);
55 TEST(VMRange
, Reset
) {
56 VMRange
range(0x100, 0x200);
57 EXPECT_FALSE(VMRange(0x200, 0x200) == range
);
58 range
.Reset(0x200, 0x200);
59 EXPECT_TRUE(VMRange(0x200, 0x200) == range
);
62 TEST(VMRange
, SetEndAddress
) {
63 VMRange
range(0x100, 0x200);
65 range
.SetEndAddress(0xFF);
66 EXPECT_EQ(0U, range
.GetByteSize());
67 EXPECT_FALSE(range
.IsValid());
69 range
.SetEndAddress(0x101);
70 EXPECT_EQ(1U, range
.GetByteSize());
71 EXPECT_TRUE(range
.IsValid());
74 TEST(VMRange
, ContainsAddr
) {
75 VMRange
range(0x100, 0x200);
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()));
88 TEST(VMRange
, ContainsRange
) {
89 VMRange
range(0x100, 0x200);
91 EXPECT_FALSE(range
.Contains(VMRange(0x0, 0x0)));
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)));
103 range
.Contains(VMRange(0x105, std::numeric_limits
<lldb::addr_t
>::max())));
106 EXPECT_TRUE(range
.Contains(VMRange(0x100, 0x100)));
109 EXPECT_FALSE(range
.Contains(VMRange(0x0, 0x0)));
112 TEST(VMRange
, Ordering
) {
113 VMRange
range1(0x44, 0x200);
114 VMRange
range2(0x100, 0x1FF);
115 VMRange
range3(0x100, 0x200);
117 EXPECT_LE(range1
, range1
);
118 EXPECT_GE(range1
, range1
);
120 EXPECT_LT(range1
, range2
);
121 EXPECT_LT(range2
, range3
);
123 EXPECT_GT(range2
, range1
);
124 EXPECT_GT(range3
, range2
);
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
);
131 EXPECT_FALSE(range1
> range1
);
132 EXPECT_FALSE(range2
> range2
);
133 EXPECT_FALSE(range3
> range3
);
136 TEST(VMRange
, CollectionContains
) {
137 VMRange::collection collection
= {VMRange(0x100, 0x105),
138 VMRange(0x108, 0x110)};
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));
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)));
149 // TODO: Implement and test ContainsRange with values that span multiple
150 // ranges in the collection.