1 //===-- Unittests for Bitset ----------------------------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "src/__support/CPP/Bitset.h" 10 #include "utils/UnitTest/Test.h" 11 12 TEST(LlvmLibcBitsetTest, SetBitForSizeEqualToOne) { 13 __llvm_libc::cpp::Bitset<1> bitset; 14 EXPECT_FALSE(bitset.test(0)); 15 bitset.set(0); 16 EXPECT_TRUE(bitset.test(0)); 17 } 18 19 TEST(LlvmLibcBitsetTest, SetsBitsForSizeEqualToTwo) { 20 __llvm_libc::cpp::Bitset<2> bitset; 21 bitset.set(0); 22 EXPECT_TRUE(bitset.test(0)); 23 bitset.set(1); 24 EXPECT_TRUE(bitset.test(1)); 25 } 26 27 TEST(LlvmLibcBitsetTest, SetsAllBitsForSizeLessThanEight) { 28 __llvm_libc::cpp::Bitset<7> bitset; 29 for (size_t i = 0; i < 7; ++i) 30 bitset.set(i); 31 // Verify all bits are now set. 32 for (size_t j = 0; j < 7; ++j) 33 EXPECT_TRUE(bitset.test(j)); 34 } 35 36 TEST(LlvmLibcBitsetTest, SetsAllBitsForSizeLessThanSixteen) { 37 __llvm_libc::cpp::Bitset<15> bitset; 38 for (size_t i = 0; i < 15; ++i) 39 bitset.set(i); 40 // Verify all bits are now set. 41 for (size_t j = 0; j < 15; ++j) 42 EXPECT_TRUE(bitset.test(j)); 43 } 44 45 TEST(LlvmLibcBitsetTest, SetsAllBitsForSizeLessThanThirtyTwo) { 46 __llvm_libc::cpp::Bitset<31> bitset; 47 for (size_t i = 0; i < 31; ++i) 48 bitset.set(i); 49 // Verify all bits are now set. 50 for (size_t j = 0; j < 31; ++j) 51 EXPECT_TRUE(bitset.test(j)); 52 } 53 54 TEST(LlvmLibcBitsetTest, DefaultHasNoSetBits) { 55 __llvm_libc::cpp::Bitset<64> bitset; 56 for (size_t i = 0; i < 64; ++i) { 57 EXPECT_FALSE(bitset.test(i)); 58 } 59 // Same for odd number. 60 __llvm_libc::cpp::Bitset<65> odd_bitset; 61 for (size_t i = 0; i < 65; ++i) { 62 EXPECT_FALSE(odd_bitset.test(i)); 63 } 64 } 65 66 TEST(LlvmLibcBitsetTest, SettingBitXDoesNotSetBitY) { 67 for (size_t i = 0; i < 256; ++i) { 68 // Initialize within the loop to start with a fresh Bitset. 69 __llvm_libc::cpp::Bitset<256> bitset; 70 bitset.set(i); 71 72 for (size_t neighbor = 0; neighbor < 256; ++neighbor) { 73 if (neighbor == i) 74 EXPECT_TRUE(bitset.test(neighbor)); 75 else 76 EXPECT_FALSE(bitset.test(neighbor)); 77 } 78 } 79 // Same for odd number. 80 for (size_t i = 0; i < 255; ++i) { 81 82 __llvm_libc::cpp::Bitset<255> bitset; 83 bitset.set(i); 84 85 for (size_t neighbor = 0; neighbor < 255; ++neighbor) { 86 if (neighbor == i) 87 EXPECT_TRUE(bitset.test(neighbor)); 88 else 89 EXPECT_FALSE(bitset.test(neighbor)); 90 } 91 } 92 } 93 94 TEST(LlvmLibcBitsetTest, SettingBitXDoesNotResetBitY) { 95 __llvm_libc::cpp::Bitset<128> bitset; 96 for (size_t i = 0; i < 128; ++i) 97 bitset.set(i); 98 99 // Verify all bits are now set. 100 for (size_t j = 0; j < 128; ++j) 101 EXPECT_TRUE(bitset.test(j)); 102 } 103