unit-modifiers.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. TEST_CASE("modifiers")
  29. {
  30. SECTION("clear()")
  31. {
  32. SECTION("boolean")
  33. {
  34. json j = true;
  35. json k = j;
  36. j.clear();
  37. CHECK(j == json(json::value_t::boolean));
  38. CHECK(j == json(k.type()));
  39. }
  40. SECTION("string")
  41. {
  42. json j = "hello world";
  43. json k = j;
  44. j.clear();
  45. CHECK(j == json(json::value_t::string));
  46. CHECK(j == json(k.type()));
  47. }
  48. SECTION("array")
  49. {
  50. SECTION("empty array")
  51. {
  52. json j = json::array();
  53. json k = j;
  54. j.clear();
  55. CHECK(j.empty());
  56. CHECK(j == json(json::value_t::array));
  57. CHECK(j == json(k.type()));
  58. }
  59. SECTION("filled array")
  60. {
  61. json j = {1, 2, 3};
  62. json k = j;
  63. j.clear();
  64. CHECK(j.empty());
  65. CHECK(j == json(json::value_t::array));
  66. CHECK(j == json(k.type()));
  67. }
  68. }
  69. SECTION("object")
  70. {
  71. SECTION("empty object")
  72. {
  73. json j = json::object();
  74. json k = j;
  75. j.clear();
  76. CHECK(j.empty());
  77. CHECK(j == json(json::value_t::object));
  78. CHECK(j == json(k.type()));
  79. }
  80. SECTION("filled object")
  81. {
  82. json j = {{"one", 1}, {"two", 2}, {"three", 3}};
  83. json k = j;
  84. j.clear();
  85. CHECK(j.empty());
  86. CHECK(j == json(json::value_t::object));
  87. CHECK(j == json(k.type()));
  88. }
  89. }
  90. SECTION("binary")
  91. {
  92. SECTION("empty binary")
  93. {
  94. json j = json::binary({});
  95. json k = j;
  96. j.clear();
  97. CHECK(not j.empty());
  98. CHECK(j == json(json::value_t::binary));
  99. CHECK(j == json(k.type()));
  100. }
  101. SECTION("filled binary")
  102. {
  103. json j = json::binary({1, 2, 3, 4, 5});
  104. json k = j;
  105. j.clear();
  106. CHECK(not j.empty());
  107. CHECK(j == json(json::value_t::binary));
  108. CHECK(j == json(k.type()));
  109. }
  110. }
  111. SECTION("number (integer)")
  112. {
  113. json j = 23;
  114. json k = j;
  115. j.clear();
  116. CHECK(j == json(json::value_t::number_integer));
  117. CHECK(j == json(k.type()));
  118. }
  119. SECTION("number (unsigned)")
  120. {
  121. json j = 23u;
  122. json k = j;
  123. j.clear();
  124. CHECK(j == json(json::value_t::number_integer));
  125. CHECK(j == json(k.type()));
  126. }
  127. SECTION("number (float)")
  128. {
  129. json j = 23.42;
  130. json k = j;
  131. j.clear();
  132. CHECK(j == json(json::value_t::number_float));
  133. CHECK(j == json(k.type()));
  134. }
  135. SECTION("null")
  136. {
  137. json j = nullptr;
  138. json k = j;
  139. j.clear();
  140. CHECK(j == json(json::value_t::null));
  141. CHECK(j == json(k.type()));
  142. }
  143. }
  144. SECTION("push_back()")
  145. {
  146. SECTION("to array")
  147. {
  148. SECTION("json&&")
  149. {
  150. SECTION("null")
  151. {
  152. json j;
  153. j.push_back(1);
  154. j.push_back(2);
  155. CHECK(j.type() == json::value_t::array);
  156. CHECK(j == json({1, 2}));
  157. }
  158. SECTION("array")
  159. {
  160. json j = {1, 2, 3};
  161. j.push_back("Hello");
  162. CHECK(j.type() == json::value_t::array);
  163. CHECK(j == json({1, 2, 3, "Hello"}));
  164. }
  165. SECTION("other type")
  166. {
  167. json j = 1;
  168. CHECK_THROWS_AS(j.push_back("Hello"), json::type_error&);
  169. CHECK_THROWS_WITH(j.push_back("Hello"), "[json.exception.type_error.308] cannot use push_back() with number");
  170. }
  171. }
  172. SECTION("const json&")
  173. {
  174. SECTION("null")
  175. {
  176. json j;
  177. json k(1);
  178. j.push_back(k);
  179. j.push_back(k);
  180. CHECK(j.type() == json::value_t::array);
  181. CHECK(j == json({1, 1}));
  182. }
  183. SECTION("array")
  184. {
  185. json j = {1, 2, 3};
  186. json k("Hello");
  187. j.push_back(k);
  188. CHECK(j.type() == json::value_t::array);
  189. CHECK(j == json({1, 2, 3, "Hello"}));
  190. }
  191. SECTION("other type")
  192. {
  193. json j = 1;
  194. json k("Hello");
  195. CHECK_THROWS_AS(j.push_back(k), json::type_error&);
  196. CHECK_THROWS_WITH(j.push_back(k), "[json.exception.type_error.308] cannot use push_back() with number");
  197. }
  198. }
  199. }
  200. SECTION("to object")
  201. {
  202. SECTION("null")
  203. {
  204. json j;
  205. j.push_back(json::object_t::value_type({"one", 1}));
  206. j.push_back(json::object_t::value_type({"two", 2}));
  207. CHECK(j.type() == json::value_t::object);
  208. CHECK(j.size() == 2);
  209. CHECK(j["one"] == json(1));
  210. CHECK(j["two"] == json(2));
  211. }
  212. SECTION("object")
  213. {
  214. json j(json::value_t::object);
  215. j.push_back(json::object_t::value_type({"one", 1}));
  216. j.push_back(json::object_t::value_type({"two", 2}));
  217. CHECK(j.size() == 2);
  218. CHECK(j["one"] == json(1));
  219. CHECK(j["two"] == json(2));
  220. }
  221. SECTION("other type")
  222. {
  223. json j = 1;
  224. json k("Hello");
  225. CHECK_THROWS_AS(j.push_back(json::object_t::value_type({"one", 1})), json::type_error&);
  226. CHECK_THROWS_WITH(j.push_back(json::object_t::value_type({"one", 1})),
  227. "[json.exception.type_error.308] cannot use push_back() with number");
  228. }
  229. }
  230. SECTION("with initializer_list")
  231. {
  232. SECTION("null")
  233. {
  234. json j;
  235. j.push_back({"foo", "bar"});
  236. CHECK(j == json::array({{"foo", "bar"}}));
  237. json k;
  238. k.push_back({1, 2, 3});
  239. CHECK(k == json::array({{1, 2, 3}}));
  240. }
  241. SECTION("array")
  242. {
  243. json j = {1, 2, 3};
  244. j.push_back({"foo", "bar"});
  245. CHECK(j == json({1, 2, 3, {"foo", "bar"}}));
  246. json k = {1, 2, 3};
  247. k.push_back({1, 2, 3});
  248. CHECK(k == json({1, 2, 3, {1, 2, 3}}));
  249. }
  250. SECTION("object")
  251. {
  252. json j = {{"key1", 1}};
  253. j.push_back({"key2", "bar"});
  254. CHECK(j == json({{"key1", 1}, {"key2", "bar"}}));
  255. // invalid values (no string/val pair)
  256. CHECK_THROWS_AS(j.push_back({1}), json::type_error&);
  257. CHECK_THROWS_WITH(j.push_back({1}), "[json.exception.type_error.308] cannot use push_back() with object");
  258. CHECK_THROWS_AS(j.push_back({1, 2}), json::type_error&);
  259. CHECK_THROWS_WITH(j.push_back({1, 2}), "[json.exception.type_error.308] cannot use push_back() with object");
  260. CHECK_THROWS_AS(j.push_back({1, 2, 3, 4}), json::type_error&);
  261. CHECK_THROWS_WITH(j.push_back({1, 2, 3, 4}), "[json.exception.type_error.308] cannot use push_back() with object");
  262. }
  263. }
  264. }
  265. SECTION("emplace_back()")
  266. {
  267. SECTION("to array")
  268. {
  269. SECTION("null")
  270. {
  271. json j;
  272. auto& x1 = j.emplace_back(1);
  273. CHECK(x1 == 1);
  274. auto& x2 = j.emplace_back(2);
  275. CHECK(x2 == 2);
  276. CHECK(j.type() == json::value_t::array);
  277. CHECK(j == json({1, 2}));
  278. }
  279. SECTION("array")
  280. {
  281. json j = {1, 2, 3};
  282. auto& x = j.emplace_back("Hello");
  283. CHECK(x == "Hello");
  284. CHECK(j.type() == json::value_t::array);
  285. CHECK(j == json({1, 2, 3, "Hello"}));
  286. }
  287. SECTION("multiple values")
  288. {
  289. json j;
  290. auto& x = j.emplace_back(3, "foo");
  291. CHECK(x == json({"foo", "foo", "foo"}));
  292. CHECK(j.type() == json::value_t::array);
  293. CHECK(j == json({{"foo", "foo", "foo"}}));
  294. }
  295. }
  296. SECTION("other type")
  297. {
  298. json j = 1;
  299. CHECK_THROWS_AS(j.emplace_back("Hello"), json::type_error&);
  300. CHECK_THROWS_WITH(j.emplace_back("Hello"),
  301. "[json.exception.type_error.311] cannot use emplace_back() with number");
  302. }
  303. }
  304. SECTION("emplace()")
  305. {
  306. SECTION("to object")
  307. {
  308. SECTION("null")
  309. {
  310. // start with a null value
  311. json j;
  312. // add a new key
  313. auto res1 = j.emplace("foo", "bar");
  314. CHECK(res1.second == true);
  315. CHECK(*res1.first == "bar");
  316. // the null value is changed to an object
  317. CHECK(j.type() == json::value_t::object);
  318. // add a new key
  319. auto res2 = j.emplace("baz", "bam");
  320. CHECK(res2.second == true);
  321. CHECK(*res2.first == "bam");
  322. // we try to insert at given key - no change
  323. auto res3 = j.emplace("baz", "bad");
  324. CHECK(res3.second == false);
  325. CHECK(*res3.first == "bam");
  326. // the final object
  327. CHECK(j == json({{"baz", "bam"}, {"foo", "bar"}}));
  328. }
  329. SECTION("object")
  330. {
  331. // start with an object
  332. json j = {{"foo", "bar"}};
  333. // add a new key
  334. auto res1 = j.emplace("baz", "bam");
  335. CHECK(res1.second == true);
  336. CHECK(*res1.first == "bam");
  337. // add an existing key
  338. auto res2 = j.emplace("foo", "bad");
  339. CHECK(res2.second == false);
  340. CHECK(*res2.first == "bar");
  341. // check final object
  342. CHECK(j == json({{"baz", "bam"}, {"foo", "bar"}}));
  343. }
  344. }
  345. SECTION("other type")
  346. {
  347. json j = 1;
  348. CHECK_THROWS_AS(j.emplace("foo", "bar"), json::type_error&);
  349. CHECK_THROWS_WITH(j.emplace("foo", "bar"),
  350. "[json.exception.type_error.311] cannot use emplace() with number");
  351. }
  352. }
  353. SECTION("operator+=")
  354. {
  355. SECTION("to array")
  356. {
  357. SECTION("json&&")
  358. {
  359. SECTION("null")
  360. {
  361. json j;
  362. j += 1;
  363. j += 2;
  364. CHECK(j.type() == json::value_t::array);
  365. CHECK(j == json({1, 2}));
  366. }
  367. SECTION("array")
  368. {
  369. json j = {1, 2, 3};
  370. j += "Hello";
  371. CHECK(j.type() == json::value_t::array);
  372. CHECK(j == json({1, 2, 3, "Hello"}));
  373. }
  374. SECTION("other type")
  375. {
  376. json j = 1;
  377. CHECK_THROWS_AS(j += "Hello", json::type_error&);
  378. CHECK_THROWS_WITH(j += "Hello", "[json.exception.type_error.308] cannot use push_back() with number");
  379. }
  380. }
  381. SECTION("const json&")
  382. {
  383. SECTION("null")
  384. {
  385. json j;
  386. json k(1);
  387. j += k;
  388. j += k;
  389. CHECK(j.type() == json::value_t::array);
  390. CHECK(j == json({1, 1}));
  391. }
  392. SECTION("array")
  393. {
  394. json j = {1, 2, 3};
  395. json k("Hello");
  396. j += k;
  397. CHECK(j.type() == json::value_t::array);
  398. CHECK(j == json({1, 2, 3, "Hello"}));
  399. }
  400. SECTION("other type")
  401. {
  402. json j = 1;
  403. json k("Hello");
  404. CHECK_THROWS_AS(j += k, json::type_error&);
  405. CHECK_THROWS_WITH(j += k, "[json.exception.type_error.308] cannot use push_back() with number");
  406. }
  407. }
  408. }
  409. SECTION("to object")
  410. {
  411. SECTION("null")
  412. {
  413. json j;
  414. j += json::object_t::value_type({"one", 1});
  415. j += json::object_t::value_type({"two", 2});
  416. CHECK(j.type() == json::value_t::object);
  417. CHECK(j.size() == 2);
  418. CHECK(j["one"] == json(1));
  419. CHECK(j["two"] == json(2));
  420. }
  421. SECTION("object")
  422. {
  423. json j(json::value_t::object);
  424. j += json::object_t::value_type({"one", 1});
  425. j += json::object_t::value_type({"two", 2});
  426. CHECK(j.size() == 2);
  427. CHECK(j["one"] == json(1));
  428. CHECK(j["two"] == json(2));
  429. }
  430. SECTION("other type")
  431. {
  432. json j = 1;
  433. json k("Hello");
  434. CHECK_THROWS_AS(j += json::object_t::value_type({"one", 1}), json::type_error&);
  435. CHECK_THROWS_WITH(j += json::object_t::value_type({"one", 1}),
  436. "[json.exception.type_error.308] cannot use push_back() with number");
  437. }
  438. }
  439. SECTION("with initializer_list")
  440. {
  441. SECTION("null")
  442. {
  443. json j;
  444. j += {"foo", "bar"};
  445. CHECK(j == json::array({{"foo", "bar"}}));
  446. json k;
  447. k += {1, 2, 3};
  448. CHECK(k == json::array({{1, 2, 3}}));
  449. }
  450. SECTION("array")
  451. {
  452. json j = {1, 2, 3};
  453. j += {"foo", "bar"};
  454. CHECK(j == json({1, 2, 3, {"foo", "bar"}}));
  455. json k = {1, 2, 3};
  456. k += {1, 2, 3};
  457. CHECK(k == json({1, 2, 3, {1, 2, 3}}));
  458. }
  459. SECTION("object")
  460. {
  461. json j = {{"key1", 1}};
  462. j += {"key2", "bar"};
  463. CHECK(j == json({{"key1", 1}, {"key2", "bar"}}));
  464. json k = {{"key1", 1}};
  465. CHECK_THROWS_AS((k += {1, 2, 3, 4}), json::type_error&);
  466. CHECK_THROWS_WITH((k += {1, 2, 3, 4}), "[json.exception.type_error.308] cannot use push_back() with object");
  467. }
  468. }
  469. }
  470. SECTION("insert()")
  471. {
  472. json j_array = {1, 2, 3, 4};
  473. json j_value = 5;
  474. SECTION("value at position")
  475. {
  476. SECTION("insert before begin()")
  477. {
  478. auto it = j_array.insert(j_array.begin(), j_value);
  479. CHECK(j_array.size() == 5);
  480. CHECK(*it == j_value);
  481. CHECK(j_array.begin() == it);
  482. CHECK(j_array == json({5, 1, 2, 3, 4}));
  483. }
  484. SECTION("insert in the middle")
  485. {
  486. auto it = j_array.insert(j_array.begin() + 2, j_value);
  487. CHECK(j_array.size() == 5);
  488. CHECK(*it == j_value);
  489. CHECK((it - j_array.begin()) == 2);
  490. CHECK(j_array == json({1, 2, 5, 3, 4}));
  491. }
  492. SECTION("insert before end()")
  493. {
  494. auto it = j_array.insert(j_array.end(), j_value);
  495. CHECK(j_array.size() == 5);
  496. CHECK(*it == j_value);
  497. CHECK((j_array.end() - it) == 1);
  498. CHECK(j_array == json({1, 2, 3, 4, 5}));
  499. }
  500. }
  501. SECTION("rvalue at position")
  502. {
  503. SECTION("insert before begin()")
  504. {
  505. auto it = j_array.insert(j_array.begin(), 5);
  506. CHECK(j_array.size() == 5);
  507. CHECK(*it == j_value);
  508. CHECK(j_array.begin() == it);
  509. CHECK(j_array == json({5, 1, 2, 3, 4}));
  510. }
  511. SECTION("insert in the middle")
  512. {
  513. auto it = j_array.insert(j_array.begin() + 2, 5);
  514. CHECK(j_array.size() == 5);
  515. CHECK(*it == j_value);
  516. CHECK((it - j_array.begin()) == 2);
  517. CHECK(j_array == json({1, 2, 5, 3, 4}));
  518. }
  519. SECTION("insert before end()")
  520. {
  521. auto it = j_array.insert(j_array.end(), 5);
  522. CHECK(j_array.size() == 5);
  523. CHECK(*it == j_value);
  524. CHECK((j_array.end() - it) == 1);
  525. CHECK(j_array == json({1, 2, 3, 4, 5}));
  526. }
  527. }
  528. SECTION("copies at position")
  529. {
  530. SECTION("insert before begin()")
  531. {
  532. auto it = j_array.insert(j_array.begin(), 3, 5);
  533. CHECK(j_array.size() == 7);
  534. CHECK(*it == j_value);
  535. CHECK(j_array.begin() == it);
  536. CHECK(j_array == json({5, 5, 5, 1, 2, 3, 4}));
  537. }
  538. SECTION("insert in the middle")
  539. {
  540. auto it = j_array.insert(j_array.begin() + 2, 3, 5);
  541. CHECK(j_array.size() == 7);
  542. CHECK(*it == j_value);
  543. CHECK((it - j_array.begin()) == 2);
  544. CHECK(j_array == json({1, 2, 5, 5, 5, 3, 4}));
  545. }
  546. SECTION("insert before end()")
  547. {
  548. auto it = j_array.insert(j_array.end(), 3, 5);
  549. CHECK(j_array.size() == 7);
  550. CHECK(*it == j_value);
  551. CHECK((j_array.end() - it) == 3);
  552. CHECK(j_array == json({1, 2, 3, 4, 5, 5, 5}));
  553. }
  554. SECTION("insert nothing (count = 0)")
  555. {
  556. auto it = j_array.insert(j_array.end(), 0, 5);
  557. CHECK(j_array.size() == 4);
  558. // the returned iterator points to the first inserted element;
  559. // there were 4 elements, so it should point to the 5th
  560. CHECK(it == j_array.begin() + 4);
  561. CHECK(j_array == json({1, 2, 3, 4}));
  562. }
  563. }
  564. SECTION("range for array")
  565. {
  566. json j_other_array = {"first", "second"};
  567. SECTION("proper usage")
  568. {
  569. auto it = j_array.insert(j_array.end(), j_other_array.begin(), j_other_array.end());
  570. CHECK(j_array.size() == 6);
  571. CHECK(*it == *j_other_array.begin());
  572. CHECK((j_array.end() - it) == 2);
  573. CHECK(j_array == json({1, 2, 3, 4, "first", "second"}));
  574. }
  575. SECTION("empty range")
  576. {
  577. auto it = j_array.insert(j_array.end(), j_other_array.begin(), j_other_array.begin());
  578. CHECK(j_array.size() == 4);
  579. CHECK(it == j_array.end());
  580. CHECK(j_array == json({1, 2, 3, 4}));
  581. }
  582. SECTION("invalid iterators")
  583. {
  584. json j_other_array2 = {"first", "second"};
  585. CHECK_THROWS_AS(j_array.insert(j_array.end(), j_array.begin(), j_array.end()),
  586. json::invalid_iterator&);
  587. CHECK_THROWS_AS(j_array.insert(j_array.end(), j_other_array.begin(), j_other_array2.end()),
  588. json::invalid_iterator&);
  589. CHECK_THROWS_WITH(j_array.insert(j_array.end(), j_array.begin(), j_array.end()),
  590. "[json.exception.invalid_iterator.211] passed iterators may not belong to container");
  591. CHECK_THROWS_WITH(j_array.insert(j_array.end(), j_other_array.begin(), j_other_array2.end()),
  592. "[json.exception.invalid_iterator.210] iterators do not fit");
  593. }
  594. }
  595. SECTION("range for object")
  596. {
  597. json j_object1 = {{"one", "eins"}, {"two", "zwei"}};
  598. json j_object2 = {{"eleven", "elf"}, {"seventeen", "siebzehn"}};
  599. SECTION("proper usage")
  600. {
  601. j_object1.insert(j_object2.begin(), j_object2.end());
  602. CHECK(j_object1.size() == 4);
  603. }
  604. SECTION("empty range")
  605. {
  606. j_object1.insert(j_object2.begin(), j_object2.begin());
  607. CHECK(j_object1.size() == 2);
  608. }
  609. SECTION("invalid iterators")
  610. {
  611. json j_other_array2 = {"first", "second"};
  612. CHECK_THROWS_AS(j_array.insert(j_object2.begin(), j_object2.end()), json::type_error&);
  613. CHECK_THROWS_AS(j_object1.insert(j_object1.begin(), j_object2.end()), json::invalid_iterator&);
  614. CHECK_THROWS_AS(j_object1.insert(j_array.begin(), j_array.end()), json::invalid_iterator&);
  615. CHECK_THROWS_WITH(j_array.insert(j_object2.begin(), j_object2.end()),
  616. "[json.exception.type_error.309] cannot use insert() with array");
  617. CHECK_THROWS_WITH(j_object1.insert(j_object1.begin(), j_object2.end()),
  618. "[json.exception.invalid_iterator.210] iterators do not fit");
  619. CHECK_THROWS_WITH(j_object1.insert(j_array.begin(), j_array.end()),
  620. "[json.exception.invalid_iterator.202] iterators first and last must point to objects");
  621. }
  622. }
  623. SECTION("initializer list at position")
  624. {
  625. SECTION("insert before begin()")
  626. {
  627. auto it = j_array.insert(j_array.begin(), {7, 8, 9});
  628. CHECK(j_array.size() == 7);
  629. CHECK(*it == json(7));
  630. CHECK(j_array.begin() == it);
  631. CHECK(j_array == json({7, 8, 9, 1, 2, 3, 4}));
  632. }
  633. SECTION("insert in the middle")
  634. {
  635. auto it = j_array.insert(j_array.begin() + 2, {7, 8, 9});
  636. CHECK(j_array.size() == 7);
  637. CHECK(*it == json(7));
  638. CHECK((it - j_array.begin()) == 2);
  639. CHECK(j_array == json({1, 2, 7, 8, 9, 3, 4}));
  640. }
  641. SECTION("insert before end()")
  642. {
  643. auto it = j_array.insert(j_array.end(), {7, 8, 9});
  644. CHECK(j_array.size() == 7);
  645. CHECK(*it == json(7));
  646. CHECK((j_array.end() - it) == 3);
  647. CHECK(j_array == json({1, 2, 3, 4, 7, 8, 9}));
  648. }
  649. }
  650. SECTION("invalid iterator")
  651. {
  652. // pass iterator to a different array
  653. json j_another_array = {1, 2};
  654. json j_yet_another_array = {"first", "second"};
  655. CHECK_THROWS_AS(j_array.insert(j_another_array.end(), 10), json::invalid_iterator&);
  656. CHECK_THROWS_AS(j_array.insert(j_another_array.end(), j_value), json::invalid_iterator&);
  657. CHECK_THROWS_AS(j_array.insert(j_another_array.end(), 10, 11), json::invalid_iterator&);
  658. CHECK_THROWS_AS(j_array.insert(j_another_array.end(), j_yet_another_array.begin(), j_yet_another_array.end()), json::invalid_iterator&);
  659. CHECK_THROWS_AS(j_array.insert(j_another_array.end(), {1, 2, 3, 4}), json::invalid_iterator&);
  660. CHECK_THROWS_WITH(j_array.insert(j_another_array.end(), 10),
  661. "[json.exception.invalid_iterator.202] iterator does not fit current value");
  662. CHECK_THROWS_WITH(j_array.insert(j_another_array.end(), j_value),
  663. "[json.exception.invalid_iterator.202] iterator does not fit current value");
  664. CHECK_THROWS_WITH(j_array.insert(j_another_array.end(), 10, 11),
  665. "[json.exception.invalid_iterator.202] iterator does not fit current value");
  666. CHECK_THROWS_WITH(j_array.insert(j_another_array.end(), j_yet_another_array.begin(), j_yet_another_array.end()),
  667. "[json.exception.invalid_iterator.202] iterator does not fit current value");
  668. CHECK_THROWS_WITH(j_array.insert(j_another_array.end(), {1, 2, 3, 4}),
  669. "[json.exception.invalid_iterator.202] iterator does not fit current value");
  670. }
  671. SECTION("non-array type")
  672. {
  673. // call insert on a non-array type
  674. json j_nonarray = 3;
  675. json j_yet_another_array = {"first", "second"};
  676. CHECK_THROWS_AS(j_nonarray.insert(j_nonarray.end(), 10), json::type_error&);
  677. CHECK_THROWS_AS(j_nonarray.insert(j_nonarray.end(), j_value), json::type_error&);
  678. CHECK_THROWS_AS(j_nonarray.insert(j_nonarray.end(), 10, 11), json::type_error&);
  679. CHECK_THROWS_AS(j_nonarray.insert(j_nonarray.end(), j_yet_another_array.begin(),
  680. j_yet_another_array.end()), json::type_error&);
  681. CHECK_THROWS_AS(j_nonarray.insert(j_nonarray.end(), {1, 2, 3, 4}), json::type_error&);
  682. CHECK_THROWS_WITH(j_nonarray.insert(j_nonarray.end(), 10), "[json.exception.type_error.309] cannot use insert() with number");
  683. CHECK_THROWS_WITH(j_nonarray.insert(j_nonarray.end(), j_value), "[json.exception.type_error.309] cannot use insert() with number");
  684. CHECK_THROWS_WITH(j_nonarray.insert(j_nonarray.end(), 10, 11), "[json.exception.type_error.309] cannot use insert() with number");
  685. CHECK_THROWS_WITH(j_nonarray.insert(j_nonarray.end(), j_yet_another_array.begin(),
  686. j_yet_another_array.end()), "[json.exception.type_error.309] cannot use insert() with number");
  687. CHECK_THROWS_WITH(j_nonarray.insert(j_nonarray.end(), {1, 2, 3, 4}),
  688. "[json.exception.type_error.309] cannot use insert() with number");
  689. }
  690. }
  691. SECTION("update()")
  692. {
  693. json j_object1 = {{"one", "eins"}, {"two", "zwei"}};
  694. json j_object2 = {{"three", "drei"}, {"two", "zwo"}};
  695. json j_array = {1, 2, 3, 4};
  696. SECTION("const reference")
  697. {
  698. SECTION("proper usage")
  699. {
  700. j_object1.update(j_object2);
  701. CHECK(j_object1 == json({{"one", "eins"}, {"two", "zwo"}, {"three", "drei"}}));
  702. json j_null;
  703. j_null.update(j_object2);
  704. CHECK(j_null == j_object2);
  705. }
  706. SECTION("wrong types")
  707. {
  708. CHECK_THROWS_AS(j_array.update(j_object1), json::type_error&);
  709. CHECK_THROWS_WITH(j_array.update(j_object1), "[json.exception.type_error.312] cannot use update() with array");
  710. CHECK_THROWS_AS(j_object1.update(j_array), json::type_error&);
  711. CHECK_THROWS_WITH(j_object1.update(j_array), "[json.exception.type_error.312] cannot use update() with array");
  712. }
  713. }
  714. SECTION("iterator range")
  715. {
  716. SECTION("proper usage")
  717. {
  718. j_object1.update(j_object2.begin(), j_object2.end());
  719. CHECK(j_object1 == json({{"one", "eins"}, {"two", "zwo"}, {"three", "drei"}}));
  720. json j_null;
  721. j_null.update(j_object2.begin(), j_object2.end());
  722. CHECK(j_null == j_object2);
  723. }
  724. SECTION("empty range")
  725. {
  726. j_object1.update(j_object2.begin(), j_object2.begin());
  727. CHECK(j_object1 == json({{"one", "eins"}, {"two", "zwei"}}));
  728. }
  729. SECTION("invalid iterators")
  730. {
  731. json j_other_array2 = {"first", "second"};
  732. CHECK_THROWS_AS(j_array.update(j_object2.begin(), j_object2.end()), json::type_error&);
  733. CHECK_THROWS_AS(j_object1.update(j_object1.begin(), j_object2.end()), json::invalid_iterator&);
  734. CHECK_THROWS_AS(j_object1.update(j_array.begin(), j_array.end()), json::invalid_iterator&);
  735. CHECK_THROWS_WITH(j_array.update(j_object2.begin(), j_object2.end()),
  736. "[json.exception.type_error.312] cannot use update() with array");
  737. CHECK_THROWS_WITH(j_object1.update(j_object1.begin(), j_object2.end()),
  738. "[json.exception.invalid_iterator.210] iterators do not fit");
  739. CHECK_THROWS_WITH(j_object1.update(j_array.begin(), j_array.end()),
  740. "[json.exception.invalid_iterator.202] iterators first and last must point to objects");
  741. }
  742. }
  743. }
  744. SECTION("swap()")
  745. {
  746. SECTION("json")
  747. {
  748. SECTION("member swap")
  749. {
  750. json j("hello world");
  751. json k(42.23);
  752. j.swap(k);
  753. CHECK(j == json(42.23));
  754. CHECK(k == json("hello world"));
  755. }
  756. SECTION("nonmember swap")
  757. {
  758. json j("hello world");
  759. json k(42.23);
  760. std::swap(j, k);
  761. CHECK(j == json(42.23));
  762. CHECK(k == json("hello world"));
  763. }
  764. }
  765. SECTION("array_t")
  766. {
  767. SECTION("array_t type")
  768. {
  769. json j = {1, 2, 3, 4};
  770. json::array_t a = {"foo", "bar", "baz"};
  771. j.swap(a);
  772. CHECK(j == json({"foo", "bar", "baz"}));
  773. j.swap(a);
  774. CHECK(j == json({1, 2, 3, 4}));
  775. }
  776. SECTION("non-array_t type")
  777. {
  778. json j = 17;
  779. json::array_t a = {"foo", "bar", "baz"};
  780. CHECK_THROWS_AS(j.swap(a), json::type_error&);
  781. CHECK_THROWS_WITH(j.swap(a), "[json.exception.type_error.310] cannot use swap() with number");
  782. }
  783. }
  784. SECTION("object_t")
  785. {
  786. SECTION("object_t type")
  787. {
  788. json j = {{"one", 1}, {"two", 2}};
  789. json::object_t o = {{"cow", "Kuh"}, {"chicken", "Huhn"}};
  790. j.swap(o);
  791. CHECK(j == json({{"cow", "Kuh"}, {"chicken", "Huhn"}}));
  792. j.swap(o);
  793. CHECK(j == json({{"one", 1}, {"two", 2}}));
  794. }
  795. SECTION("non-object_t type")
  796. {
  797. json j = 17;
  798. json::object_t o = {{"cow", "Kuh"}, {"chicken", "Huhn"}};
  799. CHECK_THROWS_AS(j.swap(o), json::type_error&);
  800. CHECK_THROWS_WITH(j.swap(o), "[json.exception.type_error.310] cannot use swap() with number");
  801. }
  802. }
  803. SECTION("string_t")
  804. {
  805. SECTION("string_t type")
  806. {
  807. json j = "Hello world";
  808. json::string_t s = "Hallo Welt";
  809. j.swap(s);
  810. CHECK(j == json("Hallo Welt"));
  811. j.swap(s);
  812. CHECK(j == json("Hello world"));
  813. }
  814. SECTION("non-string_t type")
  815. {
  816. json j = 17;
  817. json::string_t s = "Hallo Welt";
  818. CHECK_THROWS_AS(j.swap(s), json::type_error&);
  819. CHECK_THROWS_WITH(j.swap(s), "[json.exception.type_error.310] cannot use swap() with number");
  820. }
  821. }
  822. SECTION("binary_t")
  823. {
  824. SECTION("binary_t type")
  825. {
  826. json j = json::binary({1, 2, 3, 4});
  827. json::binary_t s = {{5, 6, 7, 8}};
  828. j.swap(s);
  829. CHECK(j == json::binary({5, 6, 7, 8}));
  830. j.swap(s);
  831. CHECK(j == json::binary({1, 2, 3, 4}));
  832. }
  833. SECTION("binary_t::container_type type")
  834. {
  835. json j = json::binary({1, 2, 3, 4});
  836. std::vector<std::uint8_t> s = {{5, 6, 7, 8}};
  837. j.swap(s);
  838. CHECK(j == json::binary({5, 6, 7, 8}));
  839. j.swap(s);
  840. CHECK(j == json::binary({1, 2, 3, 4}));
  841. }
  842. SECTION("non-binary_t type")
  843. {
  844. json j = 17;
  845. json::binary_t s1 = {{1, 2, 3, 4}};
  846. std::vector<std::uint8_t> s2 = {{5, 6, 7, 8}};
  847. CHECK_THROWS_WITH_AS(j.swap(s1), "[json.exception.type_error.310] cannot use swap() with number", json::type_error);
  848. CHECK_THROWS_WITH_AS(j.swap(s2), "[json.exception.type_error.310] cannot use swap() with number", json::type_error);
  849. }
  850. }
  851. }
  852. }