| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_ENDIAN_TESTS_ENDIAN_INT24_HPP |
| 4 |
#define MPT_ENDIAN_TESTS_ENDIAN_INT24_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/int24.hpp" |
| 13 |
#include "mpt/endian/integer.hpp" |
| 14 |
#include "mpt/test/test.hpp" |
| 15 |
#include "mpt/test/test_macros.hpp" |
| 16 |
|
| 17 |
#include <limits> |
| 18 |
|
| 19 |
#include <cstring> |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace mpt { |
| 24 |
inline namespace MPT_INLINE_NS { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
namespace tests { |
| 29 |
namespace endian { |
| 30 |
namespace integer { |
| 31 |
|
| 32 |
#if MPT_COMPILER_CLANG |
| 33 |
#pragma clang diagnostic push |
| 34 |
#pragma clang diagnostic ignored "-Wglobal-constructors" |
| 35 |
#endif |
| 36 |
MPT_TEST_GROUP_INLINE("mpt/endian/int24") |
| 37 |
#if MPT_COMPILER_CLANG |
| 38 |
#pragma clang diagnostic pop |
| 39 |
#endif |
| 40 |
{ |
| 41 |
static_assert(std::numeric_limits<int24le>::min() == std::numeric_limits<int24>::min()); |
| 42 |
static_assert(std::numeric_limits<uint24le>::min() == std::numeric_limits<uint24>::min()); |
| 43 |
|
| 44 |
{ |
| 45 |
int24le le24; |
| 46 |
le24.set(int24(0x123456)); |
| 47 |
int24be be24; |
| 48 |
be24.set(int24(0x123456)); |
| 49 |
MPT_TEST_EXPECT_EQUAL(le24, int24(0x123456)); |
| 50 |
MPT_TEST_EXPECT_EQUAL(be24, int24(0x123456)); |
| 51 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\x56\x34\x12", 3), 0); |
| 52 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x12\x34\x56", 3), 0); |
| 53 |
} |
| 54 |
{ |
| 55 |
int24le le24; |
| 56 |
le24.set(int24(-0x7fffff)); |
| 57 |
int24be be24; |
| 58 |
be24.set(int24(-0x7fffff)); |
| 59 |
MPT_TEST_EXPECT_EQUAL(le24, int24(-0x7fffff)); |
| 60 |
MPT_TEST_EXPECT_EQUAL(be24, int24(-0x7fffff)); |
| 61 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\x01\x00\x80", 3), 0); |
| 62 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x80\x00\x01", 3), 0); |
| 63 |
} |
| 64 |
{ |
| 65 |
int24le le24; |
| 66 |
le24.set(int24(-0x000001)); |
| 67 |
int24be be24; |
| 68 |
be24.set(int24(-0x000001)); |
| 69 |
MPT_TEST_EXPECT_EQUAL(le24, int24(-0x000001)); |
| 70 |
MPT_TEST_EXPECT_EQUAL(be24, int24(-0x000001)); |
| 71 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\xff\xff\xff", 3), 0); |
| 72 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\xff\xff\xff", 3), 0); |
| 73 |
} |
| 74 |
{ |
| 75 |
int24le le24; |
| 76 |
le24.set(int24(0x7fffff)); |
| 77 |
int24be be24; |
| 78 |
be24.set(int24(0x7fffff)); |
| 79 |
MPT_TEST_EXPECT_EQUAL(le24, int24(0x7fffff)); |
| 80 |
MPT_TEST_EXPECT_EQUAL(be24, int24(0x7fffff)); |
| 81 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\xff\xff\x7f", 3), 0); |
| 82 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x7f\xff\xff", 3), 0); |
| 83 |
} |
| 84 |
{ |
| 85 |
int24le le24; |
| 86 |
le24.set(int24(-0x800000)); |
| 87 |
int24be be24; |
| 88 |
be24.set(int24(-0x800000)); |
| 89 |
MPT_TEST_EXPECT_EQUAL(le24, int24(-0x800000)); |
| 90 |
MPT_TEST_EXPECT_EQUAL(be24, int24(-0x800000)); |
| 91 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\x00\x00\x80", 3), 0); |
| 92 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x80\x00\x00", 3), 0); |
| 93 |
} |
| 94 |
|
| 95 |
{ |
| 96 |
uint24le le24; |
| 97 |
le24.set(uint24(0x123456)); |
| 98 |
uint24be be24; |
| 99 |
be24.set(uint24(0x123456)); |
| 100 |
MPT_TEST_EXPECT_EQUAL(le24, uint24(0x123456)); |
| 101 |
MPT_TEST_EXPECT_EQUAL(be24, uint24(0x123456)); |
| 102 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\x56\x34\x12", 3), 0); |
| 103 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x12\x34\x56", 3), 0); |
| 104 |
} |
| 105 |
{ |
| 106 |
uint24le le24; |
| 107 |
le24.set(uint24(0xffffff)); |
| 108 |
uint24be be24; |
| 109 |
be24.set(uint24(0xffffff)); |
| 110 |
MPT_TEST_EXPECT_EQUAL(le24, uint24(0xffffff)); |
| 111 |
MPT_TEST_EXPECT_EQUAL(be24, uint24(0xffffff)); |
| 112 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\xff\xff\xff", 3), 0); |
| 113 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\xff\xff\xff", 3), 0); |
| 114 |
} |
| 115 |
{ |
| 116 |
uint24le le24; |
| 117 |
le24.set(uint24(0x7fffff)); |
| 118 |
uint24be be24; |
| 119 |
be24.set(uint24(0x7fffff)); |
| 120 |
MPT_TEST_EXPECT_EQUAL(le24, uint24(0x7fffff)); |
| 121 |
MPT_TEST_EXPECT_EQUAL(be24, uint24(0x7fffff)); |
| 122 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\xff\xff\x7f", 3), 0); |
| 123 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x7f\xff\xff", 3), 0); |
| 124 |
} |
| 125 |
{ |
| 126 |
uint24le le24; |
| 127 |
le24.set(uint24(0x800000)); |
| 128 |
uint24be be24; |
| 129 |
be24.set(uint24(0x800000)); |
| 130 |
MPT_TEST_EXPECT_EQUAL(le24, uint24(0x800000)); |
| 131 |
MPT_TEST_EXPECT_EQUAL(be24, uint24(0x800000)); |
| 132 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\x00\x00\x80", 3), 0); |
| 133 |
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x80\x00\x00", 3), 0); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
} // namespace integer |
| 138 |
} // namespace endian |
| 139 |
} // namespace tests |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
} // namespace MPT_INLINE_NS |
| 144 |
} // namespace mpt |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
#endif // MPT_ENDIAN_TESTS_ENDIAN_INT24_HPP |