unit-items.cpp 35 KB

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