Skip to content

Commit beb14b3

Browse files
committed
refactor: Extend byte_type concept to include std::int8_t and add nested object packing tests
1 parent fbf2dff commit beb14b3

2 files changed

Lines changed: 49 additions & 4 deletions

File tree

include/msgpack23/msgpack23.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,11 @@ namespace msgpack23 {
161161
};
162162

163163
template<typename T>
164-
concept byte_type = std::same_as<T, std::byte> ||
165-
std::same_as<T, char> ||
166-
std::same_as<T, unsigned char> ||
167-
std::same_as<T, std::uint8_t>;
164+
concept byte_type = std::same_as<T, std::byte>
165+
or std::same_as<T, char>
166+
or std::same_as<T, unsigned char>
167+
or std::same_as<T, std::uint8_t>
168+
or std::same_as<T, std::int8_t>;
168169

169170
template<byte_type B, std::output_iterator<B> Iter>
170171
class Packer final {

tests/byte_type_tests.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,48 @@ namespace {
184184
EXPECT_EQ(obj.nestedStruct.values, test.nestedStruct.values);
185185
EXPECT_EQ(obj.nestedStruct.tuple, test.nestedStruct.tuple);
186186
}
187+
188+
TEST(msgpack23, int8_tNestedObjectPacking) {
189+
std::map<std::string, std::string> map;
190+
map.insert(std::pair<std::string, std::string>("first", "hello"));
191+
map.insert(std::pair<std::string, std::string>("second", "world"));
192+
std::vector<std::string> values;
193+
values.emplace_back("first");
194+
values.emplace_back("second");
195+
values.emplace_back("third");
196+
values.emplace_back("fourth");
197+
TestStruct const test{
198+
-57128,
199+
42,
200+
250.42f,
201+
3.1415926535,
202+
"hello world",
203+
{0x15, 0x16, 42},
204+
std::move(map),
205+
TestEnum::First,
206+
std::chrono::system_clock::now(),
207+
{
208+
{"John", "Bjarne", "Rene"},
209+
std::move(values),
210+
{42, "The answer to everything"},
211+
}
212+
};
213+
std::vector<std::int8_t> data{};
214+
auto inserter = std::back_insert_iterator(data);
215+
msgpack23::pack(inserter, test);
216+
auto const obj = msgpack23::unpack<std::int8_t, TestStruct>(data);
217+
218+
EXPECT_EQ(obj.int64, test.int64);
219+
EXPECT_EQ(obj.uint32, test.uint32);
220+
EXPECT_EQ(obj.float32, test.float32);
221+
EXPECT_EQ(obj.double64, test.double64);
222+
EXPECT_EQ(obj.string, test.string);
223+
EXPECT_EQ(obj.data, test.data);
224+
EXPECT_EQ(obj.map, test.map);
225+
EXPECT_EQ(obj.testEnum, test.testEnum);
226+
EXPECT_EQ(obj.time_point, test.time_point);
227+
EXPECT_EQ(obj.nestedStruct.names, test.nestedStruct.names);
228+
EXPECT_EQ(obj.nestedStruct.values, test.nestedStruct.values);
229+
EXPECT_EQ(obj.nestedStruct.tuple, test.nestedStruct.tuple);
230+
}
187231
}

0 commit comments

Comments
 (0)