openMSX
endian_test.cc
Go to the documentation of this file.
1#include "catch.hpp"
2#include "endian.hh"
3
4using namespace Endian;
5
6union T16 {
9};
10
11union T32 {
14};
15
16TEST_CASE("endian: byteswap")
17{
18 CHECK(byteswap16(0x1122) == 0x2211);
19 CHECK(byteswap32(0x11223344) == 0x44332211);
20 CHECK(byteswap64(0x1122334455667788) == 0x8877665544332211);
21
22 CHECK(byteswap(uint16_t(0x1234)) == 0x3412);
23 CHECK(byteswap(uint32_t(0x12345678)) == 0x78563412);
24 CHECK(byteswap(uint64_t(0x123456789abcdef0)) == 0xf0debc9a78563412);
25
26 ByteSwap swapper;
27 CHECK(swapper(uint16_t(0xaabb)) == 0xbbaa);
28 CHECK(swapper(uint32_t(0xaabbccdd)) == 0xddccbbaa);
29 CHECK(swapper(uint64_t(0xaabbccddeeff0011)) == 0x1100ffeeddccbbaa);
30}
31
32// TODO better coverage for aligned vs unaligned versions of the functions
33TEST_CASE("endian: 16-bit")
34{
35 T16 t;
36 REQUIRE(sizeof(t) == 2);
37
38 t.le = 0x1234;
39 CHECK(t.le == 0x1234);
40 CHECK(t.be == 0x3412);
41 CHECK(read_UA_L16(&t) == 0x1234);
42 CHECK(read_UA_B16(&t) == 0x3412);
43
44 t.be = 0x1234;
45 CHECK(t.le == 0x3412);
46 CHECK(t.be == 0x1234);
47 CHECK(read_UA_L16(&t) == 0x3412);
48 CHECK(read_UA_B16(&t) == 0x1234);
49
50 write_UA_L16(&t, 0xaabb);
51 CHECK(t.le == 0xaabb);
52 CHECK(t.be == 0xbbaa);
53 CHECK(read_UA_L16(&t) == 0xaabb);
54 CHECK(read_UA_B16(&t) == 0xbbaa);
55
56 write_UA_B16(&t, 0xaabb);
57 CHECK(t.le == 0xbbaa);
58 CHECK(t.be == 0xaabb);
59 CHECK(read_UA_L16(&t) == 0xbbaa);
60 CHECK(read_UA_B16(&t) == 0xaabb);
61}
62
63TEST_CASE("endian: 32-bit")
64{
65 T32 t;
66 REQUIRE(sizeof(t) == 4);
67
68 t.le = 0x12345678;
69 CHECK(t.le == 0x12345678);
70 CHECK(t.be == 0x78563412);
71 CHECK(read_UA_L32(&t) == 0x12345678);
72 CHECK(read_UA_B32(&t) == 0x78563412);
73
74 t.be = 0x12345678;
75 CHECK(t.le == 0x78563412);
76 CHECK(t.be == 0x12345678);
77 CHECK(read_UA_L32(&t) == 0x78563412);
78 CHECK(read_UA_B32(&t) == 0x12345678);
79
80 write_UA_L32(&t, 0xaabbccdd);
81 CHECK(t.le == 0xaabbccdd);
82 CHECK(t.be == 0xddccbbaa);
83 CHECK(read_UA_L32(&t) == 0xaabbccdd);
84 CHECK(read_UA_B32(&t) == 0xddccbbaa);
85
86 write_UA_B32(&t, 0xaabbccdd);
87 CHECK(t.le == 0xddccbbaa);
88 CHECK(t.be == 0xaabbccdd);
89 CHECK(read_UA_L32(&t) == 0xddccbbaa);
90 CHECK(read_UA_B32(&t) == 0xaabbccdd);
91}
92
93
94#if 0
95
96// Small functions to inspect code quality
97
98uint16_t testSwap16(uint16_t x) { return byteswap16(x); }
99uint16_t testSwap16() { return byteswap16(0x1234); }
100uint32_t testSwap32(uint32_t x) { return byteswap32(x); }
101uint32_t testSwap32() { return byteswap32(0x12345678); }
102
103void test1(T16& t, uint16_t x) { t.le = x; }
104void test2(T16& t, uint16_t x) { t.be = x; }
105uint16_t test3(T16& t) { return t.le; }
106uint16_t test4(T16& t) { return t.be; }
107
108void testA(uint16_t& s, uint16_t x) { write_UA_L16(&s, x); }
109void testB(uint16_t& s, uint16_t x) { write_UA_B16(&s, x); }
110uint16_t testC(uint16_t& s) { return read_UA_L16(&s); }
111uint16_t testD(uint16_t& s) { return read_UA_B16(&s); }
112
113
114void test1(T32& t, uint32_t x) { t.le = x; }
115void test2(T32& t, uint32_t x) { t.be = x; }
116uint32_t test3(T32& t) { return t.le; }
117uint32_t test4(T32& t) { return t.be; }
118
119void testA(uint32_t& s, uint32_t x) { write_UA_L32(&s, x); }
120void testB(uint32_t& s, uint32_t x) { write_UA_B32(&s, x); }
121uint32_t testC(uint32_t& s) { return read_UA_L32(&s); }
122uint32_t testD(uint32_t& s) { return read_UA_B32(&s); }
123
124#endif
TclObject t
TEST_CASE("endian: byteswap")
Definition: endian_test.cc:16
CHECK(m3==m3)
Definition: endian.hh:13
ALWAYS_INLINE uint16_t read_UA_B16(const void *p)
Definition: endian.hh:225
ALWAYS_INLINE void write_UA_B32(void *p, uint32_t x)
Definition: endian.hh:201
ALWAYS_INLINE void write_UA_B16(void *p, uint16_t x)
Definition: endian.hh:185
ALWAYS_INLINE uint32_t read_UA_B32(const void *p)
Definition: endian.hh:238
ALWAYS_INLINE uint16_t read_UA_L16(const void *p)
Definition: endian.hh:229
ALWAYS_INLINE void write_UA_L32(void *p, uint32_t x)
Definition: endian.hh:205
ALWAYS_INLINE void write_UA_L16(void *p, uint16_t x)
Definition: endian.hh:189
ALWAYS_INLINE uint32_t read_UA_L32(const void *p)
Definition: endian.hh:242
B16 be
Definition: endian_test.cc:7
L16 le
Definition: endian_test.cc:8
B32 be
Definition: endian_test.cc:12
L32 le
Definition: endian_test.cc:13