unit-items.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  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. #if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
  29. #define JSON_HAS_CPP_17
  30. #define JSON_HAS_CPP_14
  31. #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
  32. #define JSON_HAS_CPP_14
  33. #endif
  34. TEST_CASE("iterator_wrapper")
  35. {
  36. SECTION("object")
  37. {
  38. SECTION("value")
  39. {
  40. json j = { {"A", 1}, {"B", 2} };
  41. int counter = 1;
  42. for (auto i : json::iterator_wrapper(j))
  43. {
  44. switch (counter++)
  45. {
  46. case 1:
  47. {
  48. CHECK(i.key() == "A");
  49. CHECK(i.value() == json(1));
  50. break;
  51. }
  52. case 2:
  53. {
  54. CHECK(i.key() == "B");
  55. CHECK(i.value() == json(2));
  56. break;
  57. }
  58. default:
  59. {
  60. break;
  61. }
  62. }
  63. }
  64. CHECK(counter == 3);
  65. }
  66. SECTION("reference")
  67. {
  68. json j = { {"A", 1}, {"B", 2} };
  69. int counter = 1;
  70. for (auto& i : json::iterator_wrapper(j))
  71. {
  72. switch (counter++)
  73. {
  74. case 1:
  75. {
  76. CHECK(i.key() == "A");
  77. CHECK(i.value() == json(1));
  78. // change the value
  79. i.value() = json(11);
  80. CHECK(i.value() == json(11));
  81. break;
  82. }
  83. case 2:
  84. {
  85. CHECK(i.key() == "B");
  86. CHECK(i.value() == json(2));
  87. // change the value
  88. i.value() = json(22);
  89. CHECK(i.value() == json(22));
  90. break;
  91. }
  92. default:
  93. {
  94. break;
  95. }
  96. }
  97. }
  98. CHECK(counter == 3);
  99. // check if values where changed
  100. CHECK(j == json({ {"A", 11}, {"B", 22} }));
  101. }
  102. SECTION("const value")
  103. {
  104. json j = { {"A", 1}, {"B", 2} };
  105. int counter = 1;
  106. for (const auto i : json::iterator_wrapper(j))
  107. {
  108. switch (counter++)
  109. {
  110. case 1:
  111. {
  112. CHECK(i.key() == "A");
  113. CHECK(i.value() == json(1));
  114. break;
  115. }
  116. case 2:
  117. {
  118. CHECK(i.key() == "B");
  119. CHECK(i.value() == json(2));
  120. break;
  121. }
  122. default:
  123. {
  124. break;
  125. }
  126. }
  127. }
  128. CHECK(counter == 3);
  129. }
  130. SECTION("const reference")
  131. {
  132. json j = { {"A", 1}, {"B", 2} };
  133. int counter = 1;
  134. for (const auto& i : json::iterator_wrapper(j))
  135. {
  136. switch (counter++)
  137. {
  138. case 1:
  139. {
  140. CHECK(i.key() == "A");
  141. CHECK(i.value() == json(1));
  142. break;
  143. }
  144. case 2:
  145. {
  146. CHECK(i.key() == "B");
  147. CHECK(i.value() == json(2));
  148. break;
  149. }
  150. default:
  151. {
  152. break;
  153. }
  154. }
  155. }
  156. CHECK(counter == 3);
  157. }
  158. }
  159. SECTION("const object")
  160. {
  161. SECTION("value")
  162. {
  163. const json j = { {"A", 1}, {"B", 2} };
  164. int counter = 1;
  165. for (auto i : json::iterator_wrapper(j))
  166. {
  167. switch (counter++)
  168. {
  169. case 1:
  170. {
  171. CHECK(i.key() == "A");
  172. CHECK(i.value() == json(1));
  173. break;
  174. }
  175. case 2:
  176. {
  177. CHECK(i.key() == "B");
  178. CHECK(i.value() == json(2));
  179. break;
  180. }
  181. default:
  182. {
  183. break;
  184. }
  185. }
  186. }
  187. CHECK(counter == 3);
  188. }
  189. SECTION("reference")
  190. {
  191. const json j = { {"A", 1}, {"B", 2} };
  192. int counter = 1;
  193. for (auto& i : json::iterator_wrapper(j))
  194. {
  195. switch (counter++)
  196. {
  197. case 1:
  198. {
  199. CHECK(i.key() == "A");
  200. CHECK(i.value() == json(1));
  201. break;
  202. }
  203. case 2:
  204. {
  205. CHECK(i.key() == "B");
  206. CHECK(i.value() == json(2));
  207. break;
  208. }
  209. default:
  210. {
  211. break;
  212. }
  213. }
  214. }
  215. CHECK(counter == 3);
  216. }
  217. SECTION("const value")
  218. {
  219. const json j = { {"A", 1}, {"B", 2} };
  220. int counter = 1;
  221. for (const auto i : json::iterator_wrapper(j))
  222. {
  223. switch (counter++)
  224. {
  225. case 1:
  226. {
  227. CHECK(i.key() == "A");
  228. CHECK(i.value() == json(1));
  229. break;
  230. }
  231. case 2:
  232. {
  233. CHECK(i.key() == "B");
  234. CHECK(i.value() == json(2));
  235. break;
  236. }
  237. default:
  238. {
  239. break;
  240. }
  241. }
  242. }
  243. CHECK(counter == 3);
  244. }
  245. SECTION("const reference")
  246. {
  247. const json j = { {"A", 1}, {"B", 2} };
  248. int counter = 1;
  249. for (const auto& i : json::iterator_wrapper(j))
  250. {
  251. switch (counter++)
  252. {
  253. case 1:
  254. {
  255. CHECK(i.key() == "A");
  256. CHECK(i.value() == json(1));
  257. break;
  258. }
  259. case 2:
  260. {
  261. CHECK(i.key() == "B");
  262. CHECK(i.value() == json(2));
  263. break;
  264. }
  265. default:
  266. {
  267. break;
  268. }
  269. }
  270. }
  271. CHECK(counter == 3);
  272. }
  273. }
  274. SECTION("array")
  275. {
  276. SECTION("value")
  277. {
  278. json j = { "A", "B" };
  279. int counter = 1;
  280. for (auto i : json::iterator_wrapper(j))
  281. {
  282. switch (counter++)
  283. {
  284. case 1:
  285. {
  286. CHECK(i.key() == "0");
  287. CHECK(i.value() == "A");
  288. break;
  289. }
  290. case 2:
  291. {
  292. CHECK(i.key() == "1");
  293. CHECK(i.value() == "B");
  294. break;
  295. }
  296. default:
  297. {
  298. break;
  299. }
  300. }
  301. }
  302. CHECK(counter == 3);
  303. }
  304. SECTION("reference")
  305. {
  306. json j = { "A", "B" };
  307. int counter = 1;
  308. for (auto& i : json::iterator_wrapper(j))
  309. {
  310. switch (counter++)
  311. {
  312. case 1:
  313. {
  314. CHECK(i.key() == "0");
  315. CHECK(i.value() == "A");
  316. // change the value
  317. i.value() = "AA";
  318. CHECK(i.value() == "AA");
  319. break;
  320. }
  321. case 2:
  322. {
  323. CHECK(i.key() == "1");
  324. CHECK(i.value() == "B");
  325. // change the value
  326. i.value() = "BB";
  327. CHECK(i.value() == "BB");
  328. break;
  329. }
  330. default:
  331. {
  332. break;
  333. }
  334. }
  335. }
  336. CHECK(counter == 3);
  337. // check if values where changed
  338. CHECK(j == json({ "AA", "BB" }));
  339. }
  340. SECTION("const value")
  341. {
  342. json j = { "A", "B" };
  343. int counter = 1;
  344. for (const auto i : json::iterator_wrapper(j))
  345. {
  346. switch (counter++)
  347. {
  348. case 1:
  349. {
  350. CHECK(i.key() == "0");
  351. CHECK(i.value() == "A");
  352. break;
  353. }
  354. case 2:
  355. {
  356. CHECK(i.key() == "1");
  357. CHECK(i.value() == "B");
  358. break;
  359. }
  360. default:
  361. {
  362. break;
  363. }
  364. }
  365. }
  366. CHECK(counter == 3);
  367. }
  368. SECTION("const reference")
  369. {
  370. json j = { "A", "B" };
  371. int counter = 1;
  372. for (const auto& i : json::iterator_wrapper(j))
  373. {
  374. switch (counter++)
  375. {
  376. case 1:
  377. {
  378. CHECK(i.key() == "0");
  379. CHECK(i.value() == "A");
  380. break;
  381. }
  382. case 2:
  383. {
  384. CHECK(i.key() == "1");
  385. CHECK(i.value() == "B");
  386. break;
  387. }
  388. default:
  389. {
  390. break;
  391. }
  392. }
  393. }
  394. CHECK(counter == 3);
  395. }
  396. }
  397. SECTION("const array")
  398. {
  399. SECTION("value")
  400. {
  401. const json j = { "A", "B" };
  402. int counter = 1;
  403. for (auto i : json::iterator_wrapper(j))
  404. {
  405. switch (counter++)
  406. {
  407. case 1:
  408. {
  409. CHECK(i.key() == "0");
  410. CHECK(i.value() == "A");
  411. break;
  412. }
  413. case 2:
  414. {
  415. CHECK(i.key() == "1");
  416. CHECK(i.value() == "B");
  417. break;
  418. }
  419. default:
  420. {
  421. break;
  422. }
  423. }
  424. }
  425. CHECK(counter == 3);
  426. }
  427. SECTION("reference")
  428. {
  429. const json j = { "A", "B" };
  430. int counter = 1;
  431. for (auto& i : json::iterator_wrapper(j))
  432. {
  433. switch (counter++)
  434. {
  435. case 1:
  436. {
  437. CHECK(i.key() == "0");
  438. CHECK(i.value() == "A");
  439. break;
  440. }
  441. case 2:
  442. {
  443. CHECK(i.key() == "1");
  444. CHECK(i.value() == "B");
  445. break;
  446. }
  447. default:
  448. {
  449. break;
  450. }
  451. }
  452. }
  453. CHECK(counter == 3);
  454. }
  455. SECTION("const value")
  456. {
  457. const json j = { "A", "B" };
  458. int counter = 1;
  459. for (const auto i : json::iterator_wrapper(j))
  460. {
  461. switch (counter++)
  462. {
  463. case 1:
  464. {
  465. CHECK(i.key() == "0");
  466. CHECK(i.value() == "A");
  467. break;
  468. }
  469. case 2:
  470. {
  471. CHECK(i.key() == "1");
  472. CHECK(i.value() == "B");
  473. break;
  474. }
  475. default:
  476. {
  477. break;
  478. }
  479. }
  480. }
  481. CHECK(counter == 3);
  482. }
  483. SECTION("const reference")
  484. {
  485. const json j = { "A", "B" };
  486. int counter = 1;
  487. for (const auto& i : json::iterator_wrapper(j))
  488. {
  489. switch (counter++)
  490. {
  491. case 1:
  492. {
  493. CHECK(i.key() == "0");
  494. CHECK(i.value() == "A");
  495. break;
  496. }
  497. case 2:
  498. {
  499. CHECK(i.key() == "1");
  500. CHECK(i.value() == "B");
  501. break;
  502. }
  503. default:
  504. {
  505. break;
  506. }
  507. }
  508. }
  509. CHECK(counter == 3);
  510. }
  511. }
  512. SECTION("primitive")
  513. {
  514. SECTION("value")
  515. {
  516. json j = 1;
  517. int counter = 1;
  518. for (auto i : json::iterator_wrapper(j))
  519. {
  520. ++counter;
  521. CHECK(i.key() == "");
  522. CHECK(i.value() == json(1));
  523. }
  524. CHECK(counter == 2);
  525. }
  526. SECTION("reference")
  527. {
  528. json j = 1;
  529. int counter = 1;
  530. for (auto& i : json::iterator_wrapper(j))
  531. {
  532. ++counter;
  533. CHECK(i.key() == "");
  534. CHECK(i.value() == json(1));
  535. // change value
  536. i.value() = json(2);
  537. }
  538. CHECK(counter == 2);
  539. // check if value has changed
  540. CHECK(j == json(2));
  541. }
  542. SECTION("const value")
  543. {
  544. json j = 1;
  545. int counter = 1;
  546. for (const auto i : json::iterator_wrapper(j))
  547. {
  548. ++counter;
  549. CHECK(i.key() == "");
  550. CHECK(i.value() == json(1));
  551. }
  552. CHECK(counter == 2);
  553. }
  554. SECTION("const reference")
  555. {
  556. json j = 1;
  557. int counter = 1;
  558. for (const auto& i : json::iterator_wrapper(j))
  559. {
  560. ++counter;
  561. CHECK(i.key() == "");
  562. CHECK(i.value() == json(1));
  563. }
  564. CHECK(counter == 2);
  565. }
  566. }
  567. SECTION("const primitive")
  568. {
  569. SECTION("value")
  570. {
  571. const json j = 1;
  572. int counter = 1;
  573. for (auto i : json::iterator_wrapper(j))
  574. {
  575. ++counter;
  576. CHECK(i.key() == "");
  577. CHECK(i.value() == json(1));
  578. }
  579. CHECK(counter == 2);
  580. }
  581. SECTION("reference")
  582. {
  583. const json j = 1;
  584. int counter = 1;
  585. for (auto& i : json::iterator_wrapper(j))
  586. {
  587. ++counter;
  588. CHECK(i.key() == "");
  589. CHECK(i.value() == json(1));
  590. }
  591. CHECK(counter == 2);
  592. }
  593. SECTION("const value")
  594. {
  595. const json j = 1;
  596. int counter = 1;
  597. for (const auto i : json::iterator_wrapper(j))
  598. {
  599. ++counter;
  600. CHECK(i.key() == "");
  601. CHECK(i.value() == json(1));
  602. }
  603. CHECK(counter == 2);
  604. }
  605. SECTION("const reference")
  606. {
  607. const json j = 1;
  608. int counter = 1;
  609. for (const auto& i : json::iterator_wrapper(j))
  610. {
  611. ++counter;
  612. CHECK(i.key() == "");
  613. CHECK(i.value() == json(1));
  614. }
  615. CHECK(counter == 2);
  616. }
  617. }
  618. }
  619. TEST_CASE("items()")
  620. {
  621. SECTION("object")
  622. {
  623. SECTION("value")
  624. {
  625. json j = { {"A", 1}, {"B", 2} };
  626. int counter = 1;
  627. for (auto i : j.items())
  628. {
  629. switch (counter++)
  630. {
  631. case 1:
  632. {
  633. CHECK(i.key() == "A");
  634. CHECK(i.value() == json(1));
  635. break;
  636. }
  637. case 2:
  638. {
  639. CHECK(i.key() == "B");
  640. CHECK(i.value() == json(2));
  641. break;
  642. }
  643. default:
  644. {
  645. break;
  646. }
  647. }
  648. }
  649. CHECK(counter == 3);
  650. }
  651. SECTION("reference")
  652. {
  653. json j = { {"A", 1}, {"B", 2} };
  654. int counter = 1;
  655. for (auto& i : j.items())
  656. {
  657. switch (counter++)
  658. {
  659. case 1:
  660. {
  661. CHECK(i.key() == "A");
  662. CHECK(i.value() == json(1));
  663. // change the value
  664. i.value() = json(11);
  665. CHECK(i.value() == json(11));
  666. break;
  667. }
  668. case 2:
  669. {
  670. CHECK(i.key() == "B");
  671. CHECK(i.value() == json(2));
  672. // change the value
  673. i.value() = json(22);
  674. CHECK(i.value() == json(22));
  675. break;
  676. }
  677. default:
  678. {
  679. break;
  680. }
  681. }
  682. }
  683. CHECK(counter == 3);
  684. // check if values where changed
  685. CHECK(j == json({ {"A", 11}, {"B", 22} }));
  686. }
  687. SECTION("const value")
  688. {
  689. json j = { {"A", 1}, {"B", 2} };
  690. int counter = 1;
  691. for (const auto i : j.items())
  692. {
  693. switch (counter++)
  694. {
  695. case 1:
  696. {
  697. CHECK(i.key() == "A");
  698. CHECK(i.value() == json(1));
  699. break;
  700. }
  701. case 2:
  702. {
  703. CHECK(i.key() == "B");
  704. CHECK(i.value() == json(2));
  705. break;
  706. }
  707. default:
  708. {
  709. break;
  710. }
  711. }
  712. }
  713. CHECK(counter == 3);
  714. }
  715. SECTION("const reference")
  716. {
  717. json j = { {"A", 1}, {"B", 2} };
  718. int counter = 1;
  719. for (const auto& i : j.items())
  720. {
  721. switch (counter++)
  722. {
  723. case 1:
  724. {
  725. CHECK(i.key() == "A");
  726. CHECK(i.value() == json(1));
  727. break;
  728. }
  729. case 2:
  730. {
  731. CHECK(i.key() == "B");
  732. CHECK(i.value() == json(2));
  733. break;
  734. }
  735. default:
  736. {
  737. break;
  738. }
  739. }
  740. }
  741. CHECK(counter == 3);
  742. }
  743. #ifdef JSON_HAS_CPP_17
  744. SECTION("structured bindings")
  745. {
  746. json j = { {"A", 1}, {"B", 2} };
  747. std::map<std::string, int> m;
  748. for (auto const&[key, value] : j.items())
  749. {
  750. m.emplace(key, value);
  751. }
  752. CHECK(j.get<decltype(m)>() == m);
  753. }
  754. #endif
  755. }
  756. SECTION("const object")
  757. {
  758. SECTION("value")
  759. {
  760. const json j = { {"A", 1}, {"B", 2} };
  761. int counter = 1;
  762. for (auto i : j.items())
  763. {
  764. switch (counter++)
  765. {
  766. case 1:
  767. {
  768. CHECK(i.key() == "A");
  769. CHECK(i.value() == json(1));
  770. break;
  771. }
  772. case 2:
  773. {
  774. CHECK(i.key() == "B");
  775. CHECK(i.value() == json(2));
  776. break;
  777. }
  778. default:
  779. {
  780. break;
  781. }
  782. }
  783. }
  784. CHECK(counter == 3);
  785. }
  786. SECTION("reference")
  787. {
  788. const json j = { {"A", 1}, {"B", 2} };
  789. int counter = 1;
  790. for (auto& i : j.items())
  791. {
  792. switch (counter++)
  793. {
  794. case 1:
  795. {
  796. CHECK(i.key() == "A");
  797. CHECK(i.value() == json(1));
  798. break;
  799. }
  800. case 2:
  801. {
  802. CHECK(i.key() == "B");
  803. CHECK(i.value() == json(2));
  804. break;
  805. }
  806. default:
  807. {
  808. break;
  809. }
  810. }
  811. }
  812. CHECK(counter == 3);
  813. }
  814. SECTION("const value")
  815. {
  816. const json j = { {"A", 1}, {"B", 2} };
  817. int counter = 1;
  818. for (const auto i : j.items())
  819. {
  820. switch (counter++)
  821. {
  822. case 1:
  823. {
  824. CHECK(i.key() == "A");
  825. CHECK(i.value() == json(1));
  826. break;
  827. }
  828. case 2:
  829. {
  830. CHECK(i.key() == "B");
  831. CHECK(i.value() == json(2));
  832. break;
  833. }
  834. default:
  835. {
  836. break;
  837. }
  838. }
  839. }
  840. CHECK(counter == 3);
  841. }
  842. SECTION("const reference")
  843. {
  844. const json j = { {"A", 1}, {"B", 2} };
  845. int counter = 1;
  846. for (const auto& i : j.items())
  847. {
  848. switch (counter++)
  849. {
  850. case 1:
  851. {
  852. CHECK(i.key() == "A");
  853. CHECK(i.value() == json(1));
  854. break;
  855. }
  856. case 2:
  857. {
  858. CHECK(i.key() == "B");
  859. CHECK(i.value() == json(2));
  860. break;
  861. }
  862. default:
  863. {
  864. break;
  865. }
  866. }
  867. }
  868. CHECK(counter == 3);
  869. }
  870. }
  871. SECTION("array")
  872. {
  873. SECTION("value")
  874. {
  875. json j = { "A", "B" };
  876. int counter = 1;
  877. for (auto i : j.items())
  878. {
  879. switch (counter++)
  880. {
  881. case 1:
  882. {
  883. CHECK(i.key() == "0");
  884. CHECK(i.value() == "A");
  885. break;
  886. }
  887. case 2:
  888. {
  889. CHECK(i.key() == "1");
  890. CHECK(i.value() == "B");
  891. break;
  892. }
  893. default:
  894. {
  895. break;
  896. }
  897. }
  898. }
  899. CHECK(counter == 3);
  900. }
  901. SECTION("reference")
  902. {
  903. json j = { "A", "B" };
  904. int counter = 1;
  905. for (auto& i : j.items())
  906. {
  907. switch (counter++)
  908. {
  909. case 1:
  910. {
  911. CHECK(i.key() == "0");
  912. CHECK(i.value() == "A");
  913. // change the value
  914. i.value() = "AA";
  915. CHECK(i.value() == "AA");
  916. break;
  917. }
  918. case 2:
  919. {
  920. CHECK(i.key() == "1");
  921. CHECK(i.value() == "B");
  922. // change the value
  923. i.value() = "BB";
  924. CHECK(i.value() == "BB");
  925. break;
  926. }
  927. default:
  928. {
  929. break;
  930. }
  931. }
  932. }
  933. CHECK(counter == 3);
  934. // check if values where changed
  935. CHECK(j == json({ "AA", "BB" }));
  936. }
  937. SECTION("const value")
  938. {
  939. json j = { "A", "B" };
  940. int counter = 1;
  941. for (const auto i : j.items())
  942. {
  943. switch (counter++)
  944. {
  945. case 1:
  946. {
  947. CHECK(i.key() == "0");
  948. CHECK(i.value() == "A");
  949. break;
  950. }
  951. case 2:
  952. {
  953. CHECK(i.key() == "1");
  954. CHECK(i.value() == "B");
  955. break;
  956. }
  957. default:
  958. {
  959. break;
  960. }
  961. }
  962. }
  963. CHECK(counter == 3);
  964. }
  965. SECTION("const reference")
  966. {
  967. json j = { "A", "B" };
  968. int counter = 1;
  969. for (const auto& i : j.items())
  970. {
  971. switch (counter++)
  972. {
  973. case 1:
  974. {
  975. CHECK(i.key() == "0");
  976. CHECK(i.value() == "A");
  977. break;
  978. }
  979. case 2:
  980. {
  981. CHECK(i.key() == "1");
  982. CHECK(i.value() == "B");
  983. break;
  984. }
  985. default:
  986. {
  987. break;
  988. }
  989. }
  990. }
  991. CHECK(counter == 3);
  992. }
  993. }
  994. SECTION("const array")
  995. {
  996. SECTION("value")
  997. {
  998. const json j = { "A", "B" };
  999. int counter = 1;
  1000. for (auto i : j.items())
  1001. {
  1002. switch (counter++)
  1003. {
  1004. case 1:
  1005. {
  1006. CHECK(i.key() == "0");
  1007. CHECK(i.value() == "A");
  1008. break;
  1009. }
  1010. case 2:
  1011. {
  1012. CHECK(i.key() == "1");
  1013. CHECK(i.value() == "B");
  1014. break;
  1015. }
  1016. default:
  1017. {
  1018. break;
  1019. }
  1020. }
  1021. }
  1022. CHECK(counter == 3);
  1023. }
  1024. SECTION("reference")
  1025. {
  1026. const json j = { "A", "B" };
  1027. int counter = 1;
  1028. for (auto& i : j.items())
  1029. {
  1030. switch (counter++)
  1031. {
  1032. case 1:
  1033. {
  1034. CHECK(i.key() == "0");
  1035. CHECK(i.value() == "A");
  1036. break;
  1037. }
  1038. case 2:
  1039. {
  1040. CHECK(i.key() == "1");
  1041. CHECK(i.value() == "B");
  1042. break;
  1043. }
  1044. default:
  1045. {
  1046. break;
  1047. }
  1048. }
  1049. }
  1050. CHECK(counter == 3);
  1051. }
  1052. SECTION("const value")
  1053. {
  1054. const json j = { "A", "B" };
  1055. int counter = 1;
  1056. for (const auto i : j.items())
  1057. {
  1058. switch (counter++)
  1059. {
  1060. case 1:
  1061. {
  1062. CHECK(i.key() == "0");
  1063. CHECK(i.value() == "A");
  1064. break;
  1065. }
  1066. case 2:
  1067. {
  1068. CHECK(i.key() == "1");
  1069. CHECK(i.value() == "B");
  1070. break;
  1071. }
  1072. default:
  1073. {
  1074. break;
  1075. }
  1076. }
  1077. }
  1078. CHECK(counter == 3);
  1079. }
  1080. SECTION("const reference")
  1081. {
  1082. const json j = { "A", "B" };
  1083. int counter = 1;
  1084. for (const auto& i : j.items())
  1085. {
  1086. switch (counter++)
  1087. {
  1088. case 1:
  1089. {
  1090. CHECK(i.key() == "0");
  1091. CHECK(i.value() == "A");
  1092. break;
  1093. }
  1094. case 2:
  1095. {
  1096. CHECK(i.key() == "1");
  1097. CHECK(i.value() == "B");
  1098. break;
  1099. }
  1100. default:
  1101. {
  1102. break;
  1103. }
  1104. }
  1105. }
  1106. CHECK(counter == 3);
  1107. }
  1108. }
  1109. SECTION("primitive")
  1110. {
  1111. SECTION("value")
  1112. {
  1113. json j = 1;
  1114. int counter = 1;
  1115. for (auto i : j.items())
  1116. {
  1117. ++counter;
  1118. CHECK(i.key() == "");
  1119. CHECK(i.value() == json(1));
  1120. }
  1121. CHECK(counter == 2);
  1122. }
  1123. SECTION("reference")
  1124. {
  1125. json j = 1;
  1126. int counter = 1;
  1127. for (auto& i : j.items())
  1128. {
  1129. ++counter;
  1130. CHECK(i.key() == "");
  1131. CHECK(i.value() == json(1));
  1132. // change value
  1133. i.value() = json(2);
  1134. }
  1135. CHECK(counter == 2);
  1136. // check if value has changed
  1137. CHECK(j == json(2));
  1138. }
  1139. SECTION("const value")
  1140. {
  1141. json j = 1;
  1142. int counter = 1;
  1143. for (const auto i : j.items())
  1144. {
  1145. ++counter;
  1146. CHECK(i.key() == "");
  1147. CHECK(i.value() == json(1));
  1148. }
  1149. CHECK(counter == 2);
  1150. }
  1151. SECTION("const reference")
  1152. {
  1153. json j = 1;
  1154. int counter = 1;
  1155. for (const auto& i : j.items())
  1156. {
  1157. ++counter;
  1158. CHECK(i.key() == "");
  1159. CHECK(i.value() == json(1));
  1160. }
  1161. CHECK(counter == 2);
  1162. }
  1163. }
  1164. SECTION("const primitive")
  1165. {
  1166. SECTION("value")
  1167. {
  1168. const json j = 1;
  1169. int counter = 1;
  1170. for (auto i : j.items())
  1171. {
  1172. ++counter;
  1173. CHECK(i.key() == "");
  1174. CHECK(i.value() == json(1));
  1175. }
  1176. CHECK(counter == 2);
  1177. }
  1178. SECTION("reference")
  1179. {
  1180. const json j = 1;
  1181. int counter = 1;
  1182. for (auto& i : j.items())
  1183. {
  1184. ++counter;
  1185. CHECK(i.key() == "");
  1186. CHECK(i.value() == json(1));
  1187. }
  1188. CHECK(counter == 2);
  1189. }
  1190. SECTION("const value")
  1191. {
  1192. const json j = 1;
  1193. int counter = 1;
  1194. for (const auto i : j.items())
  1195. {
  1196. ++counter;
  1197. CHECK(i.key() == "");
  1198. CHECK(i.value() == json(1));
  1199. }
  1200. CHECK(counter == 2);
  1201. }
  1202. SECTION("const reference")
  1203. {
  1204. const json j = 1;
  1205. int counter = 1;
  1206. for (const auto& i : j.items())
  1207. {
  1208. ++counter;
  1209. CHECK(i.key() == "");
  1210. CHECK(i.value() == json(1));
  1211. }
  1212. CHECK(counter == 2);
  1213. }
  1214. }
  1215. }