Files
bitserial/tests/bitserial_tests.cpp

625 lines
20 KiB
C++

#include "catch_amalgamated.hpp"
#include <bit>
#include <bitserial/bitserial.hpp>
#include <cmath>
#include <cstdint>
#include <limits>
#include <span>
#include <vector>
// =============================================================================
// 1) Unsigned integers + cross-byte bit widths
// =============================================================================
#define UINT_FIELDS(BSF) \
BSF##_F(std::uint8_t, a, 4) BSF##_F(std::uint8_t, b, 4) \
BSF##_F(std::uint16_t, c, 12) BSF##_F(std::uint32_t, d, 20) \
BSF##_F(std::uint64_t, e, 40)
BITSERIAL_DEFINE(UnsignedPkt, UINT_FIELDS);
TEST_CASE("unsigned integers with cross-byte bit widths") {
static_assert(bitserial::bit_size_v<UnsignedPkt> == 4 + 4 + 12 + 20 + 40);
static_assert(
bitserial::byte_size_v<UnsignedPkt> == (4 + 4 + 12 + 20 + 40 + 7) / 8
);
UnsignedPkt x{
.a = 0xA, .b = 0x5, .c = 0xABC, .d = 0xFEDCB, .e = 0x1122334455ull
};
auto y = bitserial::from_bytes<UnsignedPkt>(bitserial::to_bytes(x));
CHECK(y.a == 0xA);
CHECK(y.b == 0x5);
CHECK(y.c == 0xABC);
CHECK(y.d == 0xFEDCB);
CHECK(y.e == 0x1122334455ull);
UnsignedPkt overflow{
.a = 0xFF, // truncated to 0xF
.b = 0xFF, // 0xF
.c = 0xFFFF, // 0xFFF
.d = 0xFFFFFFFFu, // 0xFFFFF
.e = ~0ull,
};
auto y2 = bitserial::from_bytes<UnsignedPkt>(bitserial::to_bytes(overflow));
CHECK(y2.a == 0xF);
CHECK(y2.b == 0xF);
CHECK(y2.c == 0xFFF);
CHECK(y2.d == 0xFFFFFu);
CHECK(y2.e == (std::uint64_t{1} << 40) - 1);
}
// =============================================================================
// 2) Signed integers: positive, negative, boundary, bit width truncation and sign extension
// =============================================================================
#define INT_FIELDS(BSF) \
BSF##_F(std::int8_t, i8) BSF##_F(std::int16_t, i16) \
BSF##_F(std::int32_t, i32) BSF##_F(std::int64_t, i64) \
BSF##_F(std::int16_t, small, 10) /* -512..511 */ \
BSF##_F(std::int32_t, mid, 20) /* -524288..524287 */ \
BSF##_F(std::int64_t, big, 33) /* crosses 4 bytes */
BITSERIAL_DEFINE(SignedPkt, INT_FIELDS);
TEST_CASE("signed integers: positive values") {
SignedPkt x{
.i8 = 0x12,
.i16 = 0x1234,
.i32 = 0x12345678,
.i64 = 0x1122334455667788ll,
.small = 100,
.mid = 12345,
.big = 123456789ll,
};
auto y = bitserial::from_bytes<SignedPkt>(bitserial::to_bytes(x));
CHECK(y.i8 == 0x12);
CHECK(y.i16 == 0x1234);
CHECK(y.i32 == 0x12345678);
CHECK(y.i64 == 0x1122334455667788ll);
CHECK(y.small == 100);
CHECK(y.mid == 12345);
CHECK(y.big == 123456789ll);
}
TEST_CASE("signed integers: negative values") {
SignedPkt x{
.i8 = -1,
.i16 = -2,
.i32 = -3,
.i64 = -4,
.small = -1,
.mid = -1,
.big = -1,
};
auto y = bitserial::from_bytes<SignedPkt>(bitserial::to_bytes(x));
CHECK(y.i8 == -1);
CHECK(y.i16 == -2);
CHECK(y.i32 == -3);
CHECK(y.i64 == -4);
CHECK(y.small == -1);
CHECK(y.mid == -1);
CHECK(y.big == -1);
}
TEST_CASE("signed integers: boundary values") {
using L8 = std::numeric_limits<std::int8_t>;
using L16 = std::numeric_limits<std::int16_t>;
using L32 = std::numeric_limits<std::int32_t>;
using L64 = std::numeric_limits<std::int64_t>;
SignedPkt x_min{
.i8 = L8::min(),
.i16 = L16::min(),
.i32 = L32::min(),
.i64 = L64::min(),
.small = -512,
.mid = -524288,
.big = -(1ll << 32),
};
auto y = bitserial::from_bytes<SignedPkt>(bitserial::to_bytes(x_min));
CHECK(y.i8 == L8::min());
CHECK(y.i16 == L16::min());
CHECK(y.i32 == L32::min());
CHECK(y.i64 == L64::min());
CHECK(y.small == -512);
CHECK(y.mid == -524288);
CHECK(y.big == -(1ll << 32));
SignedPkt x_max{
.i8 = L8::max(),
.i16 = L16::max(),
.i32 = L32::max(),
.i64 = L64::max(),
.small = 511,
.mid = 524287,
.big = (1ll << 32) - 1,
};
auto y2 = bitserial::from_bytes<SignedPkt>(bitserial::to_bytes(x_max));
CHECK(y2.i8 == L8::max());
CHECK(y2.i16 == L16::max());
CHECK(y2.i32 == L32::max());
CHECK(y2.i64 == L64::max());
CHECK(y2.small == 511);
CHECK(y2.mid == 524287);
CHECK(y2.big == (1ll << 32) - 1);
}
// =============================================================================
// 3) bool: single bit and multi-bit
// =============================================================================
#define BOOL_FIELDS(BSF) \
BSF##_F(bool, a) BSF##_F(bool, b) BSF##_F(bool, c, 4) BSF##_F(bool, d, 8) \
BSF##_F(std::uint8_t, pad, 2)
BITSERIAL_DEFINE(BoolPkt, BOOL_FIELDS);
TEST_CASE("bool fields: single and multi-bit") {
static_assert(bitserial::byte_size_v<BoolPkt> == 2);
BoolPkt x{.a = true, .b = false, .c = true, .d = true, .pad = 3};
auto y = bitserial::from_bytes<BoolPkt>(bitserial::to_bytes(x));
CHECK(y.a == true);
CHECK(y.b == false);
CHECK(y.c == true);
CHECK(y.d == true);
CHECK(y.pad == 3);
BoolPkt z{.a = false, .b = false, .c = false, .d = false, .pad = 0};
auto z2 = bitserial::from_bytes<BoolPkt>(bitserial::to_bytes(z));
CHECK(z2.a == false);
CHECK(z2.d == false);
}
// =============================================================================
// 4) float / double: with special values
// =============================================================================
#define FLOAT_FIELDS(BSF) \
BSF##_F(float, f1) BSF##_F(float, f2) BSF##_F(double, d1) BSF##_F(double, d2)
BITSERIAL_DEFINE(FloatPkt, FLOAT_FIELDS);
TEST_CASE("float and double: basic roundtrip") {
static_assert(bitserial::byte_size_v<FloatPkt> == 4 + 4 + 8 + 8);
FloatPkt x{
.f1 = 3.14159265f,
.f2 = -2.71828f,
.d1 = 1.41421356237309504,
.d2 = -1e-300,
};
auto y = bitserial::from_bytes<FloatPkt>(bitserial::to_bytes(x));
CHECK(
std::bit_cast<std::uint32_t>(y.f1) == std::bit_cast<std::uint32_t>(x.f1)
);
CHECK(
std::bit_cast<std::uint32_t>(y.f2) == std::bit_cast<std::uint32_t>(x.f2)
);
CHECK(
std::bit_cast<std::uint64_t>(y.d1) == std::bit_cast<std::uint64_t>(x.d1)
);
CHECK(
std::bit_cast<std::uint64_t>(y.d2) == std::bit_cast<std::uint64_t>(x.d2)
);
}
TEST_CASE("float and double: special values (inf, nan, -0, subnormal)") {
const float pinf = std::numeric_limits<float>::infinity();
const float ninf = -pinf;
const float qnan = std::numeric_limits<float>::quiet_NaN();
const double pinfd = std::numeric_limits<double>::infinity();
const double mzero = -0.0;
const float sub = std::numeric_limits<float>::denorm_min();
FloatPkt x{.f1 = pinf, .f2 = ninf, .d1 = pinfd, .d2 = mzero};
auto y = bitserial::from_bytes<FloatPkt>(bitserial::to_bytes(x));
CHECK((std::isinf(y.f1) && y.f1 > 0));
CHECK((std::isinf(y.f2) && y.f2 < 0));
CHECK(std::isinf(y.d1));
CHECK(
std::bit_cast<std::uint64_t>(y.d2) == std::bit_cast<std::uint64_t>(mzero)
);
FloatPkt z{.f1 = qnan, .f2 = sub, .d1 = 0.0, .d2 = 0.0};
auto z2 = bitserial::from_bytes<FloatPkt>(bitserial::to_bytes(z));
CHECK(std::isnan(z2.f1));
CHECK(
std::bit_cast<std::uint32_t>(z2.f2) == std::bit_cast<std::uint32_t>(sub)
);
}
// =============================================================================
// 5) Nested structures
// =============================================================================
#define INNER_FIELDS(BSF) \
BSF##_F(std::uint8_t, tag, 4) BSF##_F(std::uint16_t, val, 12)
BITSERIAL_DEFINE(Inner, INNER_FIELDS);
#define OUTER_FIELDS(BSF) \
BSF##_F(std::uint8_t, hdr) BSF##_NEST(Inner, a) BSF##_NEST(Inner, b) \
BSF##_F(std::uint8_t, ftr)
BITSERIAL_DEFINE(Outer, OUTER_FIELDS);
TEST_CASE("nested structs") {
static_assert(bitserial::bit_size_v<Inner> == 16);
static_assert(bitserial::bit_size_v<Outer> == 8 + 16 + 16 + 8);
Outer x{
.hdr = 0xAB,
.a = {.tag = 0x3, .val = 0x123},
.b = {.tag = 0xC, .val = 0xDEF},
.ftr = 0xCD,
};
auto y = bitserial::from_bytes<Outer>(bitserial::to_bytes(x));
CHECK(y.hdr == 0xAB);
CHECK(y.a.tag == 0x3);
CHECK(y.a.val == 0x123);
CHECK(y.b.tag == 0xC);
CHECK(y.b.val == 0xDEF);
CHECK(y.ftr == 0xCD);
}
// =============================================================================
// 6) Fixed-length arrays (natural bit width / custom bit width)
// =============================================================================
#define ARR_NATURAL_FIELDS(BSF) \
BSF##_ARR(std::uint8_t, bytes6, 6) BSF##_ARR(std::uint16_t, words3, 3)
BITSERIAL_DEFINE(ArrNat, ARR_NATURAL_FIELDS);
#define ARR_PACKED_FIELDS(BSF) \
BSF##_ARR(std::uint16_t, q12, 5, 12) /* 5 x 12 bits = 60 bits */ \
BSF##_ARR(std::int16_t, s10, 4, 10) /* 4 x 10 bits signed = 40 bits */ \
BSF##_F(std::uint8_t, pad, 4)
BITSERIAL_DEFINE(ArrPacked, ARR_PACKED_FIELDS);
TEST_CASE("arrays: natural bit widths") {
static_assert(bitserial::byte_size_v<ArrNat> == 6 + 6);
ArrNat x{};
for (int i = 0; i < 6; ++i)
x.bytes6[i] = static_cast<std::uint8_t>(i + 1);
x.words3[0] = 0x1111;
x.words3[1] = 0x2222;
x.words3[2] = 0x3333;
auto y = bitserial::from_bytes<ArrNat>(bitserial::to_bytes(x));
for (int i = 0; i < 6; ++i)
CHECK(y.bytes6[i] == i + 1);
CHECK(y.words3[0] == 0x1111);
CHECK(y.words3[1] == 0x2222);
CHECK(y.words3[2] == 0x3333);
}
TEST_CASE("arrays: packed with custom bit widths") {
static_assert(bitserial::bit_size_v<ArrPacked> == 60 + 40 + 4);
ArrPacked x{};
x.q12[0] = 0xABC;
x.q12[1] = 0x123;
x.q12[2] = 0xFFF;
x.q12[3] = 0x000;
x.q12[4] = 0x555;
x.s10[0] = -1;
x.s10[1] = 511;
x.s10[2] = -512;
x.s10[3] = 0;
x.pad = 0xA;
auto y = bitserial::from_bytes<ArrPacked>(bitserial::to_bytes(x));
CHECK(y.q12[0] == 0xABC);
CHECK(y.q12[1] == 0x123);
CHECK(y.q12[2] == 0xFFF);
CHECK(y.q12[3] == 0x000);
CHECK(y.q12[4] == 0x555);
CHECK(y.s10[0] == -1);
CHECK(y.s10[1] == 511);
CHECK(y.s10[2] == -512);
CHECK(y.s10[3] == 0);
CHECK(y.pad == 0xA);
}
// =============================================================================
// 7) Bidirectional HEX and bytes conversion
// =============================================================================
TEST_CASE("hex roundtrip: upper and lowercase") {
UnsignedPkt x{
.a = 0xA, .b = 0x5, .c = 0xABC, .d = 0xFEDCB, .e = 0xCAFEBABEFEull
};
auto hex_upper = bitserial::to_hex(x, /*upper=*/true);
auto hex_lower = bitserial::to_hex(x, /*upper=*/false);
CHECK(hex_upper.size() == bitserial::byte_size_v<UnsignedPkt> * 2);
CHECK(hex_lower.size() == bitserial::byte_size_v<UnsignedPkt> * 2);
auto y1 = bitserial::from_hex<UnsignedPkt>(hex_upper);
auto y2 = bitserial::from_hex<UnsignedPkt>(hex_lower);
CHECK(y1.a == x.a);
CHECK(y1.e == x.e);
CHECK(y2.c == x.c);
CHECK(y2.d == x.d);
}
// =============================================================================
// 8) Byte order: manual verification of MSB-first bit layout
// =============================================================================
TEST_CASE("byte order: MSB-first layout verification") {
UnsignedPkt x{
.a = 0xA,
.b = 0x5,
.c = 0xABC,
.d = 0xFEDCB,
.e = 0x1122334455ull,
};
auto buf = bitserial::to_bytes(x);
CHECK(buf[0] == 0xA5);
CHECK(buf[1] == 0xAB);
CHECK(buf[2] == 0xCF);
CHECK(buf[3] == 0xED);
CHECK(buf[4] == 0xCB);
CHECK(buf[5] == 0x11);
CHECK(buf[6] == 0x22);
CHECK(buf[7] == 0x33);
CHECK(buf[8] == 0x44);
CHECK(buf[9] == 0x55);
}
// =============================================================================
// 9) Boundary and error handling
// =============================================================================
TEST_CASE("error handling: buffer too small") {
UnsignedPkt x{};
std::array<std::uint8_t, bitserial::byte_size_v<UnsignedPkt> - 1> small{};
CHECK_THROWS_AS(
bitserial::to_bytes(x, std::span<std::uint8_t>(small)), std::out_of_range
);
CHECK_THROWS_AS(
bitserial::from_bytes<UnsignedPkt>(std::span<const std::uint8_t>(small)),
std::out_of_range
);
}
TEST_CASE("error handling: hex parsing errors") {
CHECK_THROWS_AS(
bitserial::from_hex<UnsignedPkt>("DEAD"), std::invalid_argument
);
std::string bad(bitserial::byte_size_v<UnsignedPkt> * 2, 'X');
CHECK_THROWS_AS(bitserial::from_hex<UnsignedPkt>(bad), std::invalid_argument);
}
// =============================================================================
// 10) Real protocol example: first 12 bytes of IPv4 header
// =============================================================================
#define IPV4_FIELDS(BSF) \
BSF##_F(std::uint8_t, version, 4) BSF##_F(std::uint8_t, ihl, 4) \
BSF##_F(std::uint8_t, tos) BSF##_F(std::uint16_t, total_len) \
BSF##_F(std::uint16_t, id) BSF##_F(std::uint8_t, flags, 3) \
BSF##_F(std::uint16_t, frag_off, 13) BSF##_F(std::uint8_t, ttl) \
BSF##_F(std::uint8_t, proto) \
BSF##_F(std::uint16_t, checksum)
BITSERIAL_DEFINE(IPv4Head, IPV4_FIELDS);
TEST_CASE("real protocol: IPv4 header layout") {
static_assert(bitserial::byte_size_v<IPv4Head> == 12);
IPv4Head h{
.version = 4,
.ihl = 5,
.tos = 0,
.total_len = 0x0073,
.id = 0x1234,
.flags = 0b010,
.frag_off = 0,
.ttl = 64,
.proto = 17,
.checksum = 0xB861,
};
const auto buf = bitserial::to_bytes(h);
const std::uint8_t expect[] = {
0x45, 0x00, 0x00, 0x73, 0x12, 0x34, 0x40, 0x00, 0x40, 0x11, 0xB8, 0x61,
};
for (std::size_t i = 0; i < sizeof(expect); ++i)
CHECK(buf[i] == expect[i]);
CHECK(bitserial::to_hex(h) == "45000073123440004011B861");
auto h2 = bitserial::from_hex<IPv4Head>("45000073123440004011B861");
CHECK(h2.version == 4);
CHECK(h2.ihl == 5);
CHECK(h2.flags == 0b010);
CHECK(h2.frag_off == 0);
CHECK(h2.ttl == 64);
CHECK(h2.proto == 17);
CHECK(h2.checksum == 0xB861);
}
// =============================================================================
// 11) Comprehensive stress: all scalar types + nested + arrays round trip
// =============================================================================
#define ALL_INNER_FIELDS(BSF) BSF##_F(std::int32_t, a) BSF##_F(double, b)
BITSERIAL_DEFINE(AllInner, ALL_INNER_FIELDS);
#define ALL_FIELDS(BSF) \
BSF##_F(bool, flag1) BSF##_F(bool, flag2, 3) BSF##_F(std::uint8_t, u8) \
BSF##_F(std::int8_t, s8) BSF##_F(std::uint16_t, u16) \
BSF##_F(std::int16_t, s16) BSF##_F(std::uint32_t, u32) \
BSF##_F(std::int32_t, s32) BSF##_F(std::uint64_t, u64) \
BSF##_F(std::int64_t, s64) BSF##_F(float, f32) \
BSF##_F(double, f64) BSF##_F(std::uint16_t, packed, 11) \
BSF##_F(std::int32_t, spacked, 19) \
BSF##_NEST(AllInner, inner) \
BSF##_ARR(std::uint8_t, bytes, 4) \
BSF##_ARR(std::int16_t, q9, 3, 9) \
BSF##_F(std::uint8_t, tail, 6)
BITSERIAL_DEFINE(AllPkt, ALL_FIELDS);
TEST_CASE("comprehensive: all types combined stress test") {
AllPkt x{
.flag1 = true,
.flag2 = true,
.u8 = 0xAB,
.s8 = -42,
.u16 = 0xBEEF,
.s16 = -32000,
.u32 = 0xDEADBEEFu,
.s32 = -123456789,
.u64 = 0x0123456789ABCDEFull,
.s64 = std::numeric_limits<std::int64_t>::min() + 1,
.f32 = -3.5f,
.f64 = 2.718281828459045,
.packed = 0x7FF,
.spacked = -262144,
.inner = {.a = -7, .b = 1.5},
.bytes = {0x11, 0x22, 0x33, 0x44},
.q9 = {-256, 0, 255},
.tail = 0x2A,
};
auto y = bitserial::from_bytes<AllPkt>(bitserial::to_bytes(x));
CHECK(y.flag1 == true);
CHECK(y.flag2 == true);
CHECK(y.u8 == 0xAB);
CHECK(y.s8 == -42);
CHECK(y.u16 == 0xBEEF);
CHECK(y.s16 == -32000);
CHECK(y.u32 == 0xDEADBEEFu);
CHECK(y.s32 == -123456789);
CHECK(y.u64 == 0x0123456789ABCDEFull);
CHECK(y.s64 == std::numeric_limits<std::int64_t>::min() + 1);
CHECK(
std::bit_cast<std::uint32_t>(y.f32) == std::bit_cast<std::uint32_t>(x.f32)
);
CHECK(
std::bit_cast<std::uint64_t>(y.f64) == std::bit_cast<std::uint64_t>(x.f64)
);
CHECK(y.packed == 0x7FF);
CHECK(y.spacked == -262144);
CHECK(y.inner.a == -7);
CHECK(
std::bit_cast<std::uint64_t>(y.inner.b) ==
std::bit_cast<std::uint64_t>(1.5)
);
CHECK(y.bytes[0] == 0x11);
CHECK(y.bytes[3] == 0x44);
CHECK(y.q9[0] == -256);
CHECK(y.q9[2] == 255);
CHECK(y.tail == 0x2A);
auto hex = bitserial::to_hex(x);
auto z = bitserial::from_hex<AllPkt>(hex);
CHECK(z.u64 == x.u64);
CHECK(z.s64 == x.s64);
}
// =============================================================================
// 12) User-provided buffer: zero-allocation path
// =============================================================================
TEST_CASE("user-provided buffer: zero-allocation path") {
AllPkt x{};
x.u8 = 0x12;
x.s32 = -1;
x.f64 = 3.14;
x.inner = {.a = 100, .b = 0.5};
std::vector<std::uint8_t> buf(bitserial::byte_size_v<AllPkt>);
bitserial::to_bytes(x, std::span<std::uint8_t>(buf));
auto y = bitserial::from_bytes<AllPkt>(std::span<const std::uint8_t>(buf));
CHECK(y.u8 == 0x12);
CHECK(y.s32 == -1);
CHECK(
std::bit_cast<std::uint64_t>(y.f64) == std::bit_cast<std::uint64_t>(3.14)
);
CHECK(y.inner.a == 100);
}
// =============================================================================
// 13) constexpr compile-time serialization
// =============================================================================
constexpr UnsignedPkt make_pkt() {
UnsignedPkt p{
.a = 0x1, .b = 0x2, .c = 0x345, .d = 0x67890, .e = 0xAABBCCDDEE
};
return p;
}
constexpr auto kBytes = bitserial::to_bytes(make_pkt());
static_assert(kBytes[0] == 0x12);
static_assert(kBytes[1] == 0x34);
static_assert(kBytes[2] == 0x56);
static_assert(kBytes[3] == 0x78);
static_assert(kBytes[4] == 0x90);
static_assert(kBytes[5] == 0xAA);
static_assert(kBytes[9] == 0xEE);
TEST_CASE("constexpr compile-time serialization") {
CHECK(kBytes[0] == 0x12);
CHECK(kBytes[2] == 0x56);
CHECK(kBytes[9] == 0xEE);
}
template <std::size_t Bits> void check_bulk_pack_unpack_edges() {
for (std::size_t n = 0; n <= 257; ++n) {
const std::size_t packed_size = (n * Bits + 7) / 8;
std::vector<std::uint8_t> src(n);
std::vector<std::uint8_t> packed_ref(packed_size, 0);
std::vector<std::uint8_t> packed_bulk(packed_size, 0);
std::vector<std::uint8_t> out_ref(n);
std::vector<std::uint8_t> out_bulk(n);
for (std::size_t i = 0; i < n; ++i) {
src[i] =
static_cast<std::uint8_t>((i * 37 + n * 11) & ((1u << Bits) - 1u));
}
std::size_t bit_pos = 0;
for (std::uint8_t x : src) {
bitserial::write_bits(packed_ref, bit_pos, Bits, x);
bit_pos += Bits;
}
bitserial::pack_bits<Bits>(src, packed_bulk);
CHECK(packed_bulk == packed_ref);
bit_pos = 0;
for (std::uint8_t &x : out_ref) {
x = static_cast<std::uint8_t>(
bitserial::read_bits(packed_ref, bit_pos, Bits)
);
bit_pos += Bits;
}
bitserial::unpack_bits<Bits>(packed_ref, out_bulk);
CHECK(out_bulk == out_ref);
}
}
TEST_CASE("bulk BAQ pack/unpack matches bit loop for edge sizes") {
check_bulk_pack_unpack_edges<3>();
check_bulk_pack_unpack_edges<4>();
check_bulk_pack_unpack_edges<5>();
}