Initial commit

This commit is contained in:
mxdyeah committed 2026-07-08 01:15:18 +08:00
commit acadc92e68
780 files changed
+468285

No files matched your search

@@ -0,0 +1,81 @@
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
#ifndef MPT_ENDIAN_TESTS_ENDIAN_FLOATINGPOINT_HPP
#define MPT_ENDIAN_TESTS_ENDIAN_FLOATINGPOINT_HPP
#include "mpt/base/detect_compiler.hpp"
#include "mpt/base/memory.hpp"
#include "mpt/base/namespace.hpp"
#include "mpt/endian/floatingpoint.hpp"
#include "mpt/test/test.hpp"
#include "mpt/test/test_macros.hpp"
namespace mpt {
inline namespace MPT_INLINE_NS {
namespace tests {
namespace endian {
namespace floatingpoint {
#if MPT_COMPILER_CLANG
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#endif
MPT_TEST_GROUP_INLINE("mpt/endian/floatingpoint")
#if MPT_COMPILER_CLANG
#pragma clang diagnostic pop
#endif
{
MPT_TEST_EXPECT_EQUAL(mpt::EncodeIEEE754binary32(1.0f), 0x3f800000u);
MPT_TEST_EXPECT_EQUAL(mpt::EncodeIEEE754binary32(-1.0f), 0xbf800000u);
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary32(0x00000000u), 0.0f);
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary32(0x41840000u), 16.5f);
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary32(0x3faa0000u), 1.328125f);
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary32(0xbfaa0000u), -1.328125f);
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary32(0x3f800000u), 1.0f);
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary32(0x00000000u), 0.0f);
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary32(0xbf800000u), -1.0f);
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary32(0x3f800000u), 1.0f);
MPT_TEST_EXPECT_EQUAL(IEEE754binary32LE(1.0f).GetInt32(), 0x3f800000u);
MPT_TEST_EXPECT_EQUAL(IEEE754binary32BE(1.0f).GetInt32(), 0x3f800000u);
MPT_TEST_EXPECT_EQUAL(IEEE754binary32LE(mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x80), mpt::as_byte(0x3f)), 1.0f);
MPT_TEST_EXPECT_EQUAL(IEEE754binary32BE(mpt::as_byte(0x3f), mpt::as_byte(0x80), mpt::as_byte(0x00), mpt::as_byte(0x00)), 1.0f);
MPT_TEST_EXPECT_EQUAL(IEEE754binary32LE(1.0f), IEEE754binary32LE(mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x80), mpt::as_byte(0x3f)));
MPT_TEST_EXPECT_EQUAL(IEEE754binary32BE(1.0f), IEEE754binary32BE(mpt::as_byte(0x3f), mpt::as_byte(0x80), mpt::as_byte(0x00), mpt::as_byte(0x00)));
MPT_TEST_EXPECT_EQUAL(mpt::EncodeIEEE754binary64(1.0), 0x3ff0000000000000ull);
MPT_TEST_EXPECT_EQUAL(mpt::EncodeIEEE754binary64(-1.0), 0xbff0000000000000ull);
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary64(0x0000000000000000ull), 0.0);
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary64(0x4030800000000000ull), 16.5);
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary64(0x3FF5400000000000ull), 1.328125);
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary64(0xBFF5400000000000ull), -1.328125);
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary64(0x3ff0000000000000ull), 1.0);
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary64(0x0000000000000000ull), 0.0);
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary64(0xbff0000000000000ull), -1.0);
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary64(0x3ff0000000000000ull), 1.0);
MPT_TEST_EXPECT_EQUAL(IEEE754binary64LE(1.0).GetInt64(), 0x3ff0000000000000ull);
MPT_TEST_EXPECT_EQUAL(IEEE754binary64BE(1.0).GetInt64(), 0x3ff0000000000000ull);
MPT_TEST_EXPECT_EQUAL(IEEE754binary64LE(mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0xf0), mpt::as_byte(0x3f)), 1.0);
MPT_TEST_EXPECT_EQUAL(IEEE754binary64BE(mpt::as_byte(0x3f), mpt::as_byte(0xf0), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00)), 1.0);
MPT_TEST_EXPECT_EQUAL(IEEE754binary64LE(1.0), IEEE754binary64LE(mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0xf0), mpt::as_byte(0x3f)));
MPT_TEST_EXPECT_EQUAL(IEEE754binary64BE(1.0), IEEE754binary64BE(mpt::as_byte(0x3f), mpt::as_byte(0xf0), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00)));
}
} // namespace floatingpoint
} // namespace endian
} // namespace tests
} // namespace MPT_INLINE_NS
} // namespace mpt
#endif // MPT_ENDIAN_TESTS_ENDIAN_FLOATINGPOINT_HPP
@@ -0,0 +1,148 @@
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
#ifndef MPT_ENDIAN_TESTS_ENDIAN_INT24_HPP
#define MPT_ENDIAN_TESTS_ENDIAN_INT24_HPP
#include "mpt/base/detect_compiler.hpp"
#include "mpt/base/integer.hpp"
#include "mpt/base/macros.hpp"
#include "mpt/base/namespace.hpp"
#include "mpt/endian/int24.hpp"
#include "mpt/endian/integer.hpp"
#include "mpt/test/test.hpp"
#include "mpt/test/test_macros.hpp"
#include <limits>
#include <cstring>
namespace mpt {
inline namespace MPT_INLINE_NS {
namespace tests {
namespace endian {
namespace integer {
#if MPT_COMPILER_CLANG
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#endif
MPT_TEST_GROUP_INLINE("mpt/endian/int24")
#if MPT_COMPILER_CLANG
#pragma clang diagnostic pop
#endif
{
static_assert(std::numeric_limits<int24le>::min() == std::numeric_limits<int24>::min());
static_assert(std::numeric_limits<uint24le>::min() == std::numeric_limits<uint24>::min());
{
int24le le24;
le24.set(int24(0x123456));
int24be be24;
be24.set(int24(0x123456));
MPT_TEST_EXPECT_EQUAL(le24, int24(0x123456));
MPT_TEST_EXPECT_EQUAL(be24, int24(0x123456));
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\x56\x34\x12", 3), 0);
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x12\x34\x56", 3), 0);
}
{
int24le le24;
le24.set(int24(-0x7fffff));
int24be be24;
be24.set(int24(-0x7fffff));
MPT_TEST_EXPECT_EQUAL(le24, int24(-0x7fffff));
MPT_TEST_EXPECT_EQUAL(be24, int24(-0x7fffff));
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\x01\x00\x80", 3), 0);
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x80\x00\x01", 3), 0);
}
{
int24le le24;
le24.set(int24(-0x000001));
int24be be24;
be24.set(int24(-0x000001));
MPT_TEST_EXPECT_EQUAL(le24, int24(-0x000001));
MPT_TEST_EXPECT_EQUAL(be24, int24(-0x000001));
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\xff\xff\xff", 3), 0);
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\xff\xff\xff", 3), 0);
}
{
int24le le24;
le24.set(int24(0x7fffff));
int24be be24;
be24.set(int24(0x7fffff));
MPT_TEST_EXPECT_EQUAL(le24, int24(0x7fffff));
MPT_TEST_EXPECT_EQUAL(be24, int24(0x7fffff));
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\xff\xff\x7f", 3), 0);
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x7f\xff\xff", 3), 0);
}
{
int24le le24;
le24.set(int24(-0x800000));
int24be be24;
be24.set(int24(-0x800000));
MPT_TEST_EXPECT_EQUAL(le24, int24(-0x800000));
MPT_TEST_EXPECT_EQUAL(be24, int24(-0x800000));
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\x00\x00\x80", 3), 0);
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x80\x00\x00", 3), 0);
}
{
uint24le le24;
le24.set(uint24(0x123456));
uint24be be24;
be24.set(uint24(0x123456));
MPT_TEST_EXPECT_EQUAL(le24, uint24(0x123456));
MPT_TEST_EXPECT_EQUAL(be24, uint24(0x123456));
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\x56\x34\x12", 3), 0);
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x12\x34\x56", 3), 0);
}
{
uint24le le24;
le24.set(uint24(0xffffff));
uint24be be24;
be24.set(uint24(0xffffff));
MPT_TEST_EXPECT_EQUAL(le24, uint24(0xffffff));
MPT_TEST_EXPECT_EQUAL(be24, uint24(0xffffff));
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\xff\xff\xff", 3), 0);
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\xff\xff\xff", 3), 0);
}
{
uint24le le24;
le24.set(uint24(0x7fffff));
uint24be be24;
be24.set(uint24(0x7fffff));
MPT_TEST_EXPECT_EQUAL(le24, uint24(0x7fffff));
MPT_TEST_EXPECT_EQUAL(be24, uint24(0x7fffff));
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\xff\xff\x7f", 3), 0);
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x7f\xff\xff", 3), 0);
}
{
uint24le le24;
le24.set(uint24(0x800000));
uint24be be24;
be24.set(uint24(0x800000));
MPT_TEST_EXPECT_EQUAL(le24, uint24(0x800000));
MPT_TEST_EXPECT_EQUAL(be24, uint24(0x800000));
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\x00\x00\x80", 3), 0);
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x80\x00\x00", 3), 0);
}
}
} // namespace integer
} // namespace endian
} // namespace tests
} // namespace MPT_INLINE_NS
} // namespace mpt
#endif // MPT_ENDIAN_TESTS_ENDIAN_INT24_HPP
@@ -0,0 +1,123 @@
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
#ifndef MPT_ENDIAN_TESTS_ENDIAN_INTEGER_HPP
#define MPT_ENDIAN_TESTS_ENDIAN_INTEGER_HPP
#include "mpt/base/detect_compiler.hpp"
#include "mpt/base/integer.hpp"
#include "mpt/base/macros.hpp"
#include "mpt/base/namespace.hpp"
#include "mpt/endian/integer.hpp"
#include "mpt/test/test.hpp"
#include "mpt/test/test_macros.hpp"
#include <limits>
#include <cstring>
namespace mpt {
inline namespace MPT_INLINE_NS {
namespace tests {
namespace endian {
namespace integer {
#if MPT_COMPILER_CLANG
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#endif
MPT_TEST_GROUP_INLINE("mpt/endian/integer")
#if MPT_COMPILER_CLANG
#pragma clang diagnostic pop
#endif
{
static_assert(std::numeric_limits<int8le>::min() == std::numeric_limits<int8>::min());
static_assert(std::numeric_limits<uint8le>::min() == std::numeric_limits<uint8>::min());
static_assert(std::numeric_limits<int16le>::min() == std::numeric_limits<int16>::min());
static_assert(std::numeric_limits<uint16le>::min() == std::numeric_limits<uint16>::min());
static_assert(std::numeric_limits<int32le>::min() == std::numeric_limits<int32>::min());
static_assert(std::numeric_limits<uint32le>::min() == std::numeric_limits<uint32>::min());
static_assert(std::numeric_limits<int64le>::min() == std::numeric_limits<int64>::min());
static_assert(std::numeric_limits<uint64le>::min() == std::numeric_limits<uint64>::min());
static_assert(std::numeric_limits<int8le>::max() == std::numeric_limits<int8>::max());
static_assert(std::numeric_limits<uint8le>::max() == std::numeric_limits<uint8>::max());
static_assert(std::numeric_limits<int16le>::max() == std::numeric_limits<int16>::max());
static_assert(std::numeric_limits<uint16le>::max() == std::numeric_limits<uint16>::max());
static_assert(std::numeric_limits<int32le>::max() == std::numeric_limits<int32>::max());
static_assert(std::numeric_limits<uint32le>::max() == std::numeric_limits<uint32>::max());
static_assert(std::numeric_limits<int64le>::max() == std::numeric_limits<int64>::max());
static_assert(std::numeric_limits<uint64le>::max() == std::numeric_limits<uint64>::max());
struct test_endian_constexpr {
static MPT_CONSTEXPR20_FUN int32le test(uint32 x) {
int32le foo{};
foo = x;
return foo;
}
};
MPT_CONSTEXPR20_VAR int32le foo = test_endian_constexpr::test(23);
static_cast<void>(foo);
// Packed integers with defined endianness
{
int8le le8;
le8.set(-128);
int8be be8;
be8.set(-128);
MPT_TEST_EXPECT_EQUAL(le8, -128);
MPT_TEST_EXPECT_EQUAL(be8, -128);
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le8, "\x80", 1), 0);
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be8, "\x80", 1), 0);
int16le le16;
le16.set(0x1234);
int16be be16;
be16.set(0x1234);
MPT_TEST_EXPECT_EQUAL(le16, 0x1234);
MPT_TEST_EXPECT_EQUAL(be16, 0x1234);
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le16, "\x34\x12", 2), 0);
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be16, "\x12\x34", 2), 0);
uint32le le32;
le32.set(0xFFEEDDCCu);
uint32be be32;
be32.set(0xFFEEDDCCu);
MPT_TEST_EXPECT_EQUAL(le32, 0xFFEEDDCCu);
MPT_TEST_EXPECT_EQUAL(be32, 0xFFEEDDCCu);
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le32, "\xCC\xDD\xEE\xFF", 4), 0);
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be32, "\xFF\xEE\xDD\xCC", 4), 0);
uint64le le64;
le64.set(0xDEADC0DE15C0FFEEull);
uint64be be64;
be64.set(0xDEADC0DE15C0FFEEull);
MPT_TEST_EXPECT_EQUAL(le64, 0xDEADC0DE15C0FFEEull);
MPT_TEST_EXPECT_EQUAL(be64, 0xDEADC0DE15C0FFEEull);
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le64, "\xEE\xFF\xC0\x15\xDE\xC0\xAD\xDE", 8), 0);
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be64, "\xDE\xAD\xC0\xDE\x15\xC0\xFF\xEE", 8), 0);
}
}
} // namespace integer
} // namespace endian
} // namespace tests
} // namespace MPT_INLINE_NS
} // namespace mpt
#endif // MPT_ENDIAN_TESTS_ENDIAN_INTEGER_HPP