🚨 fix Clang-Tidy warnings

This commit is contained in:
Niels Lohmann 2022-09-10 23:10:56 +02:00
parent f844148560
commit 9a99e19bc7
No known key found for this signature in database
GPG Key ID: 7F3CEA63AE251B69
9 changed files with 139 additions and 139 deletions

View File

@ -4718,7 +4718,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
} }
// make sure the top element of the pointer exists // make sure the top element of the pointer exists
json_pointer top_pointer = ptr.top(); json_pointer const top_pointer = ptr.top();
if (top_pointer != ptr) if (top_pointer != ptr)
{ {
result.at(top_pointer); result.at(top_pointer);
@ -4880,7 +4880,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
json_pointer from_ptr(from_path); json_pointer from_ptr(from_path);
// the "from" location must exist - use at() // the "from" location must exist - use at()
basic_json v = result.at(from_ptr); basic_json const v = result.at(from_ptr);
// The move operation is functionally identical to a // The move operation is functionally identical to a
// "remove" operation on the "from" location, followed // "remove" operation on the "from" location, followed
@ -4897,7 +4897,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
const json_pointer from_ptr(from_path); const json_pointer from_ptr(from_path);
// the "from" location must exist - use at() // the "from" location must exist - use at()
basic_json v = result.at(from_ptr); basic_json const v = result.at(from_ptr);
// The copy is functionally identical to an "add" // The copy is functionally identical to an "add"
// operation at the target location using the value // operation at the target location using the value

View File

@ -22,7 +22,7 @@ TEST_CASE("BSON")
{ {
SECTION("null") SECTION("null")
{ {
json j = nullptr; json const j = nullptr;
CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is null", json::type_error&); CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is null", json::type_error&);
} }
@ -30,45 +30,45 @@ TEST_CASE("BSON")
{ {
SECTION("true") SECTION("true")
{ {
json j = true; json const j = true;
CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is boolean", json::type_error&); CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is boolean", json::type_error&);
} }
SECTION("false") SECTION("false")
{ {
json j = false; json const j = false;
CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is boolean", json::type_error&); CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is boolean", json::type_error&);
} }
} }
SECTION("number") SECTION("number")
{ {
json j = 42; json const j = 42;
CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is number", json::type_error&); CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is number", json::type_error&);
} }
SECTION("float") SECTION("float")
{ {
json j = 4.2; json const j = 4.2;
CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is number", json::type_error&); CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is number", json::type_error&);
} }
SECTION("string") SECTION("string")
{ {
json j = "not supported"; json const j = "not supported";
CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is string", json::type_error&); CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is string", json::type_error&);
} }
SECTION("array") SECTION("array")
{ {
json j = std::vector<int> {1, 2, 3, 4, 5, 6, 7}; json const j = std::vector<int> {1, 2, 3, 4, 5, 6, 7};
CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is array", json::type_error&); CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is array", json::type_error&);
} }
} }
SECTION("keys containing code-point U+0000 cannot be serialized to BSON") SECTION("keys containing code-point U+0000 cannot be serialized to BSON")
{ {
json j = json const j =
{ {
{ std::string("en\0try", 6), true } { std::string("en\0try", 6), true }
}; };
@ -82,7 +82,7 @@ TEST_CASE("BSON")
SECTION("string length must be at least 1") SECTION("string length must be at least 1")
{ {
// from https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=11175 // from https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=11175
std::vector<std::uint8_t> v = std::vector<std::uint8_t> const v =
{ {
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x02, 0x02,
@ -597,7 +597,7 @@ TEST_CASE("BSON input/output_adapters")
{"object", {{ "string", "value" }}} {"object", {{ "string", "value" }}}
}; };
std::vector<std::uint8_t> bson_representation = std::vector<std::uint8_t> const bson_representation =
{ {
/*size */ 0x4f, 0x00, 0x00, 0x00, /*size */ 0x4f, 0x00, 0x00, 0x00,
/*entry*/ 0x01, 'd', 'o', 'u', 'b', 'l', 'e', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x45, 0x40, /*entry*/ 0x01, 'd', 'o', 'u', 'b', 'l', 'e', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x45, 0x40,
@ -726,7 +726,7 @@ TEST_CASE("Incomplete BSON Input")
{ {
SECTION("Incomplete BSON Input 1") SECTION("Incomplete BSON Input 1")
{ {
std::vector<std::uint8_t> incomplete_bson = std::vector<std::uint8_t> const incomplete_bson =
{ {
0x0D, 0x00, 0x00, 0x00, // size (little endian) 0x0D, 0x00, 0x00, 0x00, // size (little endian)
0x08, // entry: boolean 0x08, // entry: boolean
@ -744,7 +744,7 @@ TEST_CASE("Incomplete BSON Input")
SECTION("Incomplete BSON Input 2") SECTION("Incomplete BSON Input 2")
{ {
std::vector<std::uint8_t> incomplete_bson = std::vector<std::uint8_t> const incomplete_bson =
{ {
0x0D, 0x00, 0x00, 0x00, // size (little endian) 0x0D, 0x00, 0x00, 0x00, // size (little endian)
0x08, // entry: boolean, unexpected EOF 0x08, // entry: boolean, unexpected EOF
@ -760,7 +760,7 @@ TEST_CASE("Incomplete BSON Input")
SECTION("Incomplete BSON Input 3") SECTION("Incomplete BSON Input 3")
{ {
std::vector<std::uint8_t> incomplete_bson = std::vector<std::uint8_t> const incomplete_bson =
{ {
0x41, 0x00, 0x00, 0x00, // size (little endian) 0x41, 0x00, 0x00, 0x00, // size (little endian)
0x04, /// entry: embedded document 0x04, /// entry: embedded document
@ -782,7 +782,7 @@ TEST_CASE("Incomplete BSON Input")
SECTION("Incomplete BSON Input 4") SECTION("Incomplete BSON Input 4")
{ {
std::vector<std::uint8_t> incomplete_bson = std::vector<std::uint8_t> const incomplete_bson =
{ {
0x0D, 0x00, // size (incomplete), unexpected EOF 0x0D, 0x00, // size (incomplete), unexpected EOF
}; };
@ -799,7 +799,7 @@ TEST_CASE("Incomplete BSON Input")
{ {
SECTION("key") SECTION("key")
{ {
json j = {{"key", "value"}}; json const j = {{"key", "value"}};
auto bson_vec = json::to_bson(j); auto bson_vec = json::to_bson(j);
SaxCountdown scp(2); SaxCountdown scp(2);
CHECK(!json::sax_parse(bson_vec, &scp, json::input_format_t::bson)); CHECK(!json::sax_parse(bson_vec, &scp, json::input_format_t::bson));
@ -807,7 +807,7 @@ TEST_CASE("Incomplete BSON Input")
SECTION("array") SECTION("array")
{ {
json j = json const j =
{ {
{ "entry", json::array() } { "entry", json::array() }
}; };
@ -821,7 +821,7 @@ TEST_CASE("Incomplete BSON Input")
TEST_CASE("Negative size of binary value") TEST_CASE("Negative size of binary value")
{ {
// invalid BSON: the size of the binary value is -1 // invalid BSON: the size of the binary value is -1
std::vector<std::uint8_t> input = std::vector<std::uint8_t> const input =
{ {
0x21, 0x00, 0x00, 0x00, // size (little endian) 0x21, 0x00, 0x00, 0x00, // size (little endian)
0x05, // entry: binary 0x05, // entry: binary
@ -839,7 +839,7 @@ TEST_CASE("Negative size of binary value")
TEST_CASE("Unsupported BSON input") TEST_CASE("Unsupported BSON input")
{ {
std::vector<std::uint8_t> bson = std::vector<std::uint8_t> const bson =
{ {
0x0C, 0x00, 0x00, 0x00, // size (little endian) 0x0C, 0x00, 0x00, 0x00, // size (little endian)
0xFF, // entry type: Min key (not supported yet) 0xFF, // entry type: Min key (not supported yet)
@ -863,7 +863,7 @@ TEST_CASE("BSON numerical data")
{ {
SECTION("std::int64_t: INT64_MIN .. INT32_MIN-1") SECTION("std::int64_t: INT64_MIN .. INT32_MIN-1")
{ {
std::vector<int64_t> numbers std::vector<int64_t> const numbers
{ {
INT64_MIN, INT64_MIN,
-1000000000000000000LL, -1000000000000000000LL,
@ -889,7 +889,7 @@ TEST_CASE("BSON numerical data")
}; };
CHECK(j.at("entry").is_number_integer()); CHECK(j.at("entry").is_number_integer());
std::uint64_t iu = *reinterpret_cast<std::uint64_t*>(&i); std::uint64_t const iu = *reinterpret_cast<std::uint64_t*>(&i);
std::vector<std::uint8_t> expected_bson = std::vector<std::uint8_t> expected_bson =
{ {
0x14u, 0x00u, 0x00u, 0x00u, // size (little endian) 0x14u, 0x00u, 0x00u, 0x00u, // size (little endian)
@ -921,7 +921,7 @@ TEST_CASE("BSON numerical data")
SECTION("signed std::int32_t: INT32_MIN .. INT32_MAX") SECTION("signed std::int32_t: INT32_MIN .. INT32_MAX")
{ {
std::vector<int32_t> numbers std::vector<int32_t> const numbers
{ {
INT32_MIN, INT32_MIN,
-2147483647L, -2147483647L,
@ -961,7 +961,7 @@ TEST_CASE("BSON numerical data")
}; };
CHECK(j.at("entry").is_number_integer()); CHECK(j.at("entry").is_number_integer());
std::uint32_t iu = *reinterpret_cast<std::uint32_t*>(&i); std::uint32_t const iu = *reinterpret_cast<std::uint32_t*>(&i);
std::vector<std::uint8_t> expected_bson = std::vector<std::uint8_t> expected_bson =
{ {
0x10u, 0x00u, 0x00u, 0x00u, // size (little endian) 0x10u, 0x00u, 0x00u, 0x00u, // size (little endian)
@ -988,7 +988,7 @@ TEST_CASE("BSON numerical data")
SECTION("signed std::int64_t: INT32_MAX+1 .. INT64_MAX") SECTION("signed std::int64_t: INT32_MAX+1 .. INT64_MAX")
{ {
std::vector<int64_t> numbers std::vector<int64_t> const numbers
{ {
INT64_MAX, INT64_MAX,
1000000000000000000LL, 1000000000000000000LL,
@ -1014,7 +1014,7 @@ TEST_CASE("BSON numerical data")
}; };
CHECK(j.at("entry").is_number_integer()); CHECK(j.at("entry").is_number_integer());
std::uint64_t iu = *reinterpret_cast<std::uint64_t*>(&i); std::uint64_t const iu = *reinterpret_cast<std::uint64_t*>(&i);
std::vector<std::uint8_t> expected_bson = std::vector<std::uint8_t> expected_bson =
{ {
0x14u, 0x00u, 0x00u, 0x00u, // size (little endian) 0x14u, 0x00u, 0x00u, 0x00u, // size (little endian)
@ -1048,7 +1048,7 @@ TEST_CASE("BSON numerical data")
{ {
SECTION("unsigned std::uint64_t: 0 .. INT32_MAX") SECTION("unsigned std::uint64_t: 0 .. INT32_MAX")
{ {
std::vector<std::uint64_t> numbers std::vector<std::uint64_t> const numbers
{ {
0ULL, 0ULL,
1ULL, 1ULL,
@ -1103,7 +1103,7 @@ TEST_CASE("BSON numerical data")
SECTION("unsigned std::uint64_t: INT32_MAX+1 .. INT64_MAX") SECTION("unsigned std::uint64_t: INT32_MAX+1 .. INT64_MAX")
{ {
std::vector<std::uint64_t> numbers std::vector<std::uint64_t> const numbers
{ {
static_cast<std::uint64_t>(INT32_MAX) + 1, static_cast<std::uint64_t>(INT32_MAX) + 1,
4000000000ULL, 4000000000ULL,
@ -1161,7 +1161,7 @@ TEST_CASE("BSON numerical data")
SECTION("unsigned std::uint64_t: INT64_MAX+1 .. UINT64_MAX") SECTION("unsigned std::uint64_t: INT64_MAX+1 .. UINT64_MAX")
{ {
std::vector<std::uint64_t> numbers std::vector<std::uint64_t> const numbers
{ {
static_cast<std::uint64_t>(INT64_MAX) + 1ULL, static_cast<std::uint64_t>(INT64_MAX) + 1ULL,
10000000000000000000ULL, 10000000000000000000ULL,
@ -1175,13 +1175,13 @@ TEST_CASE("BSON numerical data")
CAPTURE(i) CAPTURE(i)
json j = json const j =
{ {
{ "entry", i } { "entry", i }
}; };
auto iu = i; auto iu = i;
std::vector<std::uint8_t> expected_bson = std::vector<std::uint8_t> const expected_bson =
{ {
0x14u, 0x00u, 0x00u, 0x00u, // size (little endian) 0x14u, 0x00u, 0x00u, 0x00u, // size (little endian)
0x12u, /// entry: int64 0x12u, /// entry: int64
@ -1274,7 +1274,7 @@ TEST_CASE("BSON roundtrips" * doctest::skip())
INFO_WITH_TEMP(filename + ": output to output adapters"); INFO_WITH_TEMP(filename + ": output to output adapters");
// parse JSON file // parse JSON file
std::ifstream f_json(filename); std::ifstream f_json(filename);
json j1 = json::parse(f_json); json const j1 = json::parse(f_json);
// parse BSON file // parse BSON file
auto packed = utils::read_binary_file(filename + ".bson"); auto packed = utils::read_binary_file(filename + ".bson");

View File

@ -27,26 +27,26 @@ TEST_CASE("iterator class")
SECTION("null") SECTION("null")
{ {
json j(json::value_t::null); json j(json::value_t::null);
json::iterator it(&j); json::iterator const it(&j);
} }
SECTION("object") SECTION("object")
{ {
json j(json::value_t::object); json j(json::value_t::object);
json::iterator it(&j); json::iterator const it(&j);
} }
SECTION("array") SECTION("array")
{ {
json j(json::value_t::array); json j(json::value_t::array);
json::iterator it(&j); json::iterator const it(&j);
} }
} }
SECTION("copy assignment") SECTION("copy assignment")
{ {
json j(json::value_t::null); json j(json::value_t::null);
json::iterator it(&j); json::iterator const it(&j);
json::iterator it2(&j); json::iterator it2(&j);
it2 = it; it2 = it;
} }
@ -116,7 +116,7 @@ TEST_CASE("iterator class")
SECTION("null") SECTION("null")
{ {
json j(json::value_t::null); json j(json::value_t::null);
json::iterator it = j.begin(); json::iterator const it = j.begin();
CHECK_THROWS_WITH_AS(*it, "[json.exception.invalid_iterator.214] cannot get value", json::invalid_iterator&); CHECK_THROWS_WITH_AS(*it, "[json.exception.invalid_iterator.214] cannot get value", json::invalid_iterator&);
} }
@ -132,14 +132,14 @@ TEST_CASE("iterator class")
SECTION("object") SECTION("object")
{ {
json j({{"foo", "bar"}}); json j({{"foo", "bar"}});
json::iterator it = j.begin(); json::iterator const it = j.begin();
CHECK(*it == json("bar")); CHECK(*it == json("bar"));
} }
SECTION("array") SECTION("array")
{ {
json j({1, 2, 3, 4}); json j({1, 2, 3, 4});
json::iterator it = j.begin(); json::iterator const it = j.begin();
CHECK(*it == json(1)); CHECK(*it == json(1));
} }
} }
@ -149,7 +149,7 @@ TEST_CASE("iterator class")
SECTION("null") SECTION("null")
{ {
json j(json::value_t::null); json j(json::value_t::null);
json::iterator it = j.begin(); json::iterator const it = j.begin();
CHECK_THROWS_WITH_AS(std::string(it->type_name()), "[json.exception.invalid_iterator.214] cannot get value", json::invalid_iterator&); CHECK_THROWS_WITH_AS(std::string(it->type_name()), "[json.exception.invalid_iterator.214] cannot get value", json::invalid_iterator&);
} }
@ -165,14 +165,14 @@ TEST_CASE("iterator class")
SECTION("object") SECTION("object")
{ {
json j({{"foo", "bar"}}); json j({{"foo", "bar"}});
json::iterator it = j.begin(); json::iterator const it = j.begin();
CHECK(std::string(it->type_name()) == "string"); CHECK(std::string(it->type_name()) == "string");
} }
SECTION("array") SECTION("array")
{ {
json j({1, 2, 3, 4}); json j({1, 2, 3, 4});
json::iterator it = j.begin(); json::iterator const it = j.begin();
CHECK(std::string(it->type_name()) == "number"); CHECK(std::string(it->type_name()) == "number");
} }
} }
@ -287,7 +287,7 @@ TEST_CASE("iterator class")
SECTION("null") SECTION("null")
{ {
json j(json::value_t::null); json j(json::value_t::null);
json::iterator it = j.end(); json::iterator const it = j.end();
CHECK((it.m_it.primitive_iterator.m_it == 1)); CHECK((it.m_it.primitive_iterator.m_it == 1));
} }
@ -336,7 +336,7 @@ TEST_CASE("iterator class")
SECTION("null") SECTION("null")
{ {
json j(json::value_t::null); json j(json::value_t::null);
json::iterator it = j.end(); json::iterator const it = j.end();
CHECK((it.m_it.primitive_iterator.m_it == 1)); CHECK((it.m_it.primitive_iterator.m_it == 1));
} }

View File

@ -47,7 +47,7 @@ TEST_CASE("value conversion")
SECTION("json::object_t") SECTION("json::object_t")
{ {
json::object_t const const o = j.get<json::object_t>(); json::object_t const o = j.get<json::object_t>();
CHECK(json(o) == j); CHECK(json(o) == j);
} }

View File

@ -22,7 +22,7 @@ TEST_CASE("Better diagnostics")
{ {
SECTION("empty JSON Pointer") SECTION("empty JSON Pointer")
{ {
json j = 1; json const j = 1;
std::string s; std::string s;
CHECK_THROWS_WITH_AS(s = j.get<std::string>(), "[json.exception.type_error.302] type must be string, but is number", json::type_error); CHECK_THROWS_WITH_AS(s = j.get<std::string>(), "[json.exception.type_error.302] type must be string, but is number", json::type_error);
} }
@ -240,7 +240,7 @@ TEST_CASE("Regression tests for extended diagnostics")
j_arr[5] = 5; j_arr[5] = 5;
j_arr[6] = 6; j_arr[6] = 6;
j_arr[7] = 7; j_arr[7] = 7;
json j_arr_copy = j_arr; json const j_arr_copy = j_arr;
} }
} }
} }

View File

@ -143,27 +143,27 @@ TEST_CASE("regression tests 1")
// to null), but are serialized as null. // to null), but are serialized as null.
SECTION("NAN value") SECTION("NAN value")
{ {
json j1 = NAN; json const j1 = NAN;
CHECK(j1.is_number_float()); CHECK(j1.is_number_float());
json::number_float_t f1{j1}; json::number_float_t const f1{j1};
CHECK(std::isnan(f1)); CHECK(std::isnan(f1));
json j2 = static_cast<json::number_float_t>(NAN); json const j2 = static_cast<json::number_float_t>(NAN);
CHECK(j2.is_number_float()); CHECK(j2.is_number_float());
json::number_float_t f2{j2}; json::number_float_t const f2{j2};
CHECK(std::isnan(f2)); CHECK(std::isnan(f2));
} }
SECTION("infinity") SECTION("infinity")
{ {
json j1 = INFINITY; json const j1 = INFINITY;
CHECK(j1.is_number_float()); CHECK(j1.is_number_float());
json::number_float_t f1{j1}; json::number_float_t const f1{j1};
CHECK(!std::isfinite(f1)); CHECK(!std::isfinite(f1));
json j2 = static_cast<json::number_float_t>(INFINITY); json const j2 = static_cast<json::number_float_t>(INFINITY);
CHECK(j2.is_number_float()); CHECK(j2.is_number_float());
json::number_float_t f2{j2}; json::number_float_t const f2{j2};
CHECK(!std::isfinite(f2)); CHECK(!std::isfinite(f2));
} }
} }
@ -200,7 +200,7 @@ TEST_CASE("regression tests 1")
fields["three"] = std::string("three \"four\""); fields["three"] = std::string("three \"four\"");
// create another JSON object by deserializing the serialization // create another JSON object by deserializing the serialization
std::string payload = fields.dump(); std::string const payload = fields.dump();
json parsed_fields = json::parse(payload); json parsed_fields = json::parse(payload);
// check individual fields to match both objects // check individual fields to match both objects
@ -288,7 +288,7 @@ TEST_CASE("regression tests 1")
} }
{ {
json a = {1, 2, 3}; json a = {1, 2, 3};
json::reverse_iterator rit = ++a.rbegin(); json::reverse_iterator const rit = ++a.rbegin();
CHECK(*rit == json(2)); CHECK(*rit == json(2));
CHECK(rit.value() == json(2)); CHECK(rit.value() == json(2));
} }
@ -340,8 +340,8 @@ TEST_CASE("regression tests 1")
SECTION("issue #101 - binary string causes numbers to be dumped as hex") SECTION("issue #101 - binary string causes numbers to be dumped as hex")
{ {
int64_t number = 10; int64_t const number = 10;
std::string bytes{"\x00" "asdf\n", 6}; std::string const bytes{"\x00" "asdf\n", 6};
json j; json j;
j["int64"] = number; j["int64"] = number;
j["binary string"] = bytes; j["binary string"] = bytes;
@ -352,7 +352,7 @@ TEST_CASE("regression tests 1")
SECTION("issue #111 - subsequent unicode chars") SECTION("issue #111 - subsequent unicode chars")
{ {
std::string bytes{0x7, 0x7}; std::string const bytes{0x7, 0x7};
json j; json j;
j["string"] = bytes; j["string"] = bytes;
CHECK(j["string"] == "\u0007\u0007"); CHECK(j["string"] == "\u0007\u0007");
@ -465,33 +465,33 @@ TEST_CASE("regression tests 1")
// create JSON class with nonstandard float number type // create JSON class with nonstandard float number type
// float // float
nlohmann::basic_json<std::map, std::vector, std::string, bool, int32_t, uint32_t, float> j_float = nlohmann::basic_json<std::map, std::vector, std::string, bool, int32_t, uint32_t, float> const j_float =
1.23e25f; 1.23e25f;
CHECK(j_float.get<float>() == 1.23e25f); CHECK(j_float.get<float>() == 1.23e25f);
// double // double
nlohmann::basic_json<std::map, std::vector, std::string, bool, int64_t, uint64_t, double> j_double = nlohmann::basic_json<std::map, std::vector, std::string, bool, int64_t, uint64_t, double> const j_double =
1.23e35; 1.23e35;
CHECK(j_double.get<double>() == 1.23e35); CHECK(j_double.get<double>() == 1.23e35);
// long double // long double
nlohmann::basic_json<std::map, std::vector, std::string, bool, int64_t, uint64_t, long double> nlohmann::basic_json<std::map, std::vector, std::string, bool, int64_t, uint64_t, long double>
j_long_double = 1.23e45L; const j_long_double = 1.23e45L;
CHECK(j_long_double.get<long double>() == 1.23e45L); CHECK(j_long_double.get<long double>() == 1.23e45L);
} }
SECTION("issue #228 - double values are serialized with commas as decimal points") SECTION("issue #228 - double values are serialized with commas as decimal points")
{ {
json j1a = 2312.42; json const j1a = 2312.42;
json j1b = json::parse("2312.42"); json const j1b = json::parse("2312.42");
json j2a = 2342e-2; json const j2a = 2342e-2;
//issue #230 //issue #230
//json j2b = json::parse("2342e-2"); //json j2b = json::parse("2342e-2");
json j3a = 10E3; json const j3a = 10E3;
json j3b = json::parse("10E3"); json const j3b = json::parse("10E3");
json j3c = json::parse("10e3"); json const j3c = json::parse("10e3");
// class to create a locale that would use a comma for decimals // class to create a locale that would use a comma for decimals
class CommaDecimalSeparator : public std::numpunct<char> class CommaDecimalSeparator : public std::numpunct<char>
@ -585,7 +585,7 @@ TEST_CASE("regression tests 1")
SECTION("issue #269 - diff generates incorrect patch when removing multiple array elements") SECTION("issue #269 - diff generates incorrect patch when removing multiple array elements")
{ {
json doc = R"( { "arr1": [1, 2, 3, 4] } )"_json; json const doc = R"( { "arr1": [1, 2, 3, 4] } )"_json;
json expected = R"( { "arr1": [1, 2] } )"_json; json expected = R"( { "arr1": [1, 2] } )"_json;
// check roundtrip // check roundtrip
@ -610,7 +610,7 @@ TEST_CASE("regression tests 1")
// code triggered a "warning: unused variable" warning and is left // code triggered a "warning: unused variable" warning and is left
// here to avoid the warning in the future // here to avoid the warning in the future
json object; json object;
json patch = json::array(); json const patch = json::array();
object = object.patch(patch); object = object.patch(patch);
} }
@ -837,7 +837,7 @@ TEST_CASE("regression tests 1")
SECTION("second example from #529") SECTION("second example from #529")
{ {
std::string str = "{\n\"one\" : 1,\n\"two\" : 2\n}\n{\n\"three\" : 3\n}"; std::string const str = "{\n\"one\" : 1,\n\"two\" : 2\n}\n{\n\"three\" : 3\n}";
{ {
std::ofstream file("test.json"); std::ofstream file("test.json");
@ -875,23 +875,23 @@ TEST_CASE("regression tests 1")
SECTION("issue #389 - Integer-overflow (OSS-Fuzz issue 267)") SECTION("issue #389 - Integer-overflow (OSS-Fuzz issue 267)")
{ {
// original test case // original test case
json j1 = json::parse("-9223372036854775808"); json const j1 = json::parse("-9223372036854775808");
CHECK(j1.is_number_integer()); CHECK(j1.is_number_integer());
CHECK(j1.get<json::number_integer_t>() == INT64_MIN); CHECK(j1.get<json::number_integer_t>() == INT64_MIN);
// edge case (+1; still an integer) // edge case (+1; still an integer)
json j2 = json::parse("-9223372036854775807"); json const j2 = json::parse("-9223372036854775807");
CHECK(j2.is_number_integer()); CHECK(j2.is_number_integer());
CHECK(j2.get<json::number_integer_t>() == INT64_MIN + 1); CHECK(j2.get<json::number_integer_t>() == INT64_MIN + 1);
// edge case (-1; overflow -> floats) // edge case (-1; overflow -> floats)
json j3 = json::parse("-9223372036854775809"); json const j3 = json::parse("-9223372036854775809");
CHECK(j3.is_number_float()); CHECK(j3.is_number_float());
} }
SECTION("issue #380 - bug in overflow detection when parsing integers") SECTION("issue #380 - bug in overflow detection when parsing integers")
{ {
json j = json::parse("166020696663385964490"); json const j = json::parse("166020696663385964490");
CHECK(j.is_number_float()); CHECK(j.is_number_float());
CHECK(j.get<json::number_float_t>() == 166020696663385964490.0); CHECK(j.get<json::number_float_t>() == 166020696663385964490.0);
} }
@ -899,7 +899,7 @@ TEST_CASE("regression tests 1")
SECTION("issue #405 - Heap-buffer-overflow (OSS-Fuzz issue 342)") SECTION("issue #405 - Heap-buffer-overflow (OSS-Fuzz issue 342)")
{ {
// original test case // original test case
std::vector<uint8_t> vec {0x65, 0xf5, 0x0a, 0x48, 0x21}; std::vector<uint8_t> const vec {0x65, 0xf5, 0x0a, 0x48, 0x21};
json _; json _;
CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec), "[json.exception.parse_error.110] parse error at byte 6: syntax error while parsing CBOR string: unexpected end of input", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec), "[json.exception.parse_error.110] parse error at byte 6: syntax error while parsing CBOR string: unexpected end of input", json::parse_error&);
} }
@ -909,23 +909,23 @@ TEST_CASE("regression tests 1")
json _; json _;
// original test case: incomplete float64 // original test case: incomplete float64
std::vector<uint8_t> vec1 {0xcb, 0x8f, 0x0a}; std::vector<uint8_t> const vec1 {0xcb, 0x8f, 0x0a};
CHECK_THROWS_WITH_AS(_ = json::from_msgpack(vec1), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::from_msgpack(vec1), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
// related test case: incomplete float32 // related test case: incomplete float32
std::vector<uint8_t> vec2 {0xca, 0x8f, 0x0a}; std::vector<uint8_t> const vec2 {0xca, 0x8f, 0x0a};
CHECK_THROWS_WITH_AS(_ = json::from_msgpack(vec2), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::from_msgpack(vec2), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
// related test case: incomplete Half-Precision Float (CBOR) // related test case: incomplete Half-Precision Float (CBOR)
std::vector<uint8_t> vec3 {0xf9, 0x8f}; std::vector<uint8_t> const vec3 {0xf9, 0x8f};
CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec3), "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR number: unexpected end of input", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec3), "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR number: unexpected end of input", json::parse_error&);
// related test case: incomplete Single-Precision Float (CBOR) // related test case: incomplete Single-Precision Float (CBOR)
std::vector<uint8_t> vec4 {0xfa, 0x8f, 0x0a}; std::vector<uint8_t> const vec4 {0xfa, 0x8f, 0x0a};
CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec4), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR number: unexpected end of input", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec4), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR number: unexpected end of input", json::parse_error&);
// related test case: incomplete Double-Precision Float (CBOR) // related test case: incomplete Double-Precision Float (CBOR)
std::vector<uint8_t> vec5 {0xfb, 0x8f, 0x0a}; std::vector<uint8_t> const vec5 {0xfb, 0x8f, 0x0a};
CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec5), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR number: unexpected end of input", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec5), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR number: unexpected end of input", json::parse_error&);
} }
@ -934,7 +934,7 @@ TEST_CASE("regression tests 1")
json _; json _;
// original test case // original test case
std::vector<uint8_t> vec1 {0x87}; std::vector<uint8_t> const vec1 {0x87};
CHECK_THROWS_WITH_AS(_ = json::from_msgpack(vec1), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing MessagePack string: unexpected end of input", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::from_msgpack(vec1), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing MessagePack string: unexpected end of input", json::parse_error&);
// more test cases for MessagePack // more test cases for MessagePack
@ -946,7 +946,7 @@ TEST_CASE("regression tests 1")
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf
}) })
{ {
std::vector<uint8_t> vec(1, static_cast<uint8_t>(b)); std::vector<uint8_t> const vec(1, static_cast<uint8_t>(b));
CHECK_THROWS_AS(_ = json::from_msgpack(vec), json::parse_error&); CHECK_THROWS_AS(_ = json::from_msgpack(vec), json::parse_error&);
} }
@ -961,12 +961,12 @@ TEST_CASE("regression tests 1")
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7 // map 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7 // map
}) })
{ {
std::vector<uint8_t> vec(1, static_cast<uint8_t>(b)); std::vector<uint8_t> const vec(1, static_cast<uint8_t>(b));
CHECK_THROWS_AS(_ = json::from_cbor(vec), json::parse_error&); CHECK_THROWS_AS(_ = json::from_cbor(vec), json::parse_error&);
} }
// special case: empty input // special case: empty input
std::vector<uint8_t> vec2; std::vector<uint8_t> const vec2;
CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec2), "[json.exception.parse_error.110] parse error at byte 1: syntax error while parsing CBOR value: unexpected end of input", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec2), "[json.exception.parse_error.110] parse error at byte 1: syntax error while parsing CBOR value: unexpected end of input", json::parse_error&);
CHECK_THROWS_WITH_AS(_ = json::from_msgpack(vec2), "[json.exception.parse_error.110] parse error at byte 1: syntax error while parsing MessagePack value: unexpected end of input", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::from_msgpack(vec2), "[json.exception.parse_error.110] parse error at byte 1: syntax error while parsing MessagePack value: unexpected end of input", json::parse_error&);
} }
@ -976,22 +976,22 @@ TEST_CASE("regression tests 1")
json _; json _;
// original test case: empty UTF-8 string (indefinite length) // original test case: empty UTF-8 string (indefinite length)
std::vector<uint8_t> vec1 {0x7f}; std::vector<uint8_t> const vec1 {0x7f};
CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec1), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR string: unexpected end of input", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec1), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR string: unexpected end of input", json::parse_error&);
// related test case: empty array (indefinite length) // related test case: empty array (indefinite length)
std::vector<uint8_t> vec2 {0x9f}; std::vector<uint8_t> const vec2 {0x9f};
CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec2), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR value: unexpected end of input", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec2), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR value: unexpected end of input", json::parse_error&);
// related test case: empty map (indefinite length) // related test case: empty map (indefinite length)
std::vector<uint8_t> vec3 {0xbf}; std::vector<uint8_t> const vec3 {0xbf};
CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec3), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR string: unexpected end of input", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec3), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR string: unexpected end of input", json::parse_error&);
} }
SECTION("issue #412 - Heap-buffer-overflow (OSS-Fuzz issue 367)") SECTION("issue #412 - Heap-buffer-overflow (OSS-Fuzz issue 367)")
{ {
// original test case // original test case
std::vector<uint8_t> vec std::vector<uint8_t> const vec
{ {
0xab, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0xab, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98,
0x98, 0x98, 0x98, 0x98, 0x98, 0x00, 0x00, 0x00, 0x98, 0x98, 0x98, 0x98, 0x98, 0x00, 0x00, 0x00,
@ -1016,15 +1016,15 @@ TEST_CASE("regression tests 1")
CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x98", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x98", json::parse_error&);
// related test case: nonempty UTF-8 string (indefinite length) // related test case: nonempty UTF-8 string (indefinite length)
std::vector<uint8_t> vec1 {0x7f, 0x61, 0x61}; std::vector<uint8_t> const vec1 {0x7f, 0x61, 0x61};
CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec1), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR string: unexpected end of input", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec1), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR string: unexpected end of input", json::parse_error&);
// related test case: nonempty array (indefinite length) // related test case: nonempty array (indefinite length)
std::vector<uint8_t> vec2 {0x9f, 0x01}; std::vector<uint8_t> const vec2 {0x9f, 0x01};
CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec2), "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR value: unexpected end of input", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec2), "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing CBOR value: unexpected end of input", json::parse_error&);
// related test case: nonempty map (indefinite length) // related test case: nonempty map (indefinite length)
std::vector<uint8_t> vec3 {0xbf, 0x61, 0x61, 0x01}; std::vector<uint8_t> const vec3 {0xbf, 0x61, 0x61, 0x01};
CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec3), "[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing CBOR string: unexpected end of input", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec3), "[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing CBOR string: unexpected end of input", json::parse_error&);
} }
@ -1049,7 +1049,7 @@ TEST_CASE("regression tests 1")
SECTION("issue #416 - Use-of-uninitialized-value (OSS-Fuzz issue 377)") SECTION("issue #416 - Use-of-uninitialized-value (OSS-Fuzz issue 377)")
{ {
// original test case // original test case
std::vector<uint8_t> vec1 std::vector<uint8_t> const vec1
{ {
0x94, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0x94, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa,
0x3a, 0x96, 0x96, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0x3a, 0x96, 0x96, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4,
@ -1063,7 +1063,7 @@ TEST_CASE("regression tests 1")
CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec1), "[json.exception.parse_error.113] parse error at byte 13: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0xB4", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::from_cbor(vec1), "[json.exception.parse_error.113] parse error at byte 13: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0xB4", json::parse_error&);
// related test case: double-precision // related test case: double-precision
std::vector<uint8_t> vec2 std::vector<uint8_t> const vec2
{ {
0x94, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0x94, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa,
0x3a, 0x96, 0x96, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0x3a, 0x96, 0x96, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4,
@ -1077,7 +1077,7 @@ TEST_CASE("regression tests 1")
SECTION("issue #452 - Heap-buffer-overflow (OSS-Fuzz issue 585)") SECTION("issue #452 - Heap-buffer-overflow (OSS-Fuzz issue 585)")
{ {
std::vector<uint8_t> vec = {'-', '0', '1', '2', '2', '7', '4'}; std::vector<uint8_t> const vec = {'-', '0', '1', '2', '2', '7', '4'};
json _; json _;
CHECK_THROWS_AS(_ = json::parse(vec), json::parse_error&); CHECK_THROWS_AS(_ = json::parse(vec), json::parse_error&);
} }
@ -1100,9 +1100,9 @@ TEST_CASE("regression tests 1")
SECTION("issue #465 - roundtrip error while parsing 1000000000000000010E5") SECTION("issue #465 - roundtrip error while parsing 1000000000000000010E5")
{ {
json j1 = json::parse("1000000000000000010E5"); json const j1 = json::parse("1000000000000000010E5");
std::string s1 = j1.dump(); std::string s1 = j1.dump();
json j2 = json::parse(s1); json const j2 = json::parse(s1);
std::string s2 = j2.dump(); std::string s2 = j2.dump();
CHECK(s1 == s2); CHECK(s1 == s2);
} }
@ -1110,15 +1110,15 @@ TEST_CASE("regression tests 1")
#if JSON_USE_IMPLICIT_CONVERSIONS #if JSON_USE_IMPLICIT_CONVERSIONS
SECTION("issue #473 - inconsistent behavior in conversion to array type") SECTION("issue #473 - inconsistent behavior in conversion to array type")
{ {
json j_array = {1, 2, 3, 4}; json const j_array = {1, 2, 3, 4};
json j_number = 42; json const j_number = 42;
json j_null = nullptr; json const j_null = nullptr;
SECTION("std::vector") SECTION("std::vector")
{ {
auto create = [](const json & j) auto create = [](const json & j)
{ {
std::vector<int> v = j; std::vector<int> const v = j;
}; };
CHECK_NOTHROW(create(j_array)); CHECK_NOTHROW(create(j_array));
@ -1130,7 +1130,7 @@ TEST_CASE("regression tests 1")
{ {
auto create = [](const json & j) auto create = [](const json & j)
{ {
std::list<int> v = j; std::list<int> const v = j;
}; };
CHECK_NOTHROW(create(j_array)); CHECK_NOTHROW(create(j_array));
@ -1142,7 +1142,7 @@ TEST_CASE("regression tests 1")
{ {
auto create = [](const json & j) auto create = [](const json & j)
{ {
std::forward_list<int> v = j; std::forward_list<int> const v = j;
}; };
CHECK_NOTHROW(create(j_array)); CHECK_NOTHROW(create(j_array));
@ -1161,7 +1161,7 @@ TEST_CASE("regression tests 1")
SECTION("issue #494 - conversion from vector<bool> to json fails to build") SECTION("issue #494 - conversion from vector<bool> to json fails to build")
{ {
std::vector<bool> boolVector = {false, true, false, false}; std::vector<bool> const boolVector = {false, true, false, false};
json j; json j;
j["bool_vector"] = boolVector; j["bool_vector"] = boolVector;
@ -1170,14 +1170,14 @@ TEST_CASE("regression tests 1")
SECTION("issue #504 - assertion error (OSS-Fuzz 856)") SECTION("issue #504 - assertion error (OSS-Fuzz 856)")
{ {
std::vector<uint8_t> vec1 = {0xf9, 0xff, 0xff, 0x4a, 0x3a, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x37, 0x02, 0x38}; std::vector<uint8_t> const vec1 = {0xf9, 0xff, 0xff, 0x4a, 0x3a, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x37, 0x02, 0x38};
json j1 = json::from_cbor(vec1, false); json const j1 = json::from_cbor(vec1, false);
// step 2: round trip // step 2: round trip
std::vector<uint8_t> vec2 = json::to_cbor(j1); std::vector<uint8_t> vec2 = json::to_cbor(j1);
// parse serialization // parse serialization
json j2 = json::from_cbor(vec2); json const j2 = json::from_cbor(vec2);
// NaN is dumped to "null" // NaN is dumped to "null"
CHECK(j2.is_number_float()); CHECK(j2.is_number_float());
@ -1226,7 +1226,7 @@ TEST_CASE("regression tests 1")
SECTION("issue #575 - heap-buffer-overflow (OSS-Fuzz 1400)") SECTION("issue #575 - heap-buffer-overflow (OSS-Fuzz 1400)")
{ {
json _; json _;
std::vector<uint8_t> vec = {'"', '\\', '"', 'X', '"', '"'}; std::vector<uint8_t> const vec = {'"', '\\', '"', 'X', '"', '"'};
CHECK_THROWS_AS(_ = json::parse(vec), json::parse_error&); CHECK_THROWS_AS(_ = json::parse(vec), json::parse_error&);
} }
@ -1239,7 +1239,7 @@ TEST_CASE("regression tests 1")
std::map<std::string, int> m1 {{"key", 1}}; std::map<std::string, int> m1 {{"key", 1}};
// create and print a JSON from the map // create and print a JSON from the map
json j = m1; json const j = m1;
// get the map out of JSON // get the map out of JSON
std::map<std::string, int> m2 = j; std::map<std::string, int> m2 = j;
@ -1254,7 +1254,7 @@ TEST_CASE("regression tests 1")
std::map<std::string, std::string> m1 {{"key", "val"}}; std::map<std::string, std::string> m1 {{"key", "val"}};
// create and print a JSON from the map // create and print a JSON from the map
json j = m1; json const j = m1;
// get the map out of JSON // get the map out of JSON
std::map<std::string, std::string> m2 = j; std::map<std::string, std::string> m2 = j;
@ -1277,7 +1277,7 @@ TEST_CASE("regression tests 1")
{ {
SECTION("original example") SECTION("original example")
{ {
std::valarray<double> v; std::valarray<double> const v;
nlohmann::json j; nlohmann::json j;
j["test"] = v; j["test"] = v;
} }
@ -1364,8 +1364,8 @@ TEST_CASE("regression tests 1")
SECTION("issue #838 - incorrect parse error with binary data in keys") SECTION("issue #838 - incorrect parse error with binary data in keys")
{ {
std::array<uint8_t, 28> key1 = {{ 103, 92, 117, 48, 48, 48, 55, 92, 114, 215, 126, 214, 95, 92, 34, 174, 40, 71, 38, 174, 40, 71, 38, 223, 134, 247, 127, 0 }}; std::array<uint8_t, 28> key1 = {{ 103, 92, 117, 48, 48, 48, 55, 92, 114, 215, 126, 214, 95, 92, 34, 174, 40, 71, 38, 174, 40, 71, 38, 223, 134, 247, 127, 0 }};
std::string key1_str(reinterpret_cast<char*>(key1.data())); std::string const key1_str(reinterpret_cast<char*>(key1.data()));
json j = key1_str; json const j = key1_str;
CHECK_THROWS_WITH_AS(j.dump(), "[json.exception.type_error.316] invalid UTF-8 byte at index 10: 0x7E", json::type_error&); CHECK_THROWS_WITH_AS(j.dump(), "[json.exception.type_error.316] invalid UTF-8 byte at index 10: 0x7E", json::type_error&);
} }
@ -1405,7 +1405,7 @@ TEST_CASE("regression tests 1")
SECTION("issue #961 - incorrect parsing of indefinite length CBOR strings") SECTION("issue #961 - incorrect parsing of indefinite length CBOR strings")
{ {
std::vector<uint8_t> v_cbor = std::vector<uint8_t> const v_cbor =
{ {
0x7F, 0x7F,
0x64, 0x64,
@ -1453,7 +1453,7 @@ TEST_CASE("regression tests 1")
)"; )";
// define parser callback // define parser callback
json::parser_callback_t cb = [](int /*depth*/, json::parse_event_t event, json & parsed) json::parser_callback_t const cb = [](int /*depth*/, json::parse_event_t event, json & parsed)
{ {
// skip object elements with key "Thumbnail" // skip object elements with key "Thumbnail"
return !(event == json::parse_event_t::key && parsed == json("Thumbnail")); return !(event == json::parse_event_t::key && parsed == json("Thumbnail"));
@ -1467,7 +1467,7 @@ TEST_CASE("regression tests 1")
SECTION("issue #972 - Segmentation fault on G++ when trying to assign json string literal to custom json type") SECTION("issue #972 - Segmentation fault on G++ when trying to assign json string literal to custom json type")
{ {
my_json foo = R"([1, 2, 3])"_json; my_json const foo = R"([1, 2, 3])"_json;
} }
SECTION("issue #977 - Assigning between different json types") SECTION("issue #977 - Assigning between different json types")
@ -1478,7 +1478,7 @@ TEST_CASE("regression tests 1")
CHECK(lj.size() == 1); CHECK(lj.size() == 1);
CHECK(lj["x"] == 3); CHECK(lj["x"] == 3);
CHECK(ff.x == 3); CHECK(ff.x == 3);
nlohmann::json nj = lj; // This line works as expected nlohmann::json const nj = lj; // This line works as expected
} }
} }

View File

@ -40,8 +40,8 @@ TEST_CASE("wide strings")
{ {
if (wstring_is_utf16()) if (wstring_is_utf16())
{ {
std::wstring w = L"[12.2,\"Ⴥaäö💤🧢\"]"; std::wstring const w = L"[12.2,\"Ⴥaäö💤🧢\"]";
json j = json::parse(w); json const j = json::parse(w);
CHECK(j.dump() == "[12.2,\"Ⴥaäö💤🧢\"]"); CHECK(j.dump() == "[12.2,\"Ⴥaäö💤🧢\"]");
} }
} }
@ -50,7 +50,7 @@ TEST_CASE("wide strings")
{ {
if (wstring_is_utf16()) if (wstring_is_utf16())
{ {
std::wstring w = L"\"\xDBFF"; std::wstring const w = L"\"\xDBFF";
json _; json _;
CHECK_THROWS_AS(_ = json::parse(w), json::parse_error&); CHECK_THROWS_AS(_ = json::parse(w), json::parse_error&);
} }
@ -60,8 +60,8 @@ TEST_CASE("wide strings")
{ {
if (u16string_is_utf16()) if (u16string_is_utf16())
{ {
std::u16string w = u"[12.2,\"Ⴥaäö💤🧢\"]"; std::u16string const w = u"[12.2,\"Ⴥaäö💤🧢\"]";
json j = json::parse(w); json const j = json::parse(w);
CHECK(j.dump() == "[12.2,\"Ⴥaäö💤🧢\"]"); CHECK(j.dump() == "[12.2,\"Ⴥaäö💤🧢\"]");
} }
} }
@ -70,7 +70,7 @@ TEST_CASE("wide strings")
{ {
if (wstring_is_utf16()) if (wstring_is_utf16())
{ {
std::u16string w = u"\"\xDBFF"; std::u16string const w = u"\"\xDBFF";
json _; json _;
CHECK_THROWS_AS(_ = json::parse(w), json::parse_error&); CHECK_THROWS_AS(_ = json::parse(w), json::parse_error&);
} }
@ -80,8 +80,8 @@ TEST_CASE("wide strings")
{ {
if (u32string_is_utf32()) if (u32string_is_utf32())
{ {
std::u32string w = U"[12.2,\"Ⴥaäö💤🧢\"]"; std::u32string const w = U"[12.2,\"Ⴥaäö💤🧢\"]";
json j = json::parse(w); json const j = json::parse(w);
CHECK(j.dump() == "[12.2,\"Ⴥaäö💤🧢\"]"); CHECK(j.dump() == "[12.2,\"Ⴥaäö💤🧢\"]");
} }
} }
@ -90,7 +90,7 @@ TEST_CASE("wide strings")
{ {
if (u32string_is_utf32()) if (u32string_is_utf32())
{ {
std::u32string w = U"\"\x110000"; std::u32string const w = U"\"\x110000";
json _; json _;
CHECK_THROWS_AS(_ = json::parse(w), json::parse_error&); CHECK_THROWS_AS(_ = json::parse(w), json::parse_error&);
} }