1 | //===-- Unittests for bitmask ---------------------------------------------===// |
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/HashTable/bitmask.h" |
10 | #include "test/UnitTest/Test.h" |
11 | namespace LIBC_NAMESPACE { |
12 | namespace internal { |
13 | |
14 | using ShortBitMask = BitMaskAdaptor<uint16_t, 1>; |
15 | using LargeBitMask = BitMaskAdaptor<uint64_t, 8>; |
16 | |
17 | TEST(LlvmLibcHashTableBitMaskTest, SingleBitStrideLowestSetBit) { |
18 | uint16_t data = 0xffff; |
19 | for (size_t i = 0; i < 16; ++i) { |
20 | if (ShortBitMask{.word: data}.any_bit_set()) { |
21 | ASSERT_EQ(ShortBitMask{data}.lowest_set_bit_nonzero(), i); |
22 | data <<= 1; |
23 | } |
24 | } |
25 | } |
26 | |
27 | TEST(LlvmLibcHashTableBitMaskTest, MultiBitStrideLowestSetBit) { |
28 | uint64_t data = 0xffff'ffff'ffff'ffff; |
29 | for (size_t i = 0; i < 8; ++i) { |
30 | for (size_t j = 0; j < 8; ++j) { |
31 | if (LargeBitMask{.word: data}.any_bit_set()) { |
32 | ASSERT_EQ(LargeBitMask{data}.lowest_set_bit_nonzero(), i); |
33 | data <<= 1; |
34 | } |
35 | } |
36 | } |
37 | } |
38 | |
39 | TEST(LlvmLibcHashTableBitMaskTest, SingleBitStrideIteration) { |
40 | using Iter = IteratableBitMaskAdaptor<ShortBitMask>; |
41 | uint16_t data = 0xffff; |
42 | for (size_t i = 0; i < 16; ++i) { |
43 | Iter iter = {data}; |
44 | size_t j = i; |
45 | for (auto x : iter) { |
46 | ASSERT_EQ(x, j); |
47 | j++; |
48 | } |
49 | ASSERT_EQ(j, size_t{16}); |
50 | data <<= 1; |
51 | } |
52 | } |
53 | |
54 | TEST(LlvmLibcHashTableBitMaskTest, MultiBitStrideIteration) { |
55 | using Iter = IteratableBitMaskAdaptor<LargeBitMask>; |
56 | uint64_t data = 0x8080808080808080ul; |
57 | for (size_t i = 0; i < 8; ++i) { |
58 | Iter iter = {data}; |
59 | size_t j = i; |
60 | for (auto x : iter) { |
61 | ASSERT_EQ(x, j); |
62 | j++; |
63 | } |
64 | ASSERT_EQ(j, size_t{8}); |
65 | data <<= Iter::STRIDE; |
66 | } |
67 | } |
68 | } // namespace internal |
69 | } // namespace LIBC_NAMESPACE |
70 | |