AK: Add randomized tests for BuiltinWrappers

This commit is contained in:
Martin Janiczek 2023-10-11 13:45:22 +02:00 committed by Tim Schumacher
parent 8942041eee
commit 864e83079c

View file

@ -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);
}