openMSX
endian_test.cc
Go to the documentation of this file.
1 #include "catch.hpp"
2 #include "endian.hh"
3 
4 using namespace Endian;
5 
6 union T16 {
7  B16 be;
8  L16 le;
9 };
10 
11 union T32 {
14 };
15 
16 TEST_CASE("endian: bswap")
17 {
18  CHECK(bswap16(0x1122) == 0x2211);
19  CHECK(bswap32(0x11223344) == 0x44332211);
20  CHECK(bswap64(0x1122334455667788) == 0x8877665544332211);
21 
22  CHECK(bswap(uint16_t(0x1234)) == 0x3412);
23  CHECK(bswap(uint32_t(0x12345678)) == 0x78563412);
24  CHECK(bswap(uint64_t(0x123456789abcdef0)) == 0xf0debc9a78563412);
25 
26  BSwap 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
33 TEST_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 
63 TEST_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 
98 uint16_t testSwap16(uint16_t x) { return bswap16(x); }
99 uint16_t testSwap16() { return bswap16(0x1234); }
100 uint32_t testSwap32(uint32_t x) { return bswap32(x); }
101 uint32_t testSwap32() { return bswap32(0x12345678); }
102 
103 void test1(T16& t, uint16_t x) { t.le = x; }
104 void test2(T16& t, uint16_t x) { t.be = x; }
105 uint16_t test3(T16& t) { return t.le; }
106 uint16_t test4(T16& t) { return t.be; }
107 
108 void testA(uint16_t& s, uint16_t x) { write_UA_L16(&s, x); }
109 void testB(uint16_t& s, uint16_t x) { write_UA_B16(&s, x); }
110 uint16_t testC(uint16_t& s) { return read_UA_L16(&s); }
111 uint16_t testD(uint16_t& s) { return read_UA_B16(&s); }
112 
113 
114 void test1(T32& t, uint32_t x) { t.le = x; }
115 void test2(T32& t, uint32_t x) { t.be = x; }
116 uint32_t test3(T32& t) { return t.le; }
117 uint32_t test4(T32& t) { return t.be; }
118 
119 void testA(uint32_t& s, uint32_t x) { write_UA_L32(&s, x); }
120 void testB(uint32_t& s, uint32_t x) { write_UA_B32(&s, x); }
121 uint32_t testC(uint32_t& s) { return read_UA_L32(&s); }
122 uint32_t testD(uint32_t& s) { return read_UA_B32(&s); }
123 
124 #endif
TEST_CASE("endian: bswap")
Definition: endian_test.cc:16
L16 le
Definition: endian_test.cc:8
B32 be
Definition: endian_test.cc:12
Definition: endian.hh:8
CHECK(m3==m3)
L32 le
Definition: endian_test.cc:13
B16 be
Definition: endian_test.cc:7
TclObject t