unit-msgpack.cpp 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++ (supporting code)
  3. // | | |__ | | | | | | version 3.11.2
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  7. // SPDX-License-Identifier: MIT
  8. #include "doctest_compatibility.h"
  9. #include <nlohmann/json.hpp>
  10. using nlohmann::json;
  11. #ifdef JSON_TEST_NO_GLOBAL_UDLS
  12. using namespace nlohmann::literals; // NOLINT(google-build-using-namespace)
  13. #endif
  14. #include <fstream>
  15. #include <sstream>
  16. #include <iomanip>
  17. #include <limits>
  18. #include <set>
  19. #include "make_test_data_available.hpp"
  20. #include "test_utils.hpp"
  21. namespace
  22. {
  23. class SaxCountdown
  24. {
  25. public:
  26. explicit SaxCountdown(const int count) : events_left(count)
  27. {}
  28. bool null()
  29. {
  30. return events_left-- > 0;
  31. }
  32. bool boolean(bool /*unused*/)
  33. {
  34. return events_left-- > 0;
  35. }
  36. bool number_integer(json::number_integer_t /*unused*/)
  37. {
  38. return events_left-- > 0;
  39. }
  40. bool number_unsigned(json::number_unsigned_t /*unused*/)
  41. {
  42. return events_left-- > 0;
  43. }
  44. bool number_float(json::number_float_t /*unused*/, const std::string& /*unused*/)
  45. {
  46. return events_left-- > 0;
  47. }
  48. bool string(std::string& /*unused*/)
  49. {
  50. return events_left-- > 0;
  51. }
  52. bool binary(std::vector<std::uint8_t>& /*unused*/)
  53. {
  54. return events_left-- > 0;
  55. }
  56. bool start_object(std::size_t /*unused*/)
  57. {
  58. return events_left-- > 0;
  59. }
  60. bool key(std::string& /*unused*/)
  61. {
  62. return events_left-- > 0;
  63. }
  64. bool end_object()
  65. {
  66. return events_left-- > 0;
  67. }
  68. bool start_array(std::size_t /*unused*/)
  69. {
  70. return events_left-- > 0;
  71. }
  72. bool end_array()
  73. {
  74. return events_left-- > 0;
  75. }
  76. bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/) // NOLINT(readability-convert-member-functions-to-static)
  77. {
  78. return false;
  79. }
  80. private:
  81. int events_left = 0;
  82. };
  83. } // namespace
  84. TEST_CASE("MessagePack")
  85. {
  86. SECTION("individual values")
  87. {
  88. SECTION("discarded")
  89. {
  90. // discarded values are not serialized
  91. json const j = json::value_t::discarded;
  92. const auto result = json::to_msgpack(j);
  93. CHECK(result.empty());
  94. }
  95. SECTION("null")
  96. {
  97. json const j = nullptr;
  98. std::vector<uint8_t> const expected = {0xc0};
  99. const auto result = json::to_msgpack(j);
  100. CHECK(result == expected);
  101. // roundtrip
  102. CHECK(json::from_msgpack(result) == j);
  103. CHECK(json::from_msgpack(result, true, false) == j);
  104. }
  105. SECTION("boolean")
  106. {
  107. SECTION("true")
  108. {
  109. json const j = true;
  110. std::vector<uint8_t> const expected = {0xc3};
  111. const auto result = json::to_msgpack(j);
  112. CHECK(result == expected);
  113. // roundtrip
  114. CHECK(json::from_msgpack(result) == j);
  115. CHECK(json::from_msgpack(result, true, false) == j);
  116. }
  117. SECTION("false")
  118. {
  119. json const j = false;
  120. std::vector<uint8_t> const expected = {0xc2};
  121. const auto result = json::to_msgpack(j);
  122. CHECK(result == expected);
  123. // roundtrip
  124. CHECK(json::from_msgpack(result) == j);
  125. CHECK(json::from_msgpack(result, true, false) == j);
  126. }
  127. }
  128. SECTION("number")
  129. {
  130. SECTION("signed")
  131. {
  132. SECTION("-32..-1 (negative fixnum)")
  133. {
  134. for (auto i = -32; i <= -1; ++i)
  135. {
  136. CAPTURE(i)
  137. // create JSON value with integer number
  138. json const j = i;
  139. // check type
  140. CHECK(j.is_number_integer());
  141. // create expected byte vector
  142. std::vector<uint8_t> const expected
  143. {
  144. static_cast<uint8_t>(i)
  145. };
  146. // compare result + size
  147. const auto result = json::to_msgpack(j);
  148. CHECK(result == expected);
  149. CHECK(result.size() == 1);
  150. // check individual bytes
  151. CHECK(static_cast<int8_t>(result[0]) == i);
  152. // roundtrip
  153. CHECK(json::from_msgpack(result) == j);
  154. CHECK(json::from_msgpack(result, true, false) == j);
  155. }
  156. }
  157. SECTION("0..127 (positive fixnum)")
  158. {
  159. for (size_t i = 0; i <= 127; ++i)
  160. {
  161. CAPTURE(i)
  162. // create JSON value with integer number
  163. json j = -1;
  164. j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
  165. // check type
  166. CHECK(j.is_number_integer());
  167. // create expected byte vector
  168. std::vector<uint8_t> const expected{static_cast<uint8_t>(i)};
  169. // compare result + size
  170. const auto result = json::to_msgpack(j);
  171. CHECK(result == expected);
  172. CHECK(result.size() == 1);
  173. // check individual bytes
  174. CHECK(result[0] == i);
  175. // roundtrip
  176. CHECK(json::from_msgpack(result) == j);
  177. CHECK(json::from_msgpack(result, true, false) == j);
  178. }
  179. }
  180. SECTION("128..255 (int 8)")
  181. {
  182. for (size_t i = 128; i <= 255; ++i)
  183. {
  184. CAPTURE(i)
  185. // create JSON value with integer number
  186. json j = -1;
  187. j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
  188. // check type
  189. CHECK(j.is_number_integer());
  190. // create expected byte vector
  191. std::vector<uint8_t> const expected
  192. {
  193. 0xcc,
  194. static_cast<uint8_t>(i),
  195. };
  196. // compare result + size
  197. const auto result = json::to_msgpack(j);
  198. CHECK(result == expected);
  199. CHECK(result.size() == 2);
  200. // check individual bytes
  201. CHECK(result[0] == 0xcc);
  202. auto const restored = static_cast<uint8_t>(result[1]);
  203. CHECK(restored == i);
  204. // roundtrip
  205. CHECK(json::from_msgpack(result) == j);
  206. CHECK(json::from_msgpack(result, true, false) == j);
  207. }
  208. }
  209. SECTION("256..65535 (int 16)")
  210. {
  211. for (size_t i = 256; i <= 65535; ++i)
  212. {
  213. CAPTURE(i)
  214. // create JSON value with integer number
  215. json j = -1;
  216. j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
  217. // check type
  218. CHECK(j.is_number_integer());
  219. // create expected byte vector
  220. std::vector<uint8_t> const expected
  221. {
  222. 0xcd,
  223. static_cast<uint8_t>((i >> 8) & 0xff),
  224. static_cast<uint8_t>(i & 0xff),
  225. };
  226. // compare result + size
  227. const auto result = json::to_msgpack(j);
  228. CHECK(result == expected);
  229. CHECK(result.size() == 3);
  230. // check individual bytes
  231. CHECK(result[0] == 0xcd);
  232. auto const restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
  233. CHECK(restored == i);
  234. // roundtrip
  235. CHECK(json::from_msgpack(result) == j);
  236. CHECK(json::from_msgpack(result, true, false) == j);
  237. }
  238. }
  239. SECTION("65536..4294967295 (int 32)")
  240. {
  241. for (uint32_t i :
  242. {
  243. 65536u, 77777u, 1048576u, 4294967295u
  244. })
  245. {
  246. CAPTURE(i)
  247. // create JSON value with integer number
  248. json j = -1;
  249. j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
  250. // check type
  251. CHECK(j.is_number_integer());
  252. // create expected byte vector
  253. std::vector<uint8_t> const expected
  254. {
  255. 0xce,
  256. static_cast<uint8_t>((i >> 24) & 0xff),
  257. static_cast<uint8_t>((i >> 16) & 0xff),
  258. static_cast<uint8_t>((i >> 8) & 0xff),
  259. static_cast<uint8_t>(i & 0xff),
  260. };
  261. // compare result + size
  262. const auto result = json::to_msgpack(j);
  263. CHECK(result == expected);
  264. CHECK(result.size() == 5);
  265. // check individual bytes
  266. CHECK(result[0] == 0xce);
  267. uint32_t const restored = (static_cast<uint32_t>(result[1]) << 030) +
  268. (static_cast<uint32_t>(result[2]) << 020) +
  269. (static_cast<uint32_t>(result[3]) << 010) +
  270. static_cast<uint32_t>(result[4]);
  271. CHECK(restored == i);
  272. // roundtrip
  273. CHECK(json::from_msgpack(result) == j);
  274. CHECK(json::from_msgpack(result, true, false) == j);
  275. }
  276. }
  277. SECTION("4294967296..9223372036854775807 (int 64)")
  278. {
  279. for (uint64_t i :
  280. {
  281. 4294967296LU, 9223372036854775807LU
  282. })
  283. {
  284. CAPTURE(i)
  285. // create JSON value with integer number
  286. json j = -1;
  287. j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
  288. // check type
  289. CHECK(j.is_number_integer());
  290. // create expected byte vector
  291. std::vector<uint8_t> const expected
  292. {
  293. 0xcf,
  294. static_cast<uint8_t>((i >> 070) & 0xff),
  295. static_cast<uint8_t>((i >> 060) & 0xff),
  296. static_cast<uint8_t>((i >> 050) & 0xff),
  297. static_cast<uint8_t>((i >> 040) & 0xff),
  298. static_cast<uint8_t>((i >> 030) & 0xff),
  299. static_cast<uint8_t>((i >> 020) & 0xff),
  300. static_cast<uint8_t>((i >> 010) & 0xff),
  301. static_cast<uint8_t>(i & 0xff),
  302. };
  303. // compare result + size
  304. const auto result = json::to_msgpack(j);
  305. CHECK(result == expected);
  306. CHECK(result.size() == 9);
  307. // check individual bytes
  308. CHECK(result[0] == 0xcf);
  309. uint64_t const restored = (static_cast<uint64_t>(result[1]) << 070) +
  310. (static_cast<uint64_t>(result[2]) << 060) +
  311. (static_cast<uint64_t>(result[3]) << 050) +
  312. (static_cast<uint64_t>(result[4]) << 040) +
  313. (static_cast<uint64_t>(result[5]) << 030) +
  314. (static_cast<uint64_t>(result[6]) << 020) +
  315. (static_cast<uint64_t>(result[7]) << 010) +
  316. static_cast<uint64_t>(result[8]);
  317. CHECK(restored == i);
  318. // roundtrip
  319. CHECK(json::from_msgpack(result) == j);
  320. CHECK(json::from_msgpack(result, true, false) == j);
  321. }
  322. }
  323. SECTION("-128..-33 (int 8)")
  324. {
  325. for (auto i = -128; i <= -33; ++i)
  326. {
  327. CAPTURE(i)
  328. // create JSON value with integer number
  329. json const j = i;
  330. // check type
  331. CHECK(j.is_number_integer());
  332. // create expected byte vector
  333. std::vector<uint8_t> const expected
  334. {
  335. 0xd0,
  336. static_cast<uint8_t>(i),
  337. };
  338. // compare result + size
  339. const auto result = json::to_msgpack(j);
  340. CHECK(result == expected);
  341. CHECK(result.size() == 2);
  342. // check individual bytes
  343. CHECK(result[0] == 0xd0);
  344. CHECK(static_cast<int8_t>(result[1]) == i);
  345. // roundtrip
  346. CHECK(json::from_msgpack(result) == j);
  347. CHECK(json::from_msgpack(result, true, false) == j);
  348. }
  349. }
  350. SECTION("-9263 (int 16)")
  351. {
  352. json const j = -9263;
  353. std::vector<uint8_t> const expected = {0xd1, 0xdb, 0xd1};
  354. const auto result = json::to_msgpack(j);
  355. CHECK(result == expected);
  356. auto const restored = static_cast<int16_t>((result[1] << 8) + result[2]);
  357. CHECK(restored == -9263);
  358. // roundtrip
  359. CHECK(json::from_msgpack(result) == j);
  360. CHECK(json::from_msgpack(result, true, false) == j);
  361. }
  362. SECTION("-32768..-129 (int 16)")
  363. {
  364. for (int16_t i = -32768; i <= static_cast<std::int16_t>(-129); ++i)
  365. {
  366. CAPTURE(i)
  367. // create JSON value with integer number
  368. json const j = i;
  369. // check type
  370. CHECK(j.is_number_integer());
  371. // create expected byte vector
  372. std::vector<uint8_t> const expected
  373. {
  374. 0xd1,
  375. static_cast<uint8_t>((i >> 8) & 0xff),
  376. static_cast<uint8_t>(i & 0xff),
  377. };
  378. // compare result + size
  379. const auto result = json::to_msgpack(j);
  380. CHECK(result == expected);
  381. CHECK(result.size() == 3);
  382. // check individual bytes
  383. CHECK(result[0] == 0xd1);
  384. auto const restored = static_cast<int16_t>((result[1] << 8) + result[2]);
  385. CHECK(restored == i);
  386. // roundtrip
  387. CHECK(json::from_msgpack(result) == j);
  388. CHECK(json::from_msgpack(result, true, false) == j);
  389. }
  390. }
  391. SECTION("-32769..-2147483648")
  392. {
  393. std::vector<int32_t> const numbers
  394. {
  395. -32769,
  396. -65536,
  397. -77777,
  398. -1048576,
  399. -2147483648LL,
  400. };
  401. for (auto i : numbers)
  402. {
  403. CAPTURE(i)
  404. // create JSON value with integer number
  405. json const j = i;
  406. // check type
  407. CHECK(j.is_number_integer());
  408. // create expected byte vector
  409. std::vector<uint8_t> const expected
  410. {
  411. 0xd2,
  412. static_cast<uint8_t>((i >> 24) & 0xff),
  413. static_cast<uint8_t>((i >> 16) & 0xff),
  414. static_cast<uint8_t>((i >> 8) & 0xff),
  415. static_cast<uint8_t>(i & 0xff),
  416. };
  417. // compare result + size
  418. const auto result = json::to_msgpack(j);
  419. CHECK(result == expected);
  420. CHECK(result.size() == 5);
  421. // check individual bytes
  422. CHECK(result[0] == 0xd2);
  423. uint32_t const restored = (static_cast<uint32_t>(result[1]) << 030) +
  424. (static_cast<uint32_t>(result[2]) << 020) +
  425. (static_cast<uint32_t>(result[3]) << 010) +
  426. static_cast<uint32_t>(result[4]);
  427. CHECK(static_cast<std::int32_t>(restored) == i);
  428. // roundtrip
  429. CHECK(json::from_msgpack(result) == j);
  430. CHECK(json::from_msgpack(result, true, false) == j);
  431. }
  432. }
  433. SECTION("-9223372036854775808..-2147483649 (int 64)")
  434. {
  435. std::vector<int64_t> const numbers
  436. {
  437. (std::numeric_limits<int64_t>::min)(),
  438. -2147483649LL,
  439. };
  440. for (auto i : numbers)
  441. {
  442. CAPTURE(i)
  443. // create JSON value with unsigned integer number
  444. json const j = i;
  445. // check type
  446. CHECK(j.is_number_integer());
  447. // create expected byte vector
  448. std::vector<uint8_t> const expected
  449. {
  450. 0xd3,
  451. static_cast<uint8_t>((i >> 070) & 0xff),
  452. static_cast<uint8_t>((i >> 060) & 0xff),
  453. static_cast<uint8_t>((i >> 050) & 0xff),
  454. static_cast<uint8_t>((i >> 040) & 0xff),
  455. static_cast<uint8_t>((i >> 030) & 0xff),
  456. static_cast<uint8_t>((i >> 020) & 0xff),
  457. static_cast<uint8_t>((i >> 010) & 0xff),
  458. static_cast<uint8_t>(i & 0xff),
  459. };
  460. // compare result + size
  461. const auto result = json::to_msgpack(j);
  462. CHECK(result == expected);
  463. CHECK(result.size() == 9);
  464. // check individual bytes
  465. CHECK(result[0] == 0xd3);
  466. int64_t const restored = (static_cast<int64_t>(result[1]) << 070) +
  467. (static_cast<int64_t>(result[2]) << 060) +
  468. (static_cast<int64_t>(result[3]) << 050) +
  469. (static_cast<int64_t>(result[4]) << 040) +
  470. (static_cast<int64_t>(result[5]) << 030) +
  471. (static_cast<int64_t>(result[6]) << 020) +
  472. (static_cast<int64_t>(result[7]) << 010) +
  473. static_cast<int64_t>(result[8]);
  474. CHECK(restored == i);
  475. // roundtrip
  476. CHECK(json::from_msgpack(result) == j);
  477. CHECK(json::from_msgpack(result, true, false) == j);
  478. }
  479. }
  480. }
  481. SECTION("unsigned")
  482. {
  483. SECTION("0..127 (positive fixnum)")
  484. {
  485. for (size_t i = 0; i <= 127; ++i)
  486. {
  487. CAPTURE(i)
  488. // create JSON value with unsigned integer number
  489. json const j = i;
  490. // check type
  491. CHECK(j.is_number_unsigned());
  492. // create expected byte vector
  493. std::vector<uint8_t> const expected{static_cast<uint8_t>(i)};
  494. // compare result + size
  495. const auto result = json::to_msgpack(j);
  496. CHECK(result == expected);
  497. CHECK(result.size() == 1);
  498. // check individual bytes
  499. CHECK(result[0] == i);
  500. // roundtrip
  501. CHECK(json::from_msgpack(result) == j);
  502. CHECK(json::from_msgpack(result, true, false) == j);
  503. }
  504. }
  505. SECTION("128..255 (uint 8)")
  506. {
  507. for (size_t i = 128; i <= 255; ++i)
  508. {
  509. CAPTURE(i)
  510. // create JSON value with unsigned integer number
  511. json const j = i;
  512. // check type
  513. CHECK(j.is_number_unsigned());
  514. // create expected byte vector
  515. std::vector<uint8_t> const expected
  516. {
  517. 0xcc,
  518. static_cast<uint8_t>(i),
  519. };
  520. // compare result + size
  521. const auto result = json::to_msgpack(j);
  522. CHECK(result == expected);
  523. CHECK(result.size() == 2);
  524. // check individual bytes
  525. CHECK(result[0] == 0xcc);
  526. auto const restored = static_cast<uint8_t>(result[1]);
  527. CHECK(restored == i);
  528. // roundtrip
  529. CHECK(json::from_msgpack(result) == j);
  530. CHECK(json::from_msgpack(result, true, false) == j);
  531. }
  532. }
  533. SECTION("256..65535 (uint 16)")
  534. {
  535. for (size_t i = 256; i <= 65535; ++i)
  536. {
  537. CAPTURE(i)
  538. // create JSON value with unsigned integer number
  539. json const j = i;
  540. // check type
  541. CHECK(j.is_number_unsigned());
  542. // create expected byte vector
  543. std::vector<uint8_t> const expected
  544. {
  545. 0xcd,
  546. static_cast<uint8_t>((i >> 8) & 0xff),
  547. static_cast<uint8_t>(i & 0xff),
  548. };
  549. // compare result + size
  550. const auto result = json::to_msgpack(j);
  551. CHECK(result == expected);
  552. CHECK(result.size() == 3);
  553. // check individual bytes
  554. CHECK(result[0] == 0xcd);
  555. auto const restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
  556. CHECK(restored == i);
  557. // roundtrip
  558. CHECK(json::from_msgpack(result) == j);
  559. CHECK(json::from_msgpack(result, true, false) == j);
  560. }
  561. }
  562. SECTION("65536..4294967295 (uint 32)")
  563. {
  564. for (const uint32_t i :
  565. {
  566. 65536u, 77777u, 1048576u, 4294967295u
  567. })
  568. {
  569. CAPTURE(i)
  570. // create JSON value with unsigned integer number
  571. json const j = i;
  572. // check type
  573. CHECK(j.is_number_unsigned());
  574. // create expected byte vector
  575. std::vector<uint8_t> const expected
  576. {
  577. 0xce,
  578. static_cast<uint8_t>((i >> 24) & 0xff),
  579. static_cast<uint8_t>((i >> 16) & 0xff),
  580. static_cast<uint8_t>((i >> 8) & 0xff),
  581. static_cast<uint8_t>(i & 0xff),
  582. };
  583. // compare result + size
  584. const auto result = json::to_msgpack(j);
  585. CHECK(result == expected);
  586. CHECK(result.size() == 5);
  587. // check individual bytes
  588. CHECK(result[0] == 0xce);
  589. uint32_t const restored = (static_cast<uint32_t>(result[1]) << 030) +
  590. (static_cast<uint32_t>(result[2]) << 020) +
  591. (static_cast<uint32_t>(result[3]) << 010) +
  592. static_cast<uint32_t>(result[4]);
  593. CHECK(restored == i);
  594. // roundtrip
  595. CHECK(json::from_msgpack(result) == j);
  596. CHECK(json::from_msgpack(result, true, false) == j);
  597. }
  598. }
  599. SECTION("4294967296..18446744073709551615 (uint 64)")
  600. {
  601. for (const uint64_t i :
  602. {
  603. 4294967296LU, 18446744073709551615LU
  604. })
  605. {
  606. CAPTURE(i)
  607. // create JSON value with unsigned integer number
  608. json const j = i;
  609. // check type
  610. CHECK(j.is_number_unsigned());
  611. // create expected byte vector
  612. std::vector<uint8_t> const expected
  613. {
  614. 0xcf,
  615. static_cast<uint8_t>((i >> 070) & 0xff),
  616. static_cast<uint8_t>((i >> 060) & 0xff),
  617. static_cast<uint8_t>((i >> 050) & 0xff),
  618. static_cast<uint8_t>((i >> 040) & 0xff),
  619. static_cast<uint8_t>((i >> 030) & 0xff),
  620. static_cast<uint8_t>((i >> 020) & 0xff),
  621. static_cast<uint8_t>((i >> 010) & 0xff),
  622. static_cast<uint8_t>(i & 0xff),
  623. };
  624. // compare result + size
  625. const auto result = json::to_msgpack(j);
  626. CHECK(result == expected);
  627. CHECK(result.size() == 9);
  628. // check individual bytes
  629. CHECK(result[0] == 0xcf);
  630. uint64_t const restored = (static_cast<uint64_t>(result[1]) << 070) +
  631. (static_cast<uint64_t>(result[2]) << 060) +
  632. (static_cast<uint64_t>(result[3]) << 050) +
  633. (static_cast<uint64_t>(result[4]) << 040) +
  634. (static_cast<uint64_t>(result[5]) << 030) +
  635. (static_cast<uint64_t>(result[6]) << 020) +
  636. (static_cast<uint64_t>(result[7]) << 010) +
  637. static_cast<uint64_t>(result[8]);
  638. CHECK(restored == i);
  639. // roundtrip
  640. CHECK(json::from_msgpack(result) == j);
  641. CHECK(json::from_msgpack(result, true, false) == j);
  642. }
  643. }
  644. }
  645. SECTION("float")
  646. {
  647. SECTION("3.1415925")
  648. {
  649. double const v = 3.1415925;
  650. json const j = v;
  651. std::vector<uint8_t> const expected =
  652. {
  653. 0xcb, 0x40, 0x09, 0x21, 0xfb, 0x3f, 0xa6, 0xde, 0xfc
  654. };
  655. const auto result = json::to_msgpack(j);
  656. CHECK(result == expected);
  657. // roundtrip
  658. CHECK(json::from_msgpack(result) == j);
  659. CHECK(json::from_msgpack(result) == v);
  660. CHECK(json::from_msgpack(result, true, false) == j);
  661. }
  662. SECTION("1.0")
  663. {
  664. double const v = 1.0;
  665. json const j = v;
  666. std::vector<uint8_t> const expected =
  667. {
  668. 0xca, 0x3f, 0x80, 0x00, 0x00
  669. };
  670. const auto result = json::to_msgpack(j);
  671. CHECK(result == expected);
  672. // roundtrip
  673. CHECK(json::from_msgpack(result) == j);
  674. CHECK(json::from_msgpack(result) == v);
  675. CHECK(json::from_msgpack(result, true, false) == j);
  676. }
  677. SECTION("128.128")
  678. {
  679. double const v = 128.1280059814453125;
  680. json const j = v;
  681. std::vector<uint8_t> const expected =
  682. {
  683. 0xca, 0x43, 0x00, 0x20, 0xc5
  684. };
  685. const auto result = json::to_msgpack(j);
  686. CHECK(result == expected);
  687. // roundtrip
  688. CHECK(json::from_msgpack(result) == j);
  689. CHECK(json::from_msgpack(result) == v);
  690. CHECK(json::from_msgpack(result, true, false) == j);
  691. }
  692. }
  693. }
  694. SECTION("string")
  695. {
  696. SECTION("N = 0..31")
  697. {
  698. // explicitly enumerate the first byte for all 32 strings
  699. const std::vector<uint8_t> first_bytes =
  700. {
  701. 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8,
  702. 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1,
  703. 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
  704. 0xbb, 0xbc, 0xbd, 0xbe, 0xbf
  705. };
  706. for (size_t N = 0; N < first_bytes.size(); ++N)
  707. {
  708. CAPTURE(N)
  709. // create JSON value with string containing of N * 'x'
  710. const auto s = std::string(N, 'x');
  711. json const j = s;
  712. // create expected byte vector
  713. std::vector<uint8_t> expected;
  714. expected.push_back(first_bytes[N]);
  715. for (size_t i = 0; i < N; ++i)
  716. {
  717. expected.push_back('x');
  718. }
  719. // check first byte
  720. CHECK((first_bytes[N] & 0x1f) == N);
  721. // compare result + size
  722. const auto result = json::to_msgpack(j);
  723. CHECK(result == expected);
  724. CHECK(result.size() == N + 1);
  725. // check that no null byte is appended
  726. if (N > 0)
  727. {
  728. CHECK(result.back() != '\x00');
  729. }
  730. // roundtrip
  731. CHECK(json::from_msgpack(result) == j);
  732. CHECK(json::from_msgpack(result, true, false) == j);
  733. }
  734. }
  735. SECTION("N = 32..255")
  736. {
  737. for (size_t N = 32; N <= 255; ++N)
  738. {
  739. CAPTURE(N)
  740. // create JSON value with string containing of N * 'x'
  741. const auto s = std::string(N, 'x');
  742. json const j = s;
  743. // create expected byte vector
  744. std::vector<uint8_t> expected;
  745. expected.push_back(0xd9);
  746. expected.push_back(static_cast<uint8_t>(N));
  747. for (size_t i = 0; i < N; ++i)
  748. {
  749. expected.push_back('x');
  750. }
  751. // compare result + size
  752. const auto result = json::to_msgpack(j);
  753. CHECK(result == expected);
  754. CHECK(result.size() == N + 2);
  755. // check that no null byte is appended
  756. CHECK(result.back() != '\x00');
  757. // roundtrip
  758. CHECK(json::from_msgpack(result) == j);
  759. CHECK(json::from_msgpack(result, true, false) == j);
  760. }
  761. }
  762. SECTION("N = 256..65535")
  763. {
  764. for (size_t N :
  765. {
  766. 256u, 999u, 1025u, 3333u, 2048u, 65535u
  767. })
  768. {
  769. CAPTURE(N)
  770. // create JSON value with string containing of N * 'x'
  771. const auto s = std::string(N, 'x');
  772. json const j = s;
  773. // create expected byte vector (hack: create string first)
  774. std::vector<uint8_t> expected(N, 'x');
  775. // reverse order of commands, because we insert at begin()
  776. expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
  777. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
  778. expected.insert(expected.begin(), 0xda);
  779. // compare result + size
  780. const auto result = json::to_msgpack(j);
  781. CHECK(result == expected);
  782. CHECK(result.size() == N + 3);
  783. // check that no null byte is appended
  784. CHECK(result.back() != '\x00');
  785. // roundtrip
  786. CHECK(json::from_msgpack(result) == j);
  787. CHECK(json::from_msgpack(result, true, false) == j);
  788. }
  789. }
  790. SECTION("N = 65536..4294967295")
  791. {
  792. for (size_t N :
  793. {
  794. 65536u, 77777u, 1048576u
  795. })
  796. {
  797. CAPTURE(N)
  798. // create JSON value with string containing of N * 'x'
  799. const auto s = std::string(N, 'x');
  800. json const j = s;
  801. // create expected byte vector (hack: create string first)
  802. std::vector<uint8_t> expected(N, 'x');
  803. // reverse order of commands, because we insert at begin()
  804. expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
  805. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
  806. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 16) & 0xff));
  807. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 24) & 0xff));
  808. expected.insert(expected.begin(), 0xdb);
  809. // compare result + size
  810. const auto result = json::to_msgpack(j);
  811. CHECK(result == expected);
  812. CHECK(result.size() == N + 5);
  813. // check that no null byte is appended
  814. CHECK(result.back() != '\x00');
  815. // roundtrip
  816. CHECK(json::from_msgpack(result) == j);
  817. CHECK(json::from_msgpack(result, true, false) == j);
  818. }
  819. }
  820. }
  821. SECTION("array")
  822. {
  823. SECTION("empty")
  824. {
  825. json const j = json::array();
  826. std::vector<uint8_t> const expected = {0x90};
  827. const auto result = json::to_msgpack(j);
  828. CHECK(result == expected);
  829. // roundtrip
  830. CHECK(json::from_msgpack(result) == j);
  831. CHECK(json::from_msgpack(result, true, false) == j);
  832. }
  833. SECTION("[null]")
  834. {
  835. json const j = {nullptr};
  836. std::vector<uint8_t> const expected = {0x91, 0xc0};
  837. const auto result = json::to_msgpack(j);
  838. CHECK(result == expected);
  839. // roundtrip
  840. CHECK(json::from_msgpack(result) == j);
  841. CHECK(json::from_msgpack(result, true, false) == j);
  842. }
  843. SECTION("[1,2,3,4,5]")
  844. {
  845. json const j = json::parse("[1,2,3,4,5]");
  846. std::vector<uint8_t> const expected = {0x95, 0x01, 0x02, 0x03, 0x04, 0x05};
  847. const auto result = json::to_msgpack(j);
  848. CHECK(result == expected);
  849. // roundtrip
  850. CHECK(json::from_msgpack(result) == j);
  851. CHECK(json::from_msgpack(result, true, false) == j);
  852. }
  853. SECTION("[[[[]]]]")
  854. {
  855. json const j = json::parse("[[[[]]]]");
  856. std::vector<uint8_t> const expected = {0x91, 0x91, 0x91, 0x90};
  857. const auto result = json::to_msgpack(j);
  858. CHECK(result == expected);
  859. // roundtrip
  860. CHECK(json::from_msgpack(result) == j);
  861. CHECK(json::from_msgpack(result, true, false) == j);
  862. }
  863. SECTION("array 16")
  864. {
  865. json j(16, nullptr);
  866. std::vector<uint8_t> expected(j.size() + 3, 0xc0); // all null
  867. expected[0] = 0xdc; // array 16
  868. expected[1] = 0x00; // size (0x0010), byte 0
  869. expected[2] = 0x10; // size (0x0010), byte 1
  870. const auto result = json::to_msgpack(j);
  871. CHECK(result == expected);
  872. // roundtrip
  873. CHECK(json::from_msgpack(result) == j);
  874. CHECK(json::from_msgpack(result, true, false) == j);
  875. }
  876. SECTION("array 32")
  877. {
  878. json j(65536, nullptr);
  879. std::vector<uint8_t> expected(j.size() + 5, 0xc0); // all null
  880. expected[0] = 0xdd; // array 32
  881. expected[1] = 0x00; // size (0x00100000), byte 0
  882. expected[2] = 0x01; // size (0x00100000), byte 1
  883. expected[3] = 0x00; // size (0x00100000), byte 2
  884. expected[4] = 0x00; // size (0x00100000), byte 3
  885. const auto result = json::to_msgpack(j);
  886. //CHECK(result == expected);
  887. CHECK(result.size() == expected.size());
  888. for (size_t i = 0; i < expected.size(); ++i)
  889. {
  890. CAPTURE(i)
  891. CHECK(result[i] == expected[i]);
  892. }
  893. // roundtrip
  894. CHECK(json::from_msgpack(result) == j);
  895. CHECK(json::from_msgpack(result, true, false) == j);
  896. }
  897. }
  898. SECTION("object")
  899. {
  900. SECTION("empty")
  901. {
  902. json const j = json::object();
  903. std::vector<uint8_t> const expected = {0x80};
  904. const auto result = json::to_msgpack(j);
  905. CHECK(result == expected);
  906. // roundtrip
  907. CHECK(json::from_msgpack(result) == j);
  908. CHECK(json::from_msgpack(result, true, false) == j);
  909. }
  910. SECTION("{\"\":null}")
  911. {
  912. json const j = {{"", nullptr}};
  913. std::vector<uint8_t> const expected = {0x81, 0xa0, 0xc0};
  914. const auto result = json::to_msgpack(j);
  915. CHECK(result == expected);
  916. // roundtrip
  917. CHECK(json::from_msgpack(result) == j);
  918. CHECK(json::from_msgpack(result, true, false) == j);
  919. }
  920. SECTION("{\"a\": {\"b\": {\"c\": {}}}}")
  921. {
  922. json const j = json::parse(R"({"a": {"b": {"c": {}}}})");
  923. std::vector<uint8_t> const expected =
  924. {
  925. 0x81, 0xa1, 0x61, 0x81, 0xa1, 0x62, 0x81, 0xa1, 0x63, 0x80
  926. };
  927. const auto result = json::to_msgpack(j);
  928. CHECK(result == expected);
  929. // roundtrip
  930. CHECK(json::from_msgpack(result) == j);
  931. CHECK(json::from_msgpack(result, true, false) == j);
  932. }
  933. SECTION("map 16")
  934. {
  935. json const j = R"({"00": null, "01": null, "02": null, "03": null,
  936. "04": null, "05": null, "06": null, "07": null,
  937. "08": null, "09": null, "10": null, "11": null,
  938. "12": null, "13": null, "14": null, "15": null})"_json;
  939. const auto result = json::to_msgpack(j);
  940. // Checking against an expected vector byte by byte is
  941. // difficult, because no assumption on the order of key/value
  942. // pairs are made. We therefore only check the prefix (type and
  943. // size and the overall size. The rest is then handled in the
  944. // roundtrip check.
  945. CHECK(result.size() == 67); // 1 type, 2 size, 16*4 content
  946. CHECK(result[0] == 0xde); // map 16
  947. CHECK(result[1] == 0x00); // byte 0 of size (0x0010)
  948. CHECK(result[2] == 0x10); // byte 1 of size (0x0010)
  949. // roundtrip
  950. CHECK(json::from_msgpack(result) == j);
  951. CHECK(json::from_msgpack(result, true, false) == j);
  952. }
  953. SECTION("map 32")
  954. {
  955. json j;
  956. for (auto i = 0; i < 65536; ++i)
  957. {
  958. // format i to a fixed width of 5
  959. // each entry will need 7 bytes: 6 for fixstr, 1 for null
  960. std::stringstream ss;
  961. ss << std::setw(5) << std::setfill('0') << i;
  962. j.emplace(ss.str(), nullptr);
  963. }
  964. const auto result = json::to_msgpack(j);
  965. // Checking against an expected vector byte by byte is
  966. // difficult, because no assumption on the order of key/value
  967. // pairs are made. We therefore only check the prefix (type and
  968. // size and the overall size. The rest is then handled in the
  969. // roundtrip check.
  970. CHECK(result.size() == 458757); // 1 type, 4 size, 65536*7 content
  971. CHECK(result[0] == 0xdf); // map 32
  972. CHECK(result[1] == 0x00); // byte 0 of size (0x00010000)
  973. CHECK(result[2] == 0x01); // byte 1 of size (0x00010000)
  974. CHECK(result[3] == 0x00); // byte 2 of size (0x00010000)
  975. CHECK(result[4] == 0x00); // byte 3 of size (0x00010000)
  976. // roundtrip
  977. CHECK(json::from_msgpack(result) == j);
  978. CHECK(json::from_msgpack(result, true, false) == j);
  979. }
  980. }
  981. SECTION("extension")
  982. {
  983. SECTION("N = 0..255")
  984. {
  985. for (size_t N = 0; N <= 0xFF; ++N)
  986. {
  987. CAPTURE(N)
  988. // create JSON value with byte array containing of N * 'x'
  989. const auto s = std::vector<uint8_t>(N, 'x');
  990. json j = json::binary(s);
  991. std::uint8_t const subtype = 42;
  992. j.get_binary().set_subtype(subtype);
  993. // create expected byte vector
  994. std::vector<uint8_t> expected;
  995. switch (N)
  996. {
  997. case 1:
  998. expected.push_back(static_cast<std::uint8_t>(0xD4));
  999. break;
  1000. case 2:
  1001. expected.push_back(static_cast<std::uint8_t>(0xD5));
  1002. break;
  1003. case 4:
  1004. expected.push_back(static_cast<std::uint8_t>(0xD6));
  1005. break;
  1006. case 8:
  1007. expected.push_back(static_cast<std::uint8_t>(0xD7));
  1008. break;
  1009. case 16:
  1010. expected.push_back(static_cast<std::uint8_t>(0xD8));
  1011. break;
  1012. default:
  1013. expected.push_back(static_cast<std::uint8_t>(0xC7));
  1014. expected.push_back(static_cast<std::uint8_t>(N));
  1015. break;
  1016. }
  1017. expected.push_back(subtype);
  1018. for (size_t i = 0; i < N; ++i)
  1019. {
  1020. expected.push_back(0x78);
  1021. }
  1022. // compare result + size
  1023. const auto result = json::to_msgpack(j);
  1024. CHECK(result == expected);
  1025. switch (N)
  1026. {
  1027. case 1:
  1028. case 2:
  1029. case 4:
  1030. case 8:
  1031. case 16:
  1032. CHECK(result.size() == N + 2);
  1033. break;
  1034. default:
  1035. CHECK(result.size() == N + 3);
  1036. break;
  1037. }
  1038. // check that no null byte is appended
  1039. if (N > 0)
  1040. {
  1041. CHECK(result.back() != '\x00');
  1042. }
  1043. // roundtrip
  1044. CHECK(json::from_msgpack(result) == j);
  1045. CHECK(json::from_msgpack(result, true, false) == j);
  1046. }
  1047. }
  1048. SECTION("N = 256..65535")
  1049. {
  1050. for (std::size_t N :
  1051. {
  1052. 256u, 999u, 1025u, 3333u, 2048u, 65535u
  1053. })
  1054. {
  1055. CAPTURE(N)
  1056. // create JSON value with string containing of N * 'x'
  1057. const auto s = std::vector<uint8_t>(N, 'x');
  1058. json j = json::binary(s);
  1059. std::uint8_t const subtype = 42;
  1060. j.get_binary().set_subtype(subtype);
  1061. // create expected byte vector (hack: create string first)
  1062. std::vector<uint8_t> expected(N, 'x');
  1063. // reverse order of commands, because we insert at begin()
  1064. expected.insert(expected.begin(), subtype);
  1065. expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
  1066. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
  1067. expected.insert(expected.begin(), 0xC8);
  1068. // compare result + size
  1069. const auto result = json::to_msgpack(j);
  1070. CHECK(result == expected);
  1071. CHECK(result.size() == N + 4);
  1072. // check that no null byte is appended
  1073. CHECK(result.back() != '\x00');
  1074. // roundtrip
  1075. CHECK(json::from_msgpack(result) == j);
  1076. CHECK(json::from_msgpack(result, true, false) == j);
  1077. }
  1078. }
  1079. SECTION("N = 65536..4294967295")
  1080. {
  1081. for (std::size_t N :
  1082. {
  1083. 65536u, 77777u, 1048576u
  1084. })
  1085. {
  1086. CAPTURE(N)
  1087. // create JSON value with string containing of N * 'x'
  1088. const auto s = std::vector<uint8_t>(N, 'x');
  1089. json j = json::binary(s);
  1090. std::uint8_t const subtype = 42;
  1091. j.get_binary().set_subtype(subtype);
  1092. // create expected byte vector (hack: create string first)
  1093. std::vector<uint8_t> expected(N, 'x');
  1094. // reverse order of commands, because we insert at begin()
  1095. expected.insert(expected.begin(), subtype);
  1096. expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
  1097. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
  1098. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 16) & 0xff));
  1099. expected.insert(expected.begin(), static_cast<uint8_t>((N >> 24) & 0xff));
  1100. expected.insert(expected.begin(), 0xC9);
  1101. // compare result + size
  1102. const auto result = json::to_msgpack(j);
  1103. CHECK(result == expected);
  1104. CHECK(result.size() == N + 6);
  1105. // check that no null byte is appended
  1106. CHECK(result.back() != '\x00');
  1107. // roundtrip
  1108. CHECK(json::from_msgpack(result) == j);
  1109. CHECK(json::from_msgpack(result, true, false) == j);
  1110. }
  1111. }
  1112. }
  1113. SECTION("binary")
  1114. {
  1115. SECTION("N = 0..255")
  1116. {
  1117. for (std::size_t N = 0; N <= 0xFF; ++N)
  1118. {
  1119. CAPTURE(N)
  1120. // create JSON value with byte array containing of N * 'x'
  1121. const auto s = std::vector<uint8_t>(N, 'x');
  1122. json const j = json::binary(s);
  1123. // create expected byte vector
  1124. std::vector<std::uint8_t> expected;
  1125. expected.push_back(static_cast<std::uint8_t>(0xC4));
  1126. expected.push_back(static_cast<std::uint8_t>(N));
  1127. for (size_t i = 0; i < N; ++i)
  1128. {
  1129. expected.push_back(0x78);
  1130. }
  1131. // compare result + size
  1132. const auto result = json::to_msgpack(j);
  1133. CHECK(result == expected);
  1134. CHECK(result.size() == N + 2);
  1135. // check that no null byte is appended
  1136. if (N > 0)
  1137. {
  1138. CHECK(result.back() != '\x00');
  1139. }
  1140. // roundtrip
  1141. CHECK(json::from_msgpack(result) == j);
  1142. CHECK(json::from_msgpack(result, true, false) == j);
  1143. }
  1144. }
  1145. SECTION("N = 256..65535")
  1146. {
  1147. for (std::size_t N :
  1148. {
  1149. 256u, 999u, 1025u, 3333u, 2048u, 65535u
  1150. })
  1151. {
  1152. CAPTURE(N)
  1153. // create JSON value with string containing of N * 'x'
  1154. const auto s = std::vector<std::uint8_t>(N, 'x');
  1155. json const j = json::binary(s);
  1156. // create expected byte vector (hack: create string first)
  1157. std::vector<std::uint8_t> expected(N, 'x');
  1158. // reverse order of commands, because we insert at begin()
  1159. expected.insert(expected.begin(), static_cast<std::uint8_t>(N & 0xff));
  1160. expected.insert(expected.begin(), static_cast<std::uint8_t>((N >> 8) & 0xff));
  1161. expected.insert(expected.begin(), 0xC5);
  1162. // compare result + size
  1163. const auto result = json::to_msgpack(j);
  1164. CHECK(result == expected);
  1165. CHECK(result.size() == N + 3);
  1166. // check that no null byte is appended
  1167. CHECK(result.back() != '\x00');
  1168. // roundtrip
  1169. CHECK(json::from_msgpack(result) == j);
  1170. CHECK(json::from_msgpack(result, true, false) == j);
  1171. }
  1172. }
  1173. SECTION("N = 65536..4294967295")
  1174. {
  1175. for (std::size_t N :
  1176. {
  1177. 65536u, 77777u, 1048576u
  1178. })
  1179. {
  1180. CAPTURE(N)
  1181. // create JSON value with string containing of N * 'x'
  1182. const auto s = std::vector<std::uint8_t>(N, 'x');
  1183. json const j = json::binary(s);
  1184. // create expected byte vector (hack: create string first)
  1185. std::vector<uint8_t> expected(N, 'x');
  1186. // reverse order of commands, because we insert at begin()
  1187. expected.insert(expected.begin(), static_cast<std::uint8_t>(N & 0xff));
  1188. expected.insert(expected.begin(), static_cast<std::uint8_t>((N >> 8) & 0xff));
  1189. expected.insert(expected.begin(), static_cast<std::uint8_t>((N >> 16) & 0xff));
  1190. expected.insert(expected.begin(), static_cast<std::uint8_t>((N >> 24) & 0xff));
  1191. expected.insert(expected.begin(), 0xC6);
  1192. // compare result + size
  1193. const auto result = json::to_msgpack(j);
  1194. CHECK(result == expected);
  1195. CHECK(result.size() == N + 5);
  1196. // check that no null byte is appended
  1197. CHECK(result.back() != '\x00');
  1198. // roundtrip
  1199. CHECK(json::from_msgpack(result) == j);
  1200. CHECK(json::from_msgpack(result, true, false) == j);
  1201. }
  1202. }
  1203. }
  1204. }
  1205. SECTION("from float32")
  1206. {
  1207. auto given = std::vector<uint8_t>({0xca, 0x41, 0xc8, 0x00, 0x01});
  1208. json const j = json::from_msgpack(given);
  1209. CHECK(j.get<double>() == Approx(25.0000019073486));
  1210. }
  1211. SECTION("errors")
  1212. {
  1213. SECTION("empty byte vector")
  1214. {
  1215. json _;
  1216. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>()), "[json.exception.parse_error.110] parse error at byte 1: syntax error while parsing MessagePack value: unexpected end of input", json::parse_error&);
  1217. CHECK(json::from_msgpack(std::vector<uint8_t>(), true, false).is_discarded());
  1218. }
  1219. SECTION("too short byte vector")
  1220. {
  1221. json _;
  1222. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0x87})),
  1223. "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing MessagePack string: unexpected end of input", json::parse_error&);
  1224. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcc})),
  1225. "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
  1226. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcd})),
  1227. "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
  1228. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcd, 0x00})),
  1229. "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
  1230. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xce})),
  1231. "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
  1232. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xce, 0x00})),
  1233. "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
  1234. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xce, 0x00, 0x00})),
  1235. "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
  1236. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xce, 0x00, 0x00, 0x00})),
  1237. "[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
  1238. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcf})),
  1239. "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
  1240. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00})),
  1241. "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
  1242. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00})),
  1243. "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
  1244. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00})),
  1245. "[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
  1246. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00})),
  1247. "[json.exception.parse_error.110] parse error at byte 6: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
  1248. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00, 0x00})),
  1249. "[json.exception.parse_error.110] parse error at byte 7: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
  1250. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})),
  1251. "[json.exception.parse_error.110] parse error at byte 8: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
  1252. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})),
  1253. "[json.exception.parse_error.110] parse error at byte 9: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
  1254. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xa5, 0x68, 0x65})),
  1255. "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing MessagePack string: unexpected end of input", json::parse_error&);
  1256. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0x92, 0x01})),
  1257. "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing MessagePack value: unexpected end of input", json::parse_error&);
  1258. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0x81, 0xa1, 0x61})),
  1259. "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing MessagePack value: unexpected end of input", json::parse_error&);
  1260. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xc4, 0x02})),
  1261. "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing MessagePack binary: unexpected end of input", json::parse_error&);
  1262. CHECK(json::from_msgpack(std::vector<uint8_t>({0x87}), true, false).is_discarded());
  1263. CHECK(json::from_msgpack(std::vector<uint8_t>({0xcc}), true, false).is_discarded());
  1264. CHECK(json::from_msgpack(std::vector<uint8_t>({0xcd}), true, false).is_discarded());
  1265. CHECK(json::from_msgpack(std::vector<uint8_t>({0xcd, 0x00}), true, false).is_discarded());
  1266. CHECK(json::from_msgpack(std::vector<uint8_t>({0xce}), true, false).is_discarded());
  1267. CHECK(json::from_msgpack(std::vector<uint8_t>({0xce, 0x00}), true, false).is_discarded());
  1268. CHECK(json::from_msgpack(std::vector<uint8_t>({0xce, 0x00, 0x00}), true, false).is_discarded());
  1269. CHECK(json::from_msgpack(std::vector<uint8_t>({0xce, 0x00, 0x00, 0x00}), true, false).is_discarded());
  1270. CHECK(json::from_msgpack(std::vector<uint8_t>({0xcf}), true, false).is_discarded());
  1271. CHECK(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00}), true, false).is_discarded());
  1272. CHECK(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00}), true, false).is_discarded());
  1273. CHECK(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00}), true, false).is_discarded());
  1274. CHECK(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00}), true, false).is_discarded());
  1275. CHECK(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00, 0x00}), true, false).is_discarded());
  1276. CHECK(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), true, false).is_discarded());
  1277. CHECK(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), true, false).is_discarded());
  1278. CHECK(json::from_msgpack(std::vector<uint8_t>({0xa5, 0x68, 0x65}), true, false).is_discarded());
  1279. CHECK(json::from_msgpack(std::vector<uint8_t>({0x92, 0x01}), true, false).is_discarded());
  1280. CHECK(json::from_msgpack(std::vector<uint8_t>({0x81, 0xA1, 0x61}), true, false).is_discarded());
  1281. CHECK(json::from_msgpack(std::vector<uint8_t>({0xc4, 0x02}), true, false).is_discarded());
  1282. CHECK(json::from_msgpack(std::vector<uint8_t>({0xc4}), true, false).is_discarded());
  1283. }
  1284. SECTION("unsupported bytes")
  1285. {
  1286. SECTION("concrete examples")
  1287. {
  1288. json _;
  1289. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xc1})), "[json.exception.parse_error.112] parse error at byte 1: syntax error while parsing MessagePack value: invalid byte: 0xC1", json::parse_error&);
  1290. }
  1291. SECTION("all unsupported bytes")
  1292. {
  1293. for (auto byte :
  1294. {
  1295. // never used
  1296. 0xc1
  1297. })
  1298. {
  1299. json _;
  1300. CHECK_THROWS_AS(_ = json::from_msgpack(std::vector<uint8_t>({static_cast<uint8_t>(byte)})), json::parse_error&);
  1301. CHECK(json::from_msgpack(std::vector<uint8_t>({static_cast<uint8_t>(byte)}), true, false).is_discarded());
  1302. }
  1303. }
  1304. }
  1305. SECTION("invalid string in map")
  1306. {
  1307. json _;
  1308. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::vector<uint8_t>({0x81, 0xff, 0x01})), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing MessagePack string: expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0xFF", json::parse_error&);
  1309. CHECK(json::from_msgpack(std::vector<uint8_t>({0x81, 0xff, 0x01}), true, false).is_discarded());
  1310. }
  1311. SECTION("strict mode")
  1312. {
  1313. std::vector<uint8_t> const vec = {0xc0, 0xc0};
  1314. SECTION("non-strict mode")
  1315. {
  1316. const auto result = json::from_msgpack(vec, false);
  1317. CHECK(result == json());
  1318. }
  1319. SECTION("strict mode")
  1320. {
  1321. json _;
  1322. CHECK_THROWS_WITH_AS(_ = json::from_msgpack(vec), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing MessagePack value: expected end of input; last byte: 0xC0", json::parse_error&);
  1323. CHECK(json::from_msgpack(vec, true, false).is_discarded());
  1324. }
  1325. }
  1326. }
  1327. SECTION("SAX aborts")
  1328. {
  1329. SECTION("start_array(len)")
  1330. {
  1331. std::vector<uint8_t> const v = {0x93, 0x01, 0x02, 0x03};
  1332. SaxCountdown scp(0);
  1333. CHECK(!json::sax_parse(v, &scp, json::input_format_t::msgpack));
  1334. }
  1335. SECTION("start_object(len)")
  1336. {
  1337. std::vector<uint8_t> const v = {0x81, 0xa3, 0x66, 0x6F, 0x6F, 0xc2};
  1338. SaxCountdown scp(0);
  1339. CHECK(!json::sax_parse(v, &scp, json::input_format_t::msgpack));
  1340. }
  1341. SECTION("key()")
  1342. {
  1343. std::vector<uint8_t> const v = {0x81, 0xa3, 0x66, 0x6F, 0x6F, 0xc2};
  1344. SaxCountdown scp(1);
  1345. CHECK(!json::sax_parse(v, &scp, json::input_format_t::msgpack));
  1346. }
  1347. }
  1348. }
  1349. // use this testcase outside [hide] to run it with Valgrind
  1350. TEST_CASE("single MessagePack roundtrip")
  1351. {
  1352. SECTION("sample.json")
  1353. {
  1354. std::string const filename = TEST_DATA_DIRECTORY "/json_testsuite/sample.json";
  1355. // parse JSON file
  1356. std::ifstream f_json(filename);
  1357. json j1 = json::parse(f_json);
  1358. // parse MessagePack file
  1359. auto packed = utils::read_binary_file(filename + ".msgpack");
  1360. json j2;
  1361. CHECK_NOTHROW(j2 = json::from_msgpack(packed));
  1362. // compare parsed JSON values
  1363. CHECK(j1 == j2);
  1364. SECTION("roundtrips")
  1365. {
  1366. SECTION("std::ostringstream")
  1367. {
  1368. std::basic_ostringstream<std::uint8_t> ss;
  1369. json::to_msgpack(j1, ss);
  1370. json j3 = json::from_msgpack(ss.str());
  1371. CHECK(j1 == j3);
  1372. }
  1373. SECTION("std::string")
  1374. {
  1375. std::string s;
  1376. json::to_msgpack(j1, s);
  1377. json j3 = json::from_msgpack(s);
  1378. CHECK(j1 == j3);
  1379. }
  1380. }
  1381. // check with different start index
  1382. packed.insert(packed.begin(), 5, 0xff);
  1383. CHECK(j1 == json::from_msgpack(packed.begin() + 5, packed.end()));
  1384. }
  1385. }
  1386. TEST_CASE("MessagePack roundtrips" * doctest::skip())
  1387. {
  1388. SECTION("input from msgpack-python")
  1389. {
  1390. // most of these are excluded due to differences in key order (not a real problem)
  1391. std::set<std::string> exclude_packed;
  1392. exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/1.json");
  1393. exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/2.json");
  1394. exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/3.json");
  1395. exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/4.json");
  1396. exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/5.json");
  1397. exclude_packed.insert(TEST_DATA_DIRECTORY "/json_testsuite/sample.json"); // kills AppVeyor
  1398. exclude_packed.insert(TEST_DATA_DIRECTORY "/json_tests/pass1.json");
  1399. exclude_packed.insert(TEST_DATA_DIRECTORY "/regression/working_file.json");
  1400. exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object.json");
  1401. exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_basic.json");
  1402. exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_duplicated_key.json");
  1403. exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_long_strings.json");
  1404. exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_simple.json");
  1405. exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_string_unicode.json");
  1406. for (std::string filename :
  1407. {
  1408. TEST_DATA_DIRECTORY "/json_nlohmann_tests/all_unicode.json",
  1409. TEST_DATA_DIRECTORY "/json.org/1.json",
  1410. TEST_DATA_DIRECTORY "/json.org/2.json",
  1411. TEST_DATA_DIRECTORY "/json.org/3.json",
  1412. TEST_DATA_DIRECTORY "/json.org/4.json",
  1413. TEST_DATA_DIRECTORY "/json.org/5.json",
  1414. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip01.json",
  1415. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip02.json",
  1416. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip03.json",
  1417. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip04.json",
  1418. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip05.json",
  1419. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip06.json",
  1420. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip07.json",
  1421. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip08.json",
  1422. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip09.json",
  1423. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip10.json",
  1424. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip11.json",
  1425. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip12.json",
  1426. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip13.json",
  1427. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip14.json",
  1428. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip15.json",
  1429. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip16.json",
  1430. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip17.json",
  1431. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip18.json",
  1432. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip19.json",
  1433. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip20.json",
  1434. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip21.json",
  1435. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip22.json",
  1436. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip23.json",
  1437. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip24.json",
  1438. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip25.json",
  1439. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip26.json",
  1440. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip27.json",
  1441. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip28.json",
  1442. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip29.json",
  1443. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip30.json",
  1444. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip31.json",
  1445. TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip32.json",
  1446. TEST_DATA_DIRECTORY "/json_testsuite/sample.json", // kills AppVeyor
  1447. TEST_DATA_DIRECTORY "/json_tests/pass1.json",
  1448. TEST_DATA_DIRECTORY "/json_tests/pass2.json",
  1449. TEST_DATA_DIRECTORY "/json_tests/pass3.json",
  1450. TEST_DATA_DIRECTORY "/regression/floats.json",
  1451. TEST_DATA_DIRECTORY "/regression/signed_ints.json",
  1452. TEST_DATA_DIRECTORY "/regression/unsigned_ints.json",
  1453. TEST_DATA_DIRECTORY "/regression/working_file.json",
  1454. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_arraysWithSpaces.json",
  1455. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_empty-string.json",
  1456. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_empty.json",
  1457. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_ending_with_newline.json",
  1458. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_false.json",
  1459. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_heterogeneous.json",
  1460. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_null.json",
  1461. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_with_1_and_newline.json",
  1462. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_with_leading_space.json",
  1463. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_with_several_null.json",
  1464. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_with_trailing_space.json",
  1465. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number.json",
  1466. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_0e+1.json",
  1467. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_0e1.json",
  1468. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_after_space.json",
  1469. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_double_close_to_zero.json",
  1470. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_double_huge_neg_exp.json",
  1471. //TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_huge_exp.json",
  1472. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_int_with_exp.json",
  1473. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_minus_zero.json",
  1474. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_negative_int.json",
  1475. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_negative_one.json",
  1476. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_negative_zero.json",
  1477. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_capital_e.json",
  1478. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_capital_e_neg_exp.json",
  1479. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_capital_e_pos_exp.json",
  1480. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_exponent.json",
  1481. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_fraction_exponent.json",
  1482. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_neg_exp.json",
  1483. //TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_neg_overflow.json",
  1484. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_pos_exponent.json",
  1485. //TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_pos_overflow.json",
  1486. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_real_underflow.json",
  1487. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_simple_int.json",
  1488. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_simple_real.json",
  1489. //TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_too_big_neg_int.json",
  1490. //TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_too_big_pos_int.json",
  1491. //TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_number_very_big_negative_int.json",
  1492. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object.json",
  1493. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_basic.json",
  1494. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_duplicated_key.json",
  1495. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_duplicated_key_and_value.json",
  1496. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_empty.json",
  1497. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_empty_key.json",
  1498. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_escaped_null_in_key.json",
  1499. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_extreme_numbers.json",
  1500. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_long_strings.json",
  1501. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_simple.json",
  1502. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_string_unicode.json",
  1503. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_with_newlines.json",
  1504. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_1_2_3_bytes_UTF-8_sequences.json",
  1505. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_UTF-16_Surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF.json",
  1506. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_accepted_surrogate_pair.json",
  1507. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_accepted_surrogate_pairs.json",
  1508. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_allowed_escapes.json",
  1509. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_backslash_and_u_escaped_zero.json",
  1510. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_backslash_doublequotes.json",
  1511. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_comments.json",
  1512. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_double_escape_a.json",
  1513. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_double_escape_n.json",
  1514. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_escaped_control_character.json",
  1515. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_escaped_noncharacter.json",
  1516. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_in_array.json",
  1517. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_in_array_with_leading_space.json",
  1518. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_last_surrogates_1_and_2.json",
  1519. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_newline_uescaped.json",
  1520. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_nonCharacterInUTF-8_U+10FFFF.json",
  1521. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_nonCharacterInUTF-8_U+1FFFF.json",
  1522. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_nonCharacterInUTF-8_U+FFFF.json",
  1523. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_null_escape.json",
  1524. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_one-byte-utf-8.json",
  1525. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_pi.json",
  1526. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_simple_ascii.json",
  1527. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_space.json",
  1528. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_three-byte-utf-8.json",
  1529. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_two-byte-utf-8.json",
  1530. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_u+2028_line_sep.json",
  1531. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_u+2029_par_sep.json",
  1532. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_uEscape.json",
  1533. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unescaped_char_delete.json",
  1534. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unicode.json",
  1535. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unicodeEscapedBackslash.json",
  1536. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unicode_2.json",
  1537. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unicode_U+200B_ZERO_WIDTH_SPACE.json",
  1538. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unicode_U+2064_invisible_plus.json",
  1539. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_unicode_escaped_double_quote.json",
  1540. // TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_utf16.json",
  1541. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_utf8.json",
  1542. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_string_with_del_character.json",
  1543. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_lonely_false.json",
  1544. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_lonely_int.json",
  1545. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_lonely_negative_real.json",
  1546. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_lonely_null.json",
  1547. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_lonely_string.json",
  1548. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_lonely_true.json",
  1549. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_string_empty.json",
  1550. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_trailing_newline.json",
  1551. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_true_in_array.json",
  1552. TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_structure_whitespace_array.json"
  1553. })
  1554. {
  1555. CAPTURE(filename)
  1556. {
  1557. INFO_WITH_TEMP(filename + ": std::vector<uint8_t>");
  1558. // parse JSON file
  1559. std::ifstream f_json(filename);
  1560. json j1 = json::parse(f_json);
  1561. // parse MessagePack file
  1562. auto packed = utils::read_binary_file(filename + ".msgpack");
  1563. json j2;
  1564. CHECK_NOTHROW(j2 = json::from_msgpack(packed));
  1565. // compare parsed JSON values
  1566. CHECK(j1 == j2);
  1567. }
  1568. {
  1569. INFO_WITH_TEMP(filename + ": std::ifstream");
  1570. // parse JSON file
  1571. std::ifstream f_json(filename);
  1572. json j1 = json::parse(f_json);
  1573. // parse MessagePack file
  1574. std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary);
  1575. json j2;
  1576. CHECK_NOTHROW(j2 = json::from_msgpack(f_msgpack));
  1577. // compare parsed JSON values
  1578. CHECK(j1 == j2);
  1579. }
  1580. {
  1581. INFO_WITH_TEMP(filename + ": uint8_t* and size");
  1582. // parse JSON file
  1583. std::ifstream f_json(filename);
  1584. json j1 = json::parse(f_json);
  1585. // parse MessagePack file
  1586. auto packed = utils::read_binary_file(filename + ".msgpack");
  1587. json j2;
  1588. CHECK_NOTHROW(j2 = json::from_msgpack({packed.data(), packed.size()}));
  1589. // compare parsed JSON values
  1590. CHECK(j1 == j2);
  1591. }
  1592. {
  1593. INFO_WITH_TEMP(filename + ": output to output adapters");
  1594. // parse JSON file
  1595. std::ifstream f_json(filename);
  1596. json const j1 = json::parse(f_json);
  1597. // parse MessagePack file
  1598. auto packed = utils::read_binary_file(filename + ".msgpack");
  1599. if (exclude_packed.count(filename) == 0u)
  1600. {
  1601. {
  1602. INFO_WITH_TEMP(filename + ": output adapters: std::vector<uint8_t>");
  1603. std::vector<uint8_t> vec;
  1604. json::to_msgpack(j1, vec);
  1605. CHECK(vec == packed);
  1606. }
  1607. }
  1608. }
  1609. }
  1610. }
  1611. }