CoalescingBitVectorTest.cpp 18.5 KB
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
//=== CoalescingBitVectorTest.cpp - CoalescingBitVector unit tests --------===//
//
// 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 "llvm/ADT/CoalescingBitVector.h"
#include "gtest/gtest.h"

using namespace llvm;

namespace {

using UBitVec = CoalescingBitVector<unsigned>;
using U64BitVec = CoalescingBitVector<uint64_t>;

bool elementsMatch(const UBitVec &BV, std::initializer_list<unsigned> List) {
  if (!std::equal(BV.begin(), BV.end(), List.begin(), List.end())) {
    UBitVec::Allocator Alloc;
    UBitVec Expected(Alloc);
    Expected.set(List);
    dbgs() << "elementsMatch:\n"
           << "     Expected: ";
    Expected.print(dbgs());
    dbgs() << "          Got: ";
    BV.print(dbgs());
    return false;
  }
  return true;
}

bool rangesMatch(iterator_range<UBitVec::const_iterator> R,
                 std::initializer_list<unsigned> List) {
  return std::equal(R.begin(), R.end(), List.begin(), List.end());
}

TEST(CoalescingBitVectorTest, Set) {
  UBitVec::Allocator Alloc;
  UBitVec BV1(Alloc);
  UBitVec BV2(Alloc);

  BV1.set(0);
  EXPECT_TRUE(BV1.test(0));
  EXPECT_FALSE(BV1.test(1));

  BV2.set(BV1);
  EXPECT_TRUE(BV2.test(0));
}

TEST(CoalescingBitVectorTest, Count) {
  UBitVec::Allocator Alloc;
  UBitVec BV(Alloc);
  EXPECT_EQ(BV.count(), 0u);
  BV.set(0);
  EXPECT_EQ(BV.count(), 1u);
  BV.set({11, 12, 13, 14, 15});
  EXPECT_EQ(BV.count(), 6u);
}

TEST(CoalescingBitVectorTest, ClearAndEmpty) {
  UBitVec::Allocator Alloc;
  UBitVec BV(Alloc);
  EXPECT_TRUE(BV.empty());
  BV.set(1);
  EXPECT_FALSE(BV.empty());
  BV.clear();
  EXPECT_TRUE(BV.empty());
}

TEST(CoalescingBitVector, Copy) {
  UBitVec::Allocator Alloc;
  UBitVec BV1(Alloc);
  BV1.set(0);
  UBitVec BV2 = BV1;
  EXPECT_TRUE(elementsMatch(BV1, {0}));
  EXPECT_TRUE(elementsMatch(BV2, {0}));
  BV2.set(5);
  BV2 = BV1;
  EXPECT_TRUE(elementsMatch(BV1, {0}));
  EXPECT_TRUE(elementsMatch(BV2, {0}));
}

TEST(CoalescingBitVectorTest, Iterators) {
  UBitVec::Allocator Alloc;
  UBitVec BV(Alloc);

  BV.set({0, 1, 2});

  auto It = BV.begin();
  EXPECT_TRUE(It == BV.begin());
  EXPECT_EQ(*It, 0u);
  ++It;
  EXPECT_EQ(*It, 1u);
  ++It;
  EXPECT_EQ(*It, 2u);
  ++It;
  EXPECT_TRUE(It == BV.end());
  EXPECT_TRUE(BV.end() == BV.end());

  It = BV.begin();
  EXPECT_TRUE(It == BV.begin());
  auto ItCopy = It++;
  EXPECT_TRUE(ItCopy == BV.begin());
  EXPECT_EQ(*ItCopy, 0u);
  EXPECT_EQ(*It, 1u);

  EXPECT_TRUE(elementsMatch(BV, {0, 1, 2}));

  BV.set({4, 5, 6});
  EXPECT_TRUE(elementsMatch(BV, {0, 1, 2, 4, 5, 6}));

  BV.set(3);
  EXPECT_TRUE(elementsMatch(BV, {0, 1, 2, 3, 4, 5, 6}));

  BV.set(10);
  EXPECT_TRUE(elementsMatch(BV, {0, 1, 2, 3, 4, 5, 6, 10}));

  // Should be able to reset unset bits.
  BV.reset(3);
  BV.reset(3);
  BV.reset(20000);
  BV.set({1000, 1001, 1002});
  EXPECT_TRUE(elementsMatch(BV, {0, 1, 2, 4, 5, 6, 10, 1000, 1001, 1002}));

  auto It1 = BV.begin();
  EXPECT_TRUE(It1 == BV.begin());
  EXPECT_TRUE(++It1 == ++BV.begin());
  EXPECT_TRUE(It1 != BV.begin());
  EXPECT_TRUE(It1 != BV.end());
}

TEST(CoalescingBitVectorTest, Reset) {
  UBitVec::Allocator Alloc;
  UBitVec BV(Alloc);

  BV.set(0);
  EXPECT_TRUE(BV.test(0));
  BV.reset(0);
  EXPECT_FALSE(BV.test(0));

  BV.clear();
  BV.set({1, 2, 3});
  BV.reset(1);
  EXPECT_TRUE(elementsMatch(BV, {2, 3}));

  BV.clear();
  BV.set({1, 2, 3});
  BV.reset(2);
  EXPECT_TRUE(elementsMatch(BV, {1, 3}));

  BV.clear();
  BV.set({1, 2, 3});
  BV.reset(3);
  EXPECT_TRUE(elementsMatch(BV, {1, 2}));
}

TEST(CoalescingBitVectorTest, Comparison) {
  UBitVec::Allocator Alloc;
  UBitVec BV1(Alloc);
  UBitVec BV2(Alloc);

  // Single interval.
  BV1.set({1, 2, 3});
  BV2.set({1, 2, 3});
  EXPECT_EQ(BV1, BV2);
  EXPECT_FALSE(BV1 != BV2);

  // Different number of intervals.
  BV1.clear();
  BV2.clear();
  BV1.set({1, 2, 3});
  EXPECT_NE(BV1, BV2);

  // Multiple intervals.
  BV1.clear();
  BV2.clear();
  BV1.set({1, 2, 11, 12});
  BV2.set({1, 2, 11, 12});
  EXPECT_EQ(BV1, BV2);
  BV2.reset(1);
  EXPECT_NE(BV1, BV2);
  BV2.set(1);
  BV2.reset(11);
  EXPECT_NE(BV1, BV2);
}

// A simple implementation of set union, used to double-check the human
// "expected" answer.
void simpleUnion(UBitVec &Union, const UBitVec &LHS,
                    const UBitVec &RHS) {
  for (unsigned Bit : LHS)
    Union.test_and_set(Bit);
  for (unsigned Bit : RHS)
    Union.test_and_set(Bit);
}

TEST(CoalescingBitVectorTest, Union) {
  UBitVec::Allocator Alloc;

  // Check that after doing LHS |= RHS, LHS == Expected.
  auto unionIs = [&](std::initializer_list<unsigned> LHS,
                     std::initializer_list<unsigned> RHS,
                     std::initializer_list<unsigned> Expected) {
    UBitVec BV1(Alloc);
    BV1.set(LHS);
    UBitVec BV2(Alloc);
    BV2.set(RHS);
    UBitVec DoubleCheckedExpected(Alloc);
    simpleUnion(DoubleCheckedExpected, BV1, BV2);
    ASSERT_TRUE(elementsMatch(DoubleCheckedExpected, Expected));
    BV1 |= BV2;
    ASSERT_TRUE(elementsMatch(BV1, Expected));
  };

  // Check that "LHS |= RHS" and "RHS |= LHS" both produce the expected result.
  auto testUnionSymmetrically = [&](std::initializer_list<unsigned> LHS,
                     std::initializer_list<unsigned> RHS,
                     std::initializer_list<unsigned> Expected) {
    unionIs(LHS, RHS, Expected);
    unionIs(RHS, LHS, Expected);
  };

  // Empty LHS.
  testUnionSymmetrically({}, {1, 2, 3}, {1, 2, 3});

  // Empty RHS.
  testUnionSymmetrically({1, 2, 3}, {}, {1, 2, 3});

  // Full overlap.
  testUnionSymmetrically({1}, {1}, {1});
  testUnionSymmetrically({1, 2, 11, 12}, {1, 2, 11, 12}, {1, 2, 11, 12});

  // Sliding window: fix {2, 3, 4} as the LHS, and slide a window before/after
  // it. Repeat this swapping LHS and RHS.
  testUnionSymmetrically({2, 3, 4}, {1, 2, 3}, {1, 2, 3, 4});
  testUnionSymmetrically({2, 3, 4}, {2, 3, 4}, {2, 3, 4});
  testUnionSymmetrically({2, 3, 4}, {3, 4, 5}, {2, 3, 4, 5});
  testUnionSymmetrically({1, 2, 3}, {2, 3, 4}, {1, 2, 3, 4});
  testUnionSymmetrically({3, 4, 5}, {2, 3, 4}, {2, 3, 4, 5});

  // Multiple overlaps, but at least one of the overlaps forces us to split an
  // interval (and possibly both do). For ease of understanding, fix LHS to be
  // {1, 2, 11, 12}, but vary RHS.
  testUnionSymmetrically({1, 2, 11, 12}, {1}, {1, 2, 11, 12});
  testUnionSymmetrically({1, 2, 11, 12}, {2}, {1, 2, 11, 12});
  testUnionSymmetrically({1, 2, 11, 12}, {11}, {1, 2, 11, 12});
  testUnionSymmetrically({1, 2, 11, 12}, {12}, {1, 2, 11, 12});
  testUnionSymmetrically({1, 2, 11, 12}, {1, 11}, {1, 2, 11, 12});
  testUnionSymmetrically({1, 2, 11, 12}, {1, 12}, {1, 2, 11, 12});
  testUnionSymmetrically({1, 2, 11, 12}, {2, 11}, {1, 2, 11, 12});
  testUnionSymmetrically({1, 2, 11, 12}, {2, 12}, {1, 2, 11, 12});
  testUnionSymmetrically({1, 2, 11, 12}, {1, 2, 11}, {1, 2, 11, 12});
  testUnionSymmetrically({1, 2, 11, 12}, {1, 2, 12}, {1, 2, 11, 12});
  testUnionSymmetrically({1, 2, 11, 12}, {1, 11, 12}, {1, 2, 11, 12});
  testUnionSymmetrically({1, 2, 11, 12}, {2, 11, 12}, {1, 2, 11, 12});
  testUnionSymmetrically({1, 2, 11, 12}, {0, 11, 12}, {0, 1, 2, 11, 12});
  testUnionSymmetrically({1, 2, 11, 12}, {3, 11, 12}, {1, 2, 3, 11, 12});
  testUnionSymmetrically({1, 2, 11, 12}, {1, 11, 13}, {1, 2, 11, 12, 13});
  testUnionSymmetrically({1, 2, 11, 12}, {1, 10, 11}, {1, 2, 10, 11, 12});

  // Partial overlap, but the existing interval covers future overlaps.
  testUnionSymmetrically({1, 2, 3, 4, 5, 6, 7, 8}, {2, 3, 4, 6, 7},
                         {1, 2, 3, 4, 5, 6, 7, 8});
  testUnionSymmetrically({1, 2, 3, 4, 5, 6, 7, 8}, {2, 3, 7, 8, 9},
                         {1, 2, 3, 4, 5, 6, 7, 8, 9});

  // More partial overlaps.
  testUnionSymmetrically({1, 2, 3, 4, 5}, {0, 1, 2, 4, 5, 6},
                         {0, 1, 2, 3, 4, 5, 6});
  testUnionSymmetrically({2, 3}, {1, 2, 3, 4}, {1, 2, 3, 4});
  testUnionSymmetrically({3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4});
  testUnionSymmetrically({1, 2}, {1, 2, 3, 4}, {1, 2, 3, 4});
  testUnionSymmetrically({0, 1}, {1, 2, 3, 4}, {0, 1, 2, 3, 4});

  // Merge non-overlapping.
  testUnionSymmetrically({0, 1}, {2, 3}, {0, 1, 2, 3});
  testUnionSymmetrically({0, 3}, {1, 2}, {0, 1, 2, 3});
}

// A simple implementation of set intersection, used to double-check the
// human "expected" answer.
void simpleIntersection(UBitVec &Intersection, const UBitVec &LHS,
                        const UBitVec &RHS) {
  for (unsigned Bit : LHS)
    if (RHS.test(Bit))
      Intersection.set(Bit);
}

TEST(CoalescingBitVectorTest, Intersection) {
  UBitVec::Allocator Alloc;

  // Check that after doing LHS &= RHS, LHS == Expected.
  auto intersectionIs = [&](std::initializer_list<unsigned> LHS,
                            std::initializer_list<unsigned> RHS,
                            std::initializer_list<unsigned> Expected) {
    UBitVec BV1(Alloc);
    BV1.set(LHS);
    UBitVec BV2(Alloc);
    BV2.set(RHS);
    UBitVec DoubleCheckedExpected(Alloc);
    simpleIntersection(DoubleCheckedExpected, BV1, BV2);
    ASSERT_TRUE(elementsMatch(DoubleCheckedExpected, Expected));
    BV1 &= BV2;
    ASSERT_TRUE(elementsMatch(BV1, Expected));
  };

  // Check that "LHS &= RHS" and "RHS &= LHS" both produce the expected result.
  auto testIntersectionSymmetrically = [&](std::initializer_list<unsigned> LHS,
                     std::initializer_list<unsigned> RHS,
                     std::initializer_list<unsigned> Expected) {
    intersectionIs(LHS, RHS, Expected);
    intersectionIs(RHS, LHS, Expected);
  };

  // Empty case, one-element case.
  testIntersectionSymmetrically({}, {}, {});
  testIntersectionSymmetrically({1}, {1}, {1});
  testIntersectionSymmetrically({1}, {2}, {});

  // Exact overlaps cases: single overlap and multiple overlaps.
  testIntersectionSymmetrically({1, 2}, {1, 2}, {1, 2});
  testIntersectionSymmetrically({1, 2, 11, 12}, {1, 2, 11, 12}, {1, 2, 11, 12});

  // Sliding window: fix {2, 3, 4} as the LHS, and slide a window before/after
  // it.
  testIntersectionSymmetrically({2, 3, 4}, {1, 2, 3}, {2, 3});
  testIntersectionSymmetrically({2, 3, 4}, {2, 3, 4}, {2, 3, 4});
  testIntersectionSymmetrically({2, 3, 4}, {3, 4, 5}, {3, 4});

  // No overlap, but we have multiple intervals.
  testIntersectionSymmetrically({1, 2, 11, 12}, {3, 4, 13, 14}, {});

  // Multiple overlaps, but at least one of the overlaps forces us to split an
  // interval (and possibly both do). For ease of understanding, fix LHS to be
  // {1, 2, 11, 12}, but vary RHS.
  testIntersectionSymmetrically({1, 2, 11, 12}, {1}, {1});
  testIntersectionSymmetrically({1, 2, 11, 12}, {2}, {2});
  testIntersectionSymmetrically({1, 2, 11, 12}, {11}, {11});
  testIntersectionSymmetrically({1, 2, 11, 12}, {12}, {12});
  testIntersectionSymmetrically({1, 2, 11, 12}, {1, 11}, {1, 11});
  testIntersectionSymmetrically({1, 2, 11, 12}, {1, 12}, {1, 12});
  testIntersectionSymmetrically({1, 2, 11, 12}, {2, 11}, {2, 11});
  testIntersectionSymmetrically({1, 2, 11, 12}, {2, 12}, {2, 12});
  testIntersectionSymmetrically({1, 2, 11, 12}, {1, 2, 11}, {1, 2, 11});
  testIntersectionSymmetrically({1, 2, 11, 12}, {1, 2, 12}, {1, 2, 12});
  testIntersectionSymmetrically({1, 2, 11, 12}, {1, 11, 12}, {1, 11, 12});
  testIntersectionSymmetrically({1, 2, 11, 12}, {2, 11, 12}, {2, 11, 12});
  testIntersectionSymmetrically({1, 2, 11, 12}, {0, 11, 12}, {11, 12});
  testIntersectionSymmetrically({1, 2, 11, 12}, {3, 11, 12}, {11, 12});
  testIntersectionSymmetrically({1, 2, 11, 12}, {1, 11, 13}, {1, 11});
  testIntersectionSymmetrically({1, 2, 11, 12}, {1, 10, 11}, {1, 11});

  // Partial overlap, but the existing interval covers future overlaps.
  testIntersectionSymmetrically({1, 2, 3, 4, 5, 6, 7, 8}, {2, 3, 4, 6, 7},
                                {2, 3, 4, 6, 7});
}

// A simple implementation of set intersection-with-complement, used to
// double-check the human "expected" answer.
void simpleIntersectionWithComplement(UBitVec &Intersection, const UBitVec &LHS,
                                      const UBitVec &RHS) {
  for (unsigned Bit : LHS)
    if (!RHS.test(Bit))
      Intersection.set(Bit);
}

TEST(CoalescingBitVectorTest, IntersectWithComplement) {
  UBitVec::Allocator Alloc;

  // Check that after doing LHS.intersectWithComplement(RHS), LHS == Expected.
  auto intersectionWithComplementIs =
      [&](std::initializer_list<unsigned> LHS,
          std::initializer_list<unsigned> RHS,
          std::initializer_list<unsigned> Expected) {
        UBitVec BV1(Alloc);
        BV1.set(LHS);
        UBitVec BV2(Alloc);
        BV2.set(RHS);
        UBitVec DoubleCheckedExpected(Alloc);
        simpleIntersectionWithComplement(DoubleCheckedExpected, BV1, BV2);
        ASSERT_TRUE(elementsMatch(DoubleCheckedExpected, Expected));
        BV1.intersectWithComplement(BV2);
        ASSERT_TRUE(elementsMatch(BV1, Expected));
      };

  // Empty case, one-element case.
  intersectionWithComplementIs({}, {}, {});
  intersectionWithComplementIs({1}, {1}, {});
  intersectionWithComplementIs({1}, {2}, {1});

  // Exact overlaps cases: single overlap and multiple overlaps.
  intersectionWithComplementIs({1, 2}, {1, 2}, {});
  intersectionWithComplementIs({1, 2, 11, 12}, {1, 2, 11, 12}, {});

  // Sliding window: fix {2, 3, 4} as the LHS, and slide a window before/after
  // it. Repeat this swapping LHS and RHS.
  intersectionWithComplementIs({2, 3, 4}, {1, 2, 3}, {4});
  intersectionWithComplementIs({2, 3, 4}, {2, 3, 4}, {});
  intersectionWithComplementIs({2, 3, 4}, {3, 4, 5}, {2});
  intersectionWithComplementIs({1, 2, 3}, {2, 3, 4}, {1});
  intersectionWithComplementIs({3, 4, 5}, {2, 3, 4}, {5});

  // No overlap, but we have multiple intervals.
  intersectionWithComplementIs({1, 2, 11, 12}, {3, 4, 13, 14}, {1, 2, 11, 12});

  // Multiple overlaps. For ease of understanding, fix LHS to be
  // {1, 2, 11, 12}, but vary RHS.
  intersectionWithComplementIs({1, 2, 11, 12}, {1}, {2, 11, 12});
  intersectionWithComplementIs({1, 2, 11, 12}, {2}, {1, 11, 12});
  intersectionWithComplementIs({1, 2, 11, 12}, {11}, {1, 2, 12});
  intersectionWithComplementIs({1, 2, 11, 12}, {12}, {1, 2, 11});
  intersectionWithComplementIs({1, 2, 11, 12}, {1, 11}, {2, 12});
  intersectionWithComplementIs({1, 2, 11, 12}, {1, 12}, {2, 11});
  intersectionWithComplementIs({1, 2, 11, 12}, {2, 11}, {1, 12});
  intersectionWithComplementIs({1, 2, 11, 12}, {2, 12}, {1, 11});
  intersectionWithComplementIs({1, 2, 11, 12}, {1, 2, 11}, {12});
  intersectionWithComplementIs({1, 2, 11, 12}, {1, 2, 12}, {11});
  intersectionWithComplementIs({1, 2, 11, 12}, {1, 11, 12}, {2});
  intersectionWithComplementIs({1, 2, 11, 12}, {2, 11, 12}, {1});
  intersectionWithComplementIs({1, 2, 11, 12}, {0, 11, 12}, {1, 2});
  intersectionWithComplementIs({1, 2, 11, 12}, {3, 11, 12}, {1, 2});
  intersectionWithComplementIs({1, 2, 11, 12}, {1, 11, 13}, {2, 12});
  intersectionWithComplementIs({1, 2, 11, 12}, {1, 10, 11}, {2, 12});

  // Partial overlap, but the existing interval covers future overlaps.
  intersectionWithComplementIs({1, 2, 3, 4, 5, 6, 7, 8}, {2, 3, 4, 6, 7},
                               {1, 5, 8});
}

TEST(CoalescingBitVectorTest, FindLowerBound) {
  U64BitVec::Allocator Alloc;
  U64BitVec BV(Alloc);
  uint64_t BigNum1 = uint64_t(1) << 32;
  uint64_t BigNum2 = (uint64_t(1) << 33) + 1;
  EXPECT_TRUE(BV.find(BigNum1) == BV.end());
  BV.set(BigNum1);
  auto Find1 = BV.find(BigNum1);
  EXPECT_EQ(*Find1, BigNum1);
  BV.set(BigNum2);
  auto Find2 = BV.find(BigNum1);
  EXPECT_EQ(*Find2, BigNum1);
  auto Find3 = BV.find(BigNum2);
  EXPECT_EQ(*Find3, BigNum2);
  BV.reset(BigNum1);
  auto Find4 = BV.find(BigNum1);
  EXPECT_EQ(*Find4, BigNum2);

  BV.clear();
  BV.set({1, 2, 3});
  EXPECT_EQ(*BV.find(2), 2u);
  EXPECT_EQ(*BV.find(3), 3u);
}

TEST(CoalescingBitVectorTest, AdvanceToLowerBound) {
  U64BitVec::Allocator Alloc;
  U64BitVec BV(Alloc);
  uint64_t BigNum1 = uint64_t(1) << 32;
  uint64_t BigNum2 = (uint64_t(1) << 33) + 1;

  auto advFromBegin = [&](uint64_t To) -> U64BitVec::const_iterator {
    auto It = BV.begin();
    It.advanceToLowerBound(To);
    return It;
  };

  EXPECT_TRUE(advFromBegin(BigNum1) == BV.end());
  BV.set(BigNum1);
  auto Find1 = advFromBegin(BigNum1);
  EXPECT_EQ(*Find1, BigNum1);
  BV.set(BigNum2);
  auto Find2 = advFromBegin(BigNum1);
  EXPECT_EQ(*Find2, BigNum1);
  auto Find3 = advFromBegin(BigNum2);
  EXPECT_EQ(*Find3, BigNum2);
  BV.reset(BigNum1);
  auto Find4 = advFromBegin(BigNum1);
  EXPECT_EQ(*Find4, BigNum2);

  BV.clear();
  BV.set({1, 2, 3});
  EXPECT_EQ(*advFromBegin(2), 2u);
  EXPECT_EQ(*advFromBegin(3), 3u);
  auto It = BV.begin();
  It.advanceToLowerBound(0);
  EXPECT_EQ(*It, 1u);
  It.advanceToLowerBound(100);
  EXPECT_TRUE(It == BV.end());
  It.advanceToLowerBound(100);
  EXPECT_TRUE(It == BV.end());
}

TEST(CoalescingBitVectorTest, HalfOpenRange) {
  UBitVec::Allocator Alloc;

  {
    UBitVec BV(Alloc);
    BV.set({1, 2, 3});

    EXPECT_EQ(*BV.find(0), 1U); // find(Start) > Start
    EXPECT_TRUE(rangesMatch(BV.half_open_range(0, 5), {1, 2, 3}));
    EXPECT_TRUE(rangesMatch(BV.half_open_range(1, 4), {1, 2, 3}));
    EXPECT_TRUE(rangesMatch(BV.half_open_range(1, 3), {1, 2}));
    EXPECT_TRUE(rangesMatch(BV.half_open_range(2, 3), {2}));
    EXPECT_TRUE(rangesMatch(BV.half_open_range(2, 4), {2, 3}));
    EXPECT_TRUE(rangesMatch(BV.half_open_range(4, 5), {}));
  }

  {
    UBitVec BV(Alloc);
    BV.set({1, 2, 11, 12});

    EXPECT_TRUE(rangesMatch(BV.half_open_range(0, 15), {1, 2, 11, 12}));
    EXPECT_TRUE(rangesMatch(BV.half_open_range(1, 13), {1, 2, 11, 12}));
    EXPECT_TRUE(rangesMatch(BV.half_open_range(1, 12), {1, 2, 11}));

    EXPECT_TRUE(rangesMatch(BV.half_open_range(0, 5), {1, 2}));
    EXPECT_TRUE(rangesMatch(BV.half_open_range(1, 5), {1, 2}));
    EXPECT_TRUE(rangesMatch(BV.half_open_range(2, 5), {2}));
    EXPECT_TRUE(rangesMatch(BV.half_open_range(1, 2), {1}));
    EXPECT_TRUE(rangesMatch(BV.half_open_range(13, 14), {}));

    EXPECT_TRUE(rangesMatch(BV.half_open_range(2, 10), {2}));
  }

  {
    UBitVec BV(Alloc);
    BV.set({1});

    EXPECT_EQ(*BV.find(0), 1U); // find(Start) == End
    EXPECT_TRUE(rangesMatch(BV.half_open_range(0, 1), {}));
  }

  {
    UBitVec BV(Alloc);
    BV.set({5});

    EXPECT_EQ(*BV.find(3), 5U); // find(Start) > End
    EXPECT_TRUE(rangesMatch(BV.half_open_range(3, 4), {}));
  }
}

TEST(CoalescingBitVectorTest, Print) {
  std::string S;
  {
    raw_string_ostream OS(S);
    UBitVec::Allocator Alloc;
    UBitVec BV(Alloc);
    BV.set({1});
    BV.print(OS);

    BV.clear();
    BV.set({1, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20});
    BV.print(OS);
  }
  EXPECT_EQ(S, "{[1]}"
               "{[1][11, 20]}");
}

} // namespace