1 //===-- Unittests for control group ---------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/HashTable/bitmask.h"
11 #include "src/__support/CPP/bit.h"
12 #include "src/__support/macros/config.h"
13 #include "src/stdlib/rand.h"
14 #include "test/UnitTest/Test.h"
17 namespace LIBC_NAMESPACE_DECL
{
21 alignas(Group
) uint8_t data
[sizeof(Group
) + 1]{};
24 TEST(LlvmLibcHashTableBitMaskTest
, Match
) {
25 // Any pair of targets have bit differences not only at the lowest bit.
27 uint8_t targets
[4] = {0x00, 0x11, 0xFF, 0x0F};
28 size_t count
[4] = {0, 0, 0, 0};
29 size_t appearance
[4][sizeof(Group
)];
32 int data
[sizeof(uintptr_t) / sizeof(int)];
37 uintptr_t random
= cpp::bit_cast
<uintptr_t>(data
);
39 for (size_t i
= 0; i
< sizeof(Group
); ++i
) {
40 size_t choice
= random
% 4;
42 array
.data
[i
] = targets
[choice
];
43 appearance
[choice
][count
[choice
]++] = i
;
46 for (size_t t
= 0; t
< sizeof(targets
); ++t
) {
47 auto bitmask
= Group::load(array
.data
).match_byte(targets
[t
]);
48 for (size_t i
= 0; i
< count
[t
]; ++i
) {
50 for (size_t position
: bitmask
) {
51 ASSERT_EQ(appearance
[t
][iterated
], position
);
54 ASSERT_EQ(count
[t
], iterated
);
59 TEST(LlvmLibcHashTableBitMaskTest
, MaskAvailable
) {
60 uint8_t values
[3] = {0x00, 0x0F, 0x80};
62 for (size_t i
= 0; i
< sizeof(Group
); ++i
) {
65 int data
[sizeof(uintptr_t) / sizeof(int)];
70 uintptr_t random
= cpp::bit_cast
<uintptr_t>(data
);
72 ASSERT_FALSE(Group::load(array
.data
).mask_available().any_bit_set());
75 for (size_t j
= 0; j
< sizeof(Group
); ++j
) {
78 size_t sample_space
= 2 + (j
> i
);
79 size_t choice
= random
% sample_space
;
80 random
/= sizeof(values
);
81 array
.data
[j
] = values
[choice
];
84 auto mask
= Group::load(array
.data
).mask_available();
85 ASSERT_TRUE(mask
.any_bit_set());
86 ASSERT_EQ(mask
.lowest_set_bit_nonzero(), i
);
89 } // namespace internal
90 } // namespace LIBC_NAMESPACE_DECL