1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006 |
- /*
- __ _____ _____ _____
- __| | __| | | | JSON for Modern C++ (test suite)
- | | |__ | | | | | | version 3.7.3
- |_____|_____|_____|_|___| https://github.com/nlohmann/json
- Licensed under the MIT License <http://opensource.org/licenses/MIT>.
- SPDX-License-Identifier: MIT
- Copyright (c) 2013-2019 Niels Lohmann <http://nlohmann.me>.
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- */
- #include "doctest_compatibility.h"
- #include <nlohmann/json.hpp>
- using nlohmann::json;
- TEST_CASE("element access 1")
- {
- SECTION("array")
- {
- json j = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- const json j_const = j;
- SECTION("access specified element with bounds checking")
- {
- SECTION("access within bounds")
- {
- CHECK(j.at(0) == json(1));
- CHECK(j.at(1) == json(1u));
- CHECK(j.at(2) == json(true));
- CHECK(j.at(3) == json(nullptr));
- CHECK(j.at(4) == json("string"));
- CHECK(j.at(5) == json(42.23));
- CHECK(j.at(6) == json::object());
- CHECK(j.at(7) == json({1, 2, 3}));
- CHECK(j_const.at(0) == json(1));
- CHECK(j_const.at(1) == json(1u));
- CHECK(j_const.at(2) == json(true));
- CHECK(j_const.at(3) == json(nullptr));
- CHECK(j_const.at(4) == json("string"));
- CHECK(j_const.at(5) == json(42.23));
- CHECK(j_const.at(6) == json::object());
- CHECK(j_const.at(7) == json({1, 2, 3}));
- }
- SECTION("access outside bounds")
- {
- CHECK_THROWS_AS(j.at(8), json::out_of_range&);
- CHECK_THROWS_AS(j_const.at(8), json::out_of_range&);
- CHECK_THROWS_WITH(j.at(8),
- "[json.exception.out_of_range.401] array index 8 is out of range");
- CHECK_THROWS_WITH(j_const.at(8),
- "[json.exception.out_of_range.401] array index 8 is out of range");
- }
- SECTION("access on non-array type")
- {
- SECTION("null")
- {
- json j_nonarray(json::value_t::null);
- const json j_nonarray_const(j_nonarray);
- CHECK_THROWS_AS(j_nonarray.at(0), json::type_error&);
- CHECK_THROWS_AS(j_nonarray_const.at(0), json::type_error&);
- CHECK_THROWS_WITH(j_nonarray.at(0), "[json.exception.type_error.304] cannot use at() with null");
- CHECK_THROWS_WITH(j_nonarray_const.at(0), "[json.exception.type_error.304] cannot use at() with null");
- }
- SECTION("boolean")
- {
- json j_nonarray(json::value_t::boolean);
- const json j_nonarray_const(j_nonarray);
- CHECK_THROWS_AS(j_nonarray.at(0), json::type_error&);
- CHECK_THROWS_AS(j_nonarray_const.at(0), json::type_error&);
- CHECK_THROWS_WITH(j_nonarray.at(0), "[json.exception.type_error.304] cannot use at() with boolean");
- CHECK_THROWS_WITH(j_nonarray_const.at(0), "[json.exception.type_error.304] cannot use at() with boolean");
- }
- SECTION("string")
- {
- json j_nonarray(json::value_t::string);
- const json j_nonarray_const(j_nonarray);
- CHECK_THROWS_AS(j_nonarray.at(0), json::type_error&);
- CHECK_THROWS_AS(j_nonarray_const.at(0), json::type_error&);
- CHECK_THROWS_WITH(j_nonarray.at(0), "[json.exception.type_error.304] cannot use at() with string");
- CHECK_THROWS_WITH(j_nonarray_const.at(0), "[json.exception.type_error.304] cannot use at() with string");
- }
- SECTION("object")
- {
- json j_nonarray(json::value_t::object);
- const json j_nonarray_const(j_nonarray);
- CHECK_THROWS_AS(j_nonarray.at(0), json::type_error&);
- CHECK_THROWS_AS(j_nonarray_const.at(0), json::type_error&);
- CHECK_THROWS_WITH(j_nonarray.at(0), "[json.exception.type_error.304] cannot use at() with object");
- CHECK_THROWS_WITH(j_nonarray_const.at(0), "[json.exception.type_error.304] cannot use at() with object");
- }
- SECTION("number (integer)")
- {
- json j_nonarray(json::value_t::number_integer);
- const json j_nonarray_const(j_nonarray);
- CHECK_THROWS_AS(j_nonarray.at(0), json::type_error&);
- CHECK_THROWS_AS(j_nonarray_const.at(0), json::type_error&);
- CHECK_THROWS_WITH(j_nonarray.at(0), "[json.exception.type_error.304] cannot use at() with number");
- CHECK_THROWS_WITH(j_nonarray_const.at(0), "[json.exception.type_error.304] cannot use at() with number");
- }
- SECTION("number (unsigned)")
- {
- json j_nonarray(json::value_t::number_unsigned);
- const json j_nonarray_const(j_nonarray);
- CHECK_THROWS_AS(j_nonarray.at(0), json::type_error&);
- CHECK_THROWS_AS(j_nonarray_const.at(0), json::type_error&);
- CHECK_THROWS_WITH(j_nonarray.at(0), "[json.exception.type_error.304] cannot use at() with number");
- CHECK_THROWS_WITH(j_nonarray_const.at(0), "[json.exception.type_error.304] cannot use at() with number");
- }
- SECTION("number (floating-point)")
- {
- json j_nonarray(json::value_t::number_float);
- const json j_nonarray_const(j_nonarray);
- CHECK_THROWS_AS(j_nonarray.at(0), json::type_error&);
- CHECK_THROWS_AS(j_nonarray_const.at(0), json::type_error&);
- CHECK_THROWS_WITH(j_nonarray.at(0), "[json.exception.type_error.304] cannot use at() with number");
- CHECK_THROWS_WITH(j_nonarray_const.at(0), "[json.exception.type_error.304] cannot use at() with number");
- }
- }
- }
- SECTION("front and back")
- {
- CHECK(j.front() == json(1));
- CHECK(j_const.front() == json(1));
- CHECK(j.back() == json({1, 2, 3}));
- CHECK(j_const.back() == json({1, 2, 3}));
- }
- SECTION("access specified element")
- {
- SECTION("access within bounds")
- {
- CHECK(j[0] == json(1));
- CHECK(j[1] == json(1u));
- CHECK(j[2] == json(true));
- CHECK(j[3] == json(nullptr));
- CHECK(j[4] == json("string"));
- CHECK(j[5] == json(42.23));
- CHECK(j[6] == json::object());
- CHECK(j[7] == json({1, 2, 3}));
- CHECK(j_const[0] == json(1));
- CHECK(j_const[1] == json(1u));
- CHECK(j_const[2] == json(true));
- CHECK(j_const[3] == json(nullptr));
- CHECK(j_const[4] == json("string"));
- CHECK(j_const[5] == json(42.23));
- CHECK(j_const[6] == json::object());
- CHECK(j_const[7] == json({1, 2, 3}));
- }
- SECTION("access on non-array type")
- {
- SECTION("null")
- {
- SECTION("standard tests")
- {
- json j_nonarray(json::value_t::null);
- const json j_nonarray_const(j_nonarray);
- CHECK_NOTHROW(j_nonarray[0]);
- CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&);
- CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with null");
- }
- SECTION("implicit transformation to properly filled array")
- {
- json j_nonarray;
- j_nonarray[3] = 42;
- CHECK(j_nonarray == json({nullptr, nullptr, nullptr, 42}));
- }
- }
- SECTION("boolean")
- {
- json j_nonarray(json::value_t::boolean);
- const json j_nonarray_const(j_nonarray);
- CHECK_THROWS_AS(j_nonarray[0], json::type_error&);
- CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&);
- CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with boolean");
- CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with boolean");
- }
- SECTION("string")
- {
- json j_nonarray(json::value_t::string);
- const json j_nonarray_const(j_nonarray);
- CHECK_THROWS_AS(j_nonarray[0], json::type_error&);
- CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&);
- CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with string");
- CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with string");
- }
- SECTION("object")
- {
- json j_nonarray(json::value_t::object);
- const json j_nonarray_const(j_nonarray);
- CHECK_THROWS_AS(j_nonarray[0], json::type_error&);
- CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&);
- CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with object");
- CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with object");
- }
- SECTION("number (integer)")
- {
- json j_nonarray(json::value_t::number_integer);
- const json j_nonarray_const(j_nonarray);
- CHECK_THROWS_AS(j_nonarray[0], json::type_error&);
- CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&);
- CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with number");
- CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with number");
- }
- SECTION("number (unsigned)")
- {
- json j_nonarray(json::value_t::number_unsigned);
- const json j_nonarray_const(j_nonarray);
- CHECK_THROWS_AS(j_nonarray[0], json::type_error&);
- CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&);
- CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with number");
- CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with number");
- }
- SECTION("number (floating-point)")
- {
- json j_nonarray(json::value_t::number_float);
- const json j_nonarray_const(j_nonarray);
- CHECK_THROWS_AS(j_nonarray[0], json::type_error&);
- CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&);
- CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with number");
- CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with number");
- }
- }
- }
- SECTION("remove specified element")
- {
- SECTION("remove element by index")
- {
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- jarray.erase(0);
- CHECK(jarray == json({1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
- }
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- jarray.erase(1);
- CHECK(jarray == json({1, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
- }
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- jarray.erase(2);
- CHECK(jarray == json({1, 1u, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
- }
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- jarray.erase(3);
- CHECK(jarray == json({1, 1u, true, "string", 42.23, json::object(), {1, 2, 3}}));
- }
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- jarray.erase(4);
- CHECK(jarray == json({1, 1u, true, nullptr, 42.23, json::object(), {1, 2, 3}}));
- }
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- jarray.erase(5);
- CHECK(jarray == json({1, 1u, true, nullptr, "string", json::object(), {1, 2, 3}}));
- }
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- jarray.erase(6);
- CHECK(jarray == json({1, 1u, true, nullptr, "string", 42.23, {1, 2, 3}}));
- }
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- jarray.erase(7);
- CHECK(jarray == json({1, 1u, true, nullptr, "string", 42.23, json::object()}));
- }
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- CHECK_THROWS_AS(jarray.erase(8), json::out_of_range&);
- CHECK_THROWS_WITH(jarray.erase(8),
- "[json.exception.out_of_range.401] array index 8 is out of range");
- }
- }
- SECTION("remove element by iterator")
- {
- SECTION("erase(begin())")
- {
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- json::iterator it2 = jarray.erase(jarray.begin());
- CHECK(jarray == json({1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
- CHECK(*it2 == json(1u));
- }
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- json::const_iterator it2 = jarray.erase(jarray.cbegin());
- CHECK(jarray == json({1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
- CHECK(*it2 == json(1u));
- }
- }
- SECTION("erase(begin(), end())")
- {
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- json::iterator it2 = jarray.erase(jarray.begin(), jarray.end());
- CHECK(jarray == json::array());
- CHECK(it2 == jarray.end());
- }
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- json::const_iterator it2 = jarray.erase(jarray.cbegin(), jarray.cend());
- CHECK(jarray == json::array());
- CHECK(it2 == jarray.cend());
- }
- }
- SECTION("erase(begin(), begin())")
- {
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- json::iterator it2 = jarray.erase(jarray.begin(), jarray.begin());
- CHECK(jarray == json({1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
- CHECK(*it2 == json(1));
- }
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- json::const_iterator it2 = jarray.erase(jarray.cbegin(), jarray.cbegin());
- CHECK(jarray == json({1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
- CHECK(*it2 == json(1));
- }
- }
- SECTION("erase at offset")
- {
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- json::iterator it = jarray.begin() + 4;
- json::iterator it2 = jarray.erase(it);
- CHECK(jarray == json({1, 1u, true, nullptr, 42.23, json::object(), {1, 2, 3}}));
- CHECK(*it2 == json(42.23));
- }
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- json::const_iterator it = jarray.cbegin() + 4;
- json::const_iterator it2 = jarray.erase(it);
- CHECK(jarray == json({1, 1u, true, nullptr, 42.23, json::object(), {1, 2, 3}}));
- CHECK(*it2 == json(42.23));
- }
- }
- SECTION("erase subrange")
- {
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- json::iterator it2 = jarray.erase(jarray.begin() + 3, jarray.begin() + 6);
- CHECK(jarray == json({1, 1u, true, json::object(), {1, 2, 3}}));
- CHECK(*it2 == json::object());
- }
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- json::const_iterator it2 = jarray.erase(jarray.cbegin() + 3, jarray.cbegin() + 6);
- CHECK(jarray == json({1, 1u, true, json::object(), {1, 2, 3}}));
- CHECK(*it2 == json::object());
- }
- }
- SECTION("different arrays")
- {
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- json jarray2 = {"foo", "bar"};
- CHECK_THROWS_AS(jarray.erase(jarray2.begin()), json::invalid_iterator&);
- CHECK_THROWS_AS(jarray.erase(jarray.begin(), jarray2.end()), json::invalid_iterator&);
- CHECK_THROWS_AS(jarray.erase(jarray2.begin(), jarray.end()), json::invalid_iterator&);
- CHECK_THROWS_AS(jarray.erase(jarray2.begin(), jarray2.end()), json::invalid_iterator&);
- CHECK_THROWS_WITH(jarray.erase(jarray2.begin()),
- "[json.exception.invalid_iterator.202] iterator does not fit current value");
- CHECK_THROWS_WITH(jarray.erase(jarray.begin(), jarray2.end()),
- "[json.exception.invalid_iterator.203] iterators do not fit current value");
- CHECK_THROWS_WITH(jarray.erase(jarray2.begin(), jarray.end()),
- "[json.exception.invalid_iterator.203] iterators do not fit current value");
- CHECK_THROWS_WITH(jarray.erase(jarray2.begin(), jarray2.end()),
- "[json.exception.invalid_iterator.203] iterators do not fit current value");
- }
- {
- json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
- json jarray2 = {"foo", "bar"};
- CHECK_THROWS_AS(jarray.erase(jarray2.cbegin()), json::invalid_iterator&);
- CHECK_THROWS_AS(jarray.erase(jarray.cbegin(), jarray2.cend()), json::invalid_iterator&);
- CHECK_THROWS_AS(jarray.erase(jarray2.cbegin(), jarray.cend()), json::invalid_iterator&);
- CHECK_THROWS_AS(jarray.erase(jarray2.cbegin(), jarray2.cend()), json::invalid_iterator&);
- CHECK_THROWS_WITH(jarray.erase(jarray2.cbegin()),
- "[json.exception.invalid_iterator.202] iterator does not fit current value");
- CHECK_THROWS_WITH(jarray.erase(jarray.cbegin(), jarray2.cend()),
- "[json.exception.invalid_iterator.203] iterators do not fit current value");
- CHECK_THROWS_WITH(jarray.erase(jarray2.cbegin(), jarray.cend()),
- "[json.exception.invalid_iterator.203] iterators do not fit current value");
- CHECK_THROWS_WITH(jarray.erase(jarray2.cbegin(), jarray2.cend()),
- "[json.exception.invalid_iterator.203] iterators do not fit current value");
- }
- }
- }
- SECTION("remove element by index in non-array type")
- {
- SECTION("null")
- {
- json j_nonobject(json::value_t::null);
- CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error&);
- CHECK_THROWS_WITH(j_nonobject.erase(0),
- "[json.exception.type_error.307] cannot use erase() with null");
- }
- SECTION("boolean")
- {
- json j_nonobject(json::value_t::boolean);
- CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error&);
- CHECK_THROWS_WITH(j_nonobject.erase(0),
- "[json.exception.type_error.307] cannot use erase() with boolean");
- }
- SECTION("string")
- {
- json j_nonobject(json::value_t::string);
- CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error&);
- CHECK_THROWS_WITH(j_nonobject.erase(0),
- "[json.exception.type_error.307] cannot use erase() with string");
- }
- SECTION("object")
- {
- json j_nonobject(json::value_t::object);
- CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error&);
- CHECK_THROWS_WITH(j_nonobject.erase(0),
- "[json.exception.type_error.307] cannot use erase() with object");
- }
- SECTION("number (integer)")
- {
- json j_nonobject(json::value_t::number_integer);
- CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error&);
- CHECK_THROWS_WITH(j_nonobject.erase(0),
- "[json.exception.type_error.307] cannot use erase() with number");
- }
- SECTION("number (unsigned)")
- {
- json j_nonobject(json::value_t::number_unsigned);
- CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error&);
- CHECK_THROWS_WITH(j_nonobject.erase(0),
- "[json.exception.type_error.307] cannot use erase() with number");
- }
- SECTION("number (floating-point)")
- {
- json j_nonobject(json::value_t::number_float);
- CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error&);
- CHECK_THROWS_WITH(j_nonobject.erase(0),
- "[json.exception.type_error.307] cannot use erase() with number");
- }
- }
- }
- }
- SECTION("other values")
- {
- SECTION("front and back")
- {
- SECTION("null")
- {
- {
- json j;
- CHECK_THROWS_AS(j.front(), json::invalid_iterator&);
- CHECK_THROWS_AS(j.back(), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.front(), "[json.exception.invalid_iterator.214] cannot get value");
- CHECK_THROWS_WITH(j.back(), "[json.exception.invalid_iterator.214] cannot get value");
- }
- {
- const json j{};
- CHECK_THROWS_AS(j.front(), json::invalid_iterator&);
- CHECK_THROWS_AS(j.back(), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.front(), "[json.exception.invalid_iterator.214] cannot get value");
- CHECK_THROWS_WITH(j.back(), "[json.exception.invalid_iterator.214] cannot get value");
- }
- }
- SECTION("string")
- {
- {
- json j = "foo";
- CHECK(j.front() == j);
- CHECK(j.back() == j);
- }
- {
- const json j = "bar";
- CHECK(j.front() == j);
- CHECK(j.back() == j);
- }
- }
- SECTION("number (boolean)")
- {
- {
- json j = false;
- CHECK(j.front() == j);
- CHECK(j.back() == j);
- }
- {
- const json j = true;
- CHECK(j.front() == j);
- CHECK(j.back() == j);
- }
- }
- SECTION("number (integer)")
- {
- {
- json j = 17;
- CHECK(j.front() == j);
- CHECK(j.back() == j);
- }
- {
- const json j = 17;
- CHECK(j.front() == j);
- CHECK(j.back() == j);
- }
- }
- SECTION("number (unsigned)")
- {
- {
- json j = 17u;
- CHECK(j.front() == j);
- CHECK(j.back() == j);
- }
- {
- const json j = 17u;
- CHECK(j.front() == j);
- CHECK(j.back() == j);
- }
- }
- SECTION("number (floating point)")
- {
- {
- json j = 23.42;
- CHECK(j.front() == j);
- CHECK(j.back() == j);
- }
- {
- const json j = 23.42;
- CHECK(j.front() == j);
- CHECK(j.back() == j);
- }
- }
- }
- SECTION("erase with one valid iterator")
- {
- SECTION("null")
- {
- {
- json j;
- CHECK_THROWS_AS(j.erase(j.begin()), json::type_error&);
- CHECK_THROWS_WITH(j.erase(j.begin()),
- "[json.exception.type_error.307] cannot use erase() with null");
- }
- {
- json j;
- CHECK_THROWS_AS(j.erase(j.cbegin()), json::type_error&);
- CHECK_THROWS_WITH(j.erase(j.begin()),
- "[json.exception.type_error.307] cannot use erase() with null");
- }
- }
- SECTION("string")
- {
- {
- json j = "foo";
- json::iterator it = j.erase(j.begin());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- {
- json j = "bar";
- json::const_iterator it = j.erase(j.cbegin());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- }
- SECTION("number (boolean)")
- {
- {
- json j = false;
- json::iterator it = j.erase(j.begin());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- {
- json j = true;
- json::const_iterator it = j.erase(j.cbegin());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- }
- SECTION("number (integer)")
- {
- {
- json j = 17;
- json::iterator it = j.erase(j.begin());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- {
- json j = 17;
- json::const_iterator it = j.erase(j.cbegin());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- }
- SECTION("number (unsigned)")
- {
- {
- json j = 17u;
- json::iterator it = j.erase(j.begin());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- {
- json j = 17u;
- json::const_iterator it = j.erase(j.cbegin());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- }
- SECTION("number (floating point)")
- {
- {
- json j = 23.42;
- json::iterator it = j.erase(j.begin());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- {
- json j = 23.42;
- json::const_iterator it = j.erase(j.cbegin());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- }
- SECTION("binary")
- {
- {
- json j = json::binary({1, 2, 3});
- json::iterator it = j.erase(j.begin());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- {
- json j = json::binary({1, 2, 3});
- json::const_iterator it = j.erase(j.cbegin());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- }
- }
- SECTION("erase with one invalid iterator")
- {
- SECTION("string")
- {
- {
- json j = "foo";
- CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.end()),
- "[json.exception.invalid_iterator.205] iterator out of range");
- }
- {
- json j = "bar";
- CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.cend()),
- "[json.exception.invalid_iterator.205] iterator out of range");
- }
- }
- SECTION("number (boolean)")
- {
- {
- json j = false;
- CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.end()),
- "[json.exception.invalid_iterator.205] iterator out of range");
- }
- {
- json j = true;
- CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.cend()),
- "[json.exception.invalid_iterator.205] iterator out of range");
- }
- }
- SECTION("number (integer)")
- {
- {
- json j = 17;
- CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.end()),
- "[json.exception.invalid_iterator.205] iterator out of range");
- }
- {
- json j = 17;
- CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.cend()),
- "[json.exception.invalid_iterator.205] iterator out of range");
- }
- }
- SECTION("number (unsigned)")
- {
- {
- json j = 17u;
- CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.end()),
- "[json.exception.invalid_iterator.205] iterator out of range");
- }
- {
- json j = 17u;
- CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.cend()),
- "[json.exception.invalid_iterator.205] iterator out of range");
- }
- }
- SECTION("number (floating point)")
- {
- {
- json j = 23.42;
- CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.end()),
- "[json.exception.invalid_iterator.205] iterator out of range");
- }
- {
- json j = 23.42;
- CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.cend()),
- "[json.exception.invalid_iterator.205] iterator out of range");
- }
- }
- }
- SECTION("erase with two valid iterators")
- {
- SECTION("null")
- {
- {
- json j;
- CHECK_THROWS_AS(j.erase(j.begin(), j.end()), json::type_error&);
- CHECK_THROWS_WITH(j.erase(j.begin(), j.end()),
- "[json.exception.type_error.307] cannot use erase() with null");
- }
- {
- json j;
- CHECK_THROWS_AS(j.erase(j.cbegin(), j.cend()), json::type_error&);
- CHECK_THROWS_WITH(j.erase(j.cbegin(), j.cend()),
- "[json.exception.type_error.307] cannot use erase() with null");
- }
- }
- SECTION("string")
- {
- {
- json j = "foo";
- json::iterator it = j.erase(j.begin(), j.end());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- {
- json j = "bar";
- json::const_iterator it = j.erase(j.cbegin(), j.cend());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- }
- SECTION("number (boolean)")
- {
- {
- json j = false;
- json::iterator it = j.erase(j.begin(), j.end());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- {
- json j = true;
- json::const_iterator it = j.erase(j.cbegin(), j.cend());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- }
- SECTION("number (integer)")
- {
- {
- json j = 17;
- json::iterator it = j.erase(j.begin(), j.end());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- {
- json j = 17;
- json::const_iterator it = j.erase(j.cbegin(), j.cend());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- }
- SECTION("number (unsigned)")
- {
- {
- json j = 17u;
- json::iterator it = j.erase(j.begin(), j.end());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- {
- json j = 17u;
- json::const_iterator it = j.erase(j.cbegin(), j.cend());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- }
- SECTION("number (floating point)")
- {
- {
- json j = 23.42;
- json::iterator it = j.erase(j.begin(), j.end());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- {
- json j = 23.42;
- json::const_iterator it = j.erase(j.cbegin(), j.cend());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- }
- SECTION("binary")
- {
- {
- json j = json::binary({1, 2, 3});
- json::iterator it = j.erase(j.begin(), j.end());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- {
- json j = json::binary({1, 2, 3});
- json::const_iterator it = j.erase(j.cbegin(), j.cend());
- CHECK(j.type() == json::value_t::null);
- CHECK(it == j.end());
- }
- }
- }
- SECTION("erase with two invalid iterators")
- {
- SECTION("string")
- {
- {
- json j = "foo";
- CHECK_THROWS_AS(j.erase(j.end(), j.end()), json::invalid_iterator&);
- CHECK_THROWS_AS(j.erase(j.begin(), j.begin()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.end(), j.end()), "[json.exception.invalid_iterator.204] iterators out of range");
- CHECK_THROWS_WITH(j.erase(j.begin(), j.begin()), "[json.exception.invalid_iterator.204] iterators out of range");
- }
- {
- json j = "bar";
- CHECK_THROWS_AS(j.erase(j.cend(), j.cend()), json::invalid_iterator&);
- CHECK_THROWS_AS(j.erase(j.cbegin(), j.cbegin()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.cend(), j.cend()), "[json.exception.invalid_iterator.204] iterators out of range");
- CHECK_THROWS_WITH(j.erase(j.cbegin(), j.cbegin()), "[json.exception.invalid_iterator.204] iterators out of range");
- }
- }
- SECTION("number (boolean)")
- {
- {
- json j = false;
- CHECK_THROWS_AS(j.erase(j.end(), j.end()), json::invalid_iterator&);
- CHECK_THROWS_AS(j.erase(j.begin(), j.begin()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.end(), j.end()), "[json.exception.invalid_iterator.204] iterators out of range");
- CHECK_THROWS_WITH(j.erase(j.begin(), j.begin()), "[json.exception.invalid_iterator.204] iterators out of range");
- }
- {
- json j = true;
- CHECK_THROWS_AS(j.erase(j.cend(), j.cend()), json::invalid_iterator&);
- CHECK_THROWS_AS(j.erase(j.cbegin(), j.cbegin()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.cend(), j.cend()), "[json.exception.invalid_iterator.204] iterators out of range");
- CHECK_THROWS_WITH(j.erase(j.cbegin(), j.cbegin()), "[json.exception.invalid_iterator.204] iterators out of range");
- }
- }
- SECTION("number (integer)")
- {
- {
- json j = 17;
- CHECK_THROWS_AS(j.erase(j.end(), j.end()), json::invalid_iterator&);
- CHECK_THROWS_AS(j.erase(j.begin(), j.begin()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.end(), j.end()), "[json.exception.invalid_iterator.204] iterators out of range");
- CHECK_THROWS_WITH(j.erase(j.begin(), j.begin()), "[json.exception.invalid_iterator.204] iterators out of range");
- }
- {
- json j = 17;
- CHECK_THROWS_AS(j.erase(j.cend(), j.cend()), json::invalid_iterator&);
- CHECK_THROWS_AS(j.erase(j.cbegin(), j.cbegin()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.cend(), j.cend()), "[json.exception.invalid_iterator.204] iterators out of range");
- CHECK_THROWS_WITH(j.erase(j.cbegin(), j.cbegin()), "[json.exception.invalid_iterator.204] iterators out of range");
- }
- }
- SECTION("number (unsigned)")
- {
- {
- json j = 17u;
- CHECK_THROWS_AS(j.erase(j.end(), j.end()), json::invalid_iterator&);
- CHECK_THROWS_AS(j.erase(j.begin(), j.begin()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.end(), j.end()), "[json.exception.invalid_iterator.204] iterators out of range");
- CHECK_THROWS_WITH(j.erase(j.begin(), j.begin()), "[json.exception.invalid_iterator.204] iterators out of range");
- }
- {
- json j = 17u;
- CHECK_THROWS_AS(j.erase(j.cend(), j.cend()), json::invalid_iterator&);
- CHECK_THROWS_AS(j.erase(j.cbegin(), j.cbegin()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.cend(), j.cend()), "[json.exception.invalid_iterator.204] iterators out of range");
- CHECK_THROWS_WITH(j.erase(j.cbegin(), j.cbegin()), "[json.exception.invalid_iterator.204] iterators out of range");
- }
- }
- SECTION("number (floating point)")
- {
- {
- json j = 23.42;
- CHECK_THROWS_AS(j.erase(j.end(), j.end()), json::invalid_iterator&);
- CHECK_THROWS_AS(j.erase(j.begin(), j.begin()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.end(), j.end()), "[json.exception.invalid_iterator.204] iterators out of range");
- CHECK_THROWS_WITH(j.erase(j.begin(), j.begin()), "[json.exception.invalid_iterator.204] iterators out of range");
- }
- {
- json j = 23.42;
- CHECK_THROWS_AS(j.erase(j.cend(), j.cend()), json::invalid_iterator&);
- CHECK_THROWS_AS(j.erase(j.cbegin(), j.cbegin()), json::invalid_iterator&);
- CHECK_THROWS_WITH(j.erase(j.cend(), j.cend()), "[json.exception.invalid_iterator.204] iterators out of range");
- CHECK_THROWS_WITH(j.erase(j.cbegin(), j.cbegin()), "[json.exception.invalid_iterator.204] iterators out of range");
- }
- }
- }
- }
- }
|