diff --git a/include/packme/packme.h b/include/packme/packme.h index a796f2e..20302d7 100644 --- a/include/packme/packme.h +++ b/include/packme/packme.h @@ -472,7 +472,7 @@ namespace packme template auto item_unpack_helper(const std::string &str, size_t &pos) { - // trivially copy doesn't need size. + // These types don't need a size indicator. if constexpr(tag_is || (tag_is && sizeof(T) == 1)) { std::string buf = str.substr(pos, sizeof(T)); @@ -482,7 +482,7 @@ namespace packme else { // if tag is not trivially_copy_tag, it must be int, string or custom-type, - // and they all have an integer indicating size or value at the beginning. + // and they all have an integer at the beginning indicating its size(trivially_copy) or value(int). size_t int_size = 1; for (size_t i = pos; i < str.size(); ++i, ++int_size) { diff --git a/tests/packme_tests.cpp b/tests/packme_tests.cpp index c08b538..b74ca70 100644 --- a/tests/packme_tests.cpp +++ b/tests/packme_tests.cpp @@ -8,17 +8,17 @@ namespace packme::test PACKME_TEST(czh) { // Number - auto s1 = pack(64); - PACKME_EXPECT_EQ(64, unpack(s1)) - PACKME_EXPECT_EQ(s1.size(), 1) - auto s2 = pack(-128); - PACKME_EXPECT_EQ(-128, unpack(s2)) - PACKME_EXPECT_EQ(s2.size(), 2) - auto s3 = pack((std::numeric_limits::max)()); - PACKME_EXPECT_EQ((std::numeric_limits::max)(), unpack(s3)) - auto s4 = pack((std::numeric_limits::min)()); - PACKME_EXPECT_EQ((std::numeric_limits::min)(), unpack(s4)) - + std::string str; + str = pack(64); + PACKME_EXPECT_EQ(64, unpack(str)) + PACKME_EXPECT_EQ(str.size(), 1) + str = pack(-128); + PACKME_EXPECT_EQ(-128, unpack(str)) + PACKME_EXPECT_EQ(str.size(), 2) + str = pack((std::numeric_limits::max)()); + PACKME_EXPECT_EQ((std::numeric_limits::max)(), unpack(str)) + str = pack((std::numeric_limits::min)()); + PACKME_EXPECT_EQ((std::numeric_limits::min)(), unpack(str)) auto num_test = []() { @@ -38,21 +38,30 @@ namespace packme::test num_test.operator()(); num_test.operator()(); num_test.operator()(); - + + // Enum + + enum class E + { + A, B, C, D + }; + + str = pack(E::A); + PACKME_EXPECT_EQ(unpack(str), E::A); + str = pack(E::C); + PACKME_EXPECT_EQ(unpack(str), E::C) // Array and String - auto s5 = pack("PACK ME TEST STRING"); - PACKME_EXPECT_EQ(s5.size(), 20) - auto chararray = unpack(s5); + str = pack("PACK ME TEST STRING"); + PACKME_EXPECT_EQ(str.size(), 20) + auto chararray = unpack(str); PACKME_EXPECT_TRUE(strcmp("PACK ME TEST STRING", chararray) == 0) free(const_cast(chararray)); - - s5 = pack(std::string{"PACK ME TEST STRING"}); - PACKME_EXPECT_EQ(s5.size(), 19) - PACKME_EXPECT_EQ("PACK ME TEST STRING", unpack(s5)) - - + str = pack(std::string{"PACK ME TEST STRING"}); + PACKME_EXPECT_EQ(str.size(), 19) + PACKME_EXPECT_EQ("PACK ME TEST STRING", unpack(str)) + // Trivially Copyable struct A {int a; double b; bool operator==(const A& a1) const @@ -62,8 +71,8 @@ namespace packme::test }; static_assert(std::is_trivially_copyable_v); A a{1, 2.0}; - auto s6 = pack(a); - PACKME_EXPECT_EQ(a, unpack(s6)); + str = pack(a); + PACKME_EXPECT_EQ(a, unpack(str)); // Aggregate Struct struct B {int a; std::string b; std::vector c; std::map d; A e; @@ -73,16 +82,16 @@ namespace packme::test }}; static_assert(std::is_aggregate_v); B b{1, "2", std::vector{3}, std::map{{"4", 5}}, A{6, 7.0}}; - auto s7 = pack(b); - PACKME_EXPECT_EQ(b, unpack(s7)) + str = pack(b); + PACKME_EXPECT_EQ(b, unpack(str)) // Tuple Like auto tuple_like1 = std::make_tuple(b, a, 1, 2, 3.0, 4e31); auto tuple_like2 = std::make_pair(a, b); - auto s8 = pack(tuple_like1); - auto s9 = pack(tuple_like2); - PACKME_EXPECT_EQ(tuple_like1, unpack(s8)) - PACKME_EXPECT_EQ(tuple_like2, unpack(s9)) + str = pack(tuple_like1); + PACKME_EXPECT_EQ(tuple_like1, unpack(str)) + str= pack(tuple_like2); + PACKME_EXPECT_EQ(tuple_like2, unpack(str)) // Container std::vector container1{"A", "B", "C"}; @@ -107,11 +116,11 @@ namespace packme::test }; Container container2; container2.insert(container2.end(), 1); - - auto s10 = pack(container1); - auto s11 = pack(container2); - PACKME_EXPECT_EQ(container1, unpack(s10)) - PACKME_EXPECT_EQ(container2, unpack(s11)) + + str = pack(container1); + PACKME_EXPECT_EQ(container1, unpack(str)) + str = pack(container2); + PACKME_EXPECT_EQ(container2, unpack(str)) } }