msgpack_x3_parse.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. #include "msgpack.hpp"
  2. #include <sstream>
  3. #include <limits>
  4. #include <cmath>
  5. #include <gtest/gtest.h>
  6. // To avoid link error
  7. TEST(MSGPACK_X3_PARSE, dummy)
  8. {
  9. }
  10. #if defined(MSGPACK_USE_X3_PARSE) && MSGPACK_DEFAULT_API_VERSION >= 2
  11. using namespace std;
  12. const double kEPS = 1e-10;
  13. TEST(MSGPACK_X3_PARSE, nil_t)
  14. {
  15. msgpack::type::nil_t v;
  16. std::stringstream ss;
  17. msgpack::pack(ss, v);
  18. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  19. EXPECT_TRUE(oh.get().is_nil());
  20. }
  21. TEST(MSGPACK_X3_PARSE, bool_false)
  22. {
  23. bool v = false;
  24. std::stringstream ss;
  25. msgpack::pack(ss, v);
  26. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  27. EXPECT_EQ(v, oh.get().as<bool>());
  28. }
  29. TEST(MSGPACK_X3_PARSE, bool_true)
  30. {
  31. bool v = true;
  32. std::stringstream ss;
  33. msgpack::pack(ss, v);
  34. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  35. EXPECT_EQ(v, oh.get().as<bool>());
  36. }
  37. TEST(MSGPACK_X3_PARSE, positive_fixint_1)
  38. {
  39. uint8_t v = 0;
  40. std::stringstream ss;
  41. msgpack::pack(ss, v);
  42. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  43. EXPECT_EQ(v, oh.get().as<uint8_t>());
  44. }
  45. TEST(MSGPACK_X3_PARSE, positive_fixint_2)
  46. {
  47. uint8_t v = 127;
  48. std::stringstream ss;
  49. msgpack::pack(ss, v);
  50. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  51. EXPECT_EQ(v, oh.get().as<uint8_t>());
  52. }
  53. TEST(MSGPACK_X3_PARSE, negative_fixint_1)
  54. {
  55. int8_t v = -1;
  56. std::stringstream ss;
  57. msgpack::pack(ss, v);
  58. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  59. EXPECT_EQ(v, oh.get().as<int8_t>());
  60. }
  61. TEST(MSGPACK_X3_PARSE, negative_fixint_2)
  62. {
  63. int8_t v = -32;
  64. std::stringstream ss;
  65. msgpack::pack(ss, v);
  66. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  67. EXPECT_EQ(v, oh.get().as<int8_t>());
  68. }
  69. TEST(MSGPACK_X3_PARSE, uint8_1)
  70. {
  71. uint8_t v = 128U;
  72. std::stringstream ss;
  73. msgpack::pack(ss, v);
  74. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  75. EXPECT_EQ(v, oh.get().as<uint8_t>());
  76. }
  77. TEST(MSGPACK_X3_PARSE, uint8_2)
  78. {
  79. uint8_t v = 0xffU;
  80. std::stringstream ss;
  81. msgpack::pack(ss, v);
  82. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  83. EXPECT_EQ(v, oh.get().as<uint8_t>());
  84. }
  85. TEST(MSGPACK_X3_PARSE, uint16_1)
  86. {
  87. uint16_t v = 0x100U;
  88. std::stringstream ss;
  89. msgpack::pack(ss, v);
  90. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  91. EXPECT_EQ(v, oh.get().as<uint16_t>());
  92. }
  93. TEST(MSGPACK_X3_PARSE, uint16_2)
  94. {
  95. uint16_t v = 0xffffU;
  96. std::stringstream ss;
  97. msgpack::pack(ss, v);
  98. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  99. EXPECT_EQ(v, oh.get().as<uint16_t>());
  100. }
  101. TEST(MSGPACK_X3_PARSE, uint32_1)
  102. {
  103. uint32_t v = 0x10000UL;
  104. std::stringstream ss;
  105. msgpack::pack(ss, v);
  106. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  107. EXPECT_EQ(v, oh.get().as<uint32_t>());
  108. }
  109. TEST(MSGPACK_X3_PARSE, uint32_2)
  110. {
  111. uint32_t v = 0xffffffffUL;
  112. std::stringstream ss;
  113. msgpack::pack(ss, v);
  114. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  115. EXPECT_EQ(v, oh.get().as<uint32_t>());
  116. }
  117. TEST(MSGPACK_X3_PARSE, uint64_1)
  118. {
  119. uint64_t v = 0x100000000ULL;
  120. std::stringstream ss;
  121. msgpack::pack(ss, v);
  122. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  123. EXPECT_EQ(v, oh.get().as<uint64_t>());
  124. }
  125. TEST(MSGPACK_X3_PARSE, uint64_2)
  126. {
  127. uint64_t v = 0xffffffffffffffffULL;
  128. std::stringstream ss;
  129. msgpack::pack(ss, v);
  130. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  131. EXPECT_EQ(v, oh.get().as<uint64_t>());
  132. }
  133. TEST(MSGPACK_X3_PARSE, int8_1)
  134. {
  135. int8_t v = 0b11011111;
  136. std::stringstream ss;
  137. msgpack::pack(ss, v);
  138. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  139. EXPECT_EQ(v, oh.get().as<int8_t>());
  140. }
  141. TEST(MSGPACK_X3_PARSE, int8_2)
  142. {
  143. int8_t v = 0b10000000;
  144. std::stringstream ss;
  145. msgpack::pack(ss, v);
  146. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  147. EXPECT_EQ(v, oh.get().as<int8_t>());
  148. }
  149. TEST(MSGPACK_X3_PARSE, int16_1)
  150. {
  151. int16_t v = 0xff00;
  152. std::stringstream ss;
  153. msgpack::pack(ss, v);
  154. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  155. EXPECT_EQ(v, oh.get().as<int16_t>());
  156. }
  157. TEST(MSGPACK_X3_PARSE, int16_2)
  158. {
  159. int16_t v = 0x8000;
  160. std::stringstream ss;
  161. msgpack::pack(ss, v);
  162. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  163. EXPECT_EQ(v, oh.get().as<int16_t>());
  164. }
  165. TEST(MSGPACK_X3_PARSE, int32_1)
  166. {
  167. int32_t v = 0xff000000L;
  168. std::stringstream ss;
  169. msgpack::pack(ss, v);
  170. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  171. EXPECT_EQ(v, oh.get().as<int32_t>());
  172. }
  173. TEST(MSGPACK_X3_PARSE, int32_2)
  174. {
  175. int32_t v = 0x80000000L;
  176. std::stringstream ss;
  177. msgpack::pack(ss, v);
  178. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  179. EXPECT_EQ(v, oh.get().as<int32_t>());
  180. }
  181. TEST(MSGPACK_X3_PARSE, int64_1)
  182. {
  183. int64_t v = 0xff00000000000000LL;
  184. std::stringstream ss;
  185. msgpack::pack(ss, v);
  186. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  187. EXPECT_EQ(v, oh.get().as<int64_t>());
  188. }
  189. TEST(MSGPACK_X3_PARSE, int64_2)
  190. {
  191. int64_t v = 0x8000000000000000LL;
  192. std::stringstream ss;
  193. msgpack::pack(ss, v);
  194. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  195. EXPECT_EQ(v, oh.get().as<int64_t>());
  196. }
  197. TEST(MSGPACK_X3_PARSE, array_1)
  198. {
  199. std::vector<int> v;
  200. std::stringstream ss;
  201. msgpack::pack(ss, v);
  202. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  203. EXPECT_EQ(v, oh.get().as<std::vector<int> >());
  204. }
  205. TEST(MSGPACK_X3_PARSE, array_2)
  206. {
  207. std::vector<int> v;
  208. std::stringstream ss;
  209. for (int i = 0; i != 0xffU; ++i) v.push_back(i);
  210. msgpack::pack(ss, v);
  211. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  212. EXPECT_EQ(v, oh.get().as<std::vector<int> >());
  213. }
  214. TEST(MSGPACK_X3_PARSE, array_3)
  215. {
  216. std::vector<int> v;
  217. std::stringstream ss;
  218. for (int i = 0; i != 0xffU+1U; ++i) v.push_back(i);
  219. msgpack::pack(ss, v);
  220. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  221. EXPECT_EQ(v, oh.get().as<std::vector<int> >());
  222. }
  223. TEST(MSGPACK_X3_PARSE, array_4)
  224. {
  225. std::vector<int> v;
  226. std::stringstream ss;
  227. for (int i = 0; i != 0xffffU; ++i) v.push_back(i);
  228. msgpack::pack(ss, v);
  229. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  230. EXPECT_EQ(v, oh.get().as<std::vector<int> >());
  231. }
  232. TEST(MSGPACK_X3_PARSE, array_5)
  233. {
  234. std::vector<uint32_t> v;
  235. std::stringstream ss;
  236. for (uint32_t i = 0; i != 0xffffU+1U; ++i) v.push_back(i);
  237. msgpack::pack(ss, v);
  238. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  239. EXPECT_EQ(v, oh.get().as<std::vector<uint32_t> >());
  240. }
  241. TEST(MSGPACK_X3_PARSE, map_1)
  242. {
  243. std::map<int, int> v;
  244. std::stringstream ss;
  245. msgpack::pack(ss, v);
  246. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  247. EXPECT_EQ(v, (oh.get().as<std::map<int, int> >()));
  248. }
  249. TEST(MSGPACK_X3_PARSE, map_2)
  250. {
  251. std::map<int, int> v;
  252. std::stringstream ss;
  253. for (int i = 0; i != 0xffU; ++i) v.emplace(i, i);
  254. msgpack::pack(ss, v);
  255. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  256. EXPECT_EQ(v, (oh.get().as<std::map<int, int> >()));
  257. }
  258. TEST(MSGPACK_X3_PARSE, map_3)
  259. {
  260. std::map<int, int> v;
  261. std::stringstream ss;
  262. for (int i = 0; i != 0xffU+1U; ++i) v.emplace(i, i);
  263. msgpack::pack(ss, v);
  264. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  265. EXPECT_EQ(v, (oh.get().as<std::map<int, int> >()));
  266. }
  267. TEST(MSGPACK_X3_PARSE, map_4)
  268. {
  269. std::map<int, int> v;
  270. std::stringstream ss;
  271. for (int i = 0; i != 0xffffU; ++i) v.emplace(i, i);
  272. msgpack::pack(ss, v);
  273. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  274. EXPECT_EQ(v, (oh.get().as<std::map<int, int> >()));
  275. }
  276. TEST(MSGPACK_X3_PARSE, map_5)
  277. {
  278. std::map<uint32_t, uint32_t> v;
  279. std::stringstream ss;
  280. for (uint32_t i = 0; i != 0xffffU+1U; ++i) v.emplace(i, i);
  281. msgpack::pack(ss, v);
  282. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  283. EXPECT_EQ(v, (oh.get().as<std::map<uint32_t, uint32_t> >()));
  284. }
  285. TEST(MSGPACK_X3_PARSE, float_1)
  286. {
  287. std::vector<float> v;
  288. v.push_back(0.0);
  289. v.push_back(-0.0);
  290. v.push_back(1.0);
  291. v.push_back(-1.0);
  292. v.push_back(numeric_limits<float>::min());
  293. v.push_back(numeric_limits<float>::max());
  294. v.push_back(nanf("tag"));
  295. if (numeric_limits<float>::has_infinity) {
  296. v.push_back(numeric_limits<float>::infinity());
  297. v.push_back(-numeric_limits<float>::infinity());
  298. }
  299. if (numeric_limits<float>::has_quiet_NaN) {
  300. v.push_back(numeric_limits<float>::quiet_NaN());
  301. }
  302. if (numeric_limits<float>::has_signaling_NaN) {
  303. v.push_back(numeric_limits<float>::signaling_NaN());
  304. }
  305. for (unsigned int i = 0; i < v.size() ; i++) {
  306. std::stringstream ss;
  307. float val1 = v[i];
  308. msgpack::pack(ss, val1);
  309. msgpack::object_handle oh =
  310. msgpack::unpack(ss.str().begin(), ss.str().end());
  311. float val2 = oh.get().as<float>();
  312. if (std::isnan(val1))
  313. EXPECT_TRUE(std::isnan(val2));
  314. else if (std::isinf(val1))
  315. EXPECT_TRUE(std::isinf(val2));
  316. else
  317. EXPECT_TRUE(fabs(val2 - val1) <= kEPS);
  318. }
  319. }
  320. TEST(MSGPACK_X3_PARSE, double_1)
  321. {
  322. std::vector<double> v;
  323. v.push_back(0.0);
  324. v.push_back(-0.0);
  325. v.push_back(1.0);
  326. v.push_back(-1.0);
  327. v.push_back(numeric_limits<double>::min());
  328. v.push_back(numeric_limits<double>::max());
  329. v.push_back(nanf("tag"));
  330. if (numeric_limits<double>::has_infinity) {
  331. v.push_back(numeric_limits<double>::infinity());
  332. v.push_back(-numeric_limits<double>::infinity());
  333. }
  334. if (numeric_limits<double>::has_quiet_NaN) {
  335. v.push_back(numeric_limits<double>::quiet_NaN());
  336. }
  337. if (numeric_limits<double>::has_signaling_NaN) {
  338. v.push_back(numeric_limits<double>::signaling_NaN());
  339. }
  340. for (unsigned int i = 0; i < v.size() ; i++) {
  341. std::stringstream ss;
  342. double val1 = v[i];
  343. msgpack::pack(ss, val1);
  344. msgpack::object_handle oh =
  345. msgpack::unpack(ss.str().begin(), ss.str().end());
  346. double val2 = oh.get().as<double>();
  347. if (std::isnan(val1))
  348. EXPECT_TRUE(std::isnan(val2));
  349. else if (std::isinf(val1))
  350. EXPECT_TRUE(std::isinf(val2));
  351. else
  352. EXPECT_TRUE(fabs(val2 - val1) <= kEPS);
  353. }
  354. }
  355. TEST(MSGPACK_X3_PARSE, string_1)
  356. {
  357. std::string v;
  358. std::stringstream ss;
  359. msgpack::pack(ss, v);
  360. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  361. EXPECT_EQ(v, oh.get().as<std::string>());
  362. }
  363. TEST(MSGPACK_X3_PARSE, string_2)
  364. {
  365. std::string v;
  366. for (uint64_t i = 0; i != 0x1fU; ++i) v.push_back('0'+(i%10));
  367. std::stringstream ss;
  368. msgpack::pack(ss, v);
  369. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  370. EXPECT_EQ(v, oh.get().as<std::string>());
  371. }
  372. TEST(MSGPACK_X3_PARSE, string_3)
  373. {
  374. std::string v;
  375. for (uint64_t i = 0; i != 0xffU; ++i) v.push_back('0'+(i%10));
  376. std::stringstream ss;
  377. msgpack::pack(ss, v);
  378. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  379. EXPECT_EQ(v, oh.get().as<std::string>());
  380. }
  381. TEST(MSGPACK_X3_PARSE, string_4)
  382. {
  383. std::string v;
  384. for (uint64_t i = 0; i != 0xffU+1U; ++i) v.push_back('0'+(i%10));
  385. std::stringstream ss;
  386. msgpack::pack(ss, v);
  387. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  388. EXPECT_EQ(v, oh.get().as<std::string>());
  389. }
  390. TEST(MSGPACK_X3_PARSE, string_5)
  391. {
  392. std::string v;
  393. for (uint64_t i = 0; i != 0xffffU; ++i) v.push_back('0'+(i%10));
  394. std::stringstream ss;
  395. msgpack::pack(ss, v);
  396. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  397. EXPECT_EQ(v, oh.get().as<std::string>());
  398. }
  399. TEST(MSGPACK_X3_PARSE, string_6)
  400. {
  401. std::string v;
  402. for (uint64_t i = 0; i != 0xffffUL + 1UL; ++i) v.push_back('0'+(i%10));
  403. std::stringstream ss;
  404. msgpack::pack(ss, v);
  405. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  406. EXPECT_EQ(v, oh.get().as<std::string>());
  407. }
  408. TEST(MSGPACK_X3_PARSE, bin_1)
  409. {
  410. std::vector<char> v;
  411. std::stringstream ss;
  412. msgpack::pack(ss, v);
  413. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  414. EXPECT_EQ(v, oh.get().as<std::vector<char>>());
  415. }
  416. TEST(MSGPACK_X3_PARSE, bin_2)
  417. {
  418. std::vector<char> v;
  419. for (uint64_t i = 0; i != 0x1fU; ++i) v.push_back(i%0xff);
  420. std::stringstream ss;
  421. msgpack::pack(ss, v);
  422. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  423. EXPECT_EQ(v, oh.get().as<std::vector<char>>());
  424. }
  425. TEST(MSGPACK_X3_PARSE, bin_3)
  426. {
  427. std::vector<char> v;
  428. for (uint64_t i = 0; i != 0xffU; ++i) v.push_back(i%0xff);
  429. std::stringstream ss;
  430. msgpack::pack(ss, v);
  431. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  432. EXPECT_EQ(v, oh.get().as<std::vector<char>>());
  433. }
  434. TEST(MSGPACK_X3_PARSE, bin_4)
  435. {
  436. std::vector<char> v;
  437. for (uint64_t i = 0; i != 0xffU+1U; ++i) v.push_back(i%0xff);
  438. std::stringstream ss;
  439. msgpack::pack(ss, v);
  440. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  441. EXPECT_EQ(v, oh.get().as<std::vector<char>>());
  442. }
  443. TEST(MSGPACK_X3_PARSE, bin_5)
  444. {
  445. std::vector<char> v;
  446. for (uint64_t i = 0; i != 0xffffU; ++i) v.push_back(i%0xff);
  447. std::stringstream ss;
  448. msgpack::pack(ss, v);
  449. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  450. EXPECT_EQ(v, oh.get().as<std::vector<char>>());
  451. }
  452. TEST(MSGPACK_X3_PARSE, bin_6)
  453. {
  454. std::vector<char> v;
  455. for (uint64_t i = 0; i != 0xffffUL + 1UL; ++i) v.push_back(i%0xff);
  456. std::stringstream ss;
  457. msgpack::pack(ss, v);
  458. auto oh = msgpack::unpack(ss.str().begin(), ss.str().end());
  459. EXPECT_EQ(v, oh.get().as<std::vector<char>>());
  460. }
  461. TEST(MSGPACK_X3_PARSE, fixext1)
  462. {
  463. std::stringstream ss;
  464. msgpack::packer<std::stringstream> packer(ss);
  465. char const buf [] = { 2 };
  466. packer.pack_ext(sizeof(buf), 1);
  467. packer.pack_ext_body(buf, sizeof(buf));
  468. msgpack::object_handle oh =
  469. msgpack::unpack(ss.str().begin(), ss.str().end());
  470. EXPECT_EQ(1ul, oh.get().via.ext.size);
  471. EXPECT_EQ(1, oh.get().via.ext.type());
  472. EXPECT_EQ(2, oh.get().via.ext.data()[0]);
  473. }
  474. TEST(MSGPACK_X3_PARSE, fixext2)
  475. {
  476. std::stringstream ss;
  477. msgpack::packer<std::stringstream> packer(ss);
  478. char const buf [] = { 2, 3 };
  479. packer.pack_ext(sizeof(buf), 0);
  480. packer.pack_ext_body(buf, sizeof(buf));
  481. msgpack::object_handle oh =
  482. msgpack::unpack(ss.str().begin(), ss.str().end());
  483. EXPECT_EQ(2ul, oh.get().via.ext.size);
  484. EXPECT_EQ(0, oh.get().via.ext.type());
  485. EXPECT_TRUE(
  486. std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data()));
  487. }
  488. TEST(MSGPACK_X3_PARSE, fixext4)
  489. {
  490. std::stringstream ss;
  491. msgpack::packer<std::stringstream> packer(ss);
  492. char const buf [] = { 2, 3, 4, 5 };
  493. packer.pack_ext(sizeof(buf), 1);
  494. packer.pack_ext_body(buf, sizeof(buf));
  495. msgpack::object_handle oh =
  496. msgpack::unpack(ss.str().begin(), ss.str().end());
  497. EXPECT_EQ(4ul, oh.get().via.ext.size);
  498. EXPECT_EQ(1, oh.get().via.ext.type());
  499. EXPECT_TRUE(
  500. std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data()));
  501. }
  502. TEST(MSGPACK_X3_PARSE, fixext8)
  503. {
  504. std::stringstream ss;
  505. msgpack::packer<std::stringstream> packer(ss);
  506. char const buf [] = { 2, 3, 4, 5, 6, 7, 8, 9 };
  507. packer.pack_ext(sizeof(buf), 1);
  508. packer.pack_ext_body(buf, sizeof(buf));
  509. msgpack::object_handle oh =
  510. msgpack::unpack(ss.str().begin(), ss.str().end());
  511. EXPECT_EQ(8ul, oh.get().via.ext.size);
  512. EXPECT_EQ(1, oh.get().via.ext.type());
  513. EXPECT_TRUE(
  514. std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data()));
  515. }
  516. TEST(MSGPACK_X3_PARSE, fixext16)
  517. {
  518. std::stringstream ss;
  519. msgpack::packer<std::stringstream> packer(ss);
  520. char const buf [] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 };
  521. packer.pack_ext(sizeof(buf), 1);
  522. packer.pack_ext_body(buf, sizeof(buf));
  523. msgpack::object_handle oh =
  524. msgpack::unpack(ss.str().begin(), ss.str().end());
  525. EXPECT_EQ(16ul, oh.get().via.ext.size);
  526. EXPECT_EQ(1, oh.get().via.ext.type());
  527. EXPECT_TRUE(
  528. std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data()));
  529. }
  530. TEST(MSGPACK_X3_PARSE, ext_0)
  531. {
  532. std::size_t const size = 0;
  533. std::stringstream ss;
  534. msgpack::packer<std::stringstream> packer(ss);
  535. packer.pack_ext(size, 77);
  536. msgpack::object_handle oh =
  537. msgpack::unpack(ss.str().begin(), ss.str().end());
  538. EXPECT_EQ(size, oh.get().via.ext.size);
  539. EXPECT_EQ(77, oh.get().via.ext.type());
  540. }
  541. TEST(MSGPACK_X3_PARSE, ext_255)
  542. {
  543. std::size_t const size = 255;
  544. std::stringstream ss;
  545. msgpack::packer<std::stringstream> packer(ss);
  546. char buf[size];
  547. for (std::size_t i = 0; i != size; ++i) buf[i] = static_cast<char>(i);
  548. packer.pack_ext(sizeof(buf), 77);
  549. packer.pack_ext_body(buf, sizeof(buf));
  550. msgpack::object_handle oh =
  551. msgpack::unpack(ss.str().begin(), ss.str().end());
  552. EXPECT_EQ(size, oh.get().via.ext.size);
  553. EXPECT_EQ(77, oh.get().via.ext.type());
  554. EXPECT_TRUE(
  555. std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data()));
  556. }
  557. TEST(MSGPACK_X3_PARSE, ext_256)
  558. {
  559. std::size_t const size = 256;
  560. std::stringstream ss;
  561. msgpack::packer<std::stringstream> packer(ss);
  562. char buf[size];
  563. for (std::size_t i = 0; i != size; ++i) buf[i] = static_cast<char>(i);
  564. packer.pack_ext(sizeof(buf), 77);
  565. packer.pack_ext_body(buf, sizeof(buf));
  566. msgpack::object_handle oh =
  567. msgpack::unpack(ss.str().begin(), ss.str().end());
  568. EXPECT_EQ(size, oh.get().via.ext.size);
  569. EXPECT_EQ(77, oh.get().via.ext.type());
  570. EXPECT_TRUE(
  571. std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data()));
  572. }
  573. TEST(MSGPACK_X3_PARSE, ext_65535)
  574. {
  575. std::size_t const size = 65535;
  576. std::stringstream ss;
  577. msgpack::packer<std::stringstream> packer(ss);
  578. char buf[size];
  579. for (std::size_t i = 0; i != size; ++i) buf[i] = static_cast<char>(i);
  580. packer.pack_ext(sizeof(buf), 77);
  581. packer.pack_ext_body(buf, sizeof(buf));
  582. msgpack::object_handle oh =
  583. msgpack::unpack(ss.str().begin(), ss.str().end());
  584. EXPECT_EQ(size, oh.get().via.ext.size);
  585. EXPECT_EQ(77, oh.get().via.ext.type());
  586. EXPECT_TRUE(
  587. std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data()));
  588. }
  589. TEST(MSGPACK_X3_PARSE, ext_65536)
  590. {
  591. std::size_t const size = 65536;
  592. std::stringstream ss;
  593. msgpack::packer<std::stringstream> packer(ss);
  594. char buf[size];
  595. for (std::size_t i = 0; i != size; ++i) buf[i] = static_cast<char>(i);
  596. packer.pack_ext(sizeof(buf), 77);
  597. packer.pack_ext_body(buf, sizeof(buf));
  598. msgpack::object_handle oh =
  599. msgpack::unpack(ss.str().begin(), ss.str().end());
  600. EXPECT_EQ(size, oh.get().via.ext.size);
  601. EXPECT_EQ(77, oh.get().via.ext.type());
  602. EXPECT_TRUE(
  603. std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data()));
  604. }
  605. TEST(MSGPACK_X3_PARSE, unpack_referenced_1)
  606. {
  607. std::string v = "ABC";
  608. std::stringstream ss;
  609. msgpack::pack(ss, v);
  610. bool r;
  611. msgpack::object_handle oh =
  612. msgpack::unpack(ss.str().begin(), ss.str().end(), r);
  613. EXPECT_FALSE(r);
  614. EXPECT_EQ(v, oh.get().as<std::string>());
  615. }
  616. TEST(MSGPACK_X3_PARSE, unpack_referenced_2)
  617. {
  618. std::string v = "ABC";
  619. std::stringstream ss;
  620. msgpack::pack(ss, v);
  621. // copy is required because ss.str() returns temporary object.
  622. std::string str = ss.str();
  623. bool r;
  624. msgpack::object_handle oh =
  625. msgpack::unpack(
  626. str.begin(),
  627. str.end(),
  628. r,
  629. [](msgpack::type::object_type, std::size_t, void*) {
  630. return true;
  631. }
  632. );
  633. EXPECT_TRUE(r);
  634. EXPECT_EQ(v, oh.get().as<std::string>());
  635. }
  636. TEST(MSGPACK_X3_PARSE, unpack_zone_1)
  637. {
  638. std::string v = "ABC";
  639. std::stringstream ss;
  640. msgpack::pack(ss, v);
  641. msgpack::zone z;
  642. msgpack::object obj =
  643. msgpack::unpack(z, ss.str().begin(), ss.str().end());
  644. EXPECT_EQ(v, obj.as<std::string>());
  645. }
  646. TEST(MSGPACK_X3_PARSE, unpack_zone_2)
  647. {
  648. std::string v = "ABC";
  649. std::stringstream ss;
  650. msgpack::pack(ss, v);
  651. msgpack::zone z;
  652. bool r;
  653. msgpack::object obj =
  654. msgpack::unpack(z, ss.str().begin(), ss.str().end(), r);
  655. EXPECT_EQ(v, obj.as<std::string>());
  656. EXPECT_FALSE(r);
  657. EXPECT_EQ(v, obj.as<std::string>());
  658. }
  659. TEST(MSGPACK_X3_PARSE, unpack_zone_3)
  660. {
  661. std::string v = "ABC";
  662. std::stringstream ss;
  663. msgpack::pack(ss, v);
  664. // copy is required because ss.str() returns temporary object.
  665. std::string str = ss.str();
  666. msgpack::zone z;
  667. bool r;
  668. msgpack::object obj =
  669. msgpack::unpack(
  670. z,
  671. str.begin(),
  672. str.end(),
  673. r,
  674. [](msgpack::type::object_type, std::size_t, void*) {
  675. return true;
  676. }
  677. );
  678. EXPECT_TRUE(r);
  679. EXPECT_EQ(v, obj.as<std::string>());
  680. }
  681. #endif // defined(MSGPACK_USE_X3_PARSE) && MSGPACK_DEFAULT_API_VERSION >= 2