libcruft-util/test/endian.cpp

47 lines
1.1 KiB
C++
Raw Normal View History

#include "tap.hpp"
#include "endian.hpp"
2018-05-03 18:32:08 +10:00
#include <cstdint>
///////////////////////////////////////////////////////////////////////////////
int
main (void)
{
cruft::TAP::logger tap;
{
uint32_t a = 0x12345678, b = 0x78563412;
tap.expect_eq (a, cruft::bswap (b), "u32 byteswap");
}
2018-05-03 18:32:08 +10:00
{
static std::uint8_t const bytes[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
tap.expect_eq (cruft::readle<std::uint64_t> (bytes), 0xefcdab8967452301, "readle<u64>");
2018-05-03 18:32:08 +10:00
}
2022-02-07 13:14:45 +11:00
{
static constexpr u32 A = 0x11223344;
static constexpr u32 B = 0x44332211;
f32 const a_f = cruft::cast::bit<f32> (A);
f32 const b_f = cruft::bswap (a_f);
u32 const b_u = cruft::cast::bit<u32> (b_f);
tap.expect_eq (b_u, B, "f32 bswap");
}
2018-05-03 18:32:08 +10:00
{
2022-02-07 13:14:45 +11:00
static constexpr u64 A = 0x1122334455667788;
static constexpr u64 B = 0x8877665544332211;
f64 const a_f = cruft::cast::bit<f64> (A);
f64 const b_f = cruft::bswap (a_f);
u64 const b_u = cruft::cast::bit<u64> (b_f);
tap.expect_eq (b_u, B, "u64 bswap");
}
return tap.status ();
}