1 //===- llvm/unittest/ADT/BitVectorTest.cpp - BitVector tests --------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // Some of these tests fail on PowerPC for unknown reasons. 11 #ifndef __ppc__ 12 13 #include "llvm/ADT/BitVector.h" 14 #include "llvm/ADT/SmallBitVector.h" 15 #include "gtest/gtest.h" 16 17 using namespace llvm; 18 19 namespace { 20 21 // Test fixture 22 template <typename T> 23 class BitVectorTest : public ::testing::Test { }; 24 25 // Test both BitVector and SmallBitVector with the same suite of tests. 26 typedef ::testing::Types<BitVector, SmallBitVector> BitVectorTestTypes; 27 TYPED_TEST_CASE(BitVectorTest, BitVectorTestTypes); 28 29 TYPED_TEST(BitVectorTest, TrivialOperation) { 30 TypeParam Vec; 31 EXPECT_EQ(0U, Vec.count()); 32 EXPECT_EQ(0U, Vec.size()); 33 EXPECT_FALSE(Vec.any()); 34 EXPECT_TRUE(Vec.all()); 35 EXPECT_TRUE(Vec.none()); 36 EXPECT_TRUE(Vec.empty()); 37 38 Vec.resize(5, true); 39 EXPECT_EQ(5U, Vec.count()); 40 EXPECT_EQ(5U, Vec.size()); 41 EXPECT_TRUE(Vec.any()); 42 EXPECT_TRUE(Vec.all()); 43 EXPECT_FALSE(Vec.none()); 44 EXPECT_FALSE(Vec.empty()); 45 46 Vec.resize(11); 47 EXPECT_EQ(5U, Vec.count()); 48 EXPECT_EQ(11U, Vec.size()); 49 EXPECT_TRUE(Vec.any()); 50 EXPECT_FALSE(Vec.all()); 51 EXPECT_FALSE(Vec.none()); 52 EXPECT_FALSE(Vec.empty()); 53 54 TypeParam Inv = Vec; 55 Inv.flip(); 56 EXPECT_EQ(6U, Inv.count()); 57 EXPECT_EQ(11U, Inv.size()); 58 EXPECT_TRUE(Inv.any()); 59 EXPECT_FALSE(Inv.all()); 60 EXPECT_FALSE(Inv.none()); 61 EXPECT_FALSE(Inv.empty()); 62 63 EXPECT_FALSE(Inv == Vec); 64 EXPECT_TRUE(Inv != Vec); 65 Vec.flip(); 66 EXPECT_TRUE(Inv == Vec); 67 EXPECT_FALSE(Inv != Vec); 68 69 // Add some "interesting" data to Vec. 70 Vec.resize(23, true); 71 Vec.resize(25, false); 72 Vec.resize(26, true); 73 Vec.resize(29, false); 74 Vec.resize(33, true); 75 Vec.resize(57, false); 76 unsigned Count = 0; 77 for (unsigned i = Vec.find_first(); i != -1u; i = Vec.find_next(i)) { 78 ++Count; 79 EXPECT_TRUE(Vec[i]); 80 EXPECT_TRUE(Vec.test(i)); 81 } 82 EXPECT_EQ(Count, Vec.count()); 83 EXPECT_EQ(Count, 23u); 84 EXPECT_FALSE(Vec[0]); 85 EXPECT_TRUE(Vec[32]); 86 EXPECT_FALSE(Vec[56]); 87 Vec.resize(61, false); 88 89 TypeParam Copy = Vec; 90 TypeParam Alt(3, false); 91 Alt.resize(6, true); 92 std::swap(Alt, Vec); 93 EXPECT_TRUE(Copy == Alt); 94 EXPECT_TRUE(Vec.size() == 6); 95 EXPECT_TRUE(Vec.count() == 3); 96 EXPECT_TRUE(Vec.find_first() == 3); 97 std::swap(Copy, Vec); 98 99 // Add some more "interesting" data. 100 Vec.resize(68, true); 101 Vec.resize(78, false); 102 Vec.resize(89, true); 103 Vec.resize(90, false); 104 Vec.resize(91, true); 105 Vec.resize(130, false); 106 Count = 0; 107 for (unsigned i = Vec.find_first(); i != -1u; i = Vec.find_next(i)) { 108 ++Count; 109 EXPECT_TRUE(Vec[i]); 110 EXPECT_TRUE(Vec.test(i)); 111 } 112 EXPECT_EQ(Count, Vec.count()); 113 EXPECT_EQ(Count, 42u); 114 EXPECT_FALSE(Vec[0]); 115 EXPECT_TRUE(Vec[32]); 116 EXPECT_FALSE(Vec[60]); 117 EXPECT_FALSE(Vec[129]); 118 119 Vec.flip(60); 120 EXPECT_TRUE(Vec[60]); 121 EXPECT_EQ(Count + 1, Vec.count()); 122 Vec.flip(60); 123 EXPECT_FALSE(Vec[60]); 124 EXPECT_EQ(Count, Vec.count()); 125 126 Vec.reset(32); 127 EXPECT_FALSE(Vec[32]); 128 EXPECT_EQ(Count - 1, Vec.count()); 129 Vec.set(32); 130 EXPECT_TRUE(Vec[32]); 131 EXPECT_EQ(Count, Vec.count()); 132 133 Vec.flip(); 134 EXPECT_EQ(Vec.size() - Count, Vec.count()); 135 136 Vec.reset(); 137 EXPECT_EQ(0U, Vec.count()); 138 EXPECT_EQ(130U, Vec.size()); 139 EXPECT_FALSE(Vec.any()); 140 EXPECT_FALSE(Vec.all()); 141 EXPECT_TRUE(Vec.none()); 142 EXPECT_FALSE(Vec.empty()); 143 144 Vec.flip(); 145 EXPECT_EQ(130U, Vec.count()); 146 EXPECT_EQ(130U, Vec.size()); 147 EXPECT_TRUE(Vec.any()); 148 EXPECT_TRUE(Vec.all()); 149 EXPECT_FALSE(Vec.none()); 150 EXPECT_FALSE(Vec.empty()); 151 152 Vec.resize(64); 153 EXPECT_EQ(64U, Vec.count()); 154 EXPECT_EQ(64U, Vec.size()); 155 EXPECT_TRUE(Vec.any()); 156 EXPECT_TRUE(Vec.all()); 157 EXPECT_FALSE(Vec.none()); 158 EXPECT_FALSE(Vec.empty()); 159 160 Vec.flip(); 161 EXPECT_EQ(0U, Vec.count()); 162 EXPECT_EQ(64U, Vec.size()); 163 EXPECT_FALSE(Vec.any()); 164 EXPECT_FALSE(Vec.all()); 165 EXPECT_TRUE(Vec.none()); 166 EXPECT_FALSE(Vec.empty()); 167 168 Inv = TypeParam().flip(); 169 EXPECT_EQ(0U, Inv.count()); 170 EXPECT_EQ(0U, Inv.size()); 171 EXPECT_FALSE(Inv.any()); 172 EXPECT_TRUE(Inv.all()); 173 EXPECT_TRUE(Inv.none()); 174 EXPECT_TRUE(Inv.empty()); 175 176 Vec.clear(); 177 EXPECT_EQ(0U, Vec.count()); 178 EXPECT_EQ(0U, Vec.size()); 179 EXPECT_FALSE(Vec.any()); 180 EXPECT_TRUE(Vec.all()); 181 EXPECT_TRUE(Vec.none()); 182 EXPECT_TRUE(Vec.empty()); 183 } 184 185 TYPED_TEST(BitVectorTest, FindOperations) { 186 // Test finding in an empty BitVector. 187 TypeParam A; 188 EXPECT_EQ(-1, A.find_first()); 189 EXPECT_EQ(-1, A.find_first_unset()); 190 EXPECT_EQ(-1, A.find_next(0)); 191 EXPECT_EQ(-1, A.find_next_unset(0)); 192 193 // Test finding next set and unset bits in a BitVector with multiple words 194 A.resize(100); 195 A.set(12); 196 A.set(13); 197 A.set(75); 198 199 EXPECT_EQ(12, A.find_first()); 200 EXPECT_EQ(13, A.find_next(12)); 201 EXPECT_EQ(75, A.find_next(13)); 202 EXPECT_EQ(-1, A.find_next(75)); 203 204 EXPECT_EQ(0, A.find_first_unset()); 205 EXPECT_EQ(14, A.find_next_unset(11)); 206 EXPECT_EQ(14, A.find_next_unset(12)); 207 EXPECT_EQ(14, A.find_next_unset(13)); 208 EXPECT_EQ(16, A.find_next_unset(15)); 209 EXPECT_EQ(76, A.find_next_unset(74)); 210 EXPECT_EQ(76, A.find_next_unset(75)); 211 EXPECT_EQ(-1, A.find_next_unset(99)); 212 213 A.set(0, 100); 214 EXPECT_EQ(100U, A.count()); 215 EXPECT_EQ(0, A.find_first()); 216 EXPECT_EQ(-1, A.find_first_unset()); 217 218 A.reset(0, 100); 219 EXPECT_EQ(0U, A.count()); 220 EXPECT_EQ(-1, A.find_first()); 221 EXPECT_EQ(0, A.find_first_unset()); 222 } 223 224 TYPED_TEST(BitVectorTest, CompoundAssignment) { 225 TypeParam A; 226 A.resize(10); 227 A.set(4); 228 A.set(7); 229 230 TypeParam B; 231 B.resize(50); 232 B.set(5); 233 B.set(18); 234 235 A |= B; 236 EXPECT_TRUE(A.test(4)); 237 EXPECT_TRUE(A.test(5)); 238 EXPECT_TRUE(A.test(7)); 239 EXPECT_TRUE(A.test(18)); 240 EXPECT_EQ(4U, A.count()); 241 EXPECT_EQ(50U, A.size()); 242 243 B.resize(10); 244 B.set(); 245 B.reset(2); 246 B.reset(7); 247 A &= B; 248 EXPECT_FALSE(A.test(2)); 249 EXPECT_FALSE(A.test(7)); 250 EXPECT_EQ(2U, A.count()); 251 EXPECT_EQ(50U, A.size()); 252 253 B.resize(100); 254 B.set(); 255 256 A ^= B; 257 EXPECT_TRUE(A.test(2)); 258 EXPECT_TRUE(A.test(7)); 259 EXPECT_EQ(98U, A.count()); 260 EXPECT_EQ(100U, A.size()); 261 } 262 263 TYPED_TEST(BitVectorTest, ProxyIndex) { 264 TypeParam Vec(3); 265 EXPECT_TRUE(Vec.none()); 266 Vec[0] = Vec[1] = Vec[2] = true; 267 EXPECT_EQ(Vec.size(), Vec.count()); 268 Vec[2] = Vec[1] = Vec[0] = false; 269 EXPECT_TRUE(Vec.none()); 270 } 271 272 TYPED_TEST(BitVectorTest, PortableBitMask) { 273 TypeParam A; 274 const uint32_t Mask1[] = { 0x80000000, 6, 5 }; 275 276 A.resize(10); 277 A.setBitsInMask(Mask1, 1); 278 EXPECT_EQ(10u, A.size()); 279 EXPECT_FALSE(A.test(0)); 280 281 A.resize(32); 282 A.setBitsInMask(Mask1, 1); 283 EXPECT_FALSE(A.test(0)); 284 EXPECT_TRUE(A.test(31)); 285 EXPECT_EQ(1u, A.count()); 286 287 A.resize(33); 288 A.setBitsInMask(Mask1, 1); 289 EXPECT_EQ(1u, A.count()); 290 A.setBitsInMask(Mask1, 2); 291 EXPECT_EQ(1u, A.count()); 292 293 A.resize(34); 294 A.setBitsInMask(Mask1, 2); 295 EXPECT_EQ(2u, A.count()); 296 297 A.resize(65); 298 A.setBitsInMask(Mask1, 3); 299 EXPECT_EQ(4u, A.count()); 300 301 A.setBitsNotInMask(Mask1, 1); 302 EXPECT_EQ(32u+3u, A.count()); 303 304 A.setBitsNotInMask(Mask1, 3); 305 EXPECT_EQ(65u, A.count()); 306 307 A.resize(96); 308 EXPECT_EQ(65u, A.count()); 309 310 A.clear(); 311 A.resize(128); 312 A.setBitsNotInMask(Mask1, 3); 313 EXPECT_EQ(96u-5u, A.count()); 314 315 A.clearBitsNotInMask(Mask1, 1); 316 EXPECT_EQ(64-4u, A.count()); 317 } 318 319 TYPED_TEST(BitVectorTest, BinOps) { 320 TypeParam A; 321 TypeParam B; 322 323 A.resize(65); 324 EXPECT_FALSE(A.anyCommon(B)); 325 EXPECT_FALSE(B.anyCommon(B)); 326 327 B.resize(64); 328 A.set(64); 329 EXPECT_FALSE(A.anyCommon(B)); 330 EXPECT_FALSE(B.anyCommon(A)); 331 332 B.set(63); 333 EXPECT_FALSE(A.anyCommon(B)); 334 EXPECT_FALSE(B.anyCommon(A)); 335 336 A.set(63); 337 EXPECT_TRUE(A.anyCommon(B)); 338 EXPECT_TRUE(B.anyCommon(A)); 339 340 B.resize(70); 341 B.set(64); 342 B.reset(63); 343 A.resize(64); 344 EXPECT_FALSE(A.anyCommon(B)); 345 EXPECT_FALSE(B.anyCommon(A)); 346 } 347 348 typedef std::vector<std::pair<int, int>> RangeList; 349 350 template <typename VecType> 351 static inline VecType createBitVector(uint32_t Size, 352 const RangeList &setRanges) { 353 VecType V; 354 V.resize(Size); 355 for (auto &R : setRanges) 356 V.set(R.first, R.second); 357 return V; 358 } 359 360 TYPED_TEST(BitVectorTest, ShiftOpsSingleWord) { 361 // Test that shift ops work when the desired shift amount is less 362 // than one word. 363 364 // 1. Case where the number of bits in the BitVector also fit into a single 365 // word. 366 TypeParam A = createBitVector<TypeParam>(12, {{2, 4}, {8, 10}}); 367 TypeParam B = A; 368 369 EXPECT_EQ(4U, A.count()); 370 EXPECT_TRUE(A.test(2)); 371 EXPECT_TRUE(A.test(3)); 372 EXPECT_TRUE(A.test(8)); 373 EXPECT_TRUE(A.test(9)); 374 375 A >>= 1; 376 EXPECT_EQ(createBitVector<TypeParam>(12, {{1, 3}, {7, 9}}), A); 377 378 A <<= 1; 379 EXPECT_EQ(B, A); 380 381 A >>= 10; 382 EXPECT_EQ(createBitVector<TypeParam>(12, {}), A); 383 384 A = B; 385 A <<= 10; 386 EXPECT_EQ(createBitVector<TypeParam>(12, {}), A); 387 388 // 2. Case where the number of bits in the BitVector do not fit into a single 389 // word. 390 391 // 31----------------------------------------------------------------------0 392 // XXXXXXXX XXXXXXXX XXXXXXXX 00000111 | 11111110 00000000 00001111 11111111 393 A = createBitVector<TypeParam>(40, {{0, 12}, {25, 35}}); 394 EXPECT_EQ(40U, A.size()); 395 EXPECT_EQ(22U, A.count()); 396 397 // 2a. Make sure that left shifting some 1 bits out of the vector works. 398 // 31----------------------------------------------------------------------0 399 // Before: 400 // XXXXXXXX XXXXXXXX XXXXXXXX 00000111 | 11111110 00000000 00001111 11111111 401 // After: 402 // XXXXXXXX XXXXXXXX XXXXXXXX 11111100 | 00000000 00011111 11111110 00000000 403 A <<= 9; 404 EXPECT_EQ(createBitVector<TypeParam>(40, {{9, 21}, {34, 40}}), A); 405 406 // 2b. Make sure that keeping the number of one bits unchanged works. 407 // 31----------------------------------------------------------------------0 408 // Before: 409 // XXXXXXXX XXXXXXXX XXXXXXXX 11111100 | 00000000 00011111 11111110 00000000 410 // After: 411 // XXXXXXXX XXXXXXXX XXXXXXXX 00000011 | 11110000 00000000 01111111 11111000 412 A >>= 6; 413 EXPECT_EQ(createBitVector<TypeParam>(40, {{3, 15}, {28, 34}}), A); 414 415 // 2c. Make sure that right shifting some 1 bits out of the vector works. 416 // 31----------------------------------------------------------------------0 417 // Before: 418 // XXXXXXXX XXXXXXXX XXXXXXXX 00000011 | 11110000 00000000 01111111 11111000 419 // After: 420 // XXXXXXXX XXXXXXXX XXXXXXXX 00000000 | 00000000 11111100 00000000 00011111 421 A >>= 10; 422 EXPECT_EQ(createBitVector<TypeParam>(40, {{0, 5}, {18, 24}}), A); 423 424 // 3. Big test. 425 A = createBitVector<TypeParam>(300, {{1, 30}, {60, 95}, {200, 275}}); 426 A <<= 29; 427 EXPECT_EQ(createBitVector<TypeParam>( 428 300, {{1 + 29, 30 + 29}, {60 + 29, 95 + 29}, {200 + 29, 300}}), 429 A); 430 } 431 432 TYPED_TEST(BitVectorTest, ShiftOpsMultiWord) { 433 // Test that shift ops work when the desired shift amount is greater than or 434 // equal to the size of a single word. 435 auto A = createBitVector<TypeParam>(300, {{1, 30}, {60, 95}, {200, 275}}); 436 437 // Make a copy so we can re-use it later. 438 auto B = A; 439 440 // 1. Shift left by an exact multiple of the word size. This should invoke 441 // only a memmove and no per-word bit operations. 442 A <<= 64; 443 auto Expected = createBitVector<TypeParam>( 444 300, {{1 + 64, 30 + 64}, {60 + 64, 95 + 64}, {200 + 64, 300}}); 445 EXPECT_EQ(Expected, A); 446 447 // 2. Shift left by a non multiple of the word size. This should invoke both 448 // a memmove and per-word bit operations. 449 A = B; 450 A <<= 93; 451 EXPECT_EQ(createBitVector<TypeParam>( 452 300, {{1 + 93, 30 + 93}, {60 + 93, 95 + 93}, {200 + 93, 300}}), 453 A); 454 455 // 1. Shift right by an exact multiple of the word size. This should invoke 456 // only a memmove and no per-word bit operations. 457 A = B; 458 A >>= 64; 459 EXPECT_EQ( 460 createBitVector<TypeParam>(300, {{0, 95 - 64}, {200 - 64, 275 - 64}}), A); 461 462 // 2. Shift left by a non multiple of the word size. This should invoke both 463 // a memmove and per-word bit operations. 464 A = B; 465 A >>= 93; 466 EXPECT_EQ( 467 createBitVector<TypeParam>(300, {{0, 95 - 93}, {200 - 93, 275 - 93}}), A); 468 } 469 470 TYPED_TEST(BitVectorTest, RangeOps) { 471 TypeParam A; 472 A.resize(256); 473 A.reset(); 474 A.set(1, 255); 475 476 EXPECT_FALSE(A.test(0)); 477 EXPECT_TRUE( A.test(1)); 478 EXPECT_TRUE( A.test(23)); 479 EXPECT_TRUE( A.test(254)); 480 EXPECT_FALSE(A.test(255)); 481 482 TypeParam B; 483 B.resize(256); 484 B.set(); 485 B.reset(1, 255); 486 487 EXPECT_TRUE( B.test(0)); 488 EXPECT_FALSE(B.test(1)); 489 EXPECT_FALSE(B.test(23)); 490 EXPECT_FALSE(B.test(254)); 491 EXPECT_TRUE( B.test(255)); 492 493 TypeParam C; 494 C.resize(3); 495 C.reset(); 496 C.set(0, 1); 497 498 EXPECT_TRUE(C.test(0)); 499 EXPECT_FALSE( C.test(1)); 500 EXPECT_FALSE( C.test(2)); 501 502 TypeParam D; 503 D.resize(3); 504 D.set(); 505 D.reset(0, 1); 506 507 EXPECT_FALSE(D.test(0)); 508 EXPECT_TRUE( D.test(1)); 509 EXPECT_TRUE( D.test(2)); 510 511 TypeParam E; 512 E.resize(128); 513 E.reset(); 514 E.set(1, 33); 515 516 EXPECT_FALSE(E.test(0)); 517 EXPECT_TRUE( E.test(1)); 518 EXPECT_TRUE( E.test(32)); 519 EXPECT_FALSE(E.test(33)); 520 521 TypeParam BufferOverrun; 522 unsigned size = sizeof(unsigned long) * 8; 523 BufferOverrun.resize(size); 524 BufferOverrun.reset(0, size); 525 BufferOverrun.set(0, size); 526 } 527 528 TYPED_TEST(BitVectorTest, CompoundTestReset) { 529 TypeParam A(50, true); 530 TypeParam B(50, false); 531 532 TypeParam C(100, true); 533 TypeParam D(100, false); 534 535 EXPECT_FALSE(A.test(A)); 536 EXPECT_TRUE(A.test(B)); 537 EXPECT_FALSE(A.test(C)); 538 EXPECT_TRUE(A.test(D)); 539 EXPECT_FALSE(B.test(A)); 540 EXPECT_FALSE(B.test(B)); 541 EXPECT_FALSE(B.test(C)); 542 EXPECT_FALSE(B.test(D)); 543 EXPECT_TRUE(C.test(A)); 544 EXPECT_TRUE(C.test(B)); 545 EXPECT_FALSE(C.test(C)); 546 EXPECT_TRUE(C.test(D)); 547 548 A.reset(B); 549 A.reset(D); 550 EXPECT_TRUE(A.all()); 551 A.reset(A); 552 EXPECT_TRUE(A.none()); 553 A.set(); 554 A.reset(C); 555 EXPECT_TRUE(A.none()); 556 A.set(); 557 558 C.reset(A); 559 EXPECT_EQ(50, C.find_first()); 560 C.reset(C); 561 EXPECT_TRUE(C.none()); 562 } 563 564 TYPED_TEST(BitVectorTest, MoveConstructor) { 565 TypeParam A(10, true); 566 TypeParam B(std::move(A)); 567 // Check that the move ctor leaves the moved-from object in a valid state. 568 // The following line used to crash. 569 A = B; 570 571 TypeParam C(10, true); 572 EXPECT_EQ(C, A); 573 EXPECT_EQ(C, B); 574 } 575 576 TYPED_TEST(BitVectorTest, MoveAssignment) { 577 TypeParam A(10, true); 578 TypeParam B; 579 B = std::move(A); 580 // Check that move assignment leaves the moved-from object in a valid state. 581 // The following line used to crash. 582 A = B; 583 584 TypeParam C(10, true); 585 EXPECT_EQ(C, A); 586 EXPECT_EQ(C, B); 587 } 588 589 template<class TypeParam> 590 static void testEmpty(const TypeParam &A) { 591 EXPECT_TRUE(A.empty()); 592 EXPECT_EQ((size_t)0, A.size()); 593 EXPECT_EQ((size_t)0, A.count()); 594 EXPECT_FALSE(A.any()); 595 EXPECT_TRUE(A.all()); 596 EXPECT_TRUE(A.none()); 597 EXPECT_EQ(-1, A.find_first()); 598 EXPECT_EQ(A, TypeParam()); 599 } 600 601 /// Tests whether BitVector behaves well with Bits==nullptr, Capacity==0 602 TYPED_TEST(BitVectorTest, EmptyVector) { 603 TypeParam A; 604 testEmpty(A); 605 606 TypeParam B; 607 B.reset(); 608 testEmpty(B); 609 610 TypeParam C; 611 C.clear(); 612 testEmpty(C); 613 614 TypeParam D(A); 615 testEmpty(D); 616 617 TypeParam E; 618 E = A; 619 testEmpty(E); 620 621 TypeParam F; 622 E.reset(A); 623 testEmpty(E); 624 } 625 626 } 627 #endif 628