mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-07 00:19:27 +00:00
AK: Add randomized tests for BuiltinWrappers
This commit is contained in:
parent
8942041eee
commit
864e83079c
|
@ -9,6 +9,8 @@
|
|||
#include <AK/BuiltinWrappers.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
using namespace Test::Randomized;
|
||||
|
||||
TEST_CASE(wrapped_popcount)
|
||||
{
|
||||
EXPECT_EQ(popcount(NumericLimits<u8>::max()), 8);
|
||||
|
@ -65,3 +67,38 @@ TEST_CASE(wrapped_count_required_bits)
|
|||
EXPECT_EQ(count_required_bits(0b1111u), 4ul);
|
||||
EXPECT_EQ(count_required_bits(NumericLimits<u32>::max()), sizeof(u32) * 8);
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(count_leading_zeroes)
|
||||
{
|
||||
// count_leading_zeroes(0b000...0001000...000)
|
||||
// == count_leading_zeroes(0b000...0001___...___) (where _ is 0 or 1)
|
||||
GEN(e, Gen::unsigned_int(0, 63));
|
||||
auto power_of_two = 1ULL << e;
|
||||
|
||||
// We add random one-bits below the leftmost (and only) one-bit.
|
||||
// This shouldn't change the output of count_leading_zeroes because
|
||||
// the function should only care about the most significant one.
|
||||
GEN(below, Gen::unsigned_int(0, power_of_two - 1));
|
||||
auto n = power_of_two + below;
|
||||
|
||||
EXPECT_EQ(count_leading_zeroes(n), count_leading_zeroes(power_of_two));
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(count_required_bits)
|
||||
{
|
||||
// count_required_bits(n) == log2(n) + 1
|
||||
|
||||
// log2(0) is -infinity, we don't care about that
|
||||
GEN(n, Gen::unsigned_int(1, NumericLimits<u32>::max()));
|
||||
|
||||
size_t expected = AK::log2(static_cast<double>(n)) + 1;
|
||||
|
||||
EXPECT_EQ(count_required_bits(n), expected);
|
||||
}
|
||||
|
||||
RANDOMIZED_TEST_CASE(bit_scan_forward_count_trailing_zeroes)
|
||||
{
|
||||
// Behaviour for 0 differs, so we skip it.
|
||||
GEN(n, Gen::unsigned_int(1, 1 << 31));
|
||||
EXPECT_EQ(bit_scan_forward(n), count_trailing_zeroes(n) + 1);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue