Skip to content

Commit

Permalink
libnixt/serialize: add unit test for SerializeSupport
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Dec 27, 2023
1 parent 64a98bd commit c733e4f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
6 changes: 6 additions & 0 deletions libnixt/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ nixt = declare_dependency(include_directories: libnixdInc,
dependencies: libnixtDeps,
link_with: libnixt
)

libnixt_test_exe = executable('test-libnixt',
'test/SerializeSupport.cpp',
dependencies: [ gtest_main, nixt ])

test('unit/libnixt', libnixt_test_exe)
47 changes: 47 additions & 0 deletions libnixt/test/SerializeSupport.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <gtest/gtest.h>

#include "nixt/SerializeSupport.h"

#include <bit>

namespace nixt::serialize {

/// These asserts are generated on little-endian machine.

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);
}
}

TEST(SerializeSupport, writeMisc) {
std::ostringstream OS;
EXPECT_EQ(write(OS, 1), 0);
EXPECT_EQ(write(OS, 2), 4);
EXPECT_EQ(write(OS, 3), 8);
EXPECT_EQ(write(OS, 4), 12);
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);
}
}

} // namespace nixt::serialize

0 comments on commit c733e4f

Please sign in to comment.