OXYGENHUB
◆ main openmsx / src / unittest / endian_test.cc
2.2 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
e8555c89 Andy McCall 1
#include "catch.hpp"
2
#include "endian.hh"
3
4
#include <array>
5
6
using namespace Endian;
7
8
TEST_CASE("endian: byteswap")
9
{
10
	ByteSwap swapper;
11
	CHECK(swapper(uint16_t(0xaabb)) == 0xbbaa);
12
	CHECK(swapper(uint32_t(0xaabbccdd)) == 0xddccbbaa);
13
	CHECK(swapper(uint64_t(0xaabbccddeeff0011)) == 0x1100ffeeddccbbaa);
14
}
15
16
// TODO better coverage for aligned vs unaligned versions of the functions
17
TEST_CASE("endian: 16-bit")
18
{
19
	REQUIRE(sizeof(B16) == 2);
20
	REQUIRE(sizeof(L16) == 2);
21
22
	L16 l(0x1234);
23
	CHECK(read_UA_L16(&l) == 0x1234);
24
	CHECK(read_UA_B16(&l) == 0x3412);
25
26
	B16 b(0x1234);
27
	CHECK(read_UA_L16(&b) == 0x3412);
28
	CHECK(read_UA_B16(&b) == 0x1234);
29
30
	std::array<uint8_t, 2> buf;
31
	write_UA_L16(buf.data(), 0xaabb);
32
	CHECK(read_UA_L16(buf.data()) == 0xaabb);
33
	CHECK(read_UA_B16(buf.data()) == 0xbbaa);
34
35
	write_UA_B16(buf.data(), 0xaabb);
36
	CHECK(read_UA_L16(buf.data()) == 0xbbaa);
37
	CHECK(read_UA_B16(buf.data()) == 0xaabb);
38
}
39
40
TEST_CASE("endian: 32-bit")
41
{
42
	REQUIRE(sizeof(B32) == 4);
43
	REQUIRE(sizeof(L32) == 4);
44
45
	L32 l(0x12345678);
46
	CHECK(read_UA_L32(&l) == 0x12345678);
47
	CHECK(read_UA_B32(&l) == 0x78563412);
48
49
	B32 b(0x12345678);
50
	CHECK(read_UA_L32(&b) == 0x78563412);
51
	CHECK(read_UA_B32(&b) == 0x12345678);
52
53
	std::array<uint8_t, 4> buf;
54
	write_UA_L32(buf.data(), 0xaabbccdd);
55
	CHECK(read_UA_L32(buf.data()) == 0xaabbccdd);
56
	CHECK(read_UA_B32(buf.data()) == 0xddccbbaa);
57
58
	write_UA_B32(buf.data(), 0xaabbccdd);
59
	CHECK(read_UA_L32(buf.data()) == 0xddccbbaa);
60
	CHECK(read_UA_B32(buf.data()) == 0xaabbccdd);
61
}
62
63
64
#if 0
65
66
// Small functions to inspect code quality
67
68
uint16_t testSwap16(uint16_t x) { return std::byteswap(x); }
69
uint16_t testSwap16()           { return std::byteswap(0x1234); }
70
uint32_t testSwap32(uint32_t x) { return std::byteswap(x); }
71
uint32_t testSwap32()           { return std::byteswap(0x12345678); }
72
73
void testA(uint16_t& s, uint16_t x) { write_UA_L16(&s, x); }
74
void testB(uint16_t& s, uint16_t x) { write_UA_B16(&s, x); }
75
uint16_t testC(uint16_t& s) { return read_UA_L16(&s); }
76
uint16_t testD(uint16_t& s) { return read_UA_B16(&s); }
77
78
void testA(uint32_t& s, uint32_t x) { write_UA_L32(&s, x); }
79
void testB(uint32_t& s, uint32_t x) { write_UA_B32(&s, x); }
80
uint32_t testC(uint32_t& s) { return read_UA_L32(&s); }
81
uint32_t testD(uint32_t& s) { return read_UA_B32(&s); }
82
83
#endif