229 lines
7.9 KiB
C++
229 lines
7.9 KiB
C++
#include <Void/Core/Bits.hpp>
|
|
#include <Void/Core/CheckedBits.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Suite.hpp>
|
|
|
|
#include <cstdint>
|
|
#include <limits>
|
|
|
|
namespace
|
|
{
|
|
using Void::Testing::Test;
|
|
|
|
struct Bits : Void::Testing::Suite<Bits> {
|
|
// ── popcount ──────────────────────────────────────────────
|
|
Test popcount_zero()
|
|
{
|
|
Void::Testing::expect_eq(Void::Bits::popcount(0u), 0);
|
|
}
|
|
Test popcount_all_ones_u8()
|
|
{
|
|
Void::Testing::expect_eq(
|
|
Void::Bits::popcount(std::uint8_t{0xFF}), 8);
|
|
}
|
|
Test popcount_u32()
|
|
{
|
|
Void::Testing::expect_eq(
|
|
Void::Bits::popcount(std::uint32_t{0x0F0F0F0F}), 16);
|
|
}
|
|
|
|
// ── test/set/clear/toggle ─────────────────────────────────
|
|
Test test_bit_set()
|
|
{
|
|
Void::Testing::expect(
|
|
Void::Bits::test_bit(std::uint8_t{0b00000100u}, 2));
|
|
}
|
|
Test test_bit_clear()
|
|
{
|
|
Void::Testing::expect(
|
|
!Void::Bits::test_bit(std::uint8_t{0b00000100u}, 0));
|
|
}
|
|
Test set_bit_works()
|
|
{
|
|
Void::Testing::expect_eq(
|
|
Void::Bits::set_bit(std::uint8_t{0u}, 3),
|
|
std::uint8_t{0b00001000u});
|
|
}
|
|
Test clear_bit_works()
|
|
{
|
|
Void::Testing::expect_eq(
|
|
Void::Bits::clear_bit(std::uint8_t{0xFF}, 3),
|
|
std::uint8_t{0b11110111u});
|
|
}
|
|
Test toggle_bit_works()
|
|
{
|
|
Void::Testing::expect_eq(
|
|
Void::Bits::toggle_bit(std::uint8_t{0u}, 5),
|
|
std::uint8_t{0b00100000u});
|
|
}
|
|
|
|
// ── extract / deposit ─────────────────────────────────────
|
|
Test extract_bits_works()
|
|
{
|
|
Void::Testing::expect_eq(
|
|
Void::Bits::extract_bits(std::uint8_t{0b10110100u}, 4, 4),
|
|
std::uint8_t{0b1011u});
|
|
}
|
|
Test deposit_bits_works()
|
|
{
|
|
Void::Testing::expect_eq(
|
|
Void::Bits::deposit_bits(std::uint8_t{0u},
|
|
std::uint8_t{0b1010u}, 4, 4),
|
|
std::uint8_t{0b10100000u});
|
|
}
|
|
|
|
// ── rotation ──────────────────────────────────────────────
|
|
Test rotl_8()
|
|
{
|
|
Void::Testing::expect_eq(
|
|
Void::Bits::rotl(std::uint8_t{0b10000001u}, 1),
|
|
std::uint8_t{0b00000011u});
|
|
}
|
|
Test rotr_8()
|
|
{
|
|
Void::Testing::expect_eq(
|
|
Void::Bits::rotr(std::uint8_t{0b10000001u}, 1),
|
|
std::uint8_t{0b11000000u});
|
|
}
|
|
|
|
// ── pow2 helpers ──────────────────────────────────────────
|
|
Test has_single_bit()
|
|
{
|
|
Void::Testing::expect(Void::Bits::has_single_bit(64u));
|
|
Void::Testing::expect(!Void::Bits::has_single_bit(63u));
|
|
}
|
|
Test ceil_pow2_works()
|
|
{
|
|
Void::Testing::expect_eq(Void::Bits::ceil_pow2(5u), 8u);
|
|
Void::Testing::expect_eq(Void::Bits::ceil_pow2(8u), 8u);
|
|
}
|
|
Test floor_pow2_works()
|
|
{
|
|
Void::Testing::expect_eq(Void::Bits::floor_pow2(5u), 4u);
|
|
}
|
|
|
|
// ── align ─────────────────────────────────────────────────
|
|
Test align_up_works()
|
|
{
|
|
Void::Testing::expect_eq(Void::Bits::align_up(13u, 8u), 16u);
|
|
Void::Testing::expect_eq(Void::Bits::align_up(8u, 8u), 8u);
|
|
}
|
|
Test align_down_works()
|
|
{
|
|
Void::Testing::expect_eq(Void::Bits::align_down(13u, 8u), 8u);
|
|
}
|
|
|
|
// ── masks ─────────────────────────────────────────────────
|
|
Test low_mask_works()
|
|
{
|
|
Void::Testing::expect_eq(Void::Bits::low_mask<std::uint8_t>(4),
|
|
std::uint8_t{0x0F});
|
|
}
|
|
Test range_mask_works()
|
|
{
|
|
Void::Testing::expect_eq(
|
|
Void::Bits::range_mask<std::uint8_t>(2, 5),
|
|
std::uint8_t{0b00111100u});
|
|
}
|
|
|
|
// ── byteswap / endian ─────────────────────────────────────
|
|
Test byteswap_u32()
|
|
{
|
|
Void::Testing::expect_eq(
|
|
Void::Bits::byteswap(std::uint32_t{0x12345678u}),
|
|
std::uint32_t{0x78563412u});
|
|
}
|
|
|
|
// ── checked bits / alignment ──────────────────────────────
|
|
Test checked_extract_boundaries()
|
|
{
|
|
constexpr auto zero =
|
|
Void::Bits::checked_extract(std::uint8_t{0xABu}, 8u, 0u);
|
|
static_assert(zero.has_value() && *zero == 0);
|
|
Void::Testing::expect(zero.has_value() && *zero == std::uint8_t{0});
|
|
|
|
constexpr auto full =
|
|
Void::Bits::checked_extract(std::uint8_t{0xABu}, 0u, 8u);
|
|
static_assert(full.has_value() && *full == std::uint8_t{0xABu});
|
|
Void::Testing::expect(full.has_value() &&
|
|
*full == std::uint8_t{0xABu});
|
|
|
|
constexpr auto mid =
|
|
Void::Bits::checked_extract(std::uint16_t{0xABCDu}, 4u, 8u);
|
|
static_assert(mid.has_value() && *mid == std::uint16_t{0xBCu});
|
|
Void::Testing::expect(mid.has_value() &&
|
|
*mid == std::uint16_t{0xBCu});
|
|
|
|
constexpr auto too_wide =
|
|
Void::Bits::checked_extract(std::uint8_t{0xABu}, 0u, 9u);
|
|
static_assert(!too_wide.has_value() &&
|
|
too_wide.error() == Void::Bits::BitError::invalid_range);
|
|
Void::Testing::expect(!too_wide.has_value());
|
|
|
|
constexpr auto sum_overflow = Void::Bits::checked_extract(
|
|
std::uint8_t{0xABu},
|
|
std::numeric_limits<unsigned>::max(), 1u);
|
|
static_assert(!sum_overflow.has_value() &&
|
|
sum_overflow.error() ==
|
|
Void::Bits::BitError::invalid_range);
|
|
Void::Testing::expect(!sum_overflow.has_value());
|
|
}
|
|
|
|
Test checked_deposit_boundaries()
|
|
{
|
|
constexpr auto zero = Void::Bits::checked_deposit(
|
|
std::uint8_t{0x55u}, std::uint8_t{0xFFu}, 8u, 0u);
|
|
static_assert(zero.has_value() && *zero == std::uint8_t{0x55u});
|
|
Void::Testing::expect(zero.has_value() && *zero == std::uint8_t{0x55u});
|
|
|
|
constexpr auto full = Void::Bits::checked_deposit(
|
|
std::uint8_t{0x55u}, std::uint8_t{0xC3u}, 0u, 8u);
|
|
static_assert(full.has_value() && *full == std::uint8_t{0xC3u});
|
|
Void::Testing::expect(full.has_value() && *full == std::uint8_t{0xC3u});
|
|
|
|
constexpr auto bad = Void::Bits::checked_deposit(
|
|
std::uint8_t{0u}, std::uint8_t{1u}, 6u, 3u);
|
|
static_assert(!bad.has_value() &&
|
|
bad.error() == Void::Bits::BitError::invalid_range);
|
|
Void::Testing::expect(!bad.has_value());
|
|
}
|
|
|
|
Test checked_align_boundaries()
|
|
{
|
|
constexpr auto z = Void::Bits::checked_align_up(16u, 0u);
|
|
static_assert(!z.has_value() &&
|
|
z.error() == Void::Bits::BitError::invalid_alignment);
|
|
Void::Testing::expect(!z.has_value());
|
|
|
|
constexpr auto non_pow2 = Void::Bits::checked_align_up(16u, 3u);
|
|
static_assert(!non_pow2.has_value() &&
|
|
non_pow2.error() ==
|
|
Void::Bits::BitError::invalid_alignment);
|
|
Void::Testing::expect(!non_pow2.has_value());
|
|
|
|
constexpr auto ovf = Void::Bits::checked_align_up(
|
|
std::uint8_t{250u}, std::uint8_t{16u});
|
|
static_assert(!ovf.has_value() &&
|
|
ovf.error() == Void::Bits::BitError::overflow);
|
|
Void::Testing::expect(!ovf.has_value());
|
|
|
|
constexpr auto up = Void::Bits::checked_align_up(
|
|
std::uint8_t{240u}, std::uint8_t{16u});
|
|
static_assert(up.has_value() && *up == std::uint8_t{240u});
|
|
Void::Testing::expect(up.has_value() && *up == std::uint8_t{240u});
|
|
|
|
constexpr auto down = Void::Bits::checked_align_down(
|
|
std::uint8_t{255u}, std::uint8_t{16u});
|
|
static_assert(down.has_value() && *down == std::uint8_t{240u});
|
|
Void::Testing::expect(down.has_value() && *down == std::uint8_t{240u});
|
|
|
|
constexpr auto bad_down =
|
|
Void::Bits::checked_align_down(std::uint8_t{8u}, std::uint8_t{6u});
|
|
static_assert(!bad_down.has_value() &&
|
|
bad_down.error() ==
|
|
Void::Bits::BitError::invalid_alignment);
|
|
Void::Testing::expect(!bad_down.has_value());
|
|
}
|
|
};
|
|
} // namespace
|