| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_ENDIAN_TESTS_ENDIAN_INTEGER_HPP |
| 4 |
#define MPT_ENDIAN_TESTS_ENDIAN_INTEGER_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect_compiler.hpp" |
| 9 |
#include "mpt/base/integer.hpp" |
| 10 |
#include "mpt/base/macros.hpp" |
| 11 |
#include "mpt/base/namespace.hpp" |
| 12 |
#include "mpt/endian/integer.hpp" |
| 13 |
#include "mpt/test/test.hpp" |
| 14 |
#include "mpt/test/test_macros.hpp" |
| 15 |
|
| 16 |
#include <limits> |
| 17 |
|
| 18 |
#include <cstring> |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace mpt { |
| 23 |
inline namespace MPT_INLINE_NS { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
namespace tests { |
| 28 |
namespace endian { |
| 29 |
namespace integer { |
| 30 |
|
| 31 |
#if MPT_COMPILER_CLANG |
| 32 |
#pragma clang diagnostic push |
| 33 |
#pragma clang diagnostic ignored "-Wglobal-constructors" |
| 34 |
#endif |
| 35 |
MPT_TEST_GROUP_INLINE("mpt/endian/integer") |
| 36 |
#if MPT_COMPILER_CLANG |
| 37 |
#pragma clang diagnostic pop |
| 38 |
#endif |
| 39 |
{ |
| 40 |
static_assert(std::numeric_limits<int8le>::min() == std::numeric_limits<int8>::min()); |
| 41 |
static_assert(std::numeric_limits<uint8le>::min() == std::numeric_limits<uint8>::min()); |
| 42 |
|
| 43 |
static_assert(std::numeric_limits<int16le>::min() == std::numeric_limits<int16>::min()); |
| 44 |
static_assert(std::numeric_limits<uint16le>::min() == std::numeric_limits<uint16>::min()); |
| 45 |
|
| 46 |
static_assert(std::numeric_limits<int32le>::min() == std::numeric_limits<int32>::min()); |
| 47 |
static_assert(std::numeric_limits<uint32le>::min() == std::numeric_limits<uint32>::min()); |
| 48 |
|
| 49 |
static_assert(std::numeric_limits<int64le>::min() == std::numeric_limits<int64>::min()); |
| 50 |
static_assert(std::numeric_limits<uint64le>::min() == std::numeric_limits<uint64>::min()); |
| 51 |
|
| 52 |
static_assert(std::numeric_limits<int8le>::max() == std::numeric_limits<int8>::max()); |
| 53 |
static_assert(std::numeric_limits<uint8le>::max() == std::numeric_limits<uint8>::max()); |
| 54 |
|
| 55 |
static_assert(std::numeric_limits<int16le>::max() == std::numeric_limits<int16>::max()); |
| 56 |
static_assert(std::numeric_limits<uint16le>::max() == std::numeric_limits<uint16>::max()); |
| 57 |
|
| 58 |
static_assert(std::numeric_limits<int32le>::max() == std::numeric_limits<int32>::max()); |
| 59 |
static_assert(std::numeric_limits<uint32le>::max() == std::numeric_limits<uint32>::max()); |
| 60 |
|
| 61 |
static_assert(std::numeric_limits<int64le>::max() == std::numeric_limits<int64>::max()); |
| 62 |
static_assert(std::numeric_limits<uint64le>::max() == std::numeric_limits<uint64>::max()); |
| 63 |
|
| 64 |
struct test_endian_constexpr { |
| 65 |
static MPT_CONSTEXPR20_FUN int32le test(uint32 x) { |
| 66 |
int32le foo{}; |
| 67 |
foo = x; |
| 68 |
return foo; |
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
MPT_CONSTEXPR20_VAR int32le foo = test_endian_constexpr::test(23); |
| 73 |
static_cast<void>(foo); |
| 74 |
|
| 75 |
// Packed integers with defined endianness |
| 76 |
{ |
| 77 |
int8le le8; |
| 78 |
le8.set(-128); |
| 79 |
int8be be8; |
| 80 |
be8.set(-128); |
| 81 |
MPT_TEST_EXPECT_EQUAL(le8, -128); |
| 82 |
MPT_TEST_EXPECT_EQUAL(be8, -128); |
| 83 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le8, "\x80", 1), 0); |
| 84 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be8, "\x80", 1), 0); |
| 85 |
int16le le16; |
| 86 |
le16.set(0x1234); |
| 87 |
int16be be16; |
| 88 |
be16.set(0x1234); |
| 89 |
MPT_TEST_EXPECT_EQUAL(le16, 0x1234); |
| 90 |
MPT_TEST_EXPECT_EQUAL(be16, 0x1234); |
| 91 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le16, "\x34\x12", 2), 0); |
| 92 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be16, "\x12\x34", 2), 0); |
| 93 |
uint32le le32; |
| 94 |
le32.set(0xFFEEDDCCu); |
| 95 |
uint32be be32; |
| 96 |
be32.set(0xFFEEDDCCu); |
| 97 |
MPT_TEST_EXPECT_EQUAL(le32, 0xFFEEDDCCu); |
| 98 |
MPT_TEST_EXPECT_EQUAL(be32, 0xFFEEDDCCu); |
| 99 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le32, "\xCC\xDD\xEE\xFF", 4), 0); |
| 100 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be32, "\xFF\xEE\xDD\xCC", 4), 0); |
| 101 |
uint64le le64; |
| 102 |
le64.set(0xDEADC0DE15C0FFEEull); |
| 103 |
uint64be be64; |
| 104 |
be64.set(0xDEADC0DE15C0FFEEull); |
| 105 |
MPT_TEST_EXPECT_EQUAL(le64, 0xDEADC0DE15C0FFEEull); |
| 106 |
MPT_TEST_EXPECT_EQUAL(be64, 0xDEADC0DE15C0FFEEull); |
| 107 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le64, "\xEE\xFF\xC0\x15\xDE\xC0\xAD\xDE", 8), 0); |
| 108 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be64, "\xDE\xAD\xC0\xDE\x15\xC0\xFF\xEE", 8), 0); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
} // namespace integer |
| 113 |
} // namespace endian |
| 114 |
} // namespace tests |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
} // namespace MPT_INLINE_NS |
| 119 |
} // namespace mpt |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
#endif // MPT_ENDIAN_TESTS_ENDIAN_INTEGER_HPP |