1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-06 06:32:56 +01:00

Allow SwapBE to swap non-uint types

If we want to have more semantically meaningful types (like Direction), it's useful to be able to support those in the DataSerializer too. Swapping bytes for entire structures is probably never going to make sense, but for types that are pure wrappers around integer types, we want to be able to swap them as if they were the integer they wrap.
This commit is contained in:
Richard Fine
2019-08-26 14:15:44 +01:00
parent 5ff78e48c7
commit 0e04dbeea1
3 changed files with 51 additions and 1 deletions

43
test/tests/Endianness.cpp Normal file
View File

@@ -0,0 +1,43 @@
#include "openrct2/core/Endianness.h"
#include <gtest/gtest.h>
TEST(SwapBETest, ForUInt8_DoesNothing)
{
uint8_t before = 0x12;
uint8_t after = ByteSwapBE(before);
ASSERT_EQ(before, after);
}
TEST(SwapBETest, ForUInt16_SwapsBytes)
{
uint16_t before = 0x1234;
uint16_t after = ByteSwapBE(before);
ASSERT_EQ(0x3412u, after);
}
TEST(SwapBETest, ForUInt32_SwapsBytes)
{
uint32_t before = 0x12345678;
uint32_t after = ByteSwapBE(before);
ASSERT_EQ(0x78563412u, after);
}
TEST(SwapBETest, ForUInt64_SwapsBytes)
{
uint64_t before = 0x1234567887654321;
uint64_t after = ByteSwapBE(before);
ASSERT_EQ(0x2143658778563412u, after);
}
TEST(SwapBETest, ForCustomBlittableType_SwapsBytes)
{
struct MyStruct
{
uint16_t value;
};
MyStruct before = { 0x1234 };
MyStruct after = ByteSwapBE(before);
ASSERT_EQ(0x3412, after.value);
}