unit-element_access1.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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("element access 1")
  29. {
  30. SECTION("array")
  31. {
  32. json j = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  33. const json j_const = j;
  34. SECTION("access specified element with bounds checking")
  35. {
  36. SECTION("access within bounds")
  37. {
  38. CHECK(j.at(0) == json(1));
  39. CHECK(j.at(1) == json(1u));
  40. CHECK(j.at(2) == json(true));
  41. CHECK(j.at(3) == json(nullptr));
  42. CHECK(j.at(4) == json("string"));
  43. CHECK(j.at(5) == json(42.23));
  44. CHECK(j.at(6) == json::object());
  45. CHECK(j.at(7) == json({1, 2, 3}));
  46. CHECK(j_const.at(0) == json(1));
  47. CHECK(j_const.at(1) == json(1u));
  48. CHECK(j_const.at(2) == json(true));
  49. CHECK(j_const.at(3) == json(nullptr));
  50. CHECK(j_const.at(4) == json("string"));
  51. CHECK(j_const.at(5) == json(42.23));
  52. CHECK(j_const.at(6) == json::object());
  53. CHECK(j_const.at(7) == json({1, 2, 3}));
  54. }
  55. SECTION("access outside bounds")
  56. {
  57. CHECK_THROWS_AS(j.at(8), json::out_of_range&);
  58. CHECK_THROWS_AS(j_const.at(8), json::out_of_range&);
  59. CHECK_THROWS_WITH(j.at(8),
  60. "[json.exception.out_of_range.401] array index 8 is out of range");
  61. CHECK_THROWS_WITH(j_const.at(8),
  62. "[json.exception.out_of_range.401] array index 8 is out of range");
  63. }
  64. SECTION("access on non-array type")
  65. {
  66. SECTION("null")
  67. {
  68. json j_nonarray(json::value_t::null);
  69. const json j_nonarray_const(j_nonarray);
  70. CHECK_THROWS_AS(j_nonarray.at(0), json::type_error&);
  71. CHECK_THROWS_AS(j_nonarray_const.at(0), json::type_error&);
  72. CHECK_THROWS_WITH(j_nonarray.at(0), "[json.exception.type_error.304] cannot use at() with null");
  73. CHECK_THROWS_WITH(j_nonarray_const.at(0), "[json.exception.type_error.304] cannot use at() with null");
  74. }
  75. SECTION("boolean")
  76. {
  77. json j_nonarray(json::value_t::boolean);
  78. const json j_nonarray_const(j_nonarray);
  79. CHECK_THROWS_AS(j_nonarray.at(0), json::type_error&);
  80. CHECK_THROWS_AS(j_nonarray_const.at(0), json::type_error&);
  81. CHECK_THROWS_WITH(j_nonarray.at(0), "[json.exception.type_error.304] cannot use at() with boolean");
  82. CHECK_THROWS_WITH(j_nonarray_const.at(0), "[json.exception.type_error.304] cannot use at() with boolean");
  83. }
  84. SECTION("string")
  85. {
  86. json j_nonarray(json::value_t::string);
  87. const json j_nonarray_const(j_nonarray);
  88. CHECK_THROWS_AS(j_nonarray.at(0), json::type_error&);
  89. CHECK_THROWS_AS(j_nonarray_const.at(0), json::type_error&);
  90. CHECK_THROWS_WITH(j_nonarray.at(0), "[json.exception.type_error.304] cannot use at() with string");
  91. CHECK_THROWS_WITH(j_nonarray_const.at(0), "[json.exception.type_error.304] cannot use at() with string");
  92. }
  93. SECTION("object")
  94. {
  95. json j_nonarray(json::value_t::object);
  96. const json j_nonarray_const(j_nonarray);
  97. CHECK_THROWS_AS(j_nonarray.at(0), json::type_error&);
  98. CHECK_THROWS_AS(j_nonarray_const.at(0), json::type_error&);
  99. CHECK_THROWS_WITH(j_nonarray.at(0), "[json.exception.type_error.304] cannot use at() with object");
  100. CHECK_THROWS_WITH(j_nonarray_const.at(0), "[json.exception.type_error.304] cannot use at() with object");
  101. }
  102. SECTION("number (integer)")
  103. {
  104. json j_nonarray(json::value_t::number_integer);
  105. const json j_nonarray_const(j_nonarray);
  106. CHECK_THROWS_AS(j_nonarray.at(0), json::type_error&);
  107. CHECK_THROWS_AS(j_nonarray_const.at(0), json::type_error&);
  108. CHECK_THROWS_WITH(j_nonarray.at(0), "[json.exception.type_error.304] cannot use at() with number");
  109. CHECK_THROWS_WITH(j_nonarray_const.at(0), "[json.exception.type_error.304] cannot use at() with number");
  110. }
  111. SECTION("number (unsigned)")
  112. {
  113. json j_nonarray(json::value_t::number_unsigned);
  114. const json j_nonarray_const(j_nonarray);
  115. CHECK_THROWS_AS(j_nonarray.at(0), json::type_error&);
  116. CHECK_THROWS_AS(j_nonarray_const.at(0), json::type_error&);
  117. CHECK_THROWS_WITH(j_nonarray.at(0), "[json.exception.type_error.304] cannot use at() with number");
  118. CHECK_THROWS_WITH(j_nonarray_const.at(0), "[json.exception.type_error.304] cannot use at() with number");
  119. }
  120. SECTION("number (floating-point)")
  121. {
  122. json j_nonarray(json::value_t::number_float);
  123. const json j_nonarray_const(j_nonarray);
  124. CHECK_THROWS_AS(j_nonarray.at(0), json::type_error&);
  125. CHECK_THROWS_AS(j_nonarray_const.at(0), json::type_error&);
  126. CHECK_THROWS_WITH(j_nonarray.at(0), "[json.exception.type_error.304] cannot use at() with number");
  127. CHECK_THROWS_WITH(j_nonarray_const.at(0), "[json.exception.type_error.304] cannot use at() with number");
  128. }
  129. }
  130. }
  131. SECTION("front and back")
  132. {
  133. CHECK(j.front() == json(1));
  134. CHECK(j_const.front() == json(1));
  135. CHECK(j.back() == json({1, 2, 3}));
  136. CHECK(j_const.back() == json({1, 2, 3}));
  137. }
  138. SECTION("access specified element")
  139. {
  140. SECTION("access within bounds")
  141. {
  142. CHECK(j[0] == json(1));
  143. CHECK(j[1] == json(1u));
  144. CHECK(j[2] == json(true));
  145. CHECK(j[3] == json(nullptr));
  146. CHECK(j[4] == json("string"));
  147. CHECK(j[5] == json(42.23));
  148. CHECK(j[6] == json::object());
  149. CHECK(j[7] == json({1, 2, 3}));
  150. CHECK(j_const[0] == json(1));
  151. CHECK(j_const[1] == json(1u));
  152. CHECK(j_const[2] == json(true));
  153. CHECK(j_const[3] == json(nullptr));
  154. CHECK(j_const[4] == json("string"));
  155. CHECK(j_const[5] == json(42.23));
  156. CHECK(j_const[6] == json::object());
  157. CHECK(j_const[7] == json({1, 2, 3}));
  158. }
  159. SECTION("access on non-array type")
  160. {
  161. SECTION("null")
  162. {
  163. SECTION("standard tests")
  164. {
  165. json j_nonarray(json::value_t::null);
  166. const json j_nonarray_const(j_nonarray);
  167. CHECK_NOTHROW(j_nonarray[0]);
  168. CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&);
  169. CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with null");
  170. }
  171. SECTION("implicit transformation to properly filled array")
  172. {
  173. json j_nonarray;
  174. j_nonarray[3] = 42;
  175. CHECK(j_nonarray == json({nullptr, nullptr, nullptr, 42}));
  176. }
  177. }
  178. SECTION("boolean")
  179. {
  180. json j_nonarray(json::value_t::boolean);
  181. const json j_nonarray_const(j_nonarray);
  182. CHECK_THROWS_AS(j_nonarray[0], json::type_error&);
  183. CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&);
  184. CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with boolean");
  185. CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with boolean");
  186. }
  187. SECTION("string")
  188. {
  189. json j_nonarray(json::value_t::string);
  190. const json j_nonarray_const(j_nonarray);
  191. CHECK_THROWS_AS(j_nonarray[0], json::type_error&);
  192. CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&);
  193. CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with string");
  194. CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with string");
  195. }
  196. SECTION("object")
  197. {
  198. json j_nonarray(json::value_t::object);
  199. const json j_nonarray_const(j_nonarray);
  200. CHECK_THROWS_AS(j_nonarray[0], json::type_error&);
  201. CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&);
  202. CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with object");
  203. CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with object");
  204. }
  205. SECTION("number (integer)")
  206. {
  207. json j_nonarray(json::value_t::number_integer);
  208. const json j_nonarray_const(j_nonarray);
  209. CHECK_THROWS_AS(j_nonarray[0], json::type_error&);
  210. CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&);
  211. CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with number");
  212. CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with number");
  213. }
  214. SECTION("number (unsigned)")
  215. {
  216. json j_nonarray(json::value_t::number_unsigned);
  217. const json j_nonarray_const(j_nonarray);
  218. CHECK_THROWS_AS(j_nonarray[0], json::type_error&);
  219. CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&);
  220. CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with number");
  221. CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with number");
  222. }
  223. SECTION("number (floating-point)")
  224. {
  225. json j_nonarray(json::value_t::number_float);
  226. const json j_nonarray_const(j_nonarray);
  227. CHECK_THROWS_AS(j_nonarray[0], json::type_error&);
  228. CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&);
  229. CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with number");
  230. CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with number");
  231. }
  232. }
  233. }
  234. SECTION("remove specified element")
  235. {
  236. SECTION("remove element by index")
  237. {
  238. {
  239. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  240. jarray.erase(0);
  241. CHECK(jarray == json({1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
  242. }
  243. {
  244. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  245. jarray.erase(1);
  246. CHECK(jarray == json({1, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
  247. }
  248. {
  249. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  250. jarray.erase(2);
  251. CHECK(jarray == json({1, 1u, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
  252. }
  253. {
  254. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  255. jarray.erase(3);
  256. CHECK(jarray == json({1, 1u, true, "string", 42.23, json::object(), {1, 2, 3}}));
  257. }
  258. {
  259. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  260. jarray.erase(4);
  261. CHECK(jarray == json({1, 1u, true, nullptr, 42.23, json::object(), {1, 2, 3}}));
  262. }
  263. {
  264. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  265. jarray.erase(5);
  266. CHECK(jarray == json({1, 1u, true, nullptr, "string", json::object(), {1, 2, 3}}));
  267. }
  268. {
  269. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  270. jarray.erase(6);
  271. CHECK(jarray == json({1, 1u, true, nullptr, "string", 42.23, {1, 2, 3}}));
  272. }
  273. {
  274. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  275. jarray.erase(7);
  276. CHECK(jarray == json({1, 1u, true, nullptr, "string", 42.23, json::object()}));
  277. }
  278. {
  279. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  280. CHECK_THROWS_AS(jarray.erase(8), json::out_of_range&);
  281. CHECK_THROWS_WITH(jarray.erase(8),
  282. "[json.exception.out_of_range.401] array index 8 is out of range");
  283. }
  284. }
  285. SECTION("remove element by iterator")
  286. {
  287. SECTION("erase(begin())")
  288. {
  289. {
  290. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  291. json::iterator it2 = jarray.erase(jarray.begin());
  292. CHECK(jarray == json({1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
  293. CHECK(*it2 == json(1u));
  294. }
  295. {
  296. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  297. json::const_iterator it2 = jarray.erase(jarray.cbegin());
  298. CHECK(jarray == json({1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
  299. CHECK(*it2 == json(1u));
  300. }
  301. }
  302. SECTION("erase(begin(), end())")
  303. {
  304. {
  305. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  306. json::iterator it2 = jarray.erase(jarray.begin(), jarray.end());
  307. CHECK(jarray == json::array());
  308. CHECK(it2 == jarray.end());
  309. }
  310. {
  311. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  312. json::const_iterator it2 = jarray.erase(jarray.cbegin(), jarray.cend());
  313. CHECK(jarray == json::array());
  314. CHECK(it2 == jarray.cend());
  315. }
  316. }
  317. SECTION("erase(begin(), begin())")
  318. {
  319. {
  320. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  321. json::iterator it2 = jarray.erase(jarray.begin(), jarray.begin());
  322. CHECK(jarray == json({1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
  323. CHECK(*it2 == json(1));
  324. }
  325. {
  326. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  327. json::const_iterator it2 = jarray.erase(jarray.cbegin(), jarray.cbegin());
  328. CHECK(jarray == json({1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
  329. CHECK(*it2 == json(1));
  330. }
  331. }
  332. SECTION("erase at offset")
  333. {
  334. {
  335. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  336. json::iterator it = jarray.begin() + 4;
  337. json::iterator it2 = jarray.erase(it);
  338. CHECK(jarray == json({1, 1u, true, nullptr, 42.23, json::object(), {1, 2, 3}}));
  339. CHECK(*it2 == json(42.23));
  340. }
  341. {
  342. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  343. json::const_iterator it = jarray.cbegin() + 4;
  344. json::const_iterator it2 = jarray.erase(it);
  345. CHECK(jarray == json({1, 1u, true, nullptr, 42.23, json::object(), {1, 2, 3}}));
  346. CHECK(*it2 == json(42.23));
  347. }
  348. }
  349. SECTION("erase subrange")
  350. {
  351. {
  352. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  353. json::iterator it2 = jarray.erase(jarray.begin() + 3, jarray.begin() + 6);
  354. CHECK(jarray == json({1, 1u, true, json::object(), {1, 2, 3}}));
  355. CHECK(*it2 == json::object());
  356. }
  357. {
  358. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  359. json::const_iterator it2 = jarray.erase(jarray.cbegin() + 3, jarray.cbegin() + 6);
  360. CHECK(jarray == json({1, 1u, true, json::object(), {1, 2, 3}}));
  361. CHECK(*it2 == json::object());
  362. }
  363. }
  364. SECTION("different arrays")
  365. {
  366. {
  367. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  368. json jarray2 = {"foo", "bar"};
  369. CHECK_THROWS_AS(jarray.erase(jarray2.begin()), json::invalid_iterator&);
  370. CHECK_THROWS_AS(jarray.erase(jarray.begin(), jarray2.end()), json::invalid_iterator&);
  371. CHECK_THROWS_AS(jarray.erase(jarray2.begin(), jarray.end()), json::invalid_iterator&);
  372. CHECK_THROWS_AS(jarray.erase(jarray2.begin(), jarray2.end()), json::invalid_iterator&);
  373. CHECK_THROWS_WITH(jarray.erase(jarray2.begin()),
  374. "[json.exception.invalid_iterator.202] iterator does not fit current value");
  375. CHECK_THROWS_WITH(jarray.erase(jarray.begin(), jarray2.end()),
  376. "[json.exception.invalid_iterator.203] iterators do not fit current value");
  377. CHECK_THROWS_WITH(jarray.erase(jarray2.begin(), jarray.end()),
  378. "[json.exception.invalid_iterator.203] iterators do not fit current value");
  379. CHECK_THROWS_WITH(jarray.erase(jarray2.begin(), jarray2.end()),
  380. "[json.exception.invalid_iterator.203] iterators do not fit current value");
  381. }
  382. {
  383. json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
  384. json jarray2 = {"foo", "bar"};
  385. CHECK_THROWS_AS(jarray.erase(jarray2.cbegin()), json::invalid_iterator&);
  386. CHECK_THROWS_AS(jarray.erase(jarray.cbegin(), jarray2.cend()), json::invalid_iterator&);
  387. CHECK_THROWS_AS(jarray.erase(jarray2.cbegin(), jarray.cend()), json::invalid_iterator&);
  388. CHECK_THROWS_AS(jarray.erase(jarray2.cbegin(), jarray2.cend()), json::invalid_iterator&);
  389. CHECK_THROWS_WITH(jarray.erase(jarray2.cbegin()),
  390. "[json.exception.invalid_iterator.202] iterator does not fit current value");
  391. CHECK_THROWS_WITH(jarray.erase(jarray.cbegin(), jarray2.cend()),
  392. "[json.exception.invalid_iterator.203] iterators do not fit current value");
  393. CHECK_THROWS_WITH(jarray.erase(jarray2.cbegin(), jarray.cend()),
  394. "[json.exception.invalid_iterator.203] iterators do not fit current value");
  395. CHECK_THROWS_WITH(jarray.erase(jarray2.cbegin(), jarray2.cend()),
  396. "[json.exception.invalid_iterator.203] iterators do not fit current value");
  397. }
  398. }
  399. }
  400. SECTION("remove element by index in non-array type")
  401. {
  402. SECTION("null")
  403. {
  404. json j_nonobject(json::value_t::null);
  405. CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error&);
  406. CHECK_THROWS_WITH(j_nonobject.erase(0),
  407. "[json.exception.type_error.307] cannot use erase() with null");
  408. }
  409. SECTION("boolean")
  410. {
  411. json j_nonobject(json::value_t::boolean);
  412. CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error&);
  413. CHECK_THROWS_WITH(j_nonobject.erase(0),
  414. "[json.exception.type_error.307] cannot use erase() with boolean");
  415. }
  416. SECTION("string")
  417. {
  418. json j_nonobject(json::value_t::string);
  419. CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error&);
  420. CHECK_THROWS_WITH(j_nonobject.erase(0),
  421. "[json.exception.type_error.307] cannot use erase() with string");
  422. }
  423. SECTION("object")
  424. {
  425. json j_nonobject(json::value_t::object);
  426. CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error&);
  427. CHECK_THROWS_WITH(j_nonobject.erase(0),
  428. "[json.exception.type_error.307] cannot use erase() with object");
  429. }
  430. SECTION("number (integer)")
  431. {
  432. json j_nonobject(json::value_t::number_integer);
  433. CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error&);
  434. CHECK_THROWS_WITH(j_nonobject.erase(0),
  435. "[json.exception.type_error.307] cannot use erase() with number");
  436. }
  437. SECTION("number (unsigned)")
  438. {
  439. json j_nonobject(json::value_t::number_unsigned);
  440. CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error&);
  441. CHECK_THROWS_WITH(j_nonobject.erase(0),
  442. "[json.exception.type_error.307] cannot use erase() with number");
  443. }
  444. SECTION("number (floating-point)")
  445. {
  446. json j_nonobject(json::value_t::number_float);
  447. CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error&);
  448. CHECK_THROWS_WITH(j_nonobject.erase(0),
  449. "[json.exception.type_error.307] cannot use erase() with number");
  450. }
  451. }
  452. }
  453. }
  454. SECTION("other values")
  455. {
  456. SECTION("front and back")
  457. {
  458. SECTION("null")
  459. {
  460. {
  461. json j;
  462. CHECK_THROWS_AS(j.front(), json::invalid_iterator&);
  463. CHECK_THROWS_AS(j.back(), json::invalid_iterator&);
  464. CHECK_THROWS_WITH(j.front(), "[json.exception.invalid_iterator.214] cannot get value");
  465. CHECK_THROWS_WITH(j.back(), "[json.exception.invalid_iterator.214] cannot get value");
  466. }
  467. {
  468. const json j{};
  469. CHECK_THROWS_AS(j.front(), json::invalid_iterator&);
  470. CHECK_THROWS_AS(j.back(), json::invalid_iterator&);
  471. CHECK_THROWS_WITH(j.front(), "[json.exception.invalid_iterator.214] cannot get value");
  472. CHECK_THROWS_WITH(j.back(), "[json.exception.invalid_iterator.214] cannot get value");
  473. }
  474. }
  475. SECTION("string")
  476. {
  477. {
  478. json j = "foo";
  479. CHECK(j.front() == j);
  480. CHECK(j.back() == j);
  481. }
  482. {
  483. const json j = "bar";
  484. CHECK(j.front() == j);
  485. CHECK(j.back() == j);
  486. }
  487. }
  488. SECTION("number (boolean)")
  489. {
  490. {
  491. json j = false;
  492. CHECK(j.front() == j);
  493. CHECK(j.back() == j);
  494. }
  495. {
  496. const json j = true;
  497. CHECK(j.front() == j);
  498. CHECK(j.back() == j);
  499. }
  500. }
  501. SECTION("number (integer)")
  502. {
  503. {
  504. json j = 17;
  505. CHECK(j.front() == j);
  506. CHECK(j.back() == j);
  507. }
  508. {
  509. const json j = 17;
  510. CHECK(j.front() == j);
  511. CHECK(j.back() == j);
  512. }
  513. }
  514. SECTION("number (unsigned)")
  515. {
  516. {
  517. json j = 17u;
  518. CHECK(j.front() == j);
  519. CHECK(j.back() == j);
  520. }
  521. {
  522. const json j = 17u;
  523. CHECK(j.front() == j);
  524. CHECK(j.back() == j);
  525. }
  526. }
  527. SECTION("number (floating point)")
  528. {
  529. {
  530. json j = 23.42;
  531. CHECK(j.front() == j);
  532. CHECK(j.back() == j);
  533. }
  534. {
  535. const json j = 23.42;
  536. CHECK(j.front() == j);
  537. CHECK(j.back() == j);
  538. }
  539. }
  540. }
  541. SECTION("erase with one valid iterator")
  542. {
  543. SECTION("null")
  544. {
  545. {
  546. json j;
  547. CHECK_THROWS_AS(j.erase(j.begin()), json::type_error&);
  548. CHECK_THROWS_WITH(j.erase(j.begin()),
  549. "[json.exception.type_error.307] cannot use erase() with null");
  550. }
  551. {
  552. json j;
  553. CHECK_THROWS_AS(j.erase(j.cbegin()), json::type_error&);
  554. CHECK_THROWS_WITH(j.erase(j.begin()),
  555. "[json.exception.type_error.307] cannot use erase() with null");
  556. }
  557. }
  558. SECTION("string")
  559. {
  560. {
  561. json j = "foo";
  562. json::iterator it = j.erase(j.begin());
  563. CHECK(j.type() == json::value_t::null);
  564. CHECK(it == j.end());
  565. }
  566. {
  567. json j = "bar";
  568. json::const_iterator it = j.erase(j.cbegin());
  569. CHECK(j.type() == json::value_t::null);
  570. CHECK(it == j.end());
  571. }
  572. }
  573. SECTION("number (boolean)")
  574. {
  575. {
  576. json j = false;
  577. json::iterator it = j.erase(j.begin());
  578. CHECK(j.type() == json::value_t::null);
  579. CHECK(it == j.end());
  580. }
  581. {
  582. json j = true;
  583. json::const_iterator it = j.erase(j.cbegin());
  584. CHECK(j.type() == json::value_t::null);
  585. CHECK(it == j.end());
  586. }
  587. }
  588. SECTION("number (integer)")
  589. {
  590. {
  591. json j = 17;
  592. json::iterator it = j.erase(j.begin());
  593. CHECK(j.type() == json::value_t::null);
  594. CHECK(it == j.end());
  595. }
  596. {
  597. json j = 17;
  598. json::const_iterator it = j.erase(j.cbegin());
  599. CHECK(j.type() == json::value_t::null);
  600. CHECK(it == j.end());
  601. }
  602. }
  603. SECTION("number (unsigned)")
  604. {
  605. {
  606. json j = 17u;
  607. json::iterator it = j.erase(j.begin());
  608. CHECK(j.type() == json::value_t::null);
  609. CHECK(it == j.end());
  610. }
  611. {
  612. json j = 17u;
  613. json::const_iterator it = j.erase(j.cbegin());
  614. CHECK(j.type() == json::value_t::null);
  615. CHECK(it == j.end());
  616. }
  617. }
  618. SECTION("number (floating point)")
  619. {
  620. {
  621. json j = 23.42;
  622. json::iterator it = j.erase(j.begin());
  623. CHECK(j.type() == json::value_t::null);
  624. CHECK(it == j.end());
  625. }
  626. {
  627. json j = 23.42;
  628. json::const_iterator it = j.erase(j.cbegin());
  629. CHECK(j.type() == json::value_t::null);
  630. CHECK(it == j.end());
  631. }
  632. }
  633. SECTION("binary")
  634. {
  635. {
  636. json j = json::binary({1, 2, 3});
  637. json::iterator it = j.erase(j.begin());
  638. CHECK(j.type() == json::value_t::null);
  639. CHECK(it == j.end());
  640. }
  641. {
  642. json j = json::binary({1, 2, 3});
  643. json::const_iterator it = j.erase(j.cbegin());
  644. CHECK(j.type() == json::value_t::null);
  645. CHECK(it == j.end());
  646. }
  647. }
  648. }
  649. SECTION("erase with one invalid iterator")
  650. {
  651. SECTION("string")
  652. {
  653. {
  654. json j = "foo";
  655. CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator&);
  656. CHECK_THROWS_WITH(j.erase(j.end()),
  657. "[json.exception.invalid_iterator.205] iterator out of range");
  658. }
  659. {
  660. json j = "bar";
  661. CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator&);
  662. CHECK_THROWS_WITH(j.erase(j.cend()),
  663. "[json.exception.invalid_iterator.205] iterator out of range");
  664. }
  665. }
  666. SECTION("number (boolean)")
  667. {
  668. {
  669. json j = false;
  670. CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator&);
  671. CHECK_THROWS_WITH(j.erase(j.end()),
  672. "[json.exception.invalid_iterator.205] iterator out of range");
  673. }
  674. {
  675. json j = true;
  676. CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator&);
  677. CHECK_THROWS_WITH(j.erase(j.cend()),
  678. "[json.exception.invalid_iterator.205] iterator out of range");
  679. }
  680. }
  681. SECTION("number (integer)")
  682. {
  683. {
  684. json j = 17;
  685. CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator&);
  686. CHECK_THROWS_WITH(j.erase(j.end()),
  687. "[json.exception.invalid_iterator.205] iterator out of range");
  688. }
  689. {
  690. json j = 17;
  691. CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator&);
  692. CHECK_THROWS_WITH(j.erase(j.cend()),
  693. "[json.exception.invalid_iterator.205] iterator out of range");
  694. }
  695. }
  696. SECTION("number (unsigned)")
  697. {
  698. {
  699. json j = 17u;
  700. CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator&);
  701. CHECK_THROWS_WITH(j.erase(j.end()),
  702. "[json.exception.invalid_iterator.205] iterator out of range");
  703. }
  704. {
  705. json j = 17u;
  706. CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator&);
  707. CHECK_THROWS_WITH(j.erase(j.cend()),
  708. "[json.exception.invalid_iterator.205] iterator out of range");
  709. }
  710. }
  711. SECTION("number (floating point)")
  712. {
  713. {
  714. json j = 23.42;
  715. CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator&);
  716. CHECK_THROWS_WITH(j.erase(j.end()),
  717. "[json.exception.invalid_iterator.205] iterator out of range");
  718. }
  719. {
  720. json j = 23.42;
  721. CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator&);
  722. CHECK_THROWS_WITH(j.erase(j.cend()),
  723. "[json.exception.invalid_iterator.205] iterator out of range");
  724. }
  725. }
  726. }
  727. SECTION("erase with two valid iterators")
  728. {
  729. SECTION("null")
  730. {
  731. {
  732. json j;
  733. CHECK_THROWS_AS(j.erase(j.begin(), j.end()), json::type_error&);
  734. CHECK_THROWS_WITH(j.erase(j.begin(), j.end()),
  735. "[json.exception.type_error.307] cannot use erase() with null");
  736. }
  737. {
  738. json j;
  739. CHECK_THROWS_AS(j.erase(j.cbegin(), j.cend()), json::type_error&);
  740. CHECK_THROWS_WITH(j.erase(j.cbegin(), j.cend()),
  741. "[json.exception.type_error.307] cannot use erase() with null");
  742. }
  743. }
  744. SECTION("string")
  745. {
  746. {
  747. json j = "foo";
  748. json::iterator it = j.erase(j.begin(), j.end());
  749. CHECK(j.type() == json::value_t::null);
  750. CHECK(it == j.end());
  751. }
  752. {
  753. json j = "bar";
  754. json::const_iterator it = j.erase(j.cbegin(), j.cend());
  755. CHECK(j.type() == json::value_t::null);
  756. CHECK(it == j.end());
  757. }
  758. }
  759. SECTION("number (boolean)")
  760. {
  761. {
  762. json j = false;
  763. json::iterator it = j.erase(j.begin(), j.end());
  764. CHECK(j.type() == json::value_t::null);
  765. CHECK(it == j.end());
  766. }
  767. {
  768. json j = true;
  769. json::const_iterator it = j.erase(j.cbegin(), j.cend());
  770. CHECK(j.type() == json::value_t::null);
  771. CHECK(it == j.end());
  772. }
  773. }
  774. SECTION("number (integer)")
  775. {
  776. {
  777. json j = 17;
  778. json::iterator it = j.erase(j.begin(), j.end());
  779. CHECK(j.type() == json::value_t::null);
  780. CHECK(it == j.end());
  781. }
  782. {
  783. json j = 17;
  784. json::const_iterator it = j.erase(j.cbegin(), j.cend());
  785. CHECK(j.type() == json::value_t::null);
  786. CHECK(it == j.end());
  787. }
  788. }
  789. SECTION("number (unsigned)")
  790. {
  791. {
  792. json j = 17u;
  793. json::iterator it = j.erase(j.begin(), j.end());
  794. CHECK(j.type() == json::value_t::null);
  795. CHECK(it == j.end());
  796. }
  797. {
  798. json j = 17u;
  799. json::const_iterator it = j.erase(j.cbegin(), j.cend());
  800. CHECK(j.type() == json::value_t::null);
  801. CHECK(it == j.end());
  802. }
  803. }
  804. SECTION("number (floating point)")
  805. {
  806. {
  807. json j = 23.42;
  808. json::iterator it = j.erase(j.begin(), j.end());
  809. CHECK(j.type() == json::value_t::null);
  810. CHECK(it == j.end());
  811. }
  812. {
  813. json j = 23.42;
  814. json::const_iterator it = j.erase(j.cbegin(), j.cend());
  815. CHECK(j.type() == json::value_t::null);
  816. CHECK(it == j.end());
  817. }
  818. }
  819. SECTION("binary")
  820. {
  821. {
  822. json j = json::binary({1, 2, 3});
  823. json::iterator it = j.erase(j.begin(), j.end());
  824. CHECK(j.type() == json::value_t::null);
  825. CHECK(it == j.end());
  826. }
  827. {
  828. json j = json::binary({1, 2, 3});
  829. json::const_iterator it = j.erase(j.cbegin(), j.cend());
  830. CHECK(j.type() == json::value_t::null);
  831. CHECK(it == j.end());
  832. }
  833. }
  834. }
  835. SECTION("erase with two invalid iterators")
  836. {
  837. SECTION("string")
  838. {
  839. {
  840. json j = "foo";
  841. CHECK_THROWS_AS(j.erase(j.end(), j.end()), json::invalid_iterator&);
  842. CHECK_THROWS_AS(j.erase(j.begin(), j.begin()), json::invalid_iterator&);
  843. CHECK_THROWS_WITH(j.erase(j.end(), j.end()), "[json.exception.invalid_iterator.204] iterators out of range");
  844. CHECK_THROWS_WITH(j.erase(j.begin(), j.begin()), "[json.exception.invalid_iterator.204] iterators out of range");
  845. }
  846. {
  847. json j = "bar";
  848. CHECK_THROWS_AS(j.erase(j.cend(), j.cend()), json::invalid_iterator&);
  849. CHECK_THROWS_AS(j.erase(j.cbegin(), j.cbegin()), json::invalid_iterator&);
  850. CHECK_THROWS_WITH(j.erase(j.cend(), j.cend()), "[json.exception.invalid_iterator.204] iterators out of range");
  851. CHECK_THROWS_WITH(j.erase(j.cbegin(), j.cbegin()), "[json.exception.invalid_iterator.204] iterators out of range");
  852. }
  853. }
  854. SECTION("number (boolean)")
  855. {
  856. {
  857. json j = false;
  858. CHECK_THROWS_AS(j.erase(j.end(), j.end()), json::invalid_iterator&);
  859. CHECK_THROWS_AS(j.erase(j.begin(), j.begin()), json::invalid_iterator&);
  860. CHECK_THROWS_WITH(j.erase(j.end(), j.end()), "[json.exception.invalid_iterator.204] iterators out of range");
  861. CHECK_THROWS_WITH(j.erase(j.begin(), j.begin()), "[json.exception.invalid_iterator.204] iterators out of range");
  862. }
  863. {
  864. json j = true;
  865. CHECK_THROWS_AS(j.erase(j.cend(), j.cend()), json::invalid_iterator&);
  866. CHECK_THROWS_AS(j.erase(j.cbegin(), j.cbegin()), json::invalid_iterator&);
  867. CHECK_THROWS_WITH(j.erase(j.cend(), j.cend()), "[json.exception.invalid_iterator.204] iterators out of range");
  868. CHECK_THROWS_WITH(j.erase(j.cbegin(), j.cbegin()), "[json.exception.invalid_iterator.204] iterators out of range");
  869. }
  870. }
  871. SECTION("number (integer)")
  872. {
  873. {
  874. json j = 17;
  875. CHECK_THROWS_AS(j.erase(j.end(), j.end()), json::invalid_iterator&);
  876. CHECK_THROWS_AS(j.erase(j.begin(), j.begin()), json::invalid_iterator&);
  877. CHECK_THROWS_WITH(j.erase(j.end(), j.end()), "[json.exception.invalid_iterator.204] iterators out of range");
  878. CHECK_THROWS_WITH(j.erase(j.begin(), j.begin()), "[json.exception.invalid_iterator.204] iterators out of range");
  879. }
  880. {
  881. json j = 17;
  882. CHECK_THROWS_AS(j.erase(j.cend(), j.cend()), json::invalid_iterator&);
  883. CHECK_THROWS_AS(j.erase(j.cbegin(), j.cbegin()), json::invalid_iterator&);
  884. CHECK_THROWS_WITH(j.erase(j.cend(), j.cend()), "[json.exception.invalid_iterator.204] iterators out of range");
  885. CHECK_THROWS_WITH(j.erase(j.cbegin(), j.cbegin()), "[json.exception.invalid_iterator.204] iterators out of range");
  886. }
  887. }
  888. SECTION("number (unsigned)")
  889. {
  890. {
  891. json j = 17u;
  892. CHECK_THROWS_AS(j.erase(j.end(), j.end()), json::invalid_iterator&);
  893. CHECK_THROWS_AS(j.erase(j.begin(), j.begin()), json::invalid_iterator&);
  894. CHECK_THROWS_WITH(j.erase(j.end(), j.end()), "[json.exception.invalid_iterator.204] iterators out of range");
  895. CHECK_THROWS_WITH(j.erase(j.begin(), j.begin()), "[json.exception.invalid_iterator.204] iterators out of range");
  896. }
  897. {
  898. json j = 17u;
  899. CHECK_THROWS_AS(j.erase(j.cend(), j.cend()), json::invalid_iterator&);
  900. CHECK_THROWS_AS(j.erase(j.cbegin(), j.cbegin()), json::invalid_iterator&);
  901. CHECK_THROWS_WITH(j.erase(j.cend(), j.cend()), "[json.exception.invalid_iterator.204] iterators out of range");
  902. CHECK_THROWS_WITH(j.erase(j.cbegin(), j.cbegin()), "[json.exception.invalid_iterator.204] iterators out of range");
  903. }
  904. }
  905. SECTION("number (floating point)")
  906. {
  907. {
  908. json j = 23.42;
  909. CHECK_THROWS_AS(j.erase(j.end(), j.end()), json::invalid_iterator&);
  910. CHECK_THROWS_AS(j.erase(j.begin(), j.begin()), json::invalid_iterator&);
  911. CHECK_THROWS_WITH(j.erase(j.end(), j.end()), "[json.exception.invalid_iterator.204] iterators out of range");
  912. CHECK_THROWS_WITH(j.erase(j.begin(), j.begin()), "[json.exception.invalid_iterator.204] iterators out of range");
  913. }
  914. {
  915. json j = 23.42;
  916. CHECK_THROWS_AS(j.erase(j.cend(), j.cend()), json::invalid_iterator&);
  917. CHECK_THROWS_AS(j.erase(j.cbegin(), j.cbegin()), json::invalid_iterator&);
  918. CHECK_THROWS_WITH(j.erase(j.cend(), j.cend()), "[json.exception.invalid_iterator.204] iterators out of range");
  919. CHECK_THROWS_WITH(j.erase(j.cbegin(), j.cbegin()), "[json.exception.invalid_iterator.204] iterators out of range");
  920. }
  921. }
  922. }
  923. }
  924. }