123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835 |
- #include "msgpack.hpp"
- #include <sstream>
- #include <limits>
- #include <cmath>
- #include <gtest/gtest.h>
- // To avoid link error
- TEST(MSGPACK_X3_PARSE, dummy)
- {
- }
- #if defined(MSGPACK_USE_X3_PARSE) && MSGPACK_DEFAULT_API_VERSION >= 2
- using namespace std;
- const double kEPS = 1e-10;
- TEST(MSGPACK_X3_PARSE, nil_t)
- {
- msgpack::type::nil_t v;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_TRUE(oh.get().is_nil());
- }
- TEST(MSGPACK_X3_PARSE, bool_false)
- {
- bool v = false;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<bool>());
- }
- TEST(MSGPACK_X3_PARSE, bool_true)
- {
- bool v = true;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<bool>());
- }
- TEST(MSGPACK_X3_PARSE, positive_fixint_1)
- {
- uint8_t v = 0;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<uint8_t>());
- }
- TEST(MSGPACK_X3_PARSE, positive_fixint_2)
- {
- uint8_t v = 127;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<uint8_t>());
- }
- TEST(MSGPACK_X3_PARSE, negative_fixint_1)
- {
- int8_t v = -1;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<int8_t>());
- }
- TEST(MSGPACK_X3_PARSE, negative_fixint_2)
- {
- int8_t v = -32;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<int8_t>());
- }
- TEST(MSGPACK_X3_PARSE, uint8_1)
- {
- uint8_t v = 128U;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<uint8_t>());
- }
- TEST(MSGPACK_X3_PARSE, uint8_2)
- {
- uint8_t v = 0xffU;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<uint8_t>());
- }
- TEST(MSGPACK_X3_PARSE, uint16_1)
- {
- uint16_t v = 0x100U;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<uint16_t>());
- }
- TEST(MSGPACK_X3_PARSE, uint16_2)
- {
- uint16_t v = 0xffffU;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<uint16_t>());
- }
- TEST(MSGPACK_X3_PARSE, uint32_1)
- {
- uint32_t v = 0x10000UL;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<uint32_t>());
- }
- TEST(MSGPACK_X3_PARSE, uint32_2)
- {
- uint32_t v = 0xffffffffUL;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<uint32_t>());
- }
- TEST(MSGPACK_X3_PARSE, uint64_1)
- {
- uint64_t v = 0x100000000ULL;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<uint64_t>());
- }
- TEST(MSGPACK_X3_PARSE, uint64_2)
- {
- uint64_t v = 0xffffffffffffffffULL;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<uint64_t>());
- }
- TEST(MSGPACK_X3_PARSE, int8_1)
- {
- int8_t v = 0b11011111;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<int8_t>());
- }
- TEST(MSGPACK_X3_PARSE, int8_2)
- {
- int8_t v = 0b10000000;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<int8_t>());
- }
- TEST(MSGPACK_X3_PARSE, int16_1)
- {
- int16_t v = 0xff00;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<int16_t>());
- }
- TEST(MSGPACK_X3_PARSE, int16_2)
- {
- int16_t v = 0x8000;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<int16_t>());
- }
- TEST(MSGPACK_X3_PARSE, int32_1)
- {
- int32_t v = 0xff000000L;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<int32_t>());
- }
- TEST(MSGPACK_X3_PARSE, int32_2)
- {
- int32_t v = 0x80000000L;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<int32_t>());
- }
- TEST(MSGPACK_X3_PARSE, int64_1)
- {
- int64_t v = 0xff00000000000000LL;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<int64_t>());
- }
- TEST(MSGPACK_X3_PARSE, int64_2)
- {
- int64_t v = 0x8000000000000000LL;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<int64_t>());
- }
- TEST(MSGPACK_X3_PARSE, array_1)
- {
- std::vector<int> v;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<std::vector<int> >());
- }
- TEST(MSGPACK_X3_PARSE, array_2)
- {
- std::vector<int> v;
- std::stringstream ss;
- for (int i = 0; i != 0xffU; ++i) v.push_back(i);
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<std::vector<int> >());
- }
- TEST(MSGPACK_X3_PARSE, array_3)
- {
- std::vector<int> v;
- std::stringstream ss;
- for (int i = 0; i != 0xffU+1U; ++i) v.push_back(i);
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<std::vector<int> >());
- }
- TEST(MSGPACK_X3_PARSE, array_4)
- {
- std::vector<int> v;
- std::stringstream ss;
- for (int i = 0; i != 0xffffU; ++i) v.push_back(i);
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<std::vector<int> >());
- }
- TEST(MSGPACK_X3_PARSE, array_5)
- {
- std::vector<uint32_t> v;
- std::stringstream ss;
- for (uint32_t i = 0; i != 0xffffU+1U; ++i) v.push_back(i);
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<std::vector<uint32_t> >());
- }
- TEST(MSGPACK_X3_PARSE, map_1)
- {
- std::map<int, int> v;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, (oh.get().as<std::map<int, int> >()));
- }
- TEST(MSGPACK_X3_PARSE, map_2)
- {
- std::map<int, int> v;
- std::stringstream ss;
- for (int i = 0; i != 0xffU; ++i) v.emplace(i, i);
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, (oh.get().as<std::map<int, int> >()));
- }
- TEST(MSGPACK_X3_PARSE, map_3)
- {
- std::map<int, int> v;
- std::stringstream ss;
- for (int i = 0; i != 0xffU+1U; ++i) v.emplace(i, i);
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, (oh.get().as<std::map<int, int> >()));
- }
- TEST(MSGPACK_X3_PARSE, map_4)
- {
- std::map<int, int> v;
- std::stringstream ss;
- for (int i = 0; i != 0xffffU; ++i) v.emplace(i, i);
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, (oh.get().as<std::map<int, int> >()));
- }
- TEST(MSGPACK_X3_PARSE, map_5)
- {
- std::map<uint32_t, uint32_t> v;
- std::stringstream ss;
- for (uint32_t i = 0; i != 0xffffU+1U; ++i) v.emplace(i, i);
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, (oh.get().as<std::map<uint32_t, uint32_t> >()));
- }
- TEST(MSGPACK_X3_PARSE, float_1)
- {
- std::vector<float> v;
- v.push_back(0.0);
- v.push_back(-0.0);
- v.push_back(1.0);
- v.push_back(-1.0);
- v.push_back(numeric_limits<float>::min());
- v.push_back(numeric_limits<float>::max());
- v.push_back(nanf("tag"));
- if (numeric_limits<float>::has_infinity) {
- v.push_back(numeric_limits<float>::infinity());
- v.push_back(-numeric_limits<float>::infinity());
- }
- if (numeric_limits<float>::has_quiet_NaN) {
- v.push_back(numeric_limits<float>::quiet_NaN());
- }
- if (numeric_limits<float>::has_signaling_NaN) {
- v.push_back(numeric_limits<float>::signaling_NaN());
- }
- for (unsigned int i = 0; i < v.size() ; i++) {
- std::stringstream ss;
- float val1 = v[i];
- msgpack::pack(ss, val1);
- msgpack::object_handle oh =
- msgpack::unpack(ss.str().begin(), ss.str().end());
- float val2 = oh.get().as<float>();
- if (std::isnan(val1))
- EXPECT_TRUE(std::isnan(val2));
- else if (std::isinf(val1))
- EXPECT_TRUE(std::isinf(val2));
- else
- EXPECT_TRUE(fabs(val2 - val1) <= kEPS);
- }
- }
- TEST(MSGPACK_X3_PARSE, double_1)
- {
- std::vector<double> v;
- v.push_back(0.0);
- v.push_back(-0.0);
- v.push_back(1.0);
- v.push_back(-1.0);
- v.push_back(numeric_limits<double>::min());
- v.push_back(numeric_limits<double>::max());
- v.push_back(nanf("tag"));
- if (numeric_limits<double>::has_infinity) {
- v.push_back(numeric_limits<double>::infinity());
- v.push_back(-numeric_limits<double>::infinity());
- }
- if (numeric_limits<double>::has_quiet_NaN) {
- v.push_back(numeric_limits<double>::quiet_NaN());
- }
- if (numeric_limits<double>::has_signaling_NaN) {
- v.push_back(numeric_limits<double>::signaling_NaN());
- }
- for (unsigned int i = 0; i < v.size() ; i++) {
- std::stringstream ss;
- double val1 = v[i];
- msgpack::pack(ss, val1);
- msgpack::object_handle oh =
- msgpack::unpack(ss.str().begin(), ss.str().end());
- double val2 = oh.get().as<double>();
- if (std::isnan(val1))
- EXPECT_TRUE(std::isnan(val2));
- else if (std::isinf(val1))
- EXPECT_TRUE(std::isinf(val2));
- else
- EXPECT_TRUE(fabs(val2 - val1) <= kEPS);
- }
- }
- TEST(MSGPACK_X3_PARSE, string_1)
- {
- std::string v;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<std::string>());
- }
- TEST(MSGPACK_X3_PARSE, string_2)
- {
- std::string v;
- for (uint64_t i = 0; i != 0x1fU; ++i) v.push_back('0'+(i%10));
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<std::string>());
- }
- TEST(MSGPACK_X3_PARSE, string_3)
- {
- std::string v;
- for (uint64_t i = 0; i != 0xffU; ++i) v.push_back('0'+(i%10));
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<std::string>());
- }
- TEST(MSGPACK_X3_PARSE, string_4)
- {
- std::string v;
- for (uint64_t i = 0; i != 0xffU+1U; ++i) v.push_back('0'+(i%10));
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<std::string>());
- }
- TEST(MSGPACK_X3_PARSE, string_5)
- {
- std::string v;
- for (uint64_t i = 0; i != 0xffffU; ++i) v.push_back('0'+(i%10));
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<std::string>());
- }
- TEST(MSGPACK_X3_PARSE, string_6)
- {
- std::string v;
- for (uint64_t i = 0; i != 0xffffUL + 1UL; ++i) v.push_back('0'+(i%10));
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<std::string>());
- }
- TEST(MSGPACK_X3_PARSE, bin_1)
- {
- std::vector<char> v;
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<std::vector<char>>());
- }
- TEST(MSGPACK_X3_PARSE, bin_2)
- {
- std::vector<char> v;
- for (uint64_t i = 0; i != 0x1fU; ++i) v.push_back(i%0xff);
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<std::vector<char>>());
- }
- TEST(MSGPACK_X3_PARSE, bin_3)
- {
- std::vector<char> v;
- for (uint64_t i = 0; i != 0xffU; ++i) v.push_back(i%0xff);
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<std::vector<char>>());
- }
- TEST(MSGPACK_X3_PARSE, bin_4)
- {
- std::vector<char> v;
- for (uint64_t i = 0; i != 0xffU+1U; ++i) v.push_back(i%0xff);
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<std::vector<char>>());
- }
- TEST(MSGPACK_X3_PARSE, bin_5)
- {
- std::vector<char> v;
- for (uint64_t i = 0; i != 0xffffU; ++i) v.push_back(i%0xff);
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<std::vector<char>>());
- }
- TEST(MSGPACK_X3_PARSE, bin_6)
- {
- std::vector<char> v;
- for (uint64_t i = 0; i != 0xffffUL + 1UL; ++i) v.push_back(i%0xff);
- std::stringstream ss;
- msgpack::pack(ss, v);
- auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, oh.get().as<std::vector<char>>());
- }
- TEST(MSGPACK_X3_PARSE, fixext1)
- {
- std::stringstream ss;
- msgpack::packer<std::stringstream> packer(ss);
- char const buf [] = { 2 };
- packer.pack_ext(sizeof(buf), 1);
- packer.pack_ext_body(buf, sizeof(buf));
- msgpack::object_handle oh =
- msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(1ul, oh.get().via.ext.size);
- EXPECT_EQ(1, oh.get().via.ext.type());
- EXPECT_EQ(2, oh.get().via.ext.data()[0]);
- }
- TEST(MSGPACK_X3_PARSE, fixext2)
- {
- std::stringstream ss;
- msgpack::packer<std::stringstream> packer(ss);
- char const buf [] = { 2, 3 };
- packer.pack_ext(sizeof(buf), 0);
- packer.pack_ext_body(buf, sizeof(buf));
- msgpack::object_handle oh =
- msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(2ul, oh.get().via.ext.size);
- EXPECT_EQ(0, oh.get().via.ext.type());
- EXPECT_TRUE(
- std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data()));
- }
- TEST(MSGPACK_X3_PARSE, fixext4)
- {
- std::stringstream ss;
- msgpack::packer<std::stringstream> packer(ss);
- char const buf [] = { 2, 3, 4, 5 };
- packer.pack_ext(sizeof(buf), 1);
- packer.pack_ext_body(buf, sizeof(buf));
- msgpack::object_handle oh =
- msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(4ul, oh.get().via.ext.size);
- EXPECT_EQ(1, oh.get().via.ext.type());
- EXPECT_TRUE(
- std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data()));
- }
- TEST(MSGPACK_X3_PARSE, fixext8)
- {
- std::stringstream ss;
- msgpack::packer<std::stringstream> packer(ss);
- char const buf [] = { 2, 3, 4, 5, 6, 7, 8, 9 };
- packer.pack_ext(sizeof(buf), 1);
- packer.pack_ext_body(buf, sizeof(buf));
- msgpack::object_handle oh =
- msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(8ul, oh.get().via.ext.size);
- EXPECT_EQ(1, oh.get().via.ext.type());
- EXPECT_TRUE(
- std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data()));
- }
- TEST(MSGPACK_X3_PARSE, fixext16)
- {
- std::stringstream ss;
- msgpack::packer<std::stringstream> packer(ss);
- char const buf [] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 };
- packer.pack_ext(sizeof(buf), 1);
- packer.pack_ext_body(buf, sizeof(buf));
- msgpack::object_handle oh =
- msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(16ul, oh.get().via.ext.size);
- EXPECT_EQ(1, oh.get().via.ext.type());
- EXPECT_TRUE(
- std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data()));
- }
- TEST(MSGPACK_X3_PARSE, ext_0)
- {
- std::size_t const size = 0;
- std::stringstream ss;
- msgpack::packer<std::stringstream> packer(ss);
- packer.pack_ext(size, 77);
- msgpack::object_handle oh =
- msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(size, oh.get().via.ext.size);
- EXPECT_EQ(77, oh.get().via.ext.type());
- }
- TEST(MSGPACK_X3_PARSE, ext_255)
- {
- std::size_t const size = 255;
- std::stringstream ss;
- msgpack::packer<std::stringstream> packer(ss);
- char buf[size];
- for (std::size_t i = 0; i != size; ++i) buf[i] = static_cast<char>(i);
- packer.pack_ext(sizeof(buf), 77);
- packer.pack_ext_body(buf, sizeof(buf));
- msgpack::object_handle oh =
- msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(size, oh.get().via.ext.size);
- EXPECT_EQ(77, oh.get().via.ext.type());
- EXPECT_TRUE(
- std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data()));
- }
- TEST(MSGPACK_X3_PARSE, ext_256)
- {
- std::size_t const size = 256;
- std::stringstream ss;
- msgpack::packer<std::stringstream> packer(ss);
- char buf[size];
- for (std::size_t i = 0; i != size; ++i) buf[i] = static_cast<char>(i);
- packer.pack_ext(sizeof(buf), 77);
- packer.pack_ext_body(buf, sizeof(buf));
- msgpack::object_handle oh =
- msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(size, oh.get().via.ext.size);
- EXPECT_EQ(77, oh.get().via.ext.type());
- EXPECT_TRUE(
- std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data()));
- }
- TEST(MSGPACK_X3_PARSE, ext_65535)
- {
- std::size_t const size = 65535;
- std::stringstream ss;
- msgpack::packer<std::stringstream> packer(ss);
- char buf[size];
- for (std::size_t i = 0; i != size; ++i) buf[i] = static_cast<char>(i);
- packer.pack_ext(sizeof(buf), 77);
- packer.pack_ext_body(buf, sizeof(buf));
- msgpack::object_handle oh =
- msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(size, oh.get().via.ext.size);
- EXPECT_EQ(77, oh.get().via.ext.type());
- EXPECT_TRUE(
- std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data()));
- }
- TEST(MSGPACK_X3_PARSE, ext_65536)
- {
- std::size_t const size = 65536;
- std::stringstream ss;
- msgpack::packer<std::stringstream> packer(ss);
- char buf[size];
- for (std::size_t i = 0; i != size; ++i) buf[i] = static_cast<char>(i);
- packer.pack_ext(sizeof(buf), 77);
- packer.pack_ext_body(buf, sizeof(buf));
- msgpack::object_handle oh =
- msgpack::unpack(ss.str().begin(), ss.str().end());
- EXPECT_EQ(size, oh.get().via.ext.size);
- EXPECT_EQ(77, oh.get().via.ext.type());
- EXPECT_TRUE(
- std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data()));
- }
- TEST(MSGPACK_X3_PARSE, unpack_referenced_1)
- {
- std::string v = "ABC";
- std::stringstream ss;
- msgpack::pack(ss, v);
- bool r;
- msgpack::object_handle oh =
- msgpack::unpack(ss.str().begin(), ss.str().end(), r);
- EXPECT_FALSE(r);
- EXPECT_EQ(v, oh.get().as<std::string>());
- }
- TEST(MSGPACK_X3_PARSE, unpack_referenced_2)
- {
- std::string v = "ABC";
- std::stringstream ss;
- msgpack::pack(ss, v);
- // copy is required because ss.str() returns temporary object.
- std::string str = ss.str();
- bool r;
- msgpack::object_handle oh =
- msgpack::unpack(
- str.begin(),
- str.end(),
- r,
- [](msgpack::type::object_type, std::size_t, void*) {
- return true;
- }
- );
- EXPECT_TRUE(r);
- EXPECT_EQ(v, oh.get().as<std::string>());
- }
- TEST(MSGPACK_X3_PARSE, unpack_zone_1)
- {
- std::string v = "ABC";
- std::stringstream ss;
- msgpack::pack(ss, v);
- msgpack::zone z;
- msgpack::object obj =
- msgpack::unpack(z, ss.str().begin(), ss.str().end());
- EXPECT_EQ(v, obj.as<std::string>());
- }
- TEST(MSGPACK_X3_PARSE, unpack_zone_2)
- {
- std::string v = "ABC";
- std::stringstream ss;
- msgpack::pack(ss, v);
- msgpack::zone z;
- bool r;
- msgpack::object obj =
- msgpack::unpack(z, ss.str().begin(), ss.str().end(), r);
- EXPECT_EQ(v, obj.as<std::string>());
- EXPECT_FALSE(r);
- EXPECT_EQ(v, obj.as<std::string>());
- }
- TEST(MSGPACK_X3_PARSE, unpack_zone_3)
- {
- std::string v = "ABC";
- std::stringstream ss;
- msgpack::pack(ss, v);
- // copy is required because ss.str() returns temporary object.
- std::string str = ss.str();
- msgpack::zone z;
- bool r;
- msgpack::object obj =
- msgpack::unpack(
- z,
- str.begin(),
- str.end(),
- r,
- [](msgpack::type::object_type, std::size_t, void*) {
- return true;
- }
- );
- EXPECT_TRUE(r);
- EXPECT_EQ(v, obj.as<std::string>());
- }
- #endif // defined(MSGPACK_USE_X3_PARSE) && MSGPACK_DEFAULT_API_VERSION >= 2
|