From 0e3509e05689cceec2ac3f5de95a1ede39cfdb2d Mon Sep 17 00:00:00 2001 From: Yingchi Long Date: Thu, 28 Dec 2023 15:12:34 +0800 Subject: [PATCH] libnixt: use static_assert for endianess check --- libnixt/test/SerializeSupport.cpp | 38 +++++++++++++++---------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/libnixt/test/SerializeSupport.cpp b/libnixt/test/SerializeSupport.cpp index d379386dd..58fc2edc8 100644 --- a/libnixt/test/SerializeSupport.cpp +++ b/libnixt/test/SerializeSupport.cpp @@ -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) { @@ -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