| #include <stdplus/numeric/endian.hpp> |
| |
| #include <array> |
| #include <cstring> |
| |
| #include <gtest/gtest.h> |
| |
| namespace stdplus |
| { |
| |
| TEST(Byteswap, Swap) |
| { |
| EXPECT_EQ(38, bswap(uint8_t{38})); |
| EXPECT_EQ(38 << 8, bswap(uint16_t{38})); |
| EXPECT_EQ(0x240082fe, bswap(uint32_t{0xfe820024})); |
| EXPECT_EQ(0x240082fe00000000, bswap(uint64_t{0xfe820024})); |
| struct |
| { |
| std::array<char, 4> a = {1, 2, 3, 4}; |
| } s; |
| EXPECT_EQ((std::array<char, 4>{4, 3, 2, 1}), bswap(s).a); |
| EXPECT_EQ(40, ntoh(hton(40))); |
| } |
| |
| TEST(EndianPacked, Uint8) |
| { |
| uint8_unt n = 0; |
| EXPECT_EQ(n.value(), 0); |
| EXPECT_EQ(n, 0); |
| |
| n = 15; |
| EXPECT_EQ(n.value(), 15); |
| EXPECT_EQ(n, 15); |
| } |
| |
| TEST(EndianPacked, Uint32) |
| { |
| uint32_unt n = 0; |
| EXPECT_EQ(n.value(), 0); |
| EXPECT_EQ(n, 0); |
| |
| n = 15; |
| EXPECT_EQ(n.value(), 15); |
| EXPECT_EQ(n, 15); |
| } |
| |
| TEST(EndianPacked, ValidateUnderlying) |
| { |
| uint32_ubt b(15); |
| uint32_ult l = {}; |
| EXPECT_EQ(b, 15); |
| EXPECT_EQ(l, 0); |
| std::memcpy(&l, &b, sizeof(l)); |
| EXPECT_EQ(0x0f000000, l); |
| } |
| |
| TEST(EndianPacked, Format) |
| { |
| EXPECT_EQ(std::format("a {}\n", int32_t{-8456}), |
| std::format("a {}\n", int32_unt{-8456})); |
| EXPECT_EQ(std::format("a {:x}\n", uint32_t{13}), |
| std::format("a {:x}\n", uint32_ult{13})); |
| } |
| |
| } // namespace stdplus |