unit-msgpack.cpp 87 KB

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