/* Copyright 2016-2021 Michele Santullo
* This file is part of "duckhandy".
*
* "duckhandy" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "duckhandy" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "duckhandy". If not, see .
*/
#include "catch2/catch.hpp"
#include "duckhandy/endianness.hpp"
#include
TEST_CASE ("Host to little-endian", "[htole][endianness]") {
using dhandy::htole;
#if __BYTE_ORDER == __BIG_ENDIAN
static_assert(htole(0x12) == 0x12, "Wrong conversion");
static_assert(htole(0x1234) == 0x3412, "Wrong conversion");
static_assert(htole(0x12345678) == 0x78563412, "Wrong conversion");
static_assert(htole(0x123456789ABCDEF0) == 0xF0DEBC9A78563412, "Wrong conversion");
#elif __BYTE_ORDER == __LITTLE_ENDIAN
static_assert(htole(0x12) == 0x12, "Wrong conversion");
static_assert(htole(0x1234) == 0x1234, "Wrong conversion");
static_assert(htole(0x12345678) == 0x12345678, "Wrong conversion");
static_assert(htole(0x123456789ABCDEF0) == 0x123456789ABCDEF0, "Wrong conversion");
#endif
CHECK(htole(0x12) == 0x12);
CHECK(htole(0x1234) == htole16(0x1234));
CHECK(htole(0x12345678) == htole32(0x12345678));
CHECK(htole(0x123456789ABCDEF0) == htole64(0x123456789ABCDEF0));
CHECK(htole(0xFE) == static_cast(0xFE));
CHECK(htole(0xFEDC) == static_cast(htole16(0xFEDC)));
CHECK(htole(0xFEDCBA09) == static_cast(htole32(0xFEDCBA09)));
CHECK(htole(0xFEDCBA0987654321) == static_cast(htole64(0xFEDCBA0987654321)));
}
TEST_CASE ("Little-endian to host", "[letoh][endianness]") {
using dhandy::letoh;
#if __BYTE_ORDER == __BIG_ENDIAN
static_assert(letoh(0x12) == 0x12, "Wrong conversion");
static_assert(letoh(0x1234) == 0x3412, "Wrong conversion");
static_assert(letoh(0x12345678) == 0x78563412, "Wrong conversion");
static_assert(letoh(0x123456789ABCDEF0) == 0xF0DEBC9A78563412, "Wrong conversion");
#elif __BYTE_ORDER == __LITTLE_ENDIAN
static_assert(letoh(0x12) == 0x12, "Wrong conversion");
static_assert(letoh(0x1234) == 0x1234, "Wrong conversion");
static_assert(letoh(0x12345678) == 0x12345678, "Wrong conversion");
static_assert(letoh(0x123456789ABCDEF0) == 0x123456789ABCDEF0, "Wrong conversion");
#endif
CHECK(letoh(0x12) == 0x12);
CHECK(letoh(0x1234) == le16toh(0x1234));
CHECK(letoh(0x12345678) == le32toh(0x12345678));
CHECK(letoh(0x123456789ABCDEF0) == le64toh(0x123456789ABCDEF0));
CHECK(letoh(0xFE) == static_cast(0xFE));
CHECK(letoh(0xFEDC) == static_cast(le16toh(0xFEDC)));
CHECK(letoh(0xFEDCBA09) == static_cast(le32toh(0xFEDCBA09)));
CHECK(letoh(0xFEDCBA0987654321) == static_cast(le64toh(0xFEDCBA0987654321)));
}
TEST_CASE ("Host to big-endian", "[htobe][endianness]") {
using dhandy::htobe;
#if __BYTE_ORDER == __BIG_ENDIAN
static_assert(htobe(0x12) == 0x12, "Wrong conversion");
static_assert(htobe(0x1234) == 0x1234, "Wrong conversion");
static_assert(htobe(0x12345678) == 0x12345678, "Wrong conversion");
static_assert(htobe(0x123456789ABCDEF0) == 0x123456789ABCDEF0, "Wrong conversion");
#elif __BYTE_ORDER == __LITTLE_ENDIAN
static_assert(htobe(0x12) == 0x12, "Wrong conversion");
static_assert(htobe(0x1234) == 0x3412, "Wrong conversion");
static_assert(htobe(0x12345678) == 0x78563412, "Wrong conversion");
static_assert(htobe(0x123456789ABCDEF0) == 0xF0DEBC9A78563412, "Wrong conversion");
#endif
CHECK(htobe(0x12) == 0x12);
CHECK(htobe(0x1234) == htobe16(0x1234));
CHECK(htobe(0x12345678) == htobe32(0x12345678));
CHECK(htobe(0x123456789ABCDEF0) == htobe64(0x123456789ABCDEF0));
CHECK(htobe(0xFE) == static_cast(0xFE));
CHECK(htobe(0xFEDC) == static_cast(htobe16(0xFEDC)));
CHECK(htobe(0xFEDCBA09) == static_cast(htobe32(0xFEDCBA09)));
CHECK(htobe(0xFEDCBA0987654321) == static_cast(htobe64(0xFEDCBA0987654321)));
}
TEST_CASE ("Big-endian to host", "[betoh][endianness]") {
using dhandy::betoh;
#if __BYTE_ORDER == __BIG_ENDIAN
static_assert(betoh(0x12) == 0x12, "Wrong conversion");
static_assert(betoh(0x1234) == 0x1234, "Wrong conversion");
static_assert(betoh(0x12345678) == 0x12345678, "Wrong conversion");
static_assert(betoh(0x123456789ABCDEF0) == 0x123456789ABCDEF0, "Wrong conversion");
#elif __BYTE_ORDER == __LITTLE_ENDIAN
static_assert(betoh(0x12) == 0x12, "Wrong conversion");
static_assert(betoh(0x1234) == 0x3412, "Wrong conversion");
static_assert(betoh(0x12345678) == 0x78563412, "Wrong conversion");
static_assert(betoh(0x123456789ABCDEF0) == 0xF0DEBC9A78563412, "Wrong conversion");
#endif
CHECK(betoh(0x12) == 0x12);
CHECK(betoh(0x1234) == be16toh(0x1234));
CHECK(betoh(0x12345678) == be32toh(0x12345678));
CHECK(betoh(0x123456789ABCDEF0) == be64toh(0x123456789ABCDEF0));
CHECK(betoh(0xFE) == static_cast(0xFE));
CHECK(betoh(0xFEDC) == static_cast(be16toh(0xFEDC)));
CHECK(betoh(0xFEDCBA09) == static_cast(be32toh(0xFEDCBA09)));
CHECK(betoh(0xFEDCBA0987654321) == static_cast(be64toh(0xFEDCBA0987654321)));
}