mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-01-30 16:45:33 +00:00
Add unit test for swapEndiannessInplace(). Part of Bug #5837
This commit is contained in:
parent
e2ec3b0db7
commit
f3271cb66b
3 changed files with 124 additions and 1 deletions
|
@ -15,6 +15,7 @@ if (GTEST_FOUND AND GMOCK_FOUND)
|
|||
esm/test_fixed_string.cpp
|
||||
|
||||
misc/test_stringops.cpp
|
||||
misc/test_endianness.cpp
|
||||
|
||||
nifloader/testbulletnifloader.cpp
|
||||
|
||||
|
|
122
apps/openmw_test_suite/misc/test_endianness.cpp
Normal file
122
apps/openmw_test_suite/misc/test_endianness.cpp
Normal file
|
@ -0,0 +1,122 @@
|
|||
#include <gtest/gtest.h>
|
||||
#include "components/misc/endianness.hpp"
|
||||
|
||||
struct EndiannessTest : public ::testing::Test {};
|
||||
|
||||
TEST_F(EndiannessTest, test_swap_endianness_inplace1)
|
||||
{
|
||||
uint8_t zero=0x00;
|
||||
uint8_t ff=0xFF;
|
||||
uint8_t fortytwo=0x42;
|
||||
uint8_t half=128;
|
||||
|
||||
Misc::swapEndiannessInplace(zero);
|
||||
EXPECT_EQ(zero, 0x00);
|
||||
|
||||
Misc::swapEndiannessInplace(ff);
|
||||
EXPECT_EQ(ff, 0xFF);
|
||||
|
||||
Misc::swapEndiannessInplace(fortytwo);
|
||||
EXPECT_EQ(fortytwo, 0x42);
|
||||
|
||||
Misc::swapEndiannessInplace(half);
|
||||
EXPECT_EQ(half, 128);
|
||||
}
|
||||
|
||||
TEST_F(EndiannessTest, test_swap_endianness_inplace2)
|
||||
{
|
||||
uint16_t zero = 0x0000;
|
||||
uint16_t ffff = 0xFFFF;
|
||||
uint16_t n12 = 0x0102;
|
||||
uint16_t fortytwo = 0x0042;
|
||||
|
||||
Misc::swapEndiannessInplace(zero);
|
||||
EXPECT_EQ(zero, 0x0000);
|
||||
Misc::swapEndiannessInplace(zero);
|
||||
EXPECT_EQ(zero, 0x0000);
|
||||
|
||||
Misc::swapEndiannessInplace(ffff);
|
||||
EXPECT_EQ(ffff, 0xFFFF);
|
||||
Misc::swapEndiannessInplace(ffff);
|
||||
EXPECT_EQ(ffff, 0xFFFF);
|
||||
|
||||
Misc::swapEndiannessInplace(n12);
|
||||
EXPECT_EQ(n12, 0x0201);
|
||||
Misc::swapEndiannessInplace(n12);
|
||||
EXPECT_EQ(n12, 0x0102);
|
||||
|
||||
Misc::swapEndiannessInplace(fortytwo);
|
||||
EXPECT_EQ(fortytwo, 0x4200);
|
||||
Misc::swapEndiannessInplace(fortytwo);
|
||||
EXPECT_EQ(fortytwo, 0x0042);
|
||||
}
|
||||
|
||||
TEST_F(EndiannessTest, test_swap_endianness_inplace4)
|
||||
{
|
||||
uint32_t zero = 0x00000000;
|
||||
uint32_t n1234 = 0x01020304;
|
||||
uint32_t ffff = 0xFFFFFFFF;
|
||||
|
||||
Misc::swapEndiannessInplace(zero);
|
||||
EXPECT_EQ(zero, 0x00000000);
|
||||
Misc::swapEndiannessInplace(zero);
|
||||
EXPECT_EQ(zero, 0x00000000);
|
||||
|
||||
Misc::swapEndiannessInplace(n1234);
|
||||
EXPECT_EQ(n1234, 0x04030201);
|
||||
Misc::swapEndiannessInplace(n1234);
|
||||
EXPECT_EQ(n1234, 0x01020304);
|
||||
|
||||
Misc::swapEndiannessInplace(ffff);
|
||||
EXPECT_EQ(ffff, 0xFFFFFFFF);
|
||||
Misc::swapEndiannessInplace(ffff);
|
||||
EXPECT_EQ(ffff, 0xFFFFFFFF);
|
||||
}
|
||||
|
||||
TEST_F(EndiannessTest, test_swap_endianness_inplace8)
|
||||
{
|
||||
uint64_t zero = 0x0000'0000'0000'0000;
|
||||
uint64_t n1234 = 0x0102'0304'0506'0708;
|
||||
uint64_t ffff = 0xFFFF'FFFF'FFFF'FFFF;
|
||||
|
||||
Misc::swapEndiannessInplace(zero);
|
||||
EXPECT_EQ(zero, 0x0000'0000'0000'0000);
|
||||
Misc::swapEndiannessInplace(zero);
|
||||
EXPECT_EQ(zero, 0x0000'0000'0000'0000);
|
||||
|
||||
Misc::swapEndiannessInplace(ffff);
|
||||
EXPECT_EQ(ffff, 0xFFFF'FFFF'FFFF'FFFF);
|
||||
Misc::swapEndiannessInplace(ffff);
|
||||
EXPECT_EQ(ffff, 0xFFFF'FFFF'FFFF'FFFF);
|
||||
|
||||
Misc::swapEndiannessInplace(n1234);
|
||||
EXPECT_EQ(n1234, 0x0807'0605'0403'0201);
|
||||
Misc::swapEndiannessInplace(n1234);
|
||||
EXPECT_EQ(n1234, 0x0102'0304'0506'0708);
|
||||
}
|
||||
|
||||
TEST_F(EndiannessTest, test_swap_endianness_inplace_float)
|
||||
{
|
||||
const uint32_t original = 0x4023d70a;
|
||||
const uint32_t expected = 0x0ad72340;
|
||||
|
||||
float number;
|
||||
memcpy(&number, &original, sizeof(original));
|
||||
|
||||
Misc::swapEndiannessInplace(number);
|
||||
|
||||
EXPECT_TRUE(!memcmp(&number, &expected, sizeof(expected)));
|
||||
}
|
||||
|
||||
TEST_F(EndiannessTest, test_swap_endianness_inplace_double)
|
||||
{
|
||||
const uint64_t original = 0x040047ae147ae147ul;
|
||||
const uint64_t expected = 0x47e17a14ae470004ul;
|
||||
|
||||
double number;
|
||||
memcpy(&number, &original, sizeof(original));
|
||||
|
||||
Misc::swapEndiannessInplace(number);
|
||||
|
||||
EXPECT_TRUE(!memcmp(&number, &expected, sizeof(expected)) );
|
||||
}
|
|
@ -26,7 +26,7 @@ namespace Misc
|
|||
{
|
||||
uint32_t v32;
|
||||
std::memcpy(&v32, &v, sizeof(T));
|
||||
v32 = (v32 >> 24) | ((v32 >> 8) & 0xff00) | ((v32 & 0xff00) << 8) | v32 << 24;
|
||||
v32 = (v32 >> 24) | ((v32 >> 8) & 0xff00) | ((v32 & 0xff00) << 8) | (v32 << 24);
|
||||
std::memcpy(&v, &v32, sizeof(T));
|
||||
}
|
||||
if constexpr (sizeof(T) == 8)
|
||||
|
|
Loading…
Reference in a new issue