Skip to content

Commit

Permalink
libnixt: use static_assert for endianess check
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Dec 28, 2023
1 parent c733e4f commit 0e3509e
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions libnixt/test/SerializeSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ namespace nixt::serialize {
TEST(SerializeSupport, StringTable) {
StringTable ST;
std::ostringstream OS;
if (std::endian::native == std::endian::little) {
EXPECT_EQ(write(OS, ST, "Hello"), 0);
EXPECT_EQ(write(OS, ST, "World"), 13);
EXPECT_EQ(write(OS, ST, "Hello"), 0);
EXPECT_EQ(write(OS, ST, "World"), 13);
const char Expected[] = "\x5\0\0\0\0\0\0\0"
"Hello"
"\x5\0\0\0\0\0\0\0"
"World";
EXPECT_TRUE(std::memcmp(Expected, OS.str().c_str(), sizeof(Expected)) == 0);
}
static_assert(std::endian::native == std::endian::little);
EXPECT_EQ(write(OS, ST, "Hello"), 0);
EXPECT_EQ(write(OS, ST, "World"), 13);
EXPECT_EQ(write(OS, ST, "Hello"), 0);
EXPECT_EQ(write(OS, ST, "World"), 13);
const char Expected[] = "\x5\0\0\0\0\0\0\0"
"Hello"
"\x5\0\0\0\0\0\0\0"
"World";
EXPECT_TRUE(std::memcmp(Expected, OS.str().c_str(), sizeof(Expected)) == 0);
}

TEST(SerializeSupport, writeMisc) {
Expand All @@ -33,15 +32,14 @@ TEST(SerializeSupport, writeMisc) {
EXPECT_EQ(write(OS, 5.5), 16);
EXPECT_EQ(write(OS, 6.5), 24);

if (std::endian::native == std::endian::little) {
const char Expected[] = "\x1\0\0\0"
"\x2\0\0\0"
"\x3\0\0\0"
"\x4\0\0\0"
"\0\0\0\0\0\0\x16\x40"
"\0\0\0\0\0\0\x1A\x40";
EXPECT_TRUE(std::memcmp(Expected, OS.str().c_str(), sizeof(Expected)) == 0);
}
static_assert(std::endian::native == std::endian::little);
const char Expected[] = "\x1\0\0\0"
"\x2\0\0\0"
"\x3\0\0\0"
"\x4\0\0\0"
"\0\0\0\0\0\0\x16\x40"
"\0\0\0\0\0\0\x1A\x40";
EXPECT_TRUE(std::memcmp(Expected, OS.str().c_str(), sizeof(Expected)) == 0);
}

} // namespace nixt::serialize

0 comments on commit 0e3509e

Please sign in to comment.