reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    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
//===-- VMRangeTest.cpp -----------------------------------------*- C++ -*-===//
//
// 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 "gtest/gtest.h"

#include <limits>

#include "lldb/Utility/VMRange.h"

using namespace lldb_private;

namespace lldb_private {
void PrintTo(const VMRange &v, std::ostream *os) {
  (*os) << "VMRange(" << v.GetBaseAddress() << ", " << v.GetEndAddress() << ")";
}
} // namespace lldb_private

TEST(VMRange, IsValid) {
  VMRange range;
  EXPECT_FALSE(range.IsValid());

  range.Reset(0x1, 0x100);
  EXPECT_TRUE(range.IsValid());

  range.Reset(0x1, 0x1);
  EXPECT_FALSE(range.IsValid());
}

TEST(VMRange, Clear) {
  VMRange range(0x100, 0x200);
  EXPECT_NE(VMRange(), range);
  range.Clear();
  EXPECT_EQ(VMRange(), range);
}

TEST(VMRange, Comparison) {
  VMRange range1(0x100, 0x200);
  VMRange range2(0x100, 0x200);
  EXPECT_EQ(range1, range2);

  EXPECT_NE(VMRange(0x100, 0x1ff), range1);
  EXPECT_NE(VMRange(0x100, 0x201), range1);
  EXPECT_NE(VMRange(0x0ff, 0x200), range1);
  EXPECT_NE(VMRange(0x101, 0x200), range1);

  range2.Clear();
  EXPECT_NE(range1, range2);
}

TEST(VMRange, Reset) {
  VMRange range(0x100, 0x200);
  EXPECT_FALSE(VMRange(0x200, 0x200) == range);
  range.Reset(0x200, 0x200);
  EXPECT_TRUE(VMRange(0x200, 0x200) == range);
}

TEST(VMRange, SetEndAddress) {
  VMRange range(0x100, 0x200);

  range.SetEndAddress(0xFF);
  EXPECT_EQ(0U, range.GetByteSize());
  EXPECT_FALSE(range.IsValid());

  range.SetEndAddress(0x101);
  EXPECT_EQ(1U, range.GetByteSize());
  EXPECT_TRUE(range.IsValid());
}

TEST(VMRange, ContainsAddr) {
  VMRange range(0x100, 0x200);

  EXPECT_FALSE(range.Contains(0x00));
  EXPECT_FALSE(range.Contains(0xFF));
  EXPECT_TRUE(range.Contains(0x100));
  EXPECT_TRUE(range.Contains(0x101));
  EXPECT_TRUE(range.Contains(0x1FF));
  EXPECT_FALSE(range.Contains(0x200));
  EXPECT_FALSE(range.Contains(0x201));
  EXPECT_FALSE(range.Contains(0xFFF));
  EXPECT_FALSE(range.Contains(std::numeric_limits<lldb::addr_t>::max()));
}

TEST(VMRange, ContainsRange) {
  VMRange range(0x100, 0x200);

  EXPECT_FALSE(range.Contains(VMRange(0x0, 0x0)));

  EXPECT_FALSE(range.Contains(VMRange(0x0, 0x100)));
  EXPECT_FALSE(range.Contains(VMRange(0x0, 0x101)));
  EXPECT_TRUE(range.Contains(VMRange(0x100, 0x105)));
  EXPECT_TRUE(range.Contains(VMRange(0x101, 0x105)));
  EXPECT_TRUE(range.Contains(VMRange(0x100, 0x1FF)));
  EXPECT_TRUE(range.Contains(VMRange(0x105, 0x200)));
  EXPECT_FALSE(range.Contains(VMRange(0x105, 0x201)));
  EXPECT_FALSE(range.Contains(VMRange(0x200, 0x201)));
  EXPECT_TRUE(range.Contains(VMRange(0x100, 0x200)));
  EXPECT_FALSE(
      range.Contains(VMRange(0x105, std::numeric_limits<lldb::addr_t>::max())));

  // Empty range.
  EXPECT_TRUE(range.Contains(VMRange(0x100, 0x100)));

  range.Clear();
  EXPECT_FALSE(range.Contains(VMRange(0x0, 0x0)));
}

TEST(VMRange, Ordering) {
  VMRange range1(0x44, 0x200);
  VMRange range2(0x100, 0x1FF);
  VMRange range3(0x100, 0x200);

  EXPECT_LE(range1, range1);
  EXPECT_GE(range1, range1);

  EXPECT_LT(range1, range2);
  EXPECT_LT(range2, range3);

  EXPECT_GT(range2, range1);
  EXPECT_GT(range3, range2);

  // Ensure that < and > are always false when comparing ranges with themselves.
  EXPECT_FALSE(range1 < range1);
  EXPECT_FALSE(range2 < range2);
  EXPECT_FALSE(range3 < range3);

  EXPECT_FALSE(range1 > range1);
  EXPECT_FALSE(range2 > range2);
  EXPECT_FALSE(range3 > range3);
}

TEST(VMRange, CollectionContains) {
  VMRange::collection collection = {VMRange(0x100, 0x105),
                                    VMRange(0x108, 0x110)};

  EXPECT_FALSE(VMRange::ContainsValue(collection, 0xFF));
  EXPECT_TRUE(VMRange::ContainsValue(collection, 0x100));
  EXPECT_FALSE(VMRange::ContainsValue(collection, 0x105));
  EXPECT_TRUE(VMRange::ContainsValue(collection, 0x109));

  EXPECT_TRUE(VMRange::ContainsRange(collection, VMRange(0x100, 0x104)));
  EXPECT_TRUE(VMRange::ContainsRange(collection, VMRange(0x108, 0x100)));
  EXPECT_FALSE(VMRange::ContainsRange(collection, VMRange(0xFF, 0x100)));

  // TODO: Implement and test ContainsRange with values that span multiple
  // ranges in the collection.
}