Skip to content

Commit

Permalink
add test for null terminated string serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
farukeryilmaz committed Dec 16, 2023
1 parent b31992e commit 9281775
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions test/string_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,31 @@ TEST_CASE("String, string view and fundamental types mixed (big-endian)")
REQUIRE(num2 == num2_);
REQUIRE(str2 == str2_);
REQUIRE(num3 == Approx(num3_).epsilon(1e-4));
}

TEST_CASE("String and string view null terminated (little-endian)")
{
std::string str = "Hello BytePack!";
double num1 = 3754.34526243;

bytepack::binary_stream<std::endian::little> bstream(32);

bstream.write(str, bytepack::StringMode::NullTerminated);
bstream.write(num1);

std::string str_{};
double num1_{};

auto buffer = bstream.data();
bytepack::binary_stream<std::endian::little> bstream_(buffer);

bstream_.read(str_, bytepack::StringMode::NullTerminated);
bstream_.read(num1_);

// 15 bytes for the string, 1 byte for the null terminator '\0'
// 8 bytes for double
// 24 bytes in total
REQUIRE(24 == buffer.size());
REQUIRE(str == str_);
REQUIRE(num1 == Approx(num1_).epsilon(1e-7));
}

0 comments on commit 9281775

Please sign in to comment.