openMSX
endian_test.cc
Go to the documentation of this file.
1#include "catch.hpp"
2#include "endian.hh"
3
4#include <array>
5
6using namespace Endian;
7
8TEST_CASE("endian: byteswap")
9{
10 CHECK(byteswap16(0x1122) == 0x2211);
11 CHECK(byteswap32(0x11223344) == 0x44332211);
12 CHECK(byteswap64(0x1122334455667788) == 0x8877665544332211);
13
14 CHECK(byteswap(uint16_t(0x1234)) == 0x3412);
15 CHECK(byteswap(uint32_t(0x12345678)) == 0x78563412);
16 CHECK(byteswap(uint64_t(0x123456789abcdef0)) == 0xf0debc9a78563412);
17
18 ByteSwap swapper;
19 CHECK(swapper(uint16_t(0xaabb)) == 0xbbaa);
20 CHECK(swapper(uint32_t(0xaabbccdd)) == 0xddccbbaa);
21 CHECK(swapper(uint64_t(0xaabbccddeeff0011)) == 0x1100ffeeddccbbaa);
22}
23
24// TODO better coverage for aligned vs unaligned versions of the functions
25TEST_CASE("endian: 16-bit")
26{
27 REQUIRE(sizeof(B16) == 2);
28 REQUIRE(sizeof(L16) == 2);
29
30 L16 l(0x1234);
31 CHECK(read_UA_L16(&l) == 0x1234);
32 CHECK(read_UA_B16(&l) == 0x3412);
33
34 B16 b(0x1234);
35 CHECK(read_UA_L16(&b) == 0x3412);
36 CHECK(read_UA_B16(&b) == 0x1234);
37
38 std::array<uint8_t, 2> buf;
39 write_UA_L16(buf.data(), 0xaabb);
40 CHECK(read_UA_L16(buf.data()) == 0xaabb);
41 CHECK(read_UA_B16(buf.data()) == 0xbbaa);
42
43 write_UA_B16(buf.data(), 0xaabb);
44 CHECK(read_UA_L16(buf.data()) == 0xbbaa);
45 CHECK(read_UA_B16(buf.data()) == 0xaabb);
46}
47
48TEST_CASE("endian: 32-bit")
49{
50 REQUIRE(sizeof(B32) == 4);
51 REQUIRE(sizeof(L32) == 4);
52
53 L32 l(0x12345678);
54 CHECK(read_UA_L32(&l) == 0x12345678);
55 CHECK(read_UA_B32(&l) == 0x78563412);
56
57 B32 b(0x12345678);
58 CHECK(read_UA_L32(&b) == 0x78563412);
59 CHECK(read_UA_B32(&b) == 0x12345678);
60
61 std::array<uint8_t, 4> buf;
62 write_UA_L32(buf.data(), 0xaabbccdd);
63 CHECK(read_UA_L32(buf.data()) == 0xaabbccdd);
64 CHECK(read_UA_B32(buf.data()) == 0xddccbbaa);
65
66 write_UA_B32(buf.data(), 0xaabbccdd);
67 CHECK(read_UA_L32(buf.data()) == 0xddccbbaa);
68 CHECK(read_UA_B32(buf.data()) == 0xaabbccdd);
69}
70
71
72#if 0
73
74// Small functions to inspect code quality
75
76uint16_t testSwap16(uint16_t x) { return byteswap16(x); }
77uint16_t testSwap16() { return byteswap16(0x1234); }
78uint32_t testSwap32(uint32_t x) { return byteswap32(x); }
79uint32_t testSwap32() { return byteswap32(0x12345678); }
80
81void testA(uint16_t& s, uint16_t x) { write_UA_L16(&s, x); }
82void testB(uint16_t& s, uint16_t x) { write_UA_B16(&s, x); }
83uint16_t testC(uint16_t& s) { return read_UA_L16(&s); }
84uint16_t testD(uint16_t& s) { return read_UA_B16(&s); }
85
86void testA(uint32_t& s, uint32_t x) { write_UA_L32(&s, x); }
87void testB(uint32_t& s, uint32_t x) { write_UA_B32(&s, x); }
88uint32_t testC(uint32_t& s) { return read_UA_L32(&s); }
89uint32_t testD(uint32_t& s) { return read_UA_B32(&s); }
90
91#endif
TEST_CASE("endian: byteswap")
Definition endian_test.cc:8
CHECK(m3==m3)
ALWAYS_INLINE uint16_t read_UA_B16(const void *p)
Definition endian.hh:226
ALWAYS_INLINE void write_UA_B32(void *p, uint32_t x)
Definition endian.hh:202
ALWAYS_INLINE void write_UA_B16(void *p, uint16_t x)
Definition endian.hh:186
ALWAYS_INLINE uint32_t read_UA_B32(const void *p)
Definition endian.hh:239
ALWAYS_INLINE uint16_t read_UA_L16(const void *p)
Definition endian.hh:230
ALWAYS_INLINE void write_UA_L32(void *p, uint32_t x)
Definition endian.hh:206
ALWAYS_INLINE void write_UA_L16(void *p, uint16_t x)
Definition endian.hh:190
ALWAYS_INLINE uint32_t read_UA_L32(const void *p)
Definition endian.hh:243