| 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 "test/UnitTest/Test.h" |
| 11 | |
| 12 | TEST(LlvmLibcBitsetTest, SetBitForSizeEqualToOne) { |
| 13 | LIBC_NAMESPACE::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 | LIBC_NAMESPACE::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 | LIBC_NAMESPACE::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 | LIBC_NAMESPACE::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 | LIBC_NAMESPACE::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 | LIBC_NAMESPACE::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 | LIBC_NAMESPACE::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 | LIBC_NAMESPACE::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 | LIBC_NAMESPACE::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 | LIBC_NAMESPACE::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 | |
| 104 | TEST(LlvmLibcBitsetTest, FlipTest) { |
| 105 | LIBC_NAMESPACE::cpp::bitset<128> bitset; |
| 106 | |
| 107 | bitset.flip(); |
| 108 | |
| 109 | // Verify all bits are now set. |
| 110 | for (size_t j = 0; j < 128; ++j) |
| 111 | EXPECT_TRUE(bitset.test(j)); |
| 112 | |
| 113 | bitset.flip(); |
| 114 | |
| 115 | // Verify all bits are now unset. |
| 116 | for (size_t j = 0; j < 128; ++j) |
| 117 | EXPECT_FALSE(bitset.test(j)); |
| 118 | |
| 119 | // Set the even bits |
| 120 | for (size_t j = 0; j < 64; ++j) |
| 121 | bitset.set(j * 2); |
| 122 | |
| 123 | // Verify |
| 124 | for (size_t j = 0; j < 128; ++j) |
| 125 | EXPECT_EQ(bitset.test(j), (j % 2) == 0); |
| 126 | |
| 127 | bitset.flip(); |
| 128 | |
| 129 | // Check that the odd set of bits is now true. |
| 130 | for (size_t j = 0; j < 128; ++j) |
| 131 | EXPECT_EQ(bitset.test(j), j % 2 != 0); |
| 132 | |
| 133 | // Set the first half of the bits. |
| 134 | for (size_t j = 0; j < 64; ++j) |
| 135 | bitset.set(j); |
| 136 | |
| 137 | // The pattern should now be 111...1110101...010 |
| 138 | |
| 139 | // Flip to get 000...0001010...101 |
| 140 | bitset.flip(); |
| 141 | |
| 142 | // Verify that the first half of bits are false and the even bits in the |
| 143 | // second half are true. |
| 144 | for (size_t j = 0; j < 128; ++j) |
| 145 | EXPECT_EQ(bitset.test(j), (j > 63) && (j % 2 == 0)); |
| 146 | } |
| 147 | |
| 148 | TEST(LlvmLibcBitsetTest, EqualTest) { |
| 149 | LIBC_NAMESPACE::cpp::bitset<128> bitset_a; |
| 150 | LIBC_NAMESPACE::cpp::bitset<128> bitset_b; |
| 151 | |
| 152 | // New sets should be empty, and so they should be equal. |
| 153 | ASSERT_TRUE(bitset_a == bitset_b); |
| 154 | |
| 155 | bitset_a.set(0); |
| 156 | |
| 157 | // Setting one bit should be enough. |
| 158 | ASSERT_FALSE(bitset_a == bitset_b); |
| 159 | |
| 160 | bitset_b.set(64); |
| 161 | |
| 162 | // Setting the same bit on a different unit shouldn't be equal. |
| 163 | ASSERT_FALSE(bitset_a == bitset_b); |
| 164 | |
| 165 | bitset_b.set(0); |
| 166 | |
| 167 | // The first unit matching shouldn't be equal. |
| 168 | ASSERT_FALSE(bitset_a == bitset_b); |
| 169 | |
| 170 | bitset_a.set(64); |
| 171 | |
| 172 | // Now they should be equal. |
| 173 | ASSERT_TRUE(bitset_a == bitset_b); |
| 174 | } |
| 175 | |
| 176 | TEST(LlvmLibcBitsetTest, SetRangeTest) { |
| 177 | LIBC_NAMESPACE::cpp::bitset<256> bitset; |
| 178 | |
| 179 | // Range from 1 to 1 should only set bit 1 |
| 180 | bitset.set_range(1, 1); |
| 181 | |
| 182 | for (size_t j = 0; j < 256; ++j) |
| 183 | EXPECT_EQ(bitset.test(j), j == 1); |
| 184 | |
| 185 | // reset all bits back to 0. |
| 186 | bitset.reset(); |
| 187 | |
| 188 | // Range from 2 to 5 should set bits 2-5 |
| 189 | bitset.set_range(2, 5); |
| 190 | for (size_t j = 0; j < 256; ++j) |
| 191 | EXPECT_EQ(bitset.test(j), (j >= 2 && j <= 5)); |
| 192 | bitset.reset(); |
| 193 | |
| 194 | // Check setting exactly one unit |
| 195 | bitset.set_range(0, 63); |
| 196 | for (size_t j = 0; j < 256; ++j) |
| 197 | EXPECT_EQ(bitset.test(j), j <= 63); |
| 198 | bitset.reset(); |
| 199 | |
| 200 | // Check ranges across unit boundaries work. |
| 201 | bitset.set_range(1, 64); |
| 202 | for (size_t j = 0; j < 256; ++j) |
| 203 | EXPECT_EQ(bitset.test(j), (j >= 1 && j <= 64)); |
| 204 | bitset.reset(); |
| 205 | |
| 206 | // Same, but closer together. |
| 207 | bitset.set_range(63, 64); |
| 208 | for (size_t j = 0; j < 256; ++j) |
| 209 | EXPECT_EQ(bitset.test(j), (j >= 63 && j <= 64)); |
| 210 | bitset.reset(); |
| 211 | |
| 212 | // Check that ranges with a unit in the middle work. |
| 213 | bitset.set_range(63, 129); |
| 214 | for (size_t j = 0; j < 256; ++j) |
| 215 | EXPECT_EQ(bitset.test(j), (j >= 63 && j <= 129)); |
| 216 | bitset.reset(); |
| 217 | |
| 218 | // Check that the whole range being set works. |
| 219 | bitset.set_range(0, 255); |
| 220 | for (size_t j = 0; j < 256; ++j) |
| 221 | EXPECT_TRUE(bitset.test(j)); |
| 222 | bitset.reset(); |
| 223 | } |
| 224 | |