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
|
//===-- interval_map_test.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
//
//===----------------------------------------------------------------------===//
//
// This file is a part of the ORC runtime.
//
//===----------------------------------------------------------------------===//
#include "interval_map.h"
#include "gtest/gtest.h"
using namespace orc_rt;
TEST(IntervalMapTest, DefaultConstructed) {
// Check that a default-constructed IntervalMap behaves as expected.
IntervalMap<unsigned, unsigned, IntervalCoalescing::Enabled> M;
EXPECT_TRUE(M.empty());
EXPECT_TRUE(M.begin() == M.end());
EXPECT_TRUE(M.find(0) == M.end());
}
TEST(IntervalMapTest, InsertSingleElement) {
// Check that a map with a single element inserted behaves as expected.
IntervalMap<unsigned, unsigned, IntervalCoalescing::Enabled> M;
M.insert(7, 8, 42);
EXPECT_FALSE(M.empty());
EXPECT_EQ(std::next(M.begin()), M.end());
EXPECT_EQ(M.find(7), M.begin());
EXPECT_EQ(M.find(8), M.end());
EXPECT_EQ(M.lookup(7), 42U);
EXPECT_EQ(M.lookup(8), 0U); // 8 not present, so should return unsigned().
}
TEST(IntervalMapTest, InsertCoalesceWithPrevious) {
// Check that insertions coalesce with previous ranges that share the same
// value. Also check that they _don't_ coalesce if the values are different.
// Check that insertion coalesces with previous range when values are equal.
IntervalMap<unsigned, unsigned, IntervalCoalescing::Enabled> M1;
M1.insert(7, 8, 42);
M1.insert(8, 9, 42);
EXPECT_FALSE(M1.empty());
EXPECT_EQ(std::next(M1.begin()), M1.end()); // Should see just one range.
EXPECT_EQ(M1.find(7), M1.find(8)); // 7 and 8 should point to same range.
EXPECT_EQ(M1.lookup(7), 42U); // Value should be preserved.
// Check that insertion does not coalesce with previous range when values are
// not equal.
IntervalMap<unsigned, unsigned, IntervalCoalescing::Enabled> M2;
M2.insert(7, 8, 42);
M2.insert(8, 9, 7);
EXPECT_FALSE(M2.empty());
EXPECT_EQ(std::next(std::next(M2.begin())), M2.end()); // Expect two ranges.
EXPECT_NE(M2.find(7), M2.find(8)); // 7 and 8 should be different ranges.
EXPECT_EQ(M2.lookup(7), 42U); // Keys 7 and 8 should map to different values.
EXPECT_EQ(M2.lookup(8), 7U);
}
TEST(IntervalMapTest, InsertCoalesceWithFollowing) {
// Check that insertions coalesce with following ranges that share the same
// value. Also check that they _don't_ coalesce if the values are different.
// Check that insertion coalesces with following range when values are equal.
IntervalMap<unsigned, unsigned, IntervalCoalescing::Enabled> M1;
M1.insert(8, 9, 42);
M1.insert(7, 8, 42);
EXPECT_FALSE(M1.empty());
EXPECT_EQ(std::next(M1.begin()), M1.end()); // Should see just one range.
EXPECT_EQ(M1.find(7), M1.find(8)); // 7 and 8 should point to same range.
EXPECT_EQ(M1.lookup(7), 42U); // Value should be preserved.
// Check that insertion does not coalesce with previous range when values are
// not equal.
IntervalMap<unsigned, unsigned, IntervalCoalescing::Enabled> M2;
M2.insert(8, 9, 42);
M2.insert(7, 8, 7);
EXPECT_FALSE(M2.empty());
EXPECT_EQ(std::next(std::next(M2.begin())), M2.end()); // Expect two ranges.
EXPECT_EQ(M2.lookup(7), 7U); // Keys 7 and 8 should map to different values.
EXPECT_EQ(M2.lookup(8), 42U);
}
TEST(IntervalMapTest, InsertCoalesceBoth) {
// Check that insertions coalesce with ranges on both sides where posssible.
// Also check that they _don't_ coalesce if the values are different.
// Check that insertion coalesces with both previous and following ranges
// when values are equal.
IntervalMap<unsigned, unsigned, IntervalCoalescing::Enabled> M1;
M1.insert(7, 8, 42);
M1.insert(9, 10, 42);
// Check no coalescing yet.
EXPECT_NE(M1.find(7), M1.find(9));
// Insert a 3rd range to trigger coalescing on both sides.
M1.insert(8, 9, 42);
EXPECT_FALSE(M1.empty());
EXPECT_EQ(std::next(M1.begin()), M1.end()); // Should see just one range.
EXPECT_EQ(M1.find(7), M1.find(8)); // 7, 8, and 9 should point to same range.
EXPECT_EQ(M1.find(8), M1.find(9));
EXPECT_EQ(M1.lookup(7), 42U); // Value should be preserved.
// Check that insertion does not coalesce with previous range when values are
// not equal.
IntervalMap<unsigned, unsigned, IntervalCoalescing::Enabled> M2;
M2.insert(7, 8, 42);
M2.insert(8, 9, 7);
M2.insert(9, 10, 42);
EXPECT_FALSE(M2.empty());
// Expect three ranges.
EXPECT_EQ(std::next(std::next(std::next(M2.begin()))), M2.end());
EXPECT_NE(M2.find(7), M2.find(8)); // All keys should map to different ranges.
EXPECT_NE(M2.find(8), M2.find(9));
EXPECT_EQ(M2.lookup(7), 42U); // Key 7, 8, and 9 should map to different vals.
EXPECT_EQ(M2.lookup(8), 7U);
EXPECT_EQ(M2.lookup(9), 42U);
}
TEST(IntervalMapTest, EraseSingleElement) {
// Check that we can insert and then remove a single range.
IntervalMap<unsigned, unsigned, IntervalCoalescing::Enabled> M;
M.insert(7, 10, 42);
EXPECT_FALSE(M.empty());
M.erase(7, 10);
EXPECT_TRUE(M.empty());
}
TEST(IntervalMapTest, EraseSplittingLeft) {
// Check that removal of a trailing subrange succeeds, but leaves the
// residual range in-place.
IntervalMap<unsigned, unsigned, IntervalCoalescing::Enabled> M;
M.insert(7, 10, 42);
EXPECT_FALSE(M.empty());
M.erase(9, 10);
EXPECT_EQ(std::next(M.begin()), M.end());
EXPECT_EQ(M.begin()->first.first, 7U);
EXPECT_EQ(M.begin()->first.second, 9U);
}
TEST(IntervalMapTest, EraseSplittingRight) {
// Check that removal of a leading subrange succeeds, but leaves the
// residual range in-place.
IntervalMap<unsigned, unsigned, IntervalCoalescing::Enabled> M;
M.insert(7, 10, 42);
EXPECT_FALSE(M.empty());
M.erase(7, 8);
EXPECT_EQ(std::next(M.begin()), M.end());
EXPECT_EQ(M.begin()->first.first, 8U);
EXPECT_EQ(M.begin()->first.second, 10U);
}
TEST(IntervalMapTest, EraseSplittingBoth) {
// Check that removal of an interior subrange leaves both the leading and
// trailing residual subranges in-place.
IntervalMap<unsigned, unsigned, IntervalCoalescing::Enabled> M;
M.insert(7, 10, 42);
EXPECT_FALSE(M.empty());
M.erase(8, 9);
EXPECT_EQ(std::next(std::next(M.begin())), M.end());
EXPECT_EQ(M.begin()->first.first, 7U);
EXPECT_EQ(M.begin()->first.second, 8U);
EXPECT_EQ(std::next(M.begin())->first.first, 9U);
EXPECT_EQ(std::next(M.begin())->first.second, 10U);
}
TEST(IntervalMapTest, NonCoalescingMapPermitsNonComparableKeys) {
// Test that values that can't be equality-compared are still usable when
// coalescing is disabled and behave as expected.
struct S {}; // Struct with no equality comparison.
IntervalMap<unsigned, S, IntervalCoalescing::Disabled> M;
M.insert(7, 8, S());
EXPECT_FALSE(M.empty());
EXPECT_EQ(std::next(M.begin()), M.end());
EXPECT_EQ(M.find(7), M.begin());
EXPECT_EQ(M.find(8), M.end());
}
|