Add clang-tools to required tools for ci_static_analysis_clang (#3724)

* 💚 add clang-tools to required tools for ci_static_analysis_clang

* 🚨 update Clang-Tidy warning selection

* 🚨 fix Clang-Tidy warnings

* 🚨 fix Clang-Tidy warnings

* 🚨 fix Clang-Tidy warnings

* 🚨 fix Clang-Tidy warnings

* 🚨 fix Clang-Tidy warnings

* 🚨 fix Clang-Tidy warnings

* 🚨 fix Clang-Tidy warnings

* 🚨 fix Clang-Tidy warnings

* 🚨 fix Clang-Tidy warnings

* 🚨 fix Clang-Tidy warnings

* 🚨 fix Clang-Tidy warnings (#3738)

*  revert fix

*  revert fix

* 🚨 fix Clang-Tidy warnings (#3739)

Co-authored-by: Florian Albrechtskirchinger <falbrechtskirchinger@gmail.com>
pull/3740/head^2
Niels Lohmann 2022-09-13 12:58:26 +02:00 committed by GitHub
parent 307c053b9b
commit 58bd97e2b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
67 changed files with 2757 additions and 2626 deletions

View File

@ -6,6 +6,7 @@ Checks: '*,
-bugprone-easily-swappable-parameters,
-cert-err58-cpp,
-concurrency-mt-unsafe,
-cppcoreguidelines-avoid-const-or-ref-data-members,
-cppcoreguidelines-avoid-goto,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-avoid-non-const-global-variables,

View File

@ -73,7 +73,7 @@ jobs:
target: [ci_clang_tidy, ci_test_clang_sanitizer, ci_clang_analyze]
steps:
- name: Install git
run: apt-get update ; apt-get install -y git
run: apt-get update ; apt-get install -y git clang-tools
- uses: actions/checkout@v3
- name: Get latest CMake and ninja
uses: lukka/get-cmake@latest

View File

@ -149,17 +149,17 @@ class parse_error : public exception
template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
static parse_error create(int id_, const position_t& pos, const std::string& what_arg, BasicJsonContext context)
{
std::string w = concat(exception::name("parse_error", id_), "parse error",
position_string(pos), ": ", exception::diagnostics(context), what_arg);
const std::string w = concat(exception::name("parse_error", id_), "parse error",
position_string(pos), ": ", exception::diagnostics(context), what_arg);
return {id_, pos.chars_read_total, w.c_str()};
}
template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, BasicJsonContext context)
{
std::string w = concat(exception::name("parse_error", id_), "parse error",
(byte_ != 0 ? (concat(" at byte ", std::to_string(byte_))) : ""),
": ", exception::diagnostics(context), what_arg);
const std::string w = concat(exception::name("parse_error", id_), "parse error",
(byte_ != 0 ? (concat(" at byte ", std::to_string(byte_))) : ""),
": ", exception::diagnostics(context), what_arg);
return {id_, byte_, w.c_str()};
}
@ -193,7 +193,7 @@ class invalid_iterator : public exception
template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
static invalid_iterator create(int id_, const std::string& what_arg, BasicJsonContext context)
{
std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg);
const std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg);
return {id_, w.c_str()};
}
@ -211,7 +211,7 @@ class type_error : public exception
template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
static type_error create(int id_, const std::string& what_arg, BasicJsonContext context)
{
std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg);
const std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg);
return {id_, w.c_str()};
}
@ -228,7 +228,7 @@ class out_of_range : public exception
template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
static out_of_range create(int id_, const std::string& what_arg, BasicJsonContext context)
{
std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg);
const std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg);
return {id_, w.c_str()};
}
@ -245,7 +245,7 @@ class other_error : public exception
template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
static other_error create(int id_, const std::string& what_arg, BasicJsonContext context)
{
std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg);
const std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg);
return {id_, w.c_str()};
}

View File

@ -330,7 +330,7 @@ class binary_reader
{
std::array<char, 3> cr{{}};
static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
std::string cr_str{cr.data()};
const std::string cr_str{cr.data()};
return sax->parse_error(element_type_parse_position, cr_str,
parse_error::create(114, element_type_parse_position, concat("Unsupported BSON record type 0x", cr_str), nullptr));
}
@ -2265,7 +2265,7 @@ class binary_reader
exception_message(input_format, concat("expected '#' after type information; last byte: 0x", last_token), "size"), nullptr));
}
bool is_error = get_ubjson_size_value(result.first, is_ndarray);
const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
if (input_format == input_format_t::bjdata && is_ndarray)
{
if (inside_ndarray)
@ -2280,7 +2280,7 @@ class binary_reader
if (current == '#')
{
bool is_error = get_ubjson_size_value(result.first, is_ndarray);
const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
if (input_format == input_format_t::bjdata && is_ndarray)
{
return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,

View File

@ -230,7 +230,7 @@ class json_pointer
const char* p = s.c_str();
char* p_end = nullptr;
errno = 0; // strtoull doesn't reset errno
unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int)
const unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int)
if (p == p_end // invalid input or empty string
|| errno == ERANGE // out of range
|| JSON_HEDLEY_UNLIKELY(static_cast<std::size_t>(p_end - p) != s.size())) // incomplete read

View File

@ -396,7 +396,7 @@
#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) \
friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { Type nlohmann_json_default_obj; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
/*!
@brief macro
@ -409,7 +409,7 @@
#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \
inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { Type nlohmann_json_default_obj; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
// inspired from https://stackoverflow.com/a/26745591

View File

@ -661,18 +661,18 @@ class binary_writer
}
else if (N <= (std::numeric_limits<std::uint16_t>::max)())
{
std::uint8_t output_type = use_ext
? 0xC8 // ext 16
: 0xC5; // bin 16
const std::uint8_t output_type = use_ext
? 0xC8 // ext 16
: 0xC5; // bin 16
oa->write_character(to_char_type(output_type));
write_number(static_cast<std::uint16_t>(N));
}
else if (N <= (std::numeric_limits<std::uint32_t>::max)())
{
std::uint8_t output_type = use_ext
? 0xC9 // ext 32
: 0xC6; // bin 32
const std::uint8_t output_type = use_ext
? 0xC9 // ext 32
: 0xC6; // bin 32
oa->write_character(to_char_type(output_type));
write_number(static_cast<std::uint32_t>(N));
@ -1258,8 +1258,8 @@ class binary_writer
*/
static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
{
std::size_t document_size = std::accumulate(value.begin(), value.end(), static_cast<std::size_t>(0),
[](size_t result, const typename BasicJsonType::object_t::value_type & el)
const std::size_t document_size = std::accumulate(value.begin(), value.end(), static_cast<std::size_t>(0),
[](size_t result, const typename BasicJsonType::object_t::value_type & el)
{
return result += calc_bson_element_size(el.first, el.second);
});

View File

@ -926,7 +926,7 @@ class serializer
? (byte & 0x3fu) | (codep << 6u)
: (0xFFu >> type) & (byte);
std::size_t index = 256u + static_cast<size_t>(state) * 16u + static_cast<size_t>(type);
const std::size_t index = 256u + static_cast<size_t>(state) * 16u + static_cast<size_t>(type);
JSON_ASSERT(index < 400);
state = utf8d[index];
return state;

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
json_pointer top_pointer = ptr.top();
json_pointer const top_pointer = ptr.top();
if (top_pointer != ptr)
{
result.at(top_pointer);
@ -4880,7 +4880,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
json_pointer from_ptr(from_path);
// 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
// "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);
// 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"
// operation at the target location using the value

View File

@ -2748,7 +2748,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP
#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) \
friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { Type nlohmann_json_default_obj; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
/*!
@brief macro
@ -2761,7 +2761,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP
#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \
inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { Type nlohmann_json_default_obj; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
// inspired from https://stackoverflow.com/a/26745591
@ -4414,17 +4414,17 @@ class parse_error : public exception
template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
static parse_error create(int id_, const position_t& pos, const std::string& what_arg, BasicJsonContext context)
{
std::string w = concat(exception::name("parse_error", id_), "parse error",
position_string(pos), ": ", exception::diagnostics(context), what_arg);
const std::string w = concat(exception::name("parse_error", id_), "parse error",
position_string(pos), ": ", exception::diagnostics(context), what_arg);
return {id_, pos.chars_read_total, w.c_str()};
}
template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, BasicJsonContext context)
{
std::string w = concat(exception::name("parse_error", id_), "parse error",
(byte_ != 0 ? (concat(" at byte ", std::to_string(byte_))) : ""),
": ", exception::diagnostics(context), what_arg);
const std::string w = concat(exception::name("parse_error", id_), "parse error",
(byte_ != 0 ? (concat(" at byte ", std::to_string(byte_))) : ""),
": ", exception::diagnostics(context), what_arg);
return {id_, byte_, w.c_str()};
}
@ -4458,7 +4458,7 @@ class invalid_iterator : public exception
template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
static invalid_iterator create(int id_, const std::string& what_arg, BasicJsonContext context)
{
std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg);
const std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg);
return {id_, w.c_str()};
}
@ -4476,7 +4476,7 @@ class type_error : public exception
template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
static type_error create(int id_, const std::string& what_arg, BasicJsonContext context)
{
std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg);
const std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg);
return {id_, w.c_str()};
}
@ -4493,7 +4493,7 @@ class out_of_range : public exception
template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
static out_of_range create(int id_, const std::string& what_arg, BasicJsonContext context)
{
std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg);
const std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg);
return {id_, w.c_str()};
}
@ -4510,7 +4510,7 @@ class other_error : public exception
template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
static other_error create(int id_, const std::string& what_arg, BasicJsonContext context)
{
std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg);
const std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg);
return {id_, w.c_str()};
}
@ -9408,7 +9408,7 @@ class binary_reader
{
std::array<char, 3> cr{{}};
static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
std::string cr_str{cr.data()};
const std::string cr_str{cr.data()};
return sax->parse_error(element_type_parse_position, cr_str,
parse_error::create(114, element_type_parse_position, concat("Unsupported BSON record type 0x", cr_str), nullptr));
}
@ -11343,7 +11343,7 @@ class binary_reader
exception_message(input_format, concat("expected '#' after type information; last byte: 0x", last_token), "size"), nullptr));
}
bool is_error = get_ubjson_size_value(result.first, is_ndarray);
const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
if (input_format == input_format_t::bjdata && is_ndarray)
{
if (inside_ndarray)
@ -11358,7 +11358,7 @@ class binary_reader
if (current == '#')
{
bool is_error = get_ubjson_size_value(result.first, is_ndarray);
const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
if (input_format == input_format_t::bjdata && is_ndarray)
{
return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
@ -13949,7 +13949,7 @@ class json_pointer
const char* p = s.c_str();
char* p_end = nullptr;
errno = 0; // strtoull doesn't reset errno
unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int)
const unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int)
if (p == p_end // invalid input or empty string
|| errno == ERANGE // out of range
|| JSON_HEDLEY_UNLIKELY(static_cast<std::size_t>(p_end - p) != s.size())) // incomplete read
@ -15614,18 +15614,18 @@ class binary_writer
}
else if (N <= (std::numeric_limits<std::uint16_t>::max)())
{
std::uint8_t output_type = use_ext
? 0xC8 // ext 16
: 0xC5; // bin 16
const std::uint8_t output_type = use_ext
? 0xC8 // ext 16
: 0xC5; // bin 16
oa->write_character(to_char_type(output_type));
write_number(static_cast<std::uint16_t>(N));
}
else if (N <= (std::numeric_limits<std::uint32_t>::max)())
{
std::uint8_t output_type = use_ext
? 0xC9 // ext 32
: 0xC6; // bin 32
const std::uint8_t output_type = use_ext
? 0xC9 // ext 32
: 0xC6; // bin 32
oa->write_character(to_char_type(output_type));
write_number(static_cast<std::uint32_t>(N));
@ -16211,8 +16211,8 @@ class binary_writer
*/
static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
{
std::size_t document_size = std::accumulate(value.begin(), value.end(), static_cast<std::size_t>(0),
[](size_t result, const typename BasicJsonType::object_t::value_type & el)
const std::size_t document_size = std::accumulate(value.begin(), value.end(), static_cast<std::size_t>(0),
[](size_t result, const typename BasicJsonType::object_t::value_type & el)
{
return result += calc_bson_element_size(el.first, el.second);
});
@ -18848,7 +18848,7 @@ class serializer
? (byte & 0x3fu) | (codep << 6u)
: (0xFFu >> type) & (byte);
std::size_t index = 256u + static_cast<size_t>(state) * 16u + static_cast<size_t>(type);
const std::size_t index = 256u + static_cast<size_t>(state) * 16u + static_cast<size_t>(type);
JSON_ASSERT(index < 400);
state = utf8d[index];
return state;
@ -23934,7 +23934,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
}
// 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)
{
result.at(top_pointer);
@ -24096,7 +24096,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
json_pointer from_ptr(from_path);
// 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
// "remove" operation on the "from" location, followed
@ -24113,7 +24113,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
const json_pointer from_ptr(from_path);
// 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"
// operation at the target location using the value

View File

@ -25,7 +25,7 @@ TEST_CASE("custom namespace")
std::string expected = "nlohmann::basic_json";
// fallback for Clang
std::string ns{STRINGIZE(NLOHMANN_JSON_NAMESPACE) "::basic_json"};
const std::string ns{STRINGIZE(NLOHMANN_JSON_NAMESPACE) "::basic_json"};
CHECK(namespace_name<nlohmann::json>(ns) == expected);
}

View File

@ -33,7 +33,7 @@ TEST_CASE("default namespace")
expected += "_" STRINGIZE(NLOHMANN_JSON_VERSION_PATCH) "::basic_json";
// fallback for Clang
std::string ns{STRINGIZE(NLOHMANN_JSON_NAMESPACE) "::basic_json"};
const std::string ns{STRINGIZE(NLOHMANN_JSON_NAMESPACE) "::basic_json"};
CHECK(namespace_name<nlohmann::json>(ns) == expected);
}

View File

@ -32,7 +32,7 @@ TEST_CASE("default namespace without version component")
expected += "::basic_json";
// fallback for Clang
std::string ns{STRINGIZE(NLOHMANN_JSON_NAMESPACE) "::basic_json"};
const std::string ns{STRINGIZE(NLOHMANN_JSON_NAMESPACE) "::basic_json"};
CHECK(namespace_name<nlohmann::json>(ns) == expected);
}

View File

@ -26,7 +26,7 @@ int main()
#endif
// copy stdin to byte vector
std::vector<uint8_t> vec;
char c;
char c = 0;
while (std::cin.get(c))
{
vec.push_back(static_cast<uint8_t>(c));

View File

@ -37,24 +37,24 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
try
{
// step 1: parse input
std::vector<uint8_t> vec1(data, data + size);
json j1 = json::from_bjdata(vec1);
std::vector<uint8_t> const vec1(data, data + size);
json const j1 = json::from_bjdata(vec1);
try
{
// step 2.1: round trip without adding size annotations to container types
std::vector<uint8_t> vec2 = json::to_bjdata(j1, false, false);
std::vector<uint8_t> const vec2 = json::to_bjdata(j1, false, false);
// step 2.2: round trip with adding size annotations but without adding type annonations to container types
std::vector<uint8_t> vec3 = json::to_bjdata(j1, true, false);
std::vector<uint8_t> const vec3 = json::to_bjdata(j1, true, false);
// step 2.3: round trip with adding size as well as type annotations to container types
std::vector<uint8_t> vec4 = json::to_bjdata(j1, true, true);
std::vector<uint8_t> const vec4 = json::to_bjdata(j1, true, true);
// parse serialization
json j2 = json::from_bjdata(vec2);
json j3 = json::from_bjdata(vec3);
json j4 = json::from_bjdata(vec4);
json const j2 = json::from_bjdata(vec2);
json const j3 = json::from_bjdata(vec3);
json const j4 = json::from_bjdata(vec4);
// serializations must match
assert(json::to_bjdata(j2, false, false) == vec2);

View File

@ -31,8 +31,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
try
{
// step 1: parse input
std::vector<uint8_t> vec1(data, data + size);
json j1 = json::from_bson(vec1);
std::vector<uint8_t> const vec1(data, data + size);
json const j1 = json::from_bson(vec1);
if (j1.is_discarded())
{
@ -42,10 +42,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
try
{
// step 2: round trip
std::vector<uint8_t> vec2 = json::to_bson(j1);
std::vector<uint8_t> const vec2 = json::to_bson(j1);
// parse serialization
json j2 = json::from_bson(vec2);
json const j2 = json::from_bson(vec2);
// serializations must match
assert(json::to_bson(j2) == vec2);

View File

@ -31,16 +31,16 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
try
{
// step 1: parse input
std::vector<uint8_t> vec1(data, data + size);
json j1 = json::from_cbor(vec1);
std::vector<uint8_t> const vec1(data, data + size);
json const j1 = json::from_cbor(vec1);
try
{
// step 2: round trip
std::vector<uint8_t> vec2 = json::to_cbor(j1);
std::vector<uint8_t> const vec2 = json::to_cbor(j1);
// parse serialization
json j2 = json::from_cbor(vec2);
json const j2 = json::from_cbor(vec2);
// serializations must match
assert(json::to_cbor(j2) == vec2);

View File

@ -32,20 +32,20 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
try
{
// step 1: parse input
json j1 = json::parse(data, data + size);
json const j1 = json::parse(data, data + size);
try
{
// step 2: round trip
// first serialization
std::string s1 = j1.dump();
std::string const s1 = j1.dump();
// parse serialization
json j2 = json::parse(s1);
json const j2 = json::parse(s1);
// second serialization
std::string s2 = j2.dump();
std::string const s2 = j2.dump();
// serializations must match
assert(s1 == s2);

View File

@ -31,16 +31,16 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
try
{
// step 1: parse input
std::vector<uint8_t> vec1(data, data + size);
json j1 = json::from_msgpack(vec1);
std::vector<uint8_t> const vec1(data, data + size);
json const j1 = json::from_msgpack(vec1);
try
{
// step 2: round trip
std::vector<uint8_t> vec2 = json::to_msgpack(j1);
std::vector<uint8_t> const vec2 = json::to_msgpack(j1);
// parse serialization
json j2 = json::from_msgpack(vec2);
json const j2 = json::from_msgpack(vec2);
// serializations must match
assert(json::to_msgpack(j2) == vec2);

View File

@ -37,24 +37,24 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
try
{
// step 1: parse input
std::vector<uint8_t> vec1(data, data + size);
json j1 = json::from_ubjson(vec1);
std::vector<uint8_t> const vec1(data, data + size);
json const j1 = json::from_ubjson(vec1);
try
{
// step 2.1: round trip without adding size annotations to container types
std::vector<uint8_t> vec2 = json::to_ubjson(j1, false, false);
std::vector<uint8_t> const vec2 = json::to_ubjson(j1, false, false);
// step 2.2: round trip with adding size annotations but without adding type annonations to container types
std::vector<uint8_t> vec3 = json::to_ubjson(j1, true, false);
std::vector<uint8_t> const vec3 = json::to_ubjson(j1, true, false);
// step 2.3: round trip with adding size as well as type annotations to container types
std::vector<uint8_t> vec4 = json::to_ubjson(j1, true, true);
std::vector<uint8_t> const vec4 = json::to_ubjson(j1, true, true);
// parse serialization
json j2 = json::from_ubjson(vec2);
json j3 = json::from_ubjson(vec3);
json j4 = json::from_ubjson(vec4);
json const j2 = json::from_ubjson(vec2);
json const j3 = json::from_ubjson(vec3);
json const j4 = json::from_ubjson(vec4);
// serializations must match
assert(json::to_ubjson(j2, false, false) == vec2);

View File

@ -18,7 +18,7 @@ namespace utils
inline bool check_testsuite_downloaded()
{
std::unique_ptr<std::FILE, decltype(&std::fclose)> file(std::fopen(TEST_DATA_DIRECTORY "/README.md", "r"), &std::fclose);
const std::unique_ptr<std::FILE, decltype(&std::fclose)> file(std::fopen(TEST_DATA_DIRECTORY "/README.md", "r"), &std::fclose);
return file != nullptr;
}

View File

@ -33,10 +33,10 @@ TEST_CASE_TEMPLATE_DEFINE("value_in_range_of trait", T, value_in_range_of_test)
constexpr bool min_in_range = T::min_in_range;
constexpr bool max_in_range = T::max_in_range;
type val_min = std::numeric_limits<type>::min();
type val_min2 = val_min + 1;
type val_max = std::numeric_limits<type>::max();
type val_max2 = val_max - 1;
type const val_min = std::numeric_limits<type>::min();
type const val_min2 = val_min + 1;
type const val_max = std::numeric_limits<type>::max();
type const val_max2 = val_max - 1;
REQUIRE(CHAR_BIT == 8);
@ -108,8 +108,8 @@ TEST_CASE("BJData")
{
SECTION("optimized array: negative size")
{
std::vector<uint8_t> vM = {'[', '$', 'M', '#', '[', 'I', 0x00, 0x20, 'M', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xFF, ']'};
std::vector<uint8_t> vMX = {'[', '$', 'U', '#', '[', 'M', 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 'U', 0x01, ']'};
std::vector<uint8_t> const vM = {'[', '$', 'M', '#', '[', 'I', 0x00, 0x20, 'M', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xFF, ']'};
std::vector<uint8_t> const vMX = {'[', '$', 'U', '#', '[', 'M', 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 'U', 0x01, ']'};
json _;
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vM), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&);
@ -121,8 +121,8 @@ TEST_CASE("BJData")
SECTION("optimized array: integer value overflow")
{
std::vector<uint8_t> vL = {'[', '#', 'L', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F};
std::vector<uint8_t> vM = {'[', '$', 'M', '#', '[', 'I', 0x00, 0x20, 'M', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xFF, ']'};
std::vector<uint8_t> const vL = {'[', '#', 'L', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F};
std::vector<uint8_t> const vM = {'[', '$', 'M', '#', '[', 'I', 0x00, 0x20, 'M', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xFF, ']'};
json _;
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vL), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&);

View File

@ -159,7 +159,7 @@ TEST_CASE("controlled bad_alloc")
SECTION("json_value(const string_t&)")
{
next_construct_fails = false;
my_json::string_t v("foo");
const my_json::string_t v("foo");
CHECK_NOTHROW(my_allocator_clean_up(my_json::json_value(v).string));
next_construct_fails = true;
CHECK_THROWS_AS(my_json::json_value(v), std::bad_alloc&);
@ -172,7 +172,7 @@ TEST_CASE("controlled bad_alloc")
SECTION("basic_json(const CompatibleObjectType&)")
{
next_construct_fails = false;
std::map<std::string, std::string> v {{"foo", "bar"}};
const std::map<std::string, std::string> v {{"foo", "bar"}};
CHECK_NOTHROW(my_json(v));
next_construct_fails = true;
CHECK_THROWS_AS(my_json(v), std::bad_alloc&);
@ -182,7 +182,7 @@ TEST_CASE("controlled bad_alloc")
SECTION("basic_json(const CompatibleArrayType&)")
{
next_construct_fails = false;
std::vector<std::string> v {"foo", "bar", "baz"};
const std::vector<std::string> v {"foo", "bar", "baz"};
CHECK_NOTHROW(my_json(v));
next_construct_fails = true;
CHECK_THROWS_AS(my_json(v), std::bad_alloc&);
@ -201,7 +201,7 @@ TEST_CASE("controlled bad_alloc")
SECTION("basic_json(const typename string_t::value_type*)")
{
next_construct_fails = false;
std::string s("foo");
const std::string s("foo");
CHECK_NOTHROW(my_json(s));
next_construct_fails = true;
CHECK_THROWS_AS(my_json(s), std::bad_alloc&);

View File

@ -148,7 +148,7 @@ class alt_string
alt_string substr(std::size_t pos = 0, std::size_t count = npos) const
{
std::string s = str_impl.substr(pos, count);
const std::string s = str_impl.substr(pos, count);
return {s.data(), s.size()};
}

View File

@ -32,7 +32,7 @@ TEST_CASE("JSON_ASSERT(x)")
assert_counter = 0;
CHECK(assert_counter == 0);
json::iterator it;
const json::iterator it{};
json j;
// in case assertions do not abort execution, an exception is thrown

View File

@ -19,7 +19,7 @@ TEST_CASE("Binary Formats" * doctest::skip())
SECTION("canada.json")
{
const auto* filename = TEST_DATA_DIRECTORY "/nativejson-benchmark/canada.json";
json j = json::parse(std::ifstream(filename));
const json j = json::parse(std::ifstream(filename));
const auto json_size = j.dump().size();
const auto bjdata_1_size = json::to_bjdata(j).size();
@ -58,7 +58,7 @@ TEST_CASE("Binary Formats" * doctest::skip())
SECTION("twitter.json")
{
const auto* filename = TEST_DATA_DIRECTORY "/nativejson-benchmark/twitter.json";
json j = json::parse(std::ifstream(filename));
const json j = json::parse(std::ifstream(filename));
const auto json_size = j.dump().size();
const auto bjdata_1_size = json::to_bjdata(j).size();
@ -97,7 +97,7 @@ TEST_CASE("Binary Formats" * doctest::skip())
SECTION("citm_catalog.json")
{
const auto* filename = TEST_DATA_DIRECTORY "/nativejson-benchmark/citm_catalog.json";
json j = json::parse(std::ifstream(filename));
const json j = json::parse(std::ifstream(filename));
const auto json_size = j.dump().size();
const auto bjdata_1_size = json::to_bjdata(j).size();
@ -175,7 +175,7 @@ TEST_CASE("Binary Formats" * doctest::skip())
SECTION("sample.json")
{
const auto* filename = TEST_DATA_DIRECTORY "/json_testsuite/sample.json";
json j = json::parse(std::ifstream(filename));
const json j = json::parse(std::ifstream(filename));
const auto json_size = j.dump().size();
const auto bjdata_1_size = json::to_bjdata(j).size();

File diff suppressed because it is too large Load Diff

View File

@ -22,7 +22,7 @@ TEST_CASE("BSON")
{
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&);
}
@ -30,45 +30,45 @@ TEST_CASE("BSON")
{
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&);
}
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&);
}
}
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&);
}
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&);
}
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&);
}
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&);
}
}
SECTION("keys containing code-point U+0000 cannot be serialized to BSON")
{
json j =
json const j =
{
{ std::string("en\0try", 6), true }
};
@ -82,7 +82,7 @@ TEST_CASE("BSON")
SECTION("string length must be at least 1")
{
// 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,
0x02,
@ -97,8 +97,8 @@ TEST_CASE("BSON")
{
SECTION("empty object")
{
json j = json::object();
std::vector<std::uint8_t> expected =
json const j = json::object();
std::vector<std::uint8_t> const expected =
{
0x05, 0x00, 0x00, 0x00, // size (little endian)
// no entries
@ -115,12 +115,12 @@ TEST_CASE("BSON")
SECTION("non-empty object with bool")
{
json j =
json const j =
{
{ "entry", true }
};
std::vector<std::uint8_t> expected =
std::vector<std::uint8_t> const expected =
{
0x0D, 0x00, 0x00, 0x00, // size (little endian)
0x08, // entry: boolean
@ -139,12 +139,12 @@ TEST_CASE("BSON")
SECTION("non-empty object with bool")
{
json j =
json const j =
{
{ "entry", false }
};
std::vector<std::uint8_t> expected =
std::vector<std::uint8_t> const expected =
{
0x0D, 0x00, 0x00, 0x00, // size (little endian)
0x08, // entry: boolean
@ -163,12 +163,12 @@ TEST_CASE("BSON")
SECTION("non-empty object with double")
{
json j =
json const j =
{
{ "entry", 4.2 }
};
std::vector<std::uint8_t> expected =
std::vector<std::uint8_t> const expected =
{
0x14, 0x00, 0x00, 0x00, // size (little endian)
0x01, /// entry: double
@ -187,12 +187,12 @@ TEST_CASE("BSON")
SECTION("non-empty object with string")
{
json j =
json const j =
{
{ "entry", "bsonstr" }
};
std::vector<std::uint8_t> expected =
std::vector<std::uint8_t> const expected =
{
0x18, 0x00, 0x00, 0x00, // size (little endian)
0x02, /// entry: string (UTF-8)
@ -211,12 +211,12 @@ TEST_CASE("BSON")
SECTION("non-empty object with null member")
{
json j =
json const j =
{
{ "entry", nullptr }
};
std::vector<std::uint8_t> expected =
std::vector<std::uint8_t> const expected =
{
0x0C, 0x00, 0x00, 0x00, // size (little endian)
0x0A, /// entry: null
@ -234,12 +234,12 @@ TEST_CASE("BSON")
SECTION("non-empty object with integer (32-bit) member")
{
json j =
json const j =
{
{ "entry", std::int32_t{0x12345678} }
};
std::vector<std::uint8_t> expected =
std::vector<std::uint8_t> const expected =
{
0x10, 0x00, 0x00, 0x00, // size (little endian)
0x10, /// entry: int32
@ -258,12 +258,12 @@ TEST_CASE("BSON")
SECTION("non-empty object with integer (64-bit) member")
{
json j =
json const j =
{
{ "entry", std::int64_t{0x1234567804030201} }
};
std::vector<std::uint8_t> expected =
std::vector<std::uint8_t> const expected =
{
0x14, 0x00, 0x00, 0x00, // size (little endian)
0x12, /// entry: int64
@ -282,12 +282,12 @@ TEST_CASE("BSON")
SECTION("non-empty object with negative integer (32-bit) member")
{
json j =
json const j =
{
{ "entry", std::int32_t{-1} }
};
std::vector<std::uint8_t> expected =
std::vector<std::uint8_t> const expected =
{
0x10, 0x00, 0x00, 0x00, // size (little endian)
0x10, /// entry: int32
@ -306,12 +306,12 @@ TEST_CASE("BSON")
SECTION("non-empty object with negative integer (64-bit) member")
{
json j =
json const j =
{
{ "entry", std::int64_t{-1} }
};
std::vector<std::uint8_t> expected =
std::vector<std::uint8_t> const expected =
{
0x10, 0x00, 0x00, 0x00, // size (little endian)
0x10, /// entry: int32
@ -331,12 +331,12 @@ TEST_CASE("BSON")
SECTION("non-empty object with unsigned integer (64-bit) member")
{
// directly encoding uint64 is not supported in bson (only for timestamp values)
json j =
json const j =
{
{ "entry", std::uint64_t{0x1234567804030201} }
};
std::vector<std::uint8_t> expected =
std::vector<std::uint8_t> const expected =
{
0x14, 0x00, 0x00, 0x00, // size (little endian)
0x12, /// entry: int64
@ -355,12 +355,12 @@ TEST_CASE("BSON")
SECTION("non-empty object with small unsigned integer member")
{
json j =
json const j =
{
{ "entry", std::uint64_t{0x42} }
};
std::vector<std::uint8_t> expected =
std::vector<std::uint8_t> const expected =
{
0x10, 0x00, 0x00, 0x00, // size (little endian)
0x10, /// entry: int32
@ -379,12 +379,12 @@ TEST_CASE("BSON")
SECTION("non-empty object with object member")
{
json j =
json const j =
{
{ "entry", json::object() }
};
std::vector<std::uint8_t> expected =
std::vector<std::uint8_t> const expected =
{
0x11, 0x00, 0x00, 0x00, // size (little endian)
0x03, /// entry: embedded document
@ -407,12 +407,12 @@ TEST_CASE("BSON")
SECTION("non-empty object with array member")
{
json j =
json const j =
{
{ "entry", json::array() }
};
std::vector<std::uint8_t> expected =
std::vector<std::uint8_t> const expected =
{
0x11, 0x00, 0x00, 0x00, // size (little endian)
0x04, /// entry: embedded document
@ -435,12 +435,12 @@ TEST_CASE("BSON")
SECTION("non-empty object with non-empty array member")
{
json j =
json const j =
{
{ "entry", json::array({1, 2, 3, 4, 5, 6, 7, 8}) }
};
std::vector<std::uint8_t> expected =
std::vector<std::uint8_t> const expected =
{
0x49, 0x00, 0x00, 0x00, // size (little endian)
0x04, /// entry: embedded document
@ -472,12 +472,12 @@ TEST_CASE("BSON")
{
const size_t N = 10;
const auto s = std::vector<std::uint8_t>(N, 'x');
json j =
json const j =
{
{ "entry", json::binary(s, 0) }
};
std::vector<std::uint8_t> expected =
std::vector<std::uint8_t> const expected =
{
0x1B, 0x00, 0x00, 0x00, // size (little endian)
0x05, // entry: binary
@ -502,12 +502,12 @@ TEST_CASE("BSON")
{
// an MD5 hash
const std::vector<std::uint8_t> md5hash = {0xd7, 0x7e, 0x27, 0x54, 0xbe, 0x12, 0x37, 0xfe, 0xd6, 0x0c, 0x33, 0x98, 0x30, 0x3b, 0x8d, 0xc4};
json j =
json const j =
{
{ "entry", json::binary(md5hash, 5) }
};
std::vector<std::uint8_t> expected =
std::vector<std::uint8_t> const expected =
{
0x21, 0x00, 0x00, 0x00, // size (little endian)
0x05, // entry: binary
@ -531,7 +531,7 @@ TEST_CASE("BSON")
SECTION("Some more complex document")
{
// directly encoding uint64 is not supported in bson (only for timestamp values)
json j =
json const j =
{
{"double", 42.5},
{"entry", 4.2},
@ -539,7 +539,7 @@ TEST_CASE("BSON")
{"object", {{ "string", "value" }}}
};
std::vector<std::uint8_t> expected =
std::vector<std::uint8_t> const expected =
{
/*size */ 0x4f, 0x00, 0x00, 0x00,
/*entry*/ 0x01, 'd', 'o', 'u', 'b', 'l', 'e', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x45, 0x40,
@ -597,7 +597,7 @@ TEST_CASE("BSON input/output_adapters")
{"object", {{ "string", "value" }}}
};
std::vector<std::uint8_t> bson_representation =
std::vector<std::uint8_t> const bson_representation =
{
/*size */ 0x4f, 0x00, 0x00, 0x00,
/*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")
{
std::vector<std::uint8_t> incomplete_bson =
std::vector<std::uint8_t> const incomplete_bson =
{
0x0D, 0x00, 0x00, 0x00, // size (little endian)
0x08, // entry: boolean
@ -744,7 +744,7 @@ TEST_CASE("Incomplete BSON Input")
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)
0x08, // entry: boolean, unexpected EOF
@ -760,7 +760,7 @@ TEST_CASE("Incomplete BSON Input")
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)
0x04, /// entry: embedded document
@ -782,7 +782,7 @@ TEST_CASE("Incomplete BSON Input")
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
};
@ -799,7 +799,7 @@ TEST_CASE("Incomplete BSON Input")
{
SECTION("key")
{
json j = {{"key", "value"}};
json const j = {{"key", "value"}};
auto bson_vec = json::to_bson(j);
SaxCountdown scp(2);
CHECK(!json::sax_parse(bson_vec, &scp, json::input_format_t::bson));
@ -807,7 +807,7 @@ TEST_CASE("Incomplete BSON Input")
SECTION("array")
{
json j =
json const j =
{
{ "entry", json::array() }
};
@ -821,7 +821,7 @@ TEST_CASE("Incomplete BSON Input")
TEST_CASE("Negative size of binary value")
{
// 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)
0x05, // entry: binary
@ -839,7 +839,7 @@ TEST_CASE("Negative size of binary value")
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)
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")
{
std::vector<int64_t> numbers
std::vector<int64_t> const numbers
{
INT64_MIN,
-1000000000000000000LL,
@ -878,19 +878,19 @@ TEST_CASE("BSON numerical data")
static_cast<std::int64_t>(INT32_MIN) - 1,
};
for (auto i : numbers)
for (const auto i : numbers)
{
CAPTURE(i)
json j =
json const j =
{
{ "entry", i }
};
CHECK(j.at("entry").is_number_integer());
std::uint64_t iu = *reinterpret_cast<std::uint64_t*>(&i);
std::vector<std::uint8_t> expected_bson =
std::uint64_t const iu = *reinterpret_cast<const std::uint64_t*>(&i);
std::vector<std::uint8_t> const expected_bson =
{
0x14u, 0x00u, 0x00u, 0x00u, // size (little endian)
0x12u, /// entry: int64
@ -921,7 +921,7 @@ TEST_CASE("BSON numerical data")
SECTION("signed std::int32_t: INT32_MIN .. INT32_MAX")
{
std::vector<int32_t> numbers
std::vector<int32_t> const numbers
{
INT32_MIN,
-2147483647L,
@ -950,19 +950,19 @@ TEST_CASE("BSON numerical data")
INT32_MAX
};
for (auto i : numbers)
for (const auto i : numbers)
{
CAPTURE(i)
json j =
json const j =
{
{ "entry", i }
};
CHECK(j.at("entry").is_number_integer());
std::uint32_t iu = *reinterpret_cast<std::uint32_t*>(&i);
std::vector<std::uint8_t> expected_bson =
std::uint32_t const iu = *reinterpret_cast<const std::uint32_t*>(&i);
std::vector<std::uint8_t> const expected_bson =
{
0x10u, 0x00u, 0x00u, 0x00u, // size (little endian)
0x10u, /// entry: int32
@ -988,7 +988,7 @@ TEST_CASE("BSON numerical data")
SECTION("signed std::int64_t: INT32_MAX+1 .. INT64_MAX")
{
std::vector<int64_t> numbers
std::vector<int64_t> const numbers
{
INT64_MAX,
1000000000000000000LL,
@ -1003,19 +1003,19 @@ TEST_CASE("BSON numerical data")
static_cast<std::int64_t>(INT32_MAX) + 1,
};
for (auto i : numbers)
for (const auto i : numbers)
{
CAPTURE(i)
json j =
json const j =
{
{ "entry", i }
};
CHECK(j.at("entry").is_number_integer());
std::uint64_t iu = *reinterpret_cast<std::uint64_t*>(&i);
std::vector<std::uint8_t> expected_bson =
std::uint64_t const iu = *reinterpret_cast<const std::uint64_t*>(&i);
std::vector<std::uint8_t> const expected_bson =
{
0x14u, 0x00u, 0x00u, 0x00u, // size (little endian)
0x12u, /// entry: int64
@ -1048,7 +1048,7 @@ TEST_CASE("BSON numerical data")
{
SECTION("unsigned std::uint64_t: 0 .. INT32_MAX")
{
std::vector<std::uint64_t> numbers
std::vector<std::uint64_t> const numbers
{
0ULL,
1ULL,
@ -1065,18 +1065,18 @@ TEST_CASE("BSON numerical data")
static_cast<std::uint64_t>(INT32_MAX)
};
for (auto i : numbers)
for (const auto i : numbers)
{
CAPTURE(i)
json j =
json const j =
{
{ "entry", i }
};
auto iu = i;
std::vector<std::uint8_t> expected_bson =
std::vector<std::uint8_t> const expected_bson =
{
0x10u, 0x00u, 0x00u, 0x00u, // size (little endian)
0x10u, /// entry: int32
@ -1103,7 +1103,7 @@ TEST_CASE("BSON numerical data")
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,
4000000000ULL,
@ -1120,18 +1120,18 @@ TEST_CASE("BSON numerical data")
static_cast<std::uint64_t>(INT64_MAX),
};
for (auto i : numbers)
for (const auto i : numbers)
{
CAPTURE(i)
json j =
json const j =
{
{ "entry", 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)
0x12u, /// entry: int64
@ -1161,7 +1161,7 @@ TEST_CASE("BSON numerical data")
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,
10000000000000000000ULL,
@ -1170,18 +1170,18 @@ TEST_CASE("BSON numerical data")
UINT64_MAX,
};
for (auto i : numbers)
for (const auto i : numbers)
{
CAPTURE(i)
json j =
json const j =
{
{ "entry", 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)
0x12u, /// entry: int64
@ -1214,7 +1214,7 @@ TEST_CASE("BSON roundtrips" * doctest::skip())
{
SECTION("reference files")
{
for (std::string filename :
for (const std::string filename :
{
TEST_DATA_DIRECTORY "/json.org/1.json",
TEST_DATA_DIRECTORY "/json.org/2.json",
@ -1274,7 +1274,7 @@ TEST_CASE("BSON roundtrips" * doctest::skip())
INFO_WITH_TEMP(filename + ": output to output adapters");
// parse JSON file
std::ifstream f_json(filename);
json j1 = json::parse(f_json);
json const j1 = json::parse(f_json);
// parse BSON file
auto packed = utils::read_binary_file(filename + ".bson");

View File

@ -45,7 +45,7 @@ TEST_CASE("byte_container_with_subtype")
SECTION("comparisons")
{
std::vector<std::uint8_t> bytes = {{0xCA, 0xFE, 0xBA, 0xBE}};
std::vector<std::uint8_t> const bytes = {{0xCA, 0xFE, 0xBA, 0xBE}};
nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> container1;
nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> container2({}, 42);
nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> container3(bytes);

View File

@ -18,8 +18,8 @@ TEST_CASE("capacity")
{
SECTION("boolean")
{
json j = true;
const json j_const(j);
json j = true; // NOLINT(misc-const-correctness)
const json j_const = true;
SECTION("result of empty")
{
@ -36,8 +36,8 @@ TEST_CASE("capacity")
SECTION("string")
{
json j = "hello world";
const json j_const(j);
json j = "hello world"; // NOLINT(misc-const-correctness)
const json j_const = "hello world";
SECTION("result of empty")
{
@ -56,8 +56,8 @@ TEST_CASE("capacity")
{
SECTION("empty array")
{
json j = json::array();
const json j_const(j);
json j = json::array(); // NOLINT(misc-const-correctness)
const json j_const = json::array();
SECTION("result of empty")
{
@ -74,8 +74,8 @@ TEST_CASE("capacity")
SECTION("filled array")
{
json j = {1, 2, 3};
const json j_const(j);
json j = {1, 2, 3}; // NOLINT(misc-const-correctness)
const json j_const = {1, 2, 3};
SECTION("result of empty")
{
@ -95,8 +95,8 @@ TEST_CASE("capacity")
{
SECTION("empty object")
{
json j = json::object();
const json j_const(j);
json j = json::object(); // NOLINT(misc-const-correctness)
const json j_const = json::object();
SECTION("result of empty")
{
@ -113,8 +113,8 @@ TEST_CASE("capacity")
SECTION("filled object")
{
json j = {{"one", 1}, {"two", 2}, {"three", 3}};
const json j_const(j);
json j = {{"one", 1}, {"two", 2}, {"three", 3}}; // NOLINT(misc-const-correctness)
const json j_const = {{"one", 1}, {"two", 2}, {"three", 3}};
SECTION("result of empty")
{
@ -132,8 +132,8 @@ TEST_CASE("capacity")
SECTION("number (integer)")
{
json j = -23;
const json j_const(j);
json j = -23; // NOLINT(misc-const-correctness)
const json j_const = -23;
SECTION("result of empty")
{
@ -150,8 +150,8 @@ TEST_CASE("capacity")
SECTION("number (unsigned)")
{
json j = 23u;
const json j_const(j);
json j = 23u; // NOLINT(misc-const-correctness)
const json j_const = 23u;
SECTION("result of empty")
{
@ -168,8 +168,8 @@ TEST_CASE("capacity")
SECTION("number (float)")
{
json j = 23.42;
const json j_const(j);
json j = 23.42; // NOLINT(misc-const-correctness)
const json j_const = 23.42;
SECTION("result of empty")
{
@ -186,8 +186,8 @@ TEST_CASE("capacity")
SECTION("null")
{
json j = nullptr;
const json j_const(j);
json j = nullptr; // NOLINT(misc-const-correctness)
const json j_const = nullptr;
SECTION("result of empty")
{
@ -207,8 +207,8 @@ TEST_CASE("capacity")
{
SECTION("boolean")
{
json j = true;
const json j_const(j);
json j = true; // NOLINT(misc-const-correctness)
const json j_const = true;
SECTION("result of size")
{
@ -227,8 +227,8 @@ TEST_CASE("capacity")
SECTION("string")
{
json j = "hello world";
const json j_const(j);
json j = "hello world"; // NOLINT(misc-const-correctness)
const json j_const = "hello world";
SECTION("result of size")
{
@ -249,8 +249,8 @@ TEST_CASE("capacity")
{
SECTION("empty array")
{
json j = json::array();
const json j_const(j);
json j = json::array(); // NOLINT(misc-const-correctness)
const json j_const = json::array();
SECTION("result of size")
{
@ -269,8 +269,8 @@ TEST_CASE("capacity")
SECTION("filled array")
{
json j = {1, 2, 3};
const json j_const(j);
json j = {1, 2, 3}; // NOLINT(misc-const-correctness)
const json j_const = {1, 2, 3};
SECTION("result of size")
{
@ -292,8 +292,8 @@ TEST_CASE("capacity")
{
SECTION("empty object")
{
json j = json::object();
const json j_const(j);
json j = json::object(); // NOLINT(misc-const-correctness)
const json j_const = json::object();
SECTION("result of size")
{
@ -312,8 +312,8 @@ TEST_CASE("capacity")
SECTION("filled object")
{
json j = {{"one", 1}, {"two", 2}, {"three", 3}};
const json j_const(j);
json j = {{"one", 1}, {"two", 2}, {"three", 3}}; // NOLINT(misc-const-correctness)
const json j_const = {{"one", 1}, {"two", 2}, {"three", 3}};
SECTION("result of size")
{
@ -333,8 +333,8 @@ TEST_CASE("capacity")
SECTION("number (integer)")
{
json j = -23;
const json j_const(j);
json j = -23; // NOLINT(misc-const-correctness)
const json j_const = -23;
SECTION("result of size")
{
@ -353,8 +353,8 @@ TEST_CASE("capacity")
SECTION("number (unsigned)")
{
json j = 23u;
const json j_const(j);
json j = 23u; // NOLINT(misc-const-correctness)
const json j_const = 23u;
SECTION("result of size")
{
@ -373,8 +373,8 @@ TEST_CASE("capacity")
SECTION("number (float)")
{
json j = 23.42;
const json j_const(j);
json j = 23.42; // NOLINT(misc-const-correctness)
const json j_const = 23.42;
SECTION("result of size")
{
@ -393,8 +393,8 @@ TEST_CASE("capacity")
SECTION("null")
{
json j = nullptr;
const json j_const(j);
json j = nullptr; // NOLINT(misc-const-correctness)
const json j_const = nullptr;
SECTION("result of size")
{
@ -416,7 +416,7 @@ TEST_CASE("capacity")
{
SECTION("boolean")
{
json j = true;
json j = true; // NOLINT(misc-const-correctness)
const json j_const = true;
SECTION("result of max_size")
@ -428,7 +428,7 @@ TEST_CASE("capacity")
SECTION("string")
{
json j = "hello world";
json j = "hello world"; // NOLINT(misc-const-correctness)
const json j_const = "hello world";
SECTION("result of max_size")
@ -442,7 +442,7 @@ TEST_CASE("capacity")
{
SECTION("empty array")
{
json j = json::array();
json j = json::array(); // NOLINT(misc-const-correctness)
const json j_const = json::array();
SECTION("result of max_size")
@ -454,7 +454,7 @@ TEST_CASE("capacity")
SECTION("filled array")
{
json j = {1, 2, 3};
json j = {1, 2, 3}; // NOLINT(misc-const-correctness)
const json j_const = {1, 2, 3};
SECTION("result of max_size")
@ -469,7 +469,7 @@ TEST_CASE("capacity")
{
SECTION("empty object")
{
json j = json::object();
json j = json::object(); // NOLINT(misc-const-correctness)
const json j_const = json::object();
SECTION("result of max_size")
@ -481,7 +481,7 @@ TEST_CASE("capacity")
SECTION("filled object")
{
json j = {{"one", 1}, {"two", 2}, {"three", 3}};
json j = {{"one", 1}, {"two", 2}, {"three", 3}}; // NOLINT(misc-const-correctness)
const json j_const = {{"one", 1}, {"two", 2}, {"three", 3}};
SECTION("result of max_size")
@ -494,7 +494,7 @@ TEST_CASE("capacity")
SECTION("number (integer)")
{
json j = -23;
json j = -23; // NOLINT(misc-const-correctness)
const json j_const = -23;
SECTION("result of max_size")
@ -506,7 +506,7 @@ TEST_CASE("capacity")
SECTION("number (unsigned)")
{
json j = 23u;
json j = 23u; // NOLINT(misc-const-correctness)
const json j_const = 23u;
SECTION("result of max_size")
@ -518,7 +518,7 @@ TEST_CASE("capacity")
SECTION("number (float)")
{
json j = 23.42;
json j = 23.42; // NOLINT(misc-const-correctness)
const json j_const = 23.42;
SECTION("result of max_size")
@ -530,7 +530,7 @@ TEST_CASE("capacity")
SECTION("null")
{
json j = nullptr;
json j = nullptr; // NOLINT(misc-const-correctness)
const json j_const = nullptr;
SECTION("result of max_size")

File diff suppressed because it is too large Load Diff

View File

@ -20,27 +20,27 @@ TEST_CASE("const_iterator class")
{
SECTION("null")
{
json j(json::value_t::null);
json::const_iterator it(&j);
json const j(json::value_t::null);
json::const_iterator const it(&j);
}
SECTION("object")
{
json j(json::value_t::object);
json::const_iterator it(&j);
json const j(json::value_t::object);
json::const_iterator const it(&j);
}
SECTION("array")
{
json j(json::value_t::array);
json::const_iterator it(&j);
json const j(json::value_t::array);
json::const_iterator const it(&j);
}
}
SECTION("copy assignment")
{
json j(json::value_t::null);
json::const_iterator it(&j);
json const j(json::value_t::null);
json::const_iterator const it(&j);
json::const_iterator it2(&j);
it2 = it;
}
@ -50,14 +50,14 @@ TEST_CASE("const_iterator class")
SECTION("create from uninitialized iterator")
{
const json::iterator it {};
json::const_iterator cit(it);
json::const_iterator const cit(it);
}
SECTION("create from initialized iterator")
{
json j;
const json::iterator it = j.begin();
json::const_iterator cit(it);
json::const_iterator const cit(it);
}
}
}
@ -68,7 +68,7 @@ TEST_CASE("const_iterator class")
{
SECTION("null")
{
json j(json::value_t::null);
json const j(json::value_t::null);
json::const_iterator it(&j);
it.set_begin();
CHECK((it == j.cbegin()));
@ -76,7 +76,7 @@ TEST_CASE("const_iterator class")
SECTION("object")
{
json j(json::value_t::object);
json const j(json::value_t::object);
json::const_iterator it(&j);
it.set_begin();
CHECK((it == j.cbegin()));
@ -84,7 +84,7 @@ TEST_CASE("const_iterator class")
SECTION("array")
{
json j(json::value_t::array);
json const j(json::value_t::array);
json::const_iterator it(&j);
it.set_begin();
CHECK((it == j.cbegin()));
@ -95,7 +95,7 @@ TEST_CASE("const_iterator class")
{
SECTION("null")
{
json j(json::value_t::null);
json const j(json::value_t::null);
json::const_iterator it(&j);
it.set_end();
CHECK((it == j.cend()));
@ -103,7 +103,7 @@ TEST_CASE("const_iterator class")
SECTION("object")
{
json j(json::value_t::object);
json const j(json::value_t::object);
json::const_iterator it(&j);
it.set_end();
CHECK((it == j.cend()));
@ -111,7 +111,7 @@ TEST_CASE("const_iterator class")
SECTION("array")
{
json j(json::value_t::array);
json const j(json::value_t::array);
json::const_iterator it(&j);
it.set_end();
CHECK((it == j.cend()));
@ -125,14 +125,14 @@ TEST_CASE("const_iterator class")
{
SECTION("null")
{
json j(json::value_t::null);
json::const_iterator it = j.cbegin();
json const j(json::value_t::null);
json::const_iterator const it = j.cbegin();
CHECK_THROWS_WITH_AS(*it, "[json.exception.invalid_iterator.214] cannot get value", json::invalid_iterator&);
}
SECTION("number")
{
json j(17);
json const j(17);
json::const_iterator it = j.cbegin();
CHECK(*it == json(17));
it = j.cend();
@ -141,15 +141,15 @@ TEST_CASE("const_iterator class")
SECTION("object")
{
json j({{"foo", "bar"}});
json::const_iterator it = j.cbegin();
json const j({{"foo", "bar"}});
json::const_iterator const it = j.cbegin();
CHECK(*it == json("bar"));
}
SECTION("array")
{
json j({1, 2, 3, 4});
json::const_iterator it = j.cbegin();
json const j({1, 2, 3, 4});
json::const_iterator const it = j.cbegin();
CHECK(*it == json(1));
}
}
@ -158,14 +158,14 @@ TEST_CASE("const_iterator class")
{
SECTION("null")
{
json j(json::value_t::null);
json::const_iterator it = j.cbegin();
json const j(json::value_t::null);
json::const_iterator const it = j.cbegin();
CHECK_THROWS_WITH_AS(std::string(it->type_name()), "[json.exception.invalid_iterator.214] cannot get value", json::invalid_iterator&);
}
SECTION("number")
{
json j(17);
json const j(17);
json::const_iterator it = j.cbegin();
CHECK(std::string(it->type_name()) == "number");
it = j.cend();
@ -174,15 +174,15 @@ TEST_CASE("const_iterator class")
SECTION("object")
{
json j({{"foo", "bar"}});
json::const_iterator it = j.cbegin();
json const j({{"foo", "bar"}});
json::const_iterator const it = j.cbegin();
CHECK(std::string(it->type_name()) == "string");
}
SECTION("array")
{
json j({1, 2, 3, 4});
json::const_iterator it = j.cbegin();
json const j({1, 2, 3, 4});
json::const_iterator const it = j.cbegin();
CHECK(std::string(it->type_name()) == "number");
}
}
@ -194,7 +194,7 @@ TEST_CASE("const_iterator class")
{
SECTION("null")
{
json j(json::value_t::null);
json const j(json::value_t::null);
json::const_iterator it = j.cbegin();
CHECK((it.m_it.primitive_iterator.m_it == 1));
it++;
@ -203,7 +203,7 @@ TEST_CASE("const_iterator class")
SECTION("number")
{
json j(17);
json const j(17);
json::const_iterator it = j.cbegin();
CHECK((it.m_it.primitive_iterator.m_it == 0));
it++;
@ -214,7 +214,7 @@ TEST_CASE("const_iterator class")
SECTION("object")
{
json j({{"foo", "bar"}});
json const j({{"foo", "bar"}});
json::const_iterator it = j.cbegin();
CHECK((it.m_it.object_iterator == it.m_object->m_value.object->begin()));
it++;
@ -223,7 +223,7 @@ TEST_CASE("const_iterator class")
SECTION("array")
{
json j({1, 2, 3, 4});
json const j({1, 2, 3, 4});
json::const_iterator it = j.cbegin();
CHECK((it.m_it.array_iterator == it.m_object->m_value.array->begin()));
it++;
@ -245,7 +245,7 @@ TEST_CASE("const_iterator class")
{
SECTION("null")
{
json j(json::value_t::null);
json const j(json::value_t::null);
json::const_iterator it = j.cbegin();
CHECK((it.m_it.primitive_iterator.m_it == 1));
++it;
@ -254,7 +254,7 @@ TEST_CASE("const_iterator class")
SECTION("number")
{
json j(17);
json const j(17);
json::const_iterator it = j.cbegin();
CHECK((it.m_it.primitive_iterator.m_it == 0));
++it;
@ -265,7 +265,7 @@ TEST_CASE("const_iterator class")
SECTION("object")
{
json j({{"foo", "bar"}});
json const j({{"foo", "bar"}});
json::const_iterator it = j.cbegin();
CHECK((it.m_it.object_iterator == it.m_object->m_value.object->begin()));
++it;
@ -274,7 +274,7 @@ TEST_CASE("const_iterator class")
SECTION("array")
{
json j({1, 2, 3, 4});
json const j({1, 2, 3, 4});
json::const_iterator it = j.cbegin();
CHECK((it.m_it.array_iterator == it.m_object->m_value.array->begin()));
++it;
@ -296,14 +296,14 @@ TEST_CASE("const_iterator class")
{
SECTION("null")
{
json j(json::value_t::null);
json::const_iterator it = j.cend();
json const j(json::value_t::null);
json::const_iterator const it = j.cend();
CHECK((it.m_it.primitive_iterator.m_it == 1));
}
SECTION("number")
{
json j(17);
json const j(17);
json::const_iterator it = j.cend();
CHECK((it.m_it.primitive_iterator.m_it == 1));
it--;
@ -314,7 +314,7 @@ TEST_CASE("const_iterator class")
SECTION("object")
{
json j({{"foo", "bar"}});
json const j({{"foo", "bar"}});
json::const_iterator it = j.cend();
CHECK((it.m_it.object_iterator == it.m_object->m_value.object->end()));
it--;
@ -323,7 +323,7 @@ TEST_CASE("const_iterator class")
SECTION("array")
{
json j({1, 2, 3, 4});
json const j({1, 2, 3, 4});
json::const_iterator it = j.cend();
CHECK((it.m_it.array_iterator == it.m_object->m_value.array->end()));
it--;
@ -345,14 +345,14 @@ TEST_CASE("const_iterator class")
{
SECTION("null")
{
json j(json::value_t::null);
json::const_iterator it = j.cend();
json const j(json::value_t::null);
json::const_iterator const it = j.cend();
CHECK((it.m_it.primitive_iterator.m_it == 1));
}
SECTION("number")
{
json j(17);
json const j(17);
json::const_iterator it = j.cend();
CHECK((it.m_it.primitive_iterator.m_it == 1));
--it;
@ -363,7 +363,7 @@ TEST_CASE("const_iterator class")
SECTION("object")
{
json j({{"foo", "bar"}});
json const j({{"foo", "bar"}});
json::const_iterator it = j.cend();
CHECK((it.m_it.object_iterator == it.m_object->m_value.object->end()));
--it;
@ -372,7 +372,7 @@ TEST_CASE("const_iterator class")
SECTION("array")
{
json j({1, 2, 3, 4});
json const j({1, 2, 3, 4});
json::const_iterator it = j.cend();
CHECK((it.m_it.array_iterator == it.m_object->m_value.array->end()));
--it;

View File

@ -27,26 +27,26 @@ TEST_CASE("iterator class")
SECTION("null")
{
json j(json::value_t::null);
json::iterator it(&j);
json::iterator const it(&j);
}
SECTION("object")
{
json j(json::value_t::object);
json::iterator it(&j);
json::iterator const it(&j);
}
SECTION("array")
{
json j(json::value_t::array);
json::iterator it(&j);
json::iterator const it(&j);
}
}
SECTION("copy assignment")
{
json j(json::value_t::null);
json::iterator it(&j);
json::iterator const it(&j);
json::iterator it2(&j);
it2 = it;
}
@ -116,7 +116,7 @@ TEST_CASE("iterator class")
SECTION("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&);
}
@ -132,14 +132,14 @@ TEST_CASE("iterator class")
SECTION("object")
{
json j({{"foo", "bar"}});
json::iterator it = j.begin();
json::iterator const it = j.begin();
CHECK(*it == json("bar"));
}
SECTION("array")
{
json j({1, 2, 3, 4});
json::iterator it = j.begin();
json::iterator const it = j.begin();
CHECK(*it == json(1));
}
}
@ -149,7 +149,7 @@ TEST_CASE("iterator class")
SECTION("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&);
}
@ -165,14 +165,14 @@ TEST_CASE("iterator class")
SECTION("object")
{
json j({{"foo", "bar"}});
json::iterator it = j.begin();
json::iterator const it = j.begin();
CHECK(std::string(it->type_name()) == "string");
}
SECTION("array")
{
json j({1, 2, 3, 4});
json::iterator it = j.begin();
json::iterator const it = j.begin();
CHECK(std::string(it->type_name()) == "number");
}
}
@ -287,7 +287,7 @@ TEST_CASE("iterator class")
SECTION("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));
}
@ -336,7 +336,7 @@ TEST_CASE("iterator class")
SECTION("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));
}

View File

@ -249,11 +249,11 @@ bool accept_helper(const std::string& s)
CHECK(json::parser(nlohmann::detail::input_adapter(s)).accept(false) == !el.errored);
// 5. parse with simple callback
json::parser_callback_t cb = [](int /*unused*/, json::parse_event_t /*unused*/, json& /*unused*/) noexcept
json::parser_callback_t const cb = [](int /*unused*/, json::parse_event_t /*unused*/, json& /*unused*/) noexcept
{
return true;
};
json j_cb = json::parse(s, cb, false);
json const j_cb = json::parse(s, cb, false);
const bool ok_noexcept_cb = !j_cb.is_discarded();
// 6. check if this approach came to the same result
@ -1093,7 +1093,7 @@ TEST_CASE("parser class")
for (int c = 1; c < 128; ++c)
{
std::string s = "\"\\u";
std::string const s = "\"\\u";
// create a string with the iterated character at each position
auto s1 = s + "000" + std::string(1, static_cast<char>(c)) + "\"";
@ -1308,7 +1308,7 @@ TEST_CASE("parser class")
for (int c = 1; c < 128; ++c)
{
std::string s = "\"\\u";
std::string const s = "\"\\u";
// create a string with the iterated character at each position
const auto s1 = s + "000" + std::string(1, static_cast<char>(c)) + "\"";
@ -1361,7 +1361,7 @@ TEST_CASE("parser class")
// test case to make sure the callback is properly evaluated after reading a key
{
json::parser_callback_t cb = [](int /*unused*/, json::parse_event_t event, json& /*unused*/) noexcept
json::parser_callback_t const cb = [](int /*unused*/, json::parse_event_t event, json& /*unused*/) noexcept
{
return event != json::parse_event_t::key;
};
@ -1417,7 +1417,7 @@ TEST_CASE("parser class")
SECTION("filter everything")
{
json j_object = json::parse(s_object, [](int /*unused*/, json::parse_event_t /*unused*/, const json& /*unused*/) noexcept
json const j_object = json::parse(s_object, [](int /*unused*/, json::parse_event_t /*unused*/, const json& /*unused*/) noexcept
{
return false;
});
@ -1425,7 +1425,7 @@ TEST_CASE("parser class")
// the top-level object will be discarded, leaving a null
CHECK (j_object.is_null());
json j_array = json::parse(s_array, [](int /*unused*/, json::parse_event_t /*unused*/, const json& /*unused*/) noexcept
json const j_array = json::parse(s_array, [](int /*unused*/, json::parse_event_t /*unused*/, const json& /*unused*/) noexcept
{
return false;
});
@ -1574,7 +1574,7 @@ TEST_CASE("parser class")
SECTION("from std::initializer_list")
{
std::initializer_list<uint8_t> v = {'t', 'r', 'u', 'e'};
std::initializer_list<uint8_t> const v = {'t', 'r', 'u', 'e'};
json j;
json::parser(nlohmann::detail::input_adapter(std::begin(v), std::end(v))).parse(true, j);
CHECK(j == json(true));
@ -1593,7 +1593,7 @@ TEST_CASE("parser class")
{
SECTION("parser with callback")
{
json::parser_callback_t cb = [](int /*unused*/, json::parse_event_t /*unused*/, json& /*unused*/) noexcept
json::parser_callback_t const cb = [](int /*unused*/, json::parse_event_t /*unused*/, json& /*unused*/) noexcept
{
return true;
};

View File

@ -391,7 +391,7 @@ TEST_CASE("lexicographical comparison operators")
}
// compare with null pointer
json j_null;
const json j_null;
CHECK((j_null != nullptr) == false);
CHECK((nullptr != j_null) == false);
CHECK((j_null != nullptr) == !(j_null == nullptr));

View File

@ -57,7 +57,7 @@ TEST_CASE("concepts")
// the expression "X u" has the post-condition "u.empty()"
{
json u;
const json u;
CHECK(u.empty());
}

File diff suppressed because it is too large Load Diff

View File

@ -83,7 +83,7 @@ TEST_CASE("other constructors and destructor")
{
json j {{"foo", "bar"}, {"baz", {1, 2, 3, 4}}, {"a", 42u}, {"b", 42.23}, {"c", nullptr}};
CHECK(j.type() == json::value_t::object);
json k(std::move(j));
const json k(std::move(j));
CHECK(k.type() == json::value_t::object);
CHECK(j.type() == json::value_t::null); // NOLINT: access after move is OK here
}

View File

@ -173,9 +173,9 @@ TEST_CASE("convenience functions")
using nlohmann::detail::concat;
const char* expected = "Hello, world!";
alt_string_iter hello_iter{"Hello, "};
alt_string_data hello_data{"Hello, "};
std::string world = "world";
alt_string_iter const hello_iter{"Hello, "};
alt_string_data const hello_data{"Hello, "};
std::string const world = "world";
SECTION("std::string")
{

View File

@ -36,7 +36,7 @@ TEST_CASE("value conversion")
{
SECTION("get an object (explicit)")
{
json::object_t o_reference = {{"object", json::object()},
const json::object_t o_reference = {{"object", json::object()},
{"array", {1, 2, 3, 4}},
{"number", 42},
{"boolean", false},
@ -47,34 +47,34 @@ TEST_CASE("value conversion")
SECTION("json::object_t")
{
json::object_t o = j.get<json::object_t>();
json::object_t const o = j.get<json::object_t>();
CHECK(json(o) == j);
}
SECTION("std::map<json::string_t, json>")
{
std::map<json::string_t, json> o =
const std::map<json::string_t, json> o =
j.get<std::map<json::string_t, json>>();
CHECK(json(o) == j);
}
SECTION("std::multimap<json::string_t, json>")
{
std::multimap<json::string_t, json> o =
const std::multimap<json::string_t, json> o =
j.get<std::multimap<json::string_t, json>>();
CHECK(json(o) == j);
}
SECTION("std::unordered_map<json::string_t, json>")
{
std::unordered_map<json::string_t, json> o =
const std::unordered_map<json::string_t, json> o =
j.get<std::unordered_map<json::string_t, json>>();
CHECK(json(o) == j);
}
SECTION("std::unordered_multimap<json::string_t, json>")
{
std::unordered_multimap<json::string_t, json> o =
const std::unordered_multimap<json::string_t, json> o =
j.get<std::unordered_multimap<json::string_t, json>>();
CHECK(json(o) == j);
}
@ -107,7 +107,7 @@ TEST_CASE("value conversion")
SECTION("get an object (explicit, get_to)")
{
json::object_t o_reference = {{"object", json::object()},
const json::object_t o_reference = {{"object", json::object()},
{"array", {1, 2, 3, 4}},
{"number", 42},
{"boolean", false},
@ -155,7 +155,7 @@ TEST_CASE("value conversion")
#if JSON_USE_IMPLICIT_CONVERSIONS
SECTION("get an object (implicit)")
{
json::object_t o_reference = {{"object", json::object()},
const json::object_t o_reference = {{"object", json::object()},
{"array", {1, 2, 3, 4}},
{"number", 42},
{"boolean", false},
@ -166,31 +166,31 @@ TEST_CASE("value conversion")
SECTION("json::object_t")
{
json::object_t o = j;
const json::object_t o = j;
CHECK(json(o) == j);
}
SECTION("std::map<json::string_t, json>")
{
std::map<json::string_t, json> o = j;
const std::map<json::string_t, json> o = j;
CHECK(json(o) == j);
}
SECTION("std::multimap<json::string_t, json>")
{
std::multimap<json::string_t, json> o = j;
const std::multimap<json::string_t, json> o = j;
CHECK(json(o) == j);
}
SECTION("std::unordered_map<json::string_t, json>")
{
std::unordered_map<json::string_t, json> o = j;
const std::unordered_map<json::string_t, json> o = j;
CHECK(json(o) == j);
}
SECTION("std::unordered_multimap<json::string_t, json>")
{
std::unordered_multimap<json::string_t, json> o = j;
const std::unordered_multimap<json::string_t, json> o = j;
CHECK(json(o) == j);
}
}
@ -198,25 +198,25 @@ TEST_CASE("value conversion")
SECTION("get an array (explicit)")
{
json::array_t a_reference{json(1), json(1u), json(2.2),
json(false), json("string"), json()};
const json::array_t a_reference{json(1), json(1u), json(2.2),
json(false), json("string"), json()};
json j(a_reference);
SECTION("json::array_t")
{
json::array_t a = j.get<json::array_t>();
const json::array_t a = j.get<json::array_t>();
CHECK(json(a) == j);
}
SECTION("std::list<json>")
{
std::list<json> a = j.get<std::list<json>>();
const std::list<json> a = j.get<std::list<json>>();
CHECK(json(a) == j);
}
SECTION("std::forward_list<json>")
{
std::forward_list<json> a = j.get<std::forward_list<json>>();
const std::forward_list<json> a = j.get<std::forward_list<json>>();
CHECK(json(a) == j);
CHECK_THROWS_WITH_AS(
@ -226,7 +226,7 @@ TEST_CASE("value conversion")
SECTION("std::vector<json>")
{
std::vector<json> a = j.get<std::vector<json>>();
const std::vector<json> a = j.get<std::vector<json>>();
CHECK(json(a) == j);
CHECK_THROWS_WITH_AS(
@ -237,7 +237,7 @@ TEST_CASE("value conversion")
SECTION("reserve is called on containers that supports it")
{
// make sure all values are properly copied
json j2({1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
const json j2({1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
auto v2 = j2.get<std::vector<int>>();
CHECK(v2.size() == 10);
}
@ -249,8 +249,8 @@ TEST_CASE("value conversion")
const char str[] = "a string"; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
const int nbs[] = {0, 1, 2}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
json j2 = nbs;
json j3 = str;
const json j2 = nbs;
const json j3 = str;
auto v = j2.get<std::vector<int>>();
auto s = j3.get<std::string>();
@ -260,7 +260,7 @@ TEST_CASE("value conversion")
SECTION("std::deque<json>")
{
std::deque<json> a = j.get<std::deque<json>>();
const std::deque<json> a = j.get<std::deque<json>>();
CHECK(json(a) == j);
}
@ -295,8 +295,8 @@ TEST_CASE("value conversion")
SECTION("get an array (explicit, get_to)")
{
json::array_t a_reference{json(1), json(1u), json(2.2),
json(false), json("string"), json()};
const json::array_t a_reference{json(1), json(1u), json(2.2),
json(false), json("string"), json()};
json j(a_reference);
SECTION("json::array_t")
@ -339,7 +339,7 @@ TEST_CASE("value conversion")
const int nbs[] = {0, 1, 2}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
int nbs2[] = {0, 0, 0}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
json j2 = nbs;
const json j2 = nbs;
j2.get_to(nbs2);
CHECK(std::equal(std::begin(nbs), std::end(nbs), std::begin(nbs2)));
}
@ -355,37 +355,37 @@ TEST_CASE("value conversion")
#if JSON_USE_IMPLICIT_CONVERSIONS
SECTION("get an array (implicit)")
{
json::array_t a_reference{json(1), json(1u), json(2.2),
json(false), json("string"), json()};
const json::array_t a_reference{json(1), json(1u), json(2.2),
json(false), json("string"), json()};
json j(a_reference);
SECTION("json::array_t")
{
json::array_t a = j;
const json::array_t a = j;
CHECK(json(a) == j);
}
SECTION("std::list<json>")
{
std::list<json> a = j;
const std::list<json> a = j;
CHECK(json(a) == j);
}
SECTION("std::forward_list<json>")
{
std::forward_list<json> a = j;
const std::forward_list<json> a = j;
CHECK(json(a) == j);
}
SECTION("std::vector<json>")
{
std::vector<json> a = j;
const std::vector<json> a = j;
CHECK(json(a) == j);
}
SECTION("std::deque<json>")
{
std::deque<json> a = j;
const std::deque<json> a = j;
CHECK(json(a) == j);
}
}
@ -393,24 +393,24 @@ TEST_CASE("value conversion")
SECTION("get a string (explicit)")
{
json::string_t s_reference{"Hello world"};
const json::string_t s_reference{"Hello world"};
json j(s_reference);
SECTION("string_t")
{
json::string_t s = j.get<json::string_t>();
const json::string_t s = j.get<json::string_t>();
CHECK(json(s) == j);
}
SECTION("std::string")
{
std::string s = j.get<std::string>();
const std::string s = j.get<std::string>();
CHECK(json(s) == j);
}
#if defined(JSON_HAS_CPP_17)
SECTION("std::string_view")
{
std::string_view s = j.get<std::string_view>();
std::string_view const s = j.get<std::string_view>();
CHECK(json(s) == j);
}
#endif
@ -463,7 +463,7 @@ TEST_CASE("value conversion")
SECTION("get a string (explicit, get_to)")
{
json::string_t s_reference{"Hello world"};
const json::string_t s_reference{"Hello world"};
json j(s_reference);
SECTION("string_t")
@ -482,7 +482,7 @@ TEST_CASE("value conversion")
#if defined(JSON_HAS_CPP_17)
SECTION("std::string_view")
{
std::string s = "previous value";
std::string const s = "previous value";
std::string_view sv = s;
j.get_to(sv);
CHECK(json(sv) == j);
@ -493,7 +493,7 @@ TEST_CASE("value conversion")
SECTION("get null (explicit)")
{
std::nullptr_t n = nullptr;
json j(n);
const json j(n);
auto n2 = j.get<std::nullptr_t>();
CHECK(n2 == n);
@ -517,26 +517,26 @@ TEST_CASE("value conversion")
#if JSON_USE_IMPLICIT_CONVERSIONS
SECTION("get a string (implicit)")
{
json::string_t s_reference{"Hello world"};
const json::string_t s_reference{"Hello world"};
json j(s_reference);
SECTION("string_t")
{
json::string_t s = j;
const json::string_t s = j;
CHECK(json(s) == j);
}
#if defined(JSON_HAS_CPP_17)
SECTION("std::string_view")
{
std::string_view s = j.get<std::string_view>();
std::string_view const s = j.get<std::string_view>();
CHECK(json(s) == j);
}
#endif
SECTION("std::string")
{
std::string s = j;
const std::string s = j;
CHECK(json(s) == j);
}
}
@ -544,7 +544,7 @@ TEST_CASE("value conversion")
SECTION("get a boolean (explicit)")
{
json::boolean_t b_reference{true};
const json::boolean_t b_reference{true};
json j(b_reference);
SECTION("boolean_t")
@ -561,7 +561,7 @@ TEST_CASE("value conversion")
SECTION("bool")
{
bool b = j.get<bool>();
const bool b = j.get<bool>();
CHECK(json(b) == j);
}
@ -600,18 +600,18 @@ TEST_CASE("value conversion")
#if JSON_USE_IMPLICIT_CONVERSIONS
SECTION("get a boolean (implicit)")
{
json::boolean_t b_reference{true};
const json::boolean_t b_reference{true};
json j(b_reference);
SECTION("boolean_t")
{
json::boolean_t b = j;
const json::boolean_t b = j;
CHECK(json(b) == j);
}
SECTION("bool")
{
bool b = j;
const bool b = j;
CHECK(json(b) == j);
}
}
@ -619,9 +619,9 @@ TEST_CASE("value conversion")
SECTION("get an integer number (explicit)")
{
json::number_integer_t n_reference{42};
const json::number_integer_t n_reference{42};
json j(n_reference);
json::number_unsigned_t n_unsigned_reference{42u};
const json::number_unsigned_t n_unsigned_reference{42u};
json j_unsigned(n_unsigned_reference);
SECTION("number_integer_t")
@ -650,7 +650,7 @@ TEST_CASE("value conversion")
SECTION("int")
{
int n = j.get<int>();
const int n = j.get<int>();
CHECK(json(n) == j);
}
@ -662,7 +662,7 @@ TEST_CASE("value conversion")
SECTION("long")
{
long n = j.get<long>();
const long n = j.get<long>();
CHECK(json(n) == j);
}
@ -857,9 +857,9 @@ TEST_CASE("value conversion")
#if JSON_USE_IMPLICIT_CONVERSIONS
SECTION("get an integer number (implicit)")
{
json::number_integer_t n_reference{42};
json::number_integer_t const n_reference{42};
json j(n_reference);
json::number_unsigned_t n_unsigned_reference{42u};
json::number_unsigned_t const n_unsigned_reference{42u};
json j_unsigned(n_unsigned_reference);
SECTION("number_integer_t")
@ -876,193 +876,193 @@ TEST_CASE("value conversion")
SECTION("short")
{
short n = j;
short const n = j;
CHECK(json(n) == j);
}
SECTION("unsigned short")
{
unsigned short n = j_unsigned;
unsigned short const n = j_unsigned;
CHECK(json(n) == j_unsigned);
}
SECTION("int")
{
int n = j;
int const n = j;
CHECK(json(n) == j);
}
SECTION("unsigned int")
{
unsigned int n = j_unsigned;
unsigned int const n = j_unsigned;
CHECK(json(n) == j_unsigned);
}
SECTION("long")
{
long n = j;
long const n = j;
CHECK(json(n) == j);
}
SECTION("unsigned long")
{
unsigned long n = j_unsigned;
unsigned long const n = j_unsigned;
CHECK(json(n) == j_unsigned);
}
SECTION("long long")
{
long long n = j;
long long const n = j;
CHECK(json(n) == j);
}
SECTION("unsigned long long")
{
unsigned long long n = j_unsigned;
unsigned long long const n = j_unsigned;
CHECK(json(n) == j_unsigned);
}
SECTION("int8_t")
{
int8_t n = j;
int8_t const n = j;
CHECK(json(n) == j);
}
SECTION("int16_t")
{
int16_t n = j;
int16_t const n = j;
CHECK(json(n) == j);
}
SECTION("int32_t")
{
int32_t n = j;
int32_t const n = j;
CHECK(json(n) == j);
}
SECTION("int64_t")
{
int64_t n = j;
int64_t const n = j;
CHECK(json(n) == j);
}
SECTION("int8_fast_t")
{
int_fast8_t n = j;
int_fast8_t const n = j;
CHECK(json(n) == j);
}
SECTION("int16_fast_t")
{
int_fast16_t n = j;
int_fast16_t const n = j;
CHECK(json(n) == j);
}
SECTION("int32_fast_t")
{
int_fast32_t n = j;
int_fast32_t const n = j;
CHECK(json(n) == j);
}
SECTION("int64_fast_t")
{
int_fast64_t n = j;
int_fast64_t const n = j;
CHECK(json(n) == j);
}
SECTION("int8_least_t")
{
int_least8_t n = j;
int_least8_t const n = j;
CHECK(json(n) == j);
}
SECTION("int16_least_t")
{
int_least16_t n = j;
int_least16_t const n = j;
CHECK(json(n) == j);
}
SECTION("int32_least_t")
{
int_least32_t n = j;
int_least32_t const n = j;
CHECK(json(n) == j);
}
SECTION("int64_least_t")
{
int_least64_t n = j;
int_least64_t const n = j;
CHECK(json(n) == j);
}
SECTION("uint8_t")
{
uint8_t n = j_unsigned;
uint8_t const n = j_unsigned;
CHECK(json(n) == j_unsigned);
}
SECTION("uint16_t")
{
uint16_t n = j_unsigned;
uint16_t const n = j_unsigned;
CHECK(json(n) == j_unsigned);
}
SECTION("uint32_t")
{
uint32_t n = j_unsigned;
uint32_t const n = j_unsigned;
CHECK(json(n) == j_unsigned);
}
SECTION("uint64_t")
{
uint64_t n = j_unsigned;
uint64_t const n = j_unsigned;
CHECK(json(n) == j_unsigned);
}
SECTION("uint8_fast_t")
{
uint_fast8_t n = j_unsigned;
uint_fast8_t const n = j_unsigned;
CHECK(json(n) == j_unsigned);
}
SECTION("uint16_fast_t")
{
uint_fast16_t n = j_unsigned;
uint_fast16_t const n = j_unsigned;
CHECK(json(n) == j_unsigned);
}
SECTION("uint32_fast_t")
{
uint_fast32_t n = j_unsigned;
uint_fast32_t const n = j_unsigned;
CHECK(json(n) == j_unsigned);
}
SECTION("uint64_fast_t")
{
uint_fast64_t n = j_unsigned;
uint_fast64_t const n = j_unsigned;
CHECK(json(n) == j_unsigned);
}
SECTION("uint8_least_t")
{
uint_least8_t n = j_unsigned;
uint_least8_t const n = j_unsigned;
CHECK(json(n) == j_unsigned);
}
SECTION("uint16_least_t")
{
uint_least16_t n = j_unsigned;
uint_least16_t const n = j_unsigned;
CHECK(json(n) == j_unsigned);
}
SECTION("uint32_least_t")
{
uint_least32_t n = j_unsigned;
uint_least32_t const n = j_unsigned;
CHECK(json(n) == j_unsigned);
}
SECTION("uint64_least_t")
{
uint_least64_t n = j_unsigned;
uint_least64_t const n = j_unsigned;
CHECK(json(n) == j_unsigned);
}
}
@ -1070,8 +1070,8 @@ TEST_CASE("value conversion")
SECTION("get a floating-point number (explicit)")
{
json::number_float_t n_reference{42.23};
json j(n_reference);
json::number_float_t const n_reference{42.23};
json const j(n_reference);
SECTION("number_float_t")
{
@ -1120,24 +1120,24 @@ TEST_CASE("value conversion")
#if JSON_USE_IMPLICIT_CONVERSIONS
SECTION("get a floating-point number (implicit)")
{
json::number_float_t n_reference{42.23};
json j(n_reference);
json::number_float_t const n_reference{42.23};
json const j(n_reference);
SECTION("number_float_t")
{
json::number_float_t n = j;
json::number_float_t const n = j;
CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float));
}
SECTION("float")
{
float n = j;
float const n = j;
CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float));
}
SECTION("double")
{
double n = j;
double const n = j;
CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float));
}
}
@ -1145,12 +1145,12 @@ TEST_CASE("value conversion")
SECTION("get a binary value (explicit)")
{
json::binary_t n_reference{{1, 2, 3}};
json::binary_t const n_reference{{1, 2, 3}};
json j(n_reference);
SECTION("binary_t")
{
json::binary_t b = j.get<json::binary_t>();
json::binary_t const b = j.get<json::binary_t>();
CHECK(*json(b).m_value.binary == *j.m_value.binary);
}
@ -1252,12 +1252,12 @@ TEST_CASE("value conversion")
#if JSON_USE_IMPLICIT_CONVERSIONS
SECTION("get a binary value (implicit)")
{
json::binary_t n_reference{{1, 2, 3}};
json j(n_reference);
json::binary_t const n_reference{{1, 2, 3}};
json const j(n_reference);
SECTION("binary_t")
{
json::binary_t b = j;
json::binary_t const b = j;
CHECK(*json(b).m_value.binary == *j.m_value.binary);
}
}
@ -1276,11 +1276,11 @@ TEST_CASE("value conversion")
{
SECTION("object-like STL containers")
{
json j1 = {{"one", 1}, {"two", 2}, {"three", 3}};
json j2 = {{"one", 1u}, {"two", 2u}, {"three", 3u}};
json j3 = {{"one", 1.1}, {"two", 2.2}, {"three", 3.3}};
json j4 = {{"one", true}, {"two", false}, {"three", true}};
json j5 = {{"one", "eins"}, {"two", "zwei"}, {"three", "drei"}};
json const j1 = {{"one", 1}, {"two", 2}, {"three", 3}};
json const j2 = {{"one", 1u}, {"two", 2u}, {"three", 3u}};
json const j3 = {{"one", 1.1}, {"two", 2.2}, {"three", 3.3}};
json const j4 = {{"one", true}, {"two", false}, {"three", true}};
json const j5 = {{"one", "eins"}, {"two", "zwei"}, {"three", "drei"}};
SECTION("std::map")
{
@ -1331,11 +1331,11 @@ TEST_CASE("value conversion")
SECTION("array-like STL containers")
{
json j1 = {1, 2, 3, 4};
json j2 = {1u, 2u, 3u, 4u};
json j3 = {1.2, 2.3, 3.4, 4.5};
json j4 = {true, false, true};
json j5 = {"one", "two", "three"};
json const j1 = {1, 2, 3, 4};
json const j2 = {1u, 2u, 3u, 4u};
json const j3 = {1.2, 2.3, 3.4, 4.5};
json const j4 = {true, false, true};
json const j5 = {"one", "two", "three"};
SECTION("std::list")
{
@ -1427,13 +1427,13 @@ TEST_CASE("value conversion")
SECTION("std::map (array of pairs)")
{
std::map<int, int> m{{0, 1}, {1, 2}, {2, 3}};
json j6 = m;
json const j6 = m;
auto m2 = j6.get<std::map<int, int>>();
CHECK(m == m2);
json j7 = {0, 1, 2, 3};
json j8 = 2;
json const j7 = {0, 1, 2, 3};
json const j8 = 2;
CHECK_THROWS_WITH_AS((j7.get<std::map<int, int>>()),
"[json.exception.type_error.302] type must be array, "
"but is number", json::type_error&);
@ -1443,7 +1443,7 @@ TEST_CASE("value conversion")
SECTION("superfluous entries")
{
json j9 = {{0, 1, 2}, {1, 2, 3}, {2, 3, 4}};
json const j9 = {{0, 1, 2}, {1, 2, 3}, {2, 3, 4}};
m2 = j9.get<std::map<int, int>>();
CHECK(m == m2);
}
@ -1452,13 +1452,13 @@ TEST_CASE("value conversion")
SECTION("std::unordered_map (array of pairs)")
{
std::unordered_map<int, int> m{{0, 1}, {1, 2}, {2, 3}};
json j6 = m;
json const j6 = m;
auto m2 = j6.get<std::unordered_map<int, int>>();
CHECK(m == m2);
json j7 = {0, 1, 2, 3};
json j8 = 2;
json const j7 = {0, 1, 2, 3};
json const j8 = 2;
CHECK_THROWS_WITH_AS((j7.get<std::unordered_map<int, int>>()),
"[json.exception.type_error.302] type must be array, "
"but is number", json::type_error&);
@ -1468,7 +1468,7 @@ TEST_CASE("value conversion")
SECTION("superfluous entries")
{
json j9{{0, 1, 2}, {1, 2, 3}, {2, 3, 4}};
json const j9{{0, 1, 2}, {1, 2, 3}, {2, 3, 4}};
m2 = j9.get<std::unordered_map<int, int>>();
CHECK(m == m2);
}

View File

@ -188,7 +188,7 @@ TEST_CASE("JSON Node Metadata")
value.metadata().emplace_back(1);
value.metadata().emplace_back(2);
json array(10, value);
json const array(10, value);
CHECK(value.metadata().size() == 2);
CHECK(value.metadata().at(0) == 1);

View File

@ -265,7 +265,7 @@ TEST_CASE("deserialization")
SECTION("string_t")
{
json::string_t s = R"(["foo",1,2,3,false,{"one":1}])";
json::string_t const s = R"(["foo",1,2,3,false,{"one":1}])";
json j = json::parse(s);
CHECK(json::accept(s));
CHECK(j == json({"foo", 1, 2, 3, false, {{"one", 1}}}));
@ -341,7 +341,7 @@ TEST_CASE("deserialization")
SECTION("string")
{
json::string_t s = R"(["foo",1,2,3,false,{"one":1})";
json::string_t const s = R"(["foo",1,2,3,false,{"one":1})";
json _;
CHECK_THROWS_WITH_AS(_ = json::parse(s), "[json.exception.parse_error.101] parse error at line 1, column 29: syntax error while parsing array - unexpected end of input; expected ']'", json::parse_error&);
CHECK(!json::accept(s));
@ -390,7 +390,7 @@ TEST_CASE("deserialization")
{
SECTION("from std::vector")
{
std::vector<uint8_t> v = {'t', 'r', 'u', 'e'};
std::vector<uint8_t> const v = {'t', 'r', 'u', 'e'};
CHECK(json::parse(v) == json(true));
CHECK(json::accept(v));
@ -402,7 +402,7 @@ TEST_CASE("deserialization")
SECTION("from std::array")
{
std::array<uint8_t, 5> v { {'t', 'r', 'u', 'e'} };
std::array<uint8_t, 5> const v { {'t', 'r', 'u', 'e'} };
CHECK(json::parse(v) == json(true));
CHECK(json::accept(v));
@ -445,7 +445,7 @@ TEST_CASE("deserialization")
SECTION("from std::string")
{
std::string v = {'t', 'r', 'u', 'e'};
std::string const v = {'t', 'r', 'u', 'e'};
CHECK(json::parse(v) == json(true));
CHECK(json::accept(v));
@ -457,7 +457,7 @@ TEST_CASE("deserialization")
SECTION("from std::initializer_list")
{
std::initializer_list<uint8_t> v = {'t', 'r', 'u', 'e'};
std::initializer_list<uint8_t> const v = {'t', 'r', 'u', 'e'};
CHECK(json::parse(v) == json(true));
CHECK(json::accept(v));
@ -469,7 +469,7 @@ TEST_CASE("deserialization")
SECTION("empty container")
{
std::vector<uint8_t> v;
std::vector<uint8_t> const v;
json _;
CHECK_THROWS_AS(_ = json::parse(v), json::parse_error&);
CHECK(!json::accept(v));
@ -534,7 +534,7 @@ TEST_CASE("deserialization")
SECTION("from std::initializer_list")
{
std::initializer_list<uint8_t> v = {'t', 'r', 'u', 'e'};
std::initializer_list<uint8_t> const v = {'t', 'r', 'u', 'e'};
CHECK(json::parse(std::begin(v), std::end(v)) == json(true));
CHECK(json::accept(std::begin(v), std::end(v)));
@ -1045,7 +1045,7 @@ TEST_CASE("deserialization")
SECTION("SAX and early abort")
{
std::string s = R"([1, ["string", 43.12], null, {"key1": true, "key2": false}])";
std::string const s = R"([1, ["string", 43.12], null, {"key1": true, "key2": false}])";
SaxEventLogger default_logger;
SaxEventLoggerExitAfterStartObject exit_after_start_object;
@ -1139,7 +1139,7 @@ TEST_CASE_TEMPLATE("deserialization of different character types (ASCII)", T,
std::int16_t, std::uint16_t,
std::int32_t, std::uint32_t)
{
std::vector<T> v = {'t', 'r', 'u', 'e'};
std::vector<T> const v = {'t', 'r', 'u', 'e'};
CHECK(json::parse(v) == json(true));
CHECK(json::accept(v));
@ -1153,7 +1153,7 @@ TEST_CASE_TEMPLATE("deserialization of different character types (UTF-8)", T,
char, unsigned char, std::uint8_t)
{
// a star emoji
std::vector<T> v = {'"', static_cast<T>(0xe2u), static_cast<T>(0xadu), static_cast<T>(0x90u), static_cast<T>(0xefu), static_cast<T>(0xb8u), static_cast<T>(0x8fu), '"'};
std::vector<T> const v = {'"', static_cast<T>(0xe2u), static_cast<T>(0xadu), static_cast<T>(0x90u), static_cast<T>(0xefu), static_cast<T>(0xb8u), static_cast<T>(0x8fu), '"'};
CHECK(json::parse(v).dump(-1, ' ', true) == "\"\\u2b50\\ufe0f\"");
CHECK(json::accept(v));
@ -1166,7 +1166,7 @@ TEST_CASE_TEMPLATE("deserialization of different character types (UTF-16)", T,
char16_t, std::uint16_t)
{
// a star emoji
std::vector<T> v = {static_cast<T>('"'), static_cast<T>(0x2b50), static_cast<T>(0xfe0f), static_cast<T>('"')};
std::vector<T> const v = {static_cast<T>('"'), static_cast<T>(0x2b50), static_cast<T>(0xfe0f), static_cast<T>('"')};
CHECK(json::parse(v).dump(-1, ' ', true) == "\"\\u2b50\\ufe0f\"");
CHECK(json::accept(v));
@ -1179,7 +1179,7 @@ TEST_CASE_TEMPLATE("deserialization of different character types (UTF-32)", T,
char32_t, std::uint32_t)
{
// a star emoji
std::vector<T> v = {static_cast<T>('"'), static_cast<T>(0x2b50), static_cast<T>(0xfe0f), static_cast<T>('"')};
std::vector<T> const v = {static_cast<T>('"'), static_cast<T>(0x2b50), static_cast<T>(0xfe0f), static_cast<T>('"')};
CHECK(json::parse(v).dump(-1, ' ', true) == "\"\\u2b50\\ufe0f\"");
CHECK(json::accept(v));

View File

@ -22,7 +22,7 @@ TEST_CASE("Better diagnostics")
{
SECTION("empty JSON Pointer")
{
json j = 1;
json const j = 1;
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);
}
@ -240,7 +240,7 @@ TEST_CASE("Regression tests for extended diagnostics")
j_arr[5] = 5;
j_arr[6] = 6;
j_arr[7] = 7;
json j_arr_copy = j_arr;
json const j_arr_copy = j_arr;
}
}
}

View File

@ -274,13 +274,13 @@ TEST_CASE("element access 1")
{
{
json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
json::iterator it2 = jarray.erase(jarray.begin());
json::iterator const 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());
json::const_iterator const it2 = jarray.erase(jarray.cbegin());
CHECK(jarray == json({1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}}));
CHECK(*it2 == json(1u));
}
@ -306,13 +306,13 @@ TEST_CASE("element access 1")
{
{
json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
json::iterator it2 = jarray.erase(jarray.begin(), jarray.begin());
json::iterator const 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());
json::const_iterator const 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));
}
@ -322,15 +322,15 @@ TEST_CASE("element access 1")
{
{
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);
json::iterator const it = jarray.begin() + 4;
json::iterator const 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);
json::const_iterator const it = jarray.cbegin() + 4;
json::const_iterator const it2 = jarray.erase(it);
CHECK(jarray == json({1, 1u, true, nullptr, 42.23, json::object(), {1, 2, 3}}));
CHECK(*it2 == json(42.23));
}
@ -340,13 +340,13 @@ TEST_CASE("element access 1")
{
{
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);
json::iterator const 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);
json::const_iterator const it2 = jarray.erase(jarray.cbegin() + 3, jarray.cbegin() + 6);
CHECK(jarray == json({1, 1u, true, json::object(), {1, 2, 3}}));
CHECK(*it2 == json::object());
}
@ -369,7 +369,7 @@ TEST_CASE("element access 1")
}
{
json jarray = {1, 1u, true, nullptr, "string", 42.23, json::object(), {1, 2, 3}};
json jarray2 = {"foo", "bar"};
json const jarray2 = {"foo", "bar"};
CHECK_THROWS_WITH_AS(jarray.erase(jarray2.cbegin()),
"[json.exception.invalid_iterator.202] iterator does not fit current value", json::invalid_iterator&);

View File

@ -469,7 +469,7 @@ TEST_CASE_TEMPLATE("element access 2", Json, nlohmann::json, nlohmann::ordered_j
}
#ifdef JSON_HAS_CPP_17
{
std::string_view key = "key";
std::string_view const key = "key";
Json j_null;
CHECK(j_null.is_null());
j_null[key] = 1;
@ -833,13 +833,13 @@ TEST_CASE_TEMPLATE("element access 2", Json, nlohmann::json, nlohmann::ordered_j
{
{
Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}};
typename Json::iterator it2 = jobject.erase(jobject.begin());
typename Json::iterator const it2 = jobject.erase(jobject.begin());
CHECK(jobject == Json({{"b", 1}, {"c", 17u}}));
CHECK(*it2 == Json(1));
}
{
Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}};
typename Json::const_iterator it2 = jobject.erase(jobject.cbegin());
typename Json::const_iterator const it2 = jobject.erase(jobject.cbegin());
CHECK(jobject == Json({{"b", 1}, {"c", 17u}}));
CHECK(*it2 == Json(1));
}
@ -865,13 +865,13 @@ TEST_CASE_TEMPLATE("element access 2", Json, nlohmann::json, nlohmann::ordered_j
{
{
Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}};
typename Json::iterator it2 = jobject.erase(jobject.begin(), jobject.begin());
typename Json::iterator const it2 = jobject.erase(jobject.begin(), jobject.begin());
CHECK(jobject == Json({{"a", "a"}, {"b", 1}, {"c", 17u}}));
CHECK(*it2 == Json("a"));
}
{
Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}};
typename Json::const_iterator it2 = jobject.erase(jobject.cbegin(), jobject.cbegin());
typename Json::const_iterator const it2 = jobject.erase(jobject.cbegin(), jobject.cbegin());
CHECK(jobject == Json({{"a", "a"}, {"b", 1}, {"c", 17u}}));
CHECK(*it2 == Json("a"));
}
@ -881,15 +881,15 @@ TEST_CASE_TEMPLATE("element access 2", Json, nlohmann::json, nlohmann::ordered_j
{
{
Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}};
typename Json::iterator it = jobject.find("b");
typename Json::iterator it2 = jobject.erase(it);
typename Json::iterator const it = jobject.find("b");
typename Json::iterator const it2 = jobject.erase(it);
CHECK(jobject == Json({{"a", "a"}, {"c", 17u}}));
CHECK(*it2 == Json(17));
}
{
Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}};
typename Json::const_iterator it = jobject.find("b");
typename Json::const_iterator it2 = jobject.erase(it);
typename Json::const_iterator const it = jobject.find("b");
typename Json::const_iterator const it2 = jobject.erase(it);
CHECK(jobject == Json({{"a", "a"}, {"c", 17u}}));
CHECK(*it2 == Json(17));
}
@ -899,13 +899,13 @@ TEST_CASE_TEMPLATE("element access 2", Json, nlohmann::json, nlohmann::ordered_j
{
{
Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}, {"d", false}, {"e", true}};
typename Json::iterator it2 = jobject.erase(jobject.find("b"), jobject.find("e"));
typename Json::iterator const it2 = jobject.erase(jobject.find("b"), jobject.find("e"));
CHECK(jobject == Json({{"a", "a"}, {"e", true}}));
CHECK(*it2 == Json(true));
}
{
Json jobject = {{"a", "a"}, {"b", 1}, {"c", 17u}, {"d", false}, {"e", true}};
typename Json::const_iterator it2 = jobject.erase(jobject.find("b"), jobject.find("e"));
typename Json::const_iterator const it2 = jobject.erase(jobject.find("b"), jobject.find("e"));
CHECK(jobject == Json({{"a", "a"}, {"e", true}}));
CHECK(*it2 == Json(true));
}
@ -1509,7 +1509,7 @@ TEST_CASE_TEMPLATE("element access 2 (additional value() tests)", Json, nlohmann
const char* cpstr = "default";
const char castr[] = "default"; // NOLINT(hicpp-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
string_t str = "default";
string_t const str = "default";
number_integer_t integer = 69;
std::size_t size = 69;
@ -1592,9 +1592,9 @@ TEST_CASE_TEMPLATE("element access 2 (additional value() tests)", Json, nlohmann
SECTION("string_t/object_t::key_type key")
{
string_t key = "foo";
string_t key2 = "baz";
string_t key_notfound = "bar";
string_t const key = "foo";
string_t const key2 = "baz";
string_t const key_notfound = "bar";
CHECK(j.value(key, "default") == "bar");
CHECK(j.value(key, cpstr) == "bar");
@ -1618,9 +1618,9 @@ TEST_CASE_TEMPLATE("element access 2 (additional value() tests)", Json, nlohmann
#ifdef JSON_HAS_CPP_17
SECTION("std::string_view key")
{
std::string_view key = "foo";
std::string_view key2 = "baz";
std::string_view key_notfound = "bar";
std::string_view const key = "foo";
std::string_view const key2 = "baz";
std::string_view const key_notfound = "bar";
CHECK(j.value(key, "default") == "bar");
CHECK(j.value(key, cpstr) == "bar");
@ -1730,9 +1730,9 @@ TEST_CASE_TEMPLATE("element access 2 (additional value() tests)", Json, nlohmann
SECTION("string_t/object_t::key_type key")
{
string_t key = "foo";
string_t key2 = "baz";
string_t key_notfound = "bar";
string_t const key = "foo";
string_t const key2 = "baz";
string_t const key_notfound = "bar";
CHECK(j.template value<string_t>(key, "default") == "bar");
CHECK(j.template value<string_t>(key, cpstr) == "bar");
@ -1756,9 +1756,9 @@ TEST_CASE_TEMPLATE("element access 2 (additional value() tests)", Json, nlohmann
#ifdef JSON_HAS_CPP_17
SECTION("std::string_view key")
{
std::string_view key = "foo";
std::string_view key2 = "baz";
std::string_view key_notfound = "bar";
std::string_view const key = "foo";
std::string_view const key2 = "baz";
std::string_view const key_notfound = "bar";
CHECK(j.template value<string_t>(key, "default") == "bar");
CHECK(j.template value<string_t>(key, cpstr) == "bar");

View File

@ -21,7 +21,7 @@ TEST_CASE("object inspection")
{
SECTION("object")
{
json j {{"foo", 1}, {"bar", false}};
json const j {{"foo", 1}, {"bar", false}};
CHECK(!j.is_null());
CHECK(!j.is_boolean());
CHECK(!j.is_number());
@ -39,7 +39,7 @@ TEST_CASE("object inspection")
SECTION("array")
{
json j {"foo", 1, 1u, 42.23, false};
json const j {"foo", 1, 1u, 42.23, false};
CHECK(!j.is_null());
CHECK(!j.is_boolean());
CHECK(!j.is_number());
@ -57,7 +57,7 @@ TEST_CASE("object inspection")
SECTION("null")
{
json j(nullptr);
json const j(nullptr);
CHECK(j.is_null());
CHECK(!j.is_boolean());
CHECK(!j.is_number());
@ -75,7 +75,7 @@ TEST_CASE("object inspection")
SECTION("boolean")
{
json j(true);
json const j(true);
CHECK(!j.is_null());
CHECK(j.is_boolean());
CHECK(!j.is_number());
@ -93,7 +93,7 @@ TEST_CASE("object inspection")
SECTION("string")
{
json j("Hello world");
json const j("Hello world");
CHECK(!j.is_null());
CHECK(!j.is_boolean());
CHECK(!j.is_number());
@ -111,7 +111,7 @@ TEST_CASE("object inspection")
SECTION("number (integer)")
{
json j(42);
json const j(42);
CHECK(!j.is_null());
CHECK(!j.is_boolean());
CHECK(j.is_number());
@ -129,7 +129,7 @@ TEST_CASE("object inspection")
SECTION("number (unsigned)")
{
json j(42u);
json const j(42u);
CHECK(!j.is_null());
CHECK(!j.is_boolean());
CHECK(j.is_number());
@ -147,7 +147,7 @@ TEST_CASE("object inspection")
SECTION("number (floating-point)")
{
json j(42.23);
json const j(42.23);
CHECK(!j.is_null());
CHECK(!j.is_boolean());
CHECK(j.is_number());
@ -165,7 +165,7 @@ TEST_CASE("object inspection")
SECTION("binary")
{
json j(json::value_t::binary);
json const j(json::value_t::binary);
CHECK(!j.is_null());
CHECK(!j.is_boolean());
CHECK(!j.is_number());
@ -183,7 +183,7 @@ TEST_CASE("object inspection")
SECTION("discarded")
{
json j(json::value_t::discarded);
json const j(json::value_t::discarded);
CHECK(!j.is_null());
CHECK(!j.is_boolean());
CHECK(!j.is_number());
@ -202,7 +202,7 @@ TEST_CASE("object inspection")
SECTION("serialization")
{
json j {{"object", json::object()}, {"array", {1, 2, 3, 4}}, {"number", 42}, {"boolean", false}, {"null", nullptr}, {"string", "Hello world"} };
json const j {{"object", json::object()}, {"array", {1, 2, 3, 4}}, {"number", 42}, {"boolean", false}, {"null", nullptr}, {"string", "Hello world"} };
SECTION("no indent / indent=-1")
{
@ -288,7 +288,7 @@ TEST_CASE("object inspection")
std::ifstream f_escaped(TEST_DATA_DIRECTORY "/json_nlohmann_tests/all_unicode_ascii.json");
std::ifstream f_unescaped(TEST_DATA_DIRECTORY "/json_nlohmann_tests/all_unicode.json");
json value = json::parse(f_unescaped);
json const value = json::parse(f_unescaped);
std::string text = value.dump(4, ' ', true);
std::string expected((std::istreambuf_iterator<char>(f_escaped)),
@ -299,7 +299,7 @@ TEST_CASE("object inspection")
SECTION("serialization of discarded element")
{
json j_discarded(json::value_t::discarded);
json const j_discarded(json::value_t::discarded);
CHECK(j_discarded.dump() == "<discarded>");
}
@ -315,7 +315,7 @@ TEST_CASE("object inspection")
ss.str(std::string());
// use stringstream for JSON serialization
json j_number = 3.14159265358979;
json const j_number = 3.14159265358979;
ss << j_number;
// check that precision has been overridden during serialization
@ -332,9 +332,9 @@ TEST_CASE("object inspection")
{"3.141592653589793", "1000000000000000010E5"
})
{
json j1 = json::parse(s);
json const j1 = json::parse(s);
std::string s1 = j1.dump();
json j2 = json::parse(s1);
json const j2 = json::parse(s1);
std::string s2 = j2.dump();
CHECK(s1 == s2);
}
@ -344,49 +344,49 @@ TEST_CASE("object inspection")
{
SECTION("null")
{
json j = nullptr;
json const j = nullptr;
CHECK(j.type() == json::value_t::null);
}
SECTION("object")
{
json j = {{"foo", "bar"}};
json const j = {{"foo", "bar"}};
CHECK(j.type() == json::value_t::object);
}
SECTION("array")
{
json j = {1, 2, 3, 4};
json const j = {1, 2, 3, 4};
CHECK(j.type() == json::value_t::array);
}
SECTION("boolean")
{
json j = true;
json const j = true;
CHECK(j.type() == json::value_t::boolean);
}
SECTION("string")
{
json j = "Hello world";
json const j = "Hello world";
CHECK(j.type() == json::value_t::string);
}
SECTION("number (integer)")
{
json j = 23;
json const j = 23;
CHECK(j.type() == json::value_t::number_integer);
}
SECTION("number (unsigned)")
{
json j = 23u;
json const j = 23u;
CHECK(j.type() == json::value_t::number_unsigned);
}
SECTION("number (floating-point)")
{
json j = 42.23;
json const j = 42.23;
CHECK(j.type() == json::value_t::number_float);
}
}
@ -395,63 +395,63 @@ TEST_CASE("object inspection")
{
SECTION("null")
{
json j = nullptr;
json const j = nullptr;
json::value_t t = j;
CHECK(t == j.type());
}
SECTION("object")
{
json j = {{"foo", "bar"}};
json const j = {{"foo", "bar"}};
json::value_t t = j;
CHECK(t == j.type());
}
SECTION("array")
{
json j = {1, 2, 3, 4};
json const j = {1, 2, 3, 4};
json::value_t t = j;
CHECK(t == j.type());
}
SECTION("boolean")
{
json j = true;
json const j = true;
json::value_t t = j;
CHECK(t == j.type());
}
SECTION("string")
{
json j = "Hello world";
json const j = "Hello world";
json::value_t t = j;
CHECK(t == j.type());
}
SECTION("number (integer)")
{
json j = 23;
json const j = 23;
json::value_t t = j;
CHECK(t == j.type());
}
SECTION("number (unsigned)")
{
json j = 23u;
json const j = 23u;
json::value_t t = j;
CHECK(t == j.type());
}
SECTION("number (floating-point)")
{
json j = 42.23;
json const j = 42.23;
json::value_t t = j;
CHECK(t == j.type());
}
SECTION("binary")
{
json j = json::binary({});
json const j = json::binary({});
json::value_t t = j;
CHECK(t == j.type());
}

View File

@ -35,13 +35,13 @@ TEST_CASE("JSON patch")
SECTION("4.1 add")
{
json patch1 = R"([{ "op": "add", "path": "/a/b", "value": [ "foo", "bar" ] }])"_json;
json const patch1 = R"([{ "op": "add", "path": "/a/b", "value": [ "foo", "bar" ] }])"_json;
// However, the object itself or an array containing it does need
// to exist, and it remains an error for that not to be the case.
// For example, an "add" with a target location of "/a/b" starting
// with this document
json doc1 = R"({ "a": { "foo": 1 } })"_json;
json const doc1 = R"({ "a": { "foo": 1 } })"_json;
// is not an error, because "a" exists, and "b" will be added to
// its value.
@ -57,13 +57,13 @@ TEST_CASE("JSON patch")
CHECK(doc1.patch(patch1) == doc1_ans);
// It is an error in this document:
json doc2 = R"({ "q": { "bar": 2 } })"_json;
json const doc2 = R"({ "q": { "bar": 2 } })"_json;
// because "a" does not exist.
CHECK_THROWS_WITH_AS(doc2.patch(patch1), "[json.exception.out_of_range.403] key 'a' not found", json::out_of_range&);
json doc3 = R"({ "a": {} })"_json;
json patch2 = R"([{ "op": "add", "path": "/a/b/c", "value": 1 }])"_json;
json const doc3 = R"({ "a": {} })"_json;
json const patch2 = R"([{ "op": "add", "path": "/a/b/c", "value": 1 }])"_json;
// should cause an error because "b" does not exist in doc3
#if JSON_DIAGNOSTICS
@ -77,20 +77,20 @@ TEST_CASE("JSON patch")
{
// If removing an element from an array, any elements above the
// specified index are shifted one position to the left.
json doc = {1, 2, 3, 4};
json patch = {{{"op", "remove"}, {"path", "/1"}}};
json const doc = {1, 2, 3, 4};
json const patch = {{{"op", "remove"}, {"path", "/1"}}};
CHECK(doc.patch(patch) == json({1, 3, 4}));
}
SECTION("A.1. Adding an Object Member")
{
// An example target JSON document:
json doc = R"(
json const doc = R"(
{ "foo": "bar"}
)"_json;
// A JSON Patch document:
json patch = R"(
json const patch = R"(
[
{ "op": "add", "path": "/baz", "value": "qux" }
]
@ -114,12 +114,12 @@ TEST_CASE("JSON patch")
SECTION("A.2. Adding an Array Element")
{
// An example target JSON document:
json doc = R"(
json const doc = R"(
{ "foo": [ "bar", "baz" ] }
)"_json;
// A JSON Patch document:
json patch = R"(
json const patch = R"(
[
{ "op": "add", "path": "/foo/1", "value": "qux" }
]
@ -140,7 +140,7 @@ TEST_CASE("JSON patch")
SECTION("A.3. Removing an Object Member")
{
// An example target JSON document:
json doc = R"(
json const doc = R"(
{
"baz": "qux",
"foo": "bar"
@ -148,7 +148,7 @@ TEST_CASE("JSON patch")
)"_json;
// A JSON Patch document:
json patch = R"(
json const patch = R"(
[
{ "op": "remove", "path": "/baz" }
]
@ -169,12 +169,12 @@ TEST_CASE("JSON patch")
SECTION("A.4. Removing an Array Element")
{
// An example target JSON document:
json doc = R"(
json const doc = R"(
{ "foo": [ "bar", "qux", "baz" ] }
)"_json;
// A JSON Patch document:
json patch = R"(
json const patch = R"(
[
{ "op": "remove", "path": "/foo/1" }
]
@ -195,7 +195,7 @@ TEST_CASE("JSON patch")
SECTION("A.5. Replacing a Value")
{
// An example target JSON document:
json doc = R"(
json const doc = R"(
{
"baz": "qux",
"foo": "bar"
@ -203,7 +203,7 @@ TEST_CASE("JSON patch")
)"_json;
// A JSON Patch document:
json patch = R"(
json const patch = R"(
[
{ "op": "replace", "path": "/baz", "value": "boo" }
]
@ -226,7 +226,7 @@ TEST_CASE("JSON patch")
SECTION("A.6. Moving a Value")
{
// An example target JSON document:
json doc = R"(
json const doc = R"(
{
"foo": {
"bar": "baz",
@ -239,7 +239,7 @@ TEST_CASE("JSON patch")
)"_json;
// A JSON Patch document:
json patch = R"(
json const patch = R"(
[
{ "op": "move", "from": "/foo/waldo", "path": "/qux/thud" }
]
@ -268,12 +268,12 @@ TEST_CASE("JSON patch")
SECTION("A.7. Moving a Value")
{
// An example target JSON document:
json doc = R"(
json const doc = R"(
{ "foo": [ "all", "grass", "cows", "eat" ] }
)"_json;
// A JSON Patch document:
json patch = R"(
json const patch = R"(
[
{ "op": "move", "from": "/foo/1", "path": "/foo/3" }
]
@ -302,7 +302,7 @@ TEST_CASE("JSON patch")
)"_json;
// A JSON Patch document that will result in successful evaluation:
json patch = R"(
json const patch = R"(
[
{ "op": "test", "path": "/baz", "value": "qux" },
{ "op": "test", "path": "/foo/1", "value": 2 }
@ -318,7 +318,7 @@ TEST_CASE("JSON patch")
SECTION("A.9. Testing a Value: Error")
{
// An example target JSON document:
json doc = R"(
json const doc = R"(
{ "baz": "qux" }
)"_json;
@ -341,12 +341,12 @@ TEST_CASE("JSON patch")
SECTION("A.10. Adding a Nested Member Object")
{
// An example target JSON document:
json doc = R"(
json const doc = R"(
{ "foo": "bar" }
)"_json;
// A JSON Patch document:
json patch = R"(
json const patch = R"(
[
{ "op": "add", "path": "/child", "value": { "grandchild": { } } }
]
@ -373,12 +373,12 @@ TEST_CASE("JSON patch")
SECTION("A.11. Ignoring Unrecognized Elements")
{
// An example target JSON document:
json doc = R"(
json const doc = R"(
{ "foo": "bar" }
)"_json;
// A JSON Patch document:
json patch = R"(
json const patch = R"(
[
{ "op": "add", "path": "/baz", "value": "qux", "xyz": 123 }
]
@ -401,12 +401,12 @@ TEST_CASE("JSON patch")
SECTION("A.12. Adding to a Nonexistent Target")
{
// An example target JSON document:
json doc = R"(
json const doc = R"(
{ "foo": "bar" }
)"_json;
// A JSON Patch document:
json patch = R"(
json const patch = R"(
[
{ "op": "add", "path": "/baz/bat", "value": "qux" }
]
@ -427,7 +427,7 @@ TEST_CASE("JSON patch")
SECTION("A.14. Escape Ordering")
{
// An example target JSON document:
json doc = R"(
json const doc = R"(
{
"/": 9,
"~1": 10
@ -435,7 +435,7 @@ TEST_CASE("JSON patch")
)"_json;
// A JSON Patch document:
json patch = R"(
json const patch = R"(
[
{"op": "test", "path": "/~01", "value": 10}
]
@ -458,7 +458,7 @@ TEST_CASE("JSON patch")
SECTION("A.15. Comparing Strings and Numbers")
{
// An example target JSON document:
json doc = R"(
json const doc = R"(
{
"/": 9,
"~1": 10
@ -484,12 +484,12 @@ TEST_CASE("JSON patch")
SECTION("A.16. Adding an Array Value")
{
// An example target JSON document:
json doc = R"(
json const doc = R"(
{ "foo": ["bar"] }
)"_json;
// A JSON Patch document:
json patch = R"(
json const patch = R"(
[
{ "op": "add", "path": "/foo/-", "value": ["abc", "def"] }
]
@ -519,10 +519,10 @@ TEST_CASE("JSON patch")
// document.
// An example target JSON document:
json doc = 17;
json const doc = 17;
// A JSON Patch document:
json patch = R"(
json const patch = R"(
[
{ "op": "add", "path": "", "value": [1,2,3] }
]
@ -545,10 +545,10 @@ TEST_CASE("JSON patch")
// exactly the number of elements in the array which is legal.
// An example target JSON document:
json doc = {0, 1, 2};
json const doc = {0, 1, 2};
// A JSON Patch document:
json patch = R"(
json const patch = R"(
[
{ "op": "add", "path": "/3", "value": 3 }
]
@ -568,7 +568,7 @@ TEST_CASE("JSON patch")
SECTION("copy")
{
// An example target JSON document:
json doc = R"(
json const doc = R"(
{
"foo": {
"bar": "baz",
@ -581,7 +581,7 @@ TEST_CASE("JSON patch")
)"_json;
// A JSON Patch document:
json patch = R"(
json const patch = R"(
[
{ "op": "copy", "from": "/foo/waldo", "path": "/qux/thud" }
]
@ -610,8 +610,8 @@ TEST_CASE("JSON patch")
SECTION("replace")
{
json j = "string";
json patch = {{{"op", "replace"}, {"path", ""}, {"value", 1}}};
json const j = "string";
json const patch = {{{"op", "replace"}, {"path", ""}, {"value", 1}}};
CHECK(j.patch(patch) == json(1));
}
@ -619,12 +619,12 @@ TEST_CASE("JSON patch")
{
{
// a JSON patch
json p1 = R"(
json const p1 = R"(
[{"op": "add", "path": "/GB", "value": "London"}]
)"_json;
// a JSON value
json source = R"(
json const source = R"(
{"D": "Berlin", "F": "Paris"}
)"_json;
@ -665,15 +665,15 @@ TEST_CASE("JSON patch")
{
SECTION("not an array")
{
json j;
json patch = {{"op", "add"}, {"path", ""}, {"value", 1}};
json const j;
json const patch = {{"op", "add"}, {"path", ""}, {"value", 1}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.104] parse error: JSON patch must be an array of objects", json::parse_error&);
}
SECTION("not an array of objects")
{
json j;
json patch = {"op", "add", "path", "", "value", 1};
json const j;
json const patch = {"op", "add", "path", "", "value", 1};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.104] parse error: (/0) JSON patch must be an array of objects", json::parse_error&);
#else
@ -683,8 +683,8 @@ TEST_CASE("JSON patch")
SECTION("missing 'op'")
{
json j;
json patch = {{{"foo", "bar"}}};
json const j;
json const patch = {{{"foo", "bar"}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation must have member 'op'", json::parse_error&);
#else
@ -694,8 +694,8 @@ TEST_CASE("JSON patch")
SECTION("non-string 'op'")
{
json j;
json patch = {{{"op", 1}}};
json const j;
json const patch = {{{"op", 1}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation must have string member 'op'", json::parse_error&);
#else
@ -705,8 +705,8 @@ TEST_CASE("JSON patch")
SECTION("invalid operation")
{
json j;
json patch = {{{"op", "foo"}, {"path", ""}}};
json const j;
json const patch = {{{"op", "foo"}, {"path", ""}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation value 'foo' is invalid", json::parse_error&);
#else
@ -719,8 +719,8 @@ TEST_CASE("JSON patch")
{
SECTION("missing 'path'")
{
json j;
json patch = {{{"op", "add"}}};
json const j;
json const patch = {{{"op", "add"}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have member 'path'", json::parse_error&);
#else
@ -730,8 +730,8 @@ TEST_CASE("JSON patch")
SECTION("non-string 'path'")
{
json j;
json patch = {{{"op", "add"}, {"path", 1}}};
json const j;
json const patch = {{{"op", "add"}, {"path", 1}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have string member 'path'", json::parse_error&);
#else
@ -741,8 +741,8 @@ TEST_CASE("JSON patch")
SECTION("missing 'value'")
{
json j;
json patch = {{{"op", "add"}, {"path", ""}}};
json const j;
json const patch = {{{"op", "add"}, {"path", ""}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have member 'value'", json::parse_error&);
#else
@ -752,8 +752,8 @@ TEST_CASE("JSON patch")
SECTION("invalid array index")
{
json j = {1, 2};
json patch = {{{"op", "add"}, {"path", "/4"}, {"value", 4}}};
json const j = {1, 2};
json const patch = {{{"op", "add"}, {"path", "/4"}, {"value", 4}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.401] array index 4 is out of range", json::out_of_range&);
}
}
@ -762,8 +762,8 @@ TEST_CASE("JSON patch")
{
SECTION("missing 'path'")
{
json j;
json patch = {{{"op", "remove"}}};
json const j;
json const patch = {{{"op", "remove"}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'remove' must have member 'path'", json::parse_error&);
#else
@ -773,8 +773,8 @@ TEST_CASE("JSON patch")
SECTION("non-string 'path'")
{
json j;
json patch = {{{"op", "remove"}, {"path", 1}}};
json const j;
json const patch = {{{"op", "remove"}, {"path", 1}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'remove' must have string member 'path'", json::parse_error&);
#else
@ -784,22 +784,22 @@ TEST_CASE("JSON patch")
SECTION("nonexisting target location (array)")
{
json j = {1, 2, 3};
json patch = {{{"op", "remove"}, {"path", "/17"}}};
json const j = {1, 2, 3};
json const patch = {{{"op", "remove"}, {"path", "/17"}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.401] array index 17 is out of range", json::out_of_range&);
}
SECTION("nonexisting target location (object)")
{
json j = {{"foo", 1}, {"bar", 2}};
json patch = {{{"op", "remove"}, {"path", "/baz"}}};
json const j = {{"foo", 1}, {"bar", 2}};
json const patch = {{{"op", "remove"}, {"path", "/baz"}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.403] key 'baz' not found", json::out_of_range&);
}
SECTION("root element as target location")
{
json j = "string";
json patch = {{{"op", "remove"}, {"path", ""}}};
json const j = "string";
json const patch = {{{"op", "remove"}, {"path", ""}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.405] JSON pointer has no parent", json::out_of_range&);
}
}
@ -808,8 +808,8 @@ TEST_CASE("JSON patch")
{
SECTION("missing 'path'")
{
json j;
json patch = {{{"op", "replace"}}};
json const j;
json const patch = {{{"op", "replace"}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have member 'path'", json::parse_error&);
#else
@ -819,8 +819,8 @@ TEST_CASE("JSON patch")
SECTION("non-string 'path'")
{
json j;
json patch = {{{"op", "replace"}, {"path", 1}}};
json const j;
json const patch = {{{"op", "replace"}, {"path", 1}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have string member 'path'", json::parse_error&);
#else
@ -830,8 +830,8 @@ TEST_CASE("JSON patch")
SECTION("missing 'value'")
{
json j;
json patch = {{{"op", "replace"}, {"path", ""}}};
json const j;
json const patch = {{{"op", "replace"}, {"path", ""}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have member 'value'", json::parse_error&);
#else
@ -841,15 +841,15 @@ TEST_CASE("JSON patch")
SECTION("nonexisting target location (array)")
{
json j = {1, 2, 3};
json patch = {{{"op", "replace"}, {"path", "/17"}, {"value", 19}}};
json const j = {1, 2, 3};
json const patch = {{{"op", "replace"}, {"path", "/17"}, {"value", 19}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.401] array index 17 is out of range", json::out_of_range&);
}
SECTION("nonexisting target location (object)")
{
json j = {{"foo", 1}, {"bar", 2}};
json patch = {{{"op", "replace"}, {"path", "/baz"}, {"value", 3}}};
json const j = {{"foo", 1}, {"bar", 2}};
json const patch = {{{"op", "replace"}, {"path", "/baz"}, {"value", 3}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.403] key 'baz' not found", json::out_of_range&);
}
}
@ -858,8 +858,8 @@ TEST_CASE("JSON patch")
{
SECTION("missing 'path'")
{
json j;
json patch = {{{"op", "move"}}};
json const j;
json const patch = {{{"op", "move"}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have member 'path'", json::parse_error&);
#else
@ -869,8 +869,8 @@ TEST_CASE("JSON patch")
SECTION("non-string 'path'")
{
json j;
json patch = {{{"op", "move"}, {"path", 1}}};
json const j;
json const patch = {{{"op", "move"}, {"path", 1}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have string member 'path'", json::parse_error&);
#else
@ -880,8 +880,8 @@ TEST_CASE("JSON patch")
SECTION("missing 'from'")
{
json j;
json patch = {{{"op", "move"}, {"path", ""}}};
json const j;
json const patch = {{{"op", "move"}, {"path", ""}}};
CHECK_THROWS_AS(j.patch(patch), json::parse_error&);
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have member 'from'", json::parse_error&);
@ -892,8 +892,8 @@ TEST_CASE("JSON patch")
SECTION("non-string 'from'")
{
json j;
json patch = {{{"op", "move"}, {"path", ""}, {"from", 1}}};
json const j;
json const patch = {{{"op", "move"}, {"path", ""}, {"from", 1}}};
CHECK_THROWS_AS(j.patch(patch), json::parse_error&);
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have string member 'from'", json::parse_error&);
@ -904,15 +904,15 @@ TEST_CASE("JSON patch")
SECTION("nonexisting from location (array)")
{
json j = {1, 2, 3};
json patch = {{{"op", "move"}, {"path", "/0"}, {"from", "/5"}}};
json const j = {1, 2, 3};
json const patch = {{{"op", "move"}, {"path", "/0"}, {"from", "/5"}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.401] array index 5 is out of range", json::out_of_range&);
}
SECTION("nonexisting from location (object)")
{
json j = {{"foo", 1}, {"bar", 2}};
json patch = {{{"op", "move"}, {"path", "/baz"}, {"from", "/baz"}}};
json const j = {{"foo", 1}, {"bar", 2}};
json const patch = {{{"op", "move"}, {"path", "/baz"}, {"from", "/baz"}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.403] key 'baz' not found", json::out_of_range&);
}
}
@ -921,8 +921,8 @@ TEST_CASE("JSON patch")
{
SECTION("missing 'path'")
{
json j;
json patch = {{{"op", "copy"}}};
json const j;
json const patch = {{{"op", "copy"}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have member 'path'", json::parse_error&);
#else
@ -932,8 +932,8 @@ TEST_CASE("JSON patch")
SECTION("non-string 'path'")
{
json j;
json patch = {{{"op", "copy"}, {"path", 1}}};
json const j;
json const patch = {{{"op", "copy"}, {"path", 1}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have string member 'path'", json::parse_error&);
#else
@ -943,8 +943,8 @@ TEST_CASE("JSON patch")
SECTION("missing 'from'")
{
json j;
json patch = {{{"op", "copy"}, {"path", ""}}};
json const j;
json const patch = {{{"op", "copy"}, {"path", ""}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have member 'from'", json::parse_error&);
#else
@ -954,8 +954,8 @@ TEST_CASE("JSON patch")
SECTION("non-string 'from'")
{
json j;
json patch = {{{"op", "copy"}, {"path", ""}, {"from", 1}}};
json const j;
json const patch = {{{"op", "copy"}, {"path", ""}, {"from", 1}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have string member 'from'", json::parse_error&);
#else
@ -965,15 +965,15 @@ TEST_CASE("JSON patch")
SECTION("nonexisting from location (array)")
{
json j = {1, 2, 3};
json patch = {{{"op", "copy"}, {"path", "/0"}, {"from", "/5"}}};
json const j = {1, 2, 3};
json const patch = {{{"op", "copy"}, {"path", "/0"}, {"from", "/5"}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.401] array index 5 is out of range", json::out_of_range&);
}
SECTION("nonexisting from location (object)")
{
json j = {{"foo", 1}, {"bar", 2}};
json patch = {{{"op", "copy"}, {"path", "/fob"}, {"from", "/baz"}}};
json const j = {{"foo", 1}, {"bar", 2}};
json const patch = {{{"op", "copy"}, {"path", "/fob"}, {"from", "/baz"}}};
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.out_of_range.403] key 'baz' not found", json::out_of_range&);
}
}
@ -982,8 +982,8 @@ TEST_CASE("JSON patch")
{
SECTION("missing 'path'")
{
json j;
json patch = {{{"op", "test"}}};
json const j;
json const patch = {{{"op", "test"}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have member 'path'", json::parse_error&);
#else
@ -993,8 +993,8 @@ TEST_CASE("JSON patch")
SECTION("non-string 'path'")
{
json j;
json patch = {{{"op", "test"}, {"path", 1}}};
json const j;
json const patch = {{{"op", "test"}, {"path", 1}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have string member 'path'", json::parse_error&);
#else
@ -1004,8 +1004,8 @@ TEST_CASE("JSON patch")
SECTION("missing 'value'")
{
json j;
json patch = {{{"op", "test"}, {"path", ""}}};
json const j;
json const patch = {{{"op", "test"}, {"path", ""}}};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have member 'value'", json::parse_error&);
#else
@ -1020,7 +1020,7 @@ TEST_CASE("JSON patch")
SECTION("Simple Example")
{
// The original document
json doc = R"(
json const doc = R"(
{
"baz": "qux",
"foo": "bar"
@ -1028,7 +1028,7 @@ TEST_CASE("JSON patch")
)"_json;
// The patch
json patch = R"(
json const patch = R"(
[
{ "op": "replace", "path": "/baz", "value": "boo" },
{ "op": "add", "path": "/hello", "value": ["world"] },
@ -1054,7 +1054,7 @@ TEST_CASE("JSON patch")
SECTION("Operations")
{
// The original document
json doc = R"(
json const doc = R"(
{
"biscuits": [
{"name":"Digestive"},
@ -1066,7 +1066,7 @@ TEST_CASE("JSON patch")
SECTION("add")
{
// The patch
json patch = R"(
json const patch = R"(
[
{"op": "add", "path": "/biscuits/1", "value": {"name": "Ginger Nut"}}
]
@ -1093,7 +1093,7 @@ TEST_CASE("JSON patch")
SECTION("remove")
{
// The patch
json patch = R"(
json const patch = R"(
[
{"op": "remove", "path": "/biscuits"}
]
@ -1114,7 +1114,7 @@ TEST_CASE("JSON patch")
SECTION("replace")
{
// The patch
json patch = R"(
json const patch = R"(
[
{"op": "replace", "path": "/biscuits/0/name", "value": "Chocolate Digestive"}
]
@ -1140,7 +1140,7 @@ TEST_CASE("JSON patch")
SECTION("copy")
{
// The patch
json patch = R"(
json const patch = R"(
[
{"op": "copy", "from": "/biscuits/0", "path": "/best_biscuit"}
]
@ -1169,7 +1169,7 @@ TEST_CASE("JSON patch")
SECTION("move")
{
// The patch
json patch = R"(
json const patch = R"(
[
{"op": "move", "from": "/biscuits", "path": "/cookies"}
]
@ -1290,7 +1290,7 @@ TEST_CASE("JSON patch")
{
CAPTURE(filename)
std::ifstream f(filename);
json suite = json::parse(f);
json const suite = json::parse(f);
for (const auto& test : suite)
{

View File

@ -40,7 +40,7 @@ TEST_CASE("JSON pointers")
SECTION("array index error")
{
json v = {1, 2, 3, 4};
json::json_pointer ptr("/10e");
json::json_pointer const ptr("/10e");
CHECK_THROWS_WITH_AS(v[ptr],
"[json.exception.out_of_range.404] unresolved reference token '10e'", json::out_of_range&);
}
@ -311,8 +311,8 @@ TEST_CASE("JSON pointers")
{
auto too_large_index = std::to_string((std::numeric_limits<unsigned long long>::max)()) + "1";
json::json_pointer jp(std::string("/") + too_large_index);
std::string throw_msg = std::string("[json.exception.out_of_range.404] unresolved reference token '") + too_large_index + "'";
json::json_pointer const jp(std::string("/") + too_large_index);
std::string const throw_msg = std::string("[json.exception.out_of_range.404] unresolved reference token '") + too_large_index + "'";
CHECK_THROWS_WITH_AS(j[jp] = 1, throw_msg.c_str(), json::out_of_range&);
CHECK_THROWS_WITH_AS(j_const[jp] == 1, throw_msg.c_str(), json::out_of_range&);
@ -326,8 +326,8 @@ TEST_CASE("JSON pointers")
{
auto size_type_max_uul = static_cast<unsigned long long>((std::numeric_limits<json::size_type>::max)());
auto too_large_index = std::to_string(size_type_max_uul);
json::json_pointer jp(std::string("/") + too_large_index);
std::string throw_msg = std::string("[json.exception.out_of_range.410] array index ") + too_large_index + " exceeds size_type";
json::json_pointer const jp(std::string("/") + too_large_index);
std::string const throw_msg = std::string("[json.exception.out_of_range.410] array index ") + too_large_index + " exceeds size_type";
CHECK_THROWS_WITH_AS(j[jp] = 1, throw_msg.c_str(), json::out_of_range&);
CHECK_THROWS_WITH_AS(j_const[jp] == 1, throw_msg.c_str(), json::out_of_range&);
@ -455,7 +455,7 @@ TEST_CASE("JSON pointers")
#endif
// error for conflicting values
json j_error = {{"", 42}, {"/foo", 17}};
json const j_error = {{"", 42}, {"/foo", 17}};
CHECK_THROWS_WITH_AS(j_error.unflatten(),
"[json.exception.type_error.313] invalid value to unflatten", json::type_error&);
@ -473,9 +473,9 @@ TEST_CASE("JSON pointers")
CHECK(j_string.flatten().unflatten() == j_string);
// roundtrip for empty structured values (will be unflattened to null)
json j_array(json::value_t::array);
json const j_array(json::value_t::array);
CHECK(j_array.flatten().unflatten() == json());
json j_object(json::value_t::object);
json const j_object(json::value_t::object);
CHECK(j_object.flatten().unflatten() == json());
}
@ -485,7 +485,7 @@ TEST_CASE("JSON pointers")
{"", "/foo", "/foo/0", "/", "/a~1b", "/c%d", "/e^f", "/g|h", "/i\\j", "/k\"l", "/ ", "/m~0n"
})
{
json::json_pointer ptr(ptr_str);
json::json_pointer const ptr(ptr_str);
std::stringstream ss;
ss << ptr;
CHECK(ptr.to_string() == ptr_str);
@ -742,7 +742,7 @@ TEST_CASE("JSON pointers")
CHECK(std::is_same<json_ptr_str::string_t, json_ptr_j::string_t>::value);
CHECK(std::is_same<json_ptr_str::string_t, json_ptr_oj::string_t>::value);
std::string ptr_string{"/foo/0"};
std::string const ptr_string{"/foo/0"};
json_ptr_str ptr{ptr_string};
json_ptr_j ptr_j{ptr_string};
json_ptr_oj ptr_oj{ptr_string};

View File

@ -28,7 +28,7 @@ TEST_CASE("JSON Merge Patch")
}
})"_json;
json patch = R"({
json const patch = R"({
"a": "z",
"c": {
"f": null
@ -61,7 +61,7 @@ TEST_CASE("JSON Merge Patch")
"content": "This will be unchanged"
})"_json;
json patch = R"({
json const patch = R"({
"title": "Hello!",
"phoneNumber": "+01-123-456-7890",
"author": {
@ -93,7 +93,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 1")
{
json original = R"({"a":"b"})"_json;
json patch = R"({"a":"c"})"_json;
json const patch = R"({"a":"c"})"_json;
json result = R"({"a":"c"})"_json;
original.merge_patch(patch);
@ -103,7 +103,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 2")
{
json original = R"({"a":"b"})"_json;
json patch = R"({"b":"c"})"_json;
json const patch = R"({"b":"c"})"_json;
json result = R"({"a":"b", "b":"c"})"_json;
original.merge_patch(patch);
@ -113,7 +113,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 3")
{
json original = R"({"a":"b"})"_json;
json patch = R"({"a":null})"_json;
json const patch = R"({"a":null})"_json;
json result = R"({})"_json;
original.merge_patch(patch);
@ -123,7 +123,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 4")
{
json original = R"({"a":"b","b":"c"})"_json;
json patch = R"({"a":null})"_json;
json const patch = R"({"a":null})"_json;
json result = R"({"b":"c"})"_json;
original.merge_patch(patch);
@ -133,7 +133,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 5")
{
json original = R"({"a":["b"]})"_json;
json patch = R"({"a":"c"})"_json;
json const patch = R"({"a":"c"})"_json;
json result = R"({"a":"c"})"_json;
original.merge_patch(patch);
@ -143,7 +143,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 6")
{
json original = R"({"a":"c"})"_json;
json patch = R"({"a":["b"]})"_json;
json const patch = R"({"a":["b"]})"_json;
json result = R"({"a":["b"]})"_json;
original.merge_patch(patch);
@ -153,7 +153,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 7")
{
json original = R"({"a":{"b": "c"}})"_json;
json patch = R"({"a":{"b":"d","c":null}})"_json;
json const patch = R"({"a":{"b":"d","c":null}})"_json;
json result = R"({"a": {"b": "d"}})"_json;
original.merge_patch(patch);
@ -163,7 +163,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 8")
{
json original = R"({"a":[{"b":"c"}]})"_json;
json patch = R"({"a":[1]})"_json;
json const patch = R"({"a":[1]})"_json;
json result = R"({"a":[1]})"_json;
original.merge_patch(patch);
@ -173,7 +173,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 9")
{
json original = R"(["a","b"])"_json;
json patch = R"(["c","d"])"_json;
json const patch = R"(["c","d"])"_json;
json result = R"(["c","d"])"_json;
original.merge_patch(patch);
@ -183,7 +183,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 10")
{
json original = R"({"a":"b"})"_json;
json patch = R"(["c"])"_json;
json const patch = R"(["c"])"_json;
json result = R"(["c"])"_json;
original.merge_patch(patch);
@ -193,7 +193,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 11")
{
json original = R"({"a":"foo"})"_json;
json patch = R"(null)"_json;
json const patch = R"(null)"_json;
json result = R"(null)"_json;
original.merge_patch(patch);
@ -203,7 +203,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 12")
{
json original = R"({"a":"foo"})"_json;
json patch = R"("bar")"_json;
json const patch = R"("bar")"_json;
json result = R"("bar")"_json;
original.merge_patch(patch);
@ -213,7 +213,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 13")
{
json original = R"({"e":null})"_json;
json patch = R"({"a":1})"_json;
json const patch = R"({"a":1})"_json;
json result = R"({"e":null,"a":1})"_json;
original.merge_patch(patch);
@ -223,7 +223,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 14")
{
json original = R"([1,2])"_json;
json patch = R"({"a":"b","c":null})"_json;
json const patch = R"({"a":"b","c":null})"_json;
json result = R"({"a":"b"})"_json;
original.merge_patch(patch);
@ -233,7 +233,7 @@ TEST_CASE("JSON Merge Patch")
SECTION("Example 15")
{
json original = R"({})"_json;
json patch = R"({"a":{"bb":{"ccc":null}}})"_json;
json const patch = R"({"a":{"bb":{"ccc":null}}})"_json;
json result = R"({"a":{"bb":{}}})"_json;
original.merge_patch(patch);

View File

@ -19,7 +19,7 @@ TEST_CASE("modifiers")
SECTION("boolean")
{
json j = true;
json k = j;
json const k = j;
j.clear();
CHECK(j == json(json::value_t::boolean));
@ -29,7 +29,7 @@ TEST_CASE("modifiers")
SECTION("string")
{
json j = "hello world";
json k = j;
json const k = j;
j.clear();
CHECK(j == json(json::value_t::string));
@ -41,7 +41,7 @@ TEST_CASE("modifiers")
SECTION("empty array")
{
json j = json::array();
json k = j;
json const k = j;
j.clear();
CHECK(j.empty());
@ -52,7 +52,7 @@ TEST_CASE("modifiers")
SECTION("filled array")
{
json j = {1, 2, 3};
json k = j;
json const k = j;
j.clear();
CHECK(j.empty());
@ -66,7 +66,7 @@ TEST_CASE("modifiers")
SECTION("empty object")
{
json j = json::object();
json k = j;
json const k = j;
j.clear();
CHECK(j.empty());
@ -77,7 +77,7 @@ TEST_CASE("modifiers")
SECTION("filled object")
{
json j = {{"one", 1}, {"two", 2}, {"three", 3}};
json k = j;
json const k = j;
j.clear();
CHECK(j.empty());
@ -91,7 +91,7 @@ TEST_CASE("modifiers")
SECTION("empty binary")
{
json j = json::binary({});
json k = j;
json const k = j;
j.clear();
CHECK(!j.empty());
@ -102,7 +102,7 @@ TEST_CASE("modifiers")
SECTION("filled binary")
{
json j = json::binary({1, 2, 3, 4, 5});
json k = j;
json const k = j;
j.clear();
CHECK(!j.empty());
@ -114,7 +114,7 @@ TEST_CASE("modifiers")
SECTION("number (integer)")
{
json j = 23;
json k = j;
json const k = j;
j.clear();
CHECK(j == json(json::value_t::number_integer));
@ -124,7 +124,7 @@ TEST_CASE("modifiers")
SECTION("number (unsigned)")
{
json j = 23u;
json k = j;
json const k = j;
j.clear();
CHECK(j == json(json::value_t::number_integer));
@ -134,7 +134,7 @@ TEST_CASE("modifiers")
SECTION("number (float)")
{
json j = 23.42;
json k = j;
json const k = j;
j.clear();
CHECK(j == json(json::value_t::number_float));
@ -144,7 +144,7 @@ TEST_CASE("modifiers")
SECTION("null")
{
json j = nullptr;
json k = j;
json const k = j;
j.clear();
CHECK(j == json(json::value_t::null));
@ -187,7 +187,7 @@ TEST_CASE("modifiers")
SECTION("null")
{
json j;
json k(1);
json const k(1);
j.push_back(k);
j.push_back(k);
CHECK(j.type() == json::value_t::array);
@ -197,7 +197,7 @@ TEST_CASE("modifiers")
SECTION("array")
{
json j = {1, 2, 3};
json k("Hello");
json const k("Hello");
j.push_back(k);
CHECK(j.type() == json::value_t::array);
CHECK(j == json({1, 2, 3, "Hello"}));
@ -206,7 +206,7 @@ TEST_CASE("modifiers")
SECTION("other type")
{
json j = 1;
json k("Hello");
json const k("Hello");
CHECK_THROWS_WITH_AS(j.push_back(k), "[json.exception.type_error.308] cannot use push_back() with number", json::type_error&);
}
}
@ -238,7 +238,7 @@ TEST_CASE("modifiers")
SECTION("other type")
{
json j = 1;
json k("Hello");
json const k("Hello");
CHECK_THROWS_WITH_AS(j.push_back(json::object_t::value_type({"one", 1})), "[json.exception.type_error.308] cannot use push_back() with number", json::type_error&);
}
}
@ -415,7 +415,7 @@ TEST_CASE("modifiers")
SECTION("null")
{
json j;
json k(1);
json const k(1);
j += k;
j += k;
CHECK(j.type() == json::value_t::array);
@ -425,7 +425,7 @@ TEST_CASE("modifiers")
SECTION("array")
{
json j = {1, 2, 3};
json k("Hello");
json const k("Hello");
j += k;
CHECK(j.type() == json::value_t::array);
CHECK(j == json({1, 2, 3, "Hello"}));
@ -434,7 +434,7 @@ TEST_CASE("modifiers")
SECTION("other type")
{
json j = 1;
json k("Hello");
json const k("Hello");
CHECK_THROWS_WITH_AS(j += k, "[json.exception.type_error.308] cannot use push_back() with number", json::type_error&);
}
}
@ -466,7 +466,7 @@ TEST_CASE("modifiers")
SECTION("other type")
{
json j = 1;
json k("Hello");
json const k("Hello");
CHECK_THROWS_WITH_AS(j += json::object_t::value_type({"one", 1}), "[json.exception.type_error.308] cannot use push_back() with number", json::type_error&);
}
}
@ -663,7 +663,7 @@ TEST_CASE("modifiers")
SECTION("invalid iterators")
{
json j_other_array2 = {"first", "second"};
json const j_other_array2 = {"first", "second"};
CHECK_THROWS_WITH_AS(j_array.insert(j_object2.begin(), j_object2.end()), "[json.exception.type_error.309] cannot use insert() with array", json::type_error&);
CHECK_THROWS_WITH_AS(j_object1.insert(j_object1.begin(), j_object2.end()), "[json.exception.invalid_iterator.210] iterators do not fit", json::invalid_iterator&);
@ -774,7 +774,7 @@ TEST_CASE("modifiers")
SECTION("invalid iterators")
{
json j_other_array2 = {"first", "second"};
json const j_other_array2 = {"first", "second"};
CHECK_THROWS_WITH_AS(j_array.update(j_object2.begin(), j_object2.end()), "[json.exception.type_error.312] cannot use update() with array", json::type_error&);
CHECK_THROWS_WITH_AS(j_object1.update(j_object1.begin(), j_object2.end()), "[json.exception.invalid_iterator.210] iterators do not fit", json::invalid_iterator&);
@ -790,7 +790,7 @@ TEST_CASE("modifiers")
SECTION("extend object")
{
json j1 = {{"string", "s"}, {"numbers", {{"one", 1}}}};
json j2 = {{"string", "t"}, {"numbers", {{"two", 2}}}};
json const j2 = {{"string", "t"}, {"numbers", {{"two", 2}}}};
j1.update(j2, true);
CHECK(j1 == json({{"string", "t"}, {"numbers", {{"one", 1}, {"two", 2}}}}));
}
@ -798,7 +798,7 @@ TEST_CASE("modifiers")
SECTION("replace object")
{
json j1 = {{"string", "s"}, {"numbers", {{"one", 1}}}};
json j2 = {{"string", "t"}, {"numbers", 1}};
json const j2 = {{"string", "t"}, {"numbers", 1}};
j1.update(j2, true);
CHECK(j1 == json({{"string", "t"}, {"numbers", 1}}));
}

View File

@ -106,15 +106,15 @@ TEST_CASE("MessagePack")
SECTION("discarded")
{
// discarded values are not serialized
json j = json::value_t::discarded;
json const j = json::value_t::discarded;
const auto result = json::to_msgpack(j);
CHECK(result.empty());
}
SECTION("null")
{
json j = nullptr;
std::vector<uint8_t> expected = {0xc0};
json const j = nullptr;
std::vector<uint8_t> const expected = {0xc0};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
@ -127,8 +127,8 @@ TEST_CASE("MessagePack")
{
SECTION("true")
{
json j = true;
std::vector<uint8_t> expected = {0xc3};
json const j = true;
std::vector<uint8_t> const expected = {0xc3};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
@ -139,8 +139,8 @@ TEST_CASE("MessagePack")
SECTION("false")
{
json j = false;
std::vector<uint8_t> expected = {0xc2};
json const j = false;
std::vector<uint8_t> const expected = {0xc2};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
@ -161,14 +161,16 @@ TEST_CASE("MessagePack")
CAPTURE(i)
// create JSON value with integer number
json j = i;
json const j = i;
// check type
CHECK(j.is_number_integer());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(i));
std::vector<uint8_t> const expected
{
static_cast<uint8_t>(i)
};
// compare result + size
const auto result = json::to_msgpack(j);
@ -198,8 +200,7 @@ TEST_CASE("MessagePack")
CHECK(j.is_number_integer());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(i));
std::vector<uint8_t> const expected{static_cast<uint8_t>(i)};
// compare result + size
const auto result = json::to_msgpack(j);
@ -229,9 +230,11 @@ TEST_CASE("MessagePack")
CHECK(j.is_number_integer());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0xcc);
expected.push_back(static_cast<uint8_t>(i));
std::vector<uint8_t> const expected
{
0xcc,
static_cast<uint8_t>(i),
};
// compare result + size
const auto result = json::to_msgpack(j);
@ -240,7 +243,7 @@ TEST_CASE("MessagePack")
// check individual bytes
CHECK(result[0] == 0xcc);
auto restored = static_cast<uint8_t>(result[1]);
auto const restored = static_cast<uint8_t>(result[1]);
CHECK(restored == i);
// roundtrip
@ -263,10 +266,12 @@ TEST_CASE("MessagePack")
CHECK(j.is_number_integer());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0xcd);
expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(i & 0xff));
std::vector<uint8_t> const expected
{
0xcd,
static_cast<uint8_t>((i >> 8) & 0xff),
static_cast<uint8_t>(i & 0xff),
};
// compare result + size
const auto result = json::to_msgpack(j);
@ -275,7 +280,7 @@ TEST_CASE("MessagePack")
// check individual bytes
CHECK(result[0] == 0xcd);
auto restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
auto const restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
CHECK(restored == i);
// roundtrip
@ -301,12 +306,14 @@ TEST_CASE("MessagePack")
CHECK(j.is_number_integer());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0xce);
expected.push_back(static_cast<uint8_t>((i >> 24) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 16) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(i & 0xff));
std::vector<uint8_t> const expected
{
0xce,
static_cast<uint8_t>((i >> 24) & 0xff),
static_cast<uint8_t>((i >> 16) & 0xff),
static_cast<uint8_t>((i >> 8) & 0xff),
static_cast<uint8_t>(i & 0xff),
};
// compare result + size
const auto result = json::to_msgpack(j);
@ -315,10 +322,10 @@ TEST_CASE("MessagePack")
// check individual bytes
CHECK(result[0] == 0xce);
uint32_t restored = (static_cast<uint32_t>(result[1]) << 030) +
(static_cast<uint32_t>(result[2]) << 020) +
(static_cast<uint32_t>(result[3]) << 010) +
static_cast<uint32_t>(result[4]);
uint32_t const restored = (static_cast<uint32_t>(result[1]) << 030) +
(static_cast<uint32_t>(result[2]) << 020) +
(static_cast<uint32_t>(result[3]) << 010) +
static_cast<uint32_t>(result[4]);
CHECK(restored == i);
// roundtrip
@ -344,16 +351,18 @@ TEST_CASE("MessagePack")
CHECK(j.is_number_integer());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0xcf);
expected.push_back(static_cast<uint8_t>((i >> 070) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 060) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 050) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 040) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 030) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 020) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 010) & 0xff));
expected.push_back(static_cast<uint8_t>(i & 0xff));
std::vector<uint8_t> const expected
{
0xcf,
static_cast<uint8_t>((i >> 070) & 0xff),
static_cast<uint8_t>((i >> 060) & 0xff),
static_cast<uint8_t>((i >> 050) & 0xff),
static_cast<uint8_t>((i >> 040) & 0xff),
static_cast<uint8_t>((i >> 030) & 0xff),
static_cast<uint8_t>((i >> 020) & 0xff),
static_cast<uint8_t>((i >> 010) & 0xff),
static_cast<uint8_t>(i & 0xff),
};
// compare result + size
const auto result = json::to_msgpack(j);
@ -362,14 +371,14 @@ TEST_CASE("MessagePack")
// check individual bytes
CHECK(result[0] == 0xcf);
uint64_t restored = (static_cast<uint64_t>(result[1]) << 070) +
(static_cast<uint64_t>(result[2]) << 060) +
(static_cast<uint64_t>(result[3]) << 050) +
(static_cast<uint64_t>(result[4]) << 040) +
(static_cast<uint64_t>(result[5]) << 030) +
(static_cast<uint64_t>(result[6]) << 020) +
(static_cast<uint64_t>(result[7]) << 010) +
static_cast<uint64_t>(result[8]);
uint64_t const restored = (static_cast<uint64_t>(result[1]) << 070) +
(static_cast<uint64_t>(result[2]) << 060) +
(static_cast<uint64_t>(result[3]) << 050) +
(static_cast<uint64_t>(result[4]) << 040) +
(static_cast<uint64_t>(result[5]) << 030) +
(static_cast<uint64_t>(result[6]) << 020) +
(static_cast<uint64_t>(result[7]) << 010) +
static_cast<uint64_t>(result[8]);
CHECK(restored == i);
// roundtrip
@ -385,15 +394,17 @@ TEST_CASE("MessagePack")
CAPTURE(i)
// create JSON value with integer number
json j = i;
json const j = i;
// check type
CHECK(j.is_number_integer());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0xd0);
expected.push_back(static_cast<uint8_t>(i));
std::vector<uint8_t> const expected
{
0xd0,
static_cast<uint8_t>(i),
};
// compare result + size
const auto result = json::to_msgpack(j);
@ -412,13 +423,13 @@ TEST_CASE("MessagePack")
SECTION("-9263 (int 16)")
{
json j = -9263;
std::vector<uint8_t> expected = {0xd1, 0xdb, 0xd1};
json const j = -9263;
std::vector<uint8_t> const expected = {0xd1, 0xdb, 0xd1};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
auto restored = static_cast<int16_t>((result[1] << 8) + result[2]);
auto const restored = static_cast<int16_t>((result[1] << 8) + result[2]);
CHECK(restored == -9263);
// roundtrip
@ -433,16 +444,18 @@ TEST_CASE("MessagePack")
CAPTURE(i)
// create JSON value with integer number
json j = i;
json const j = i;
// check type
CHECK(j.is_number_integer());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0xd1);
expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(i & 0xff));
std::vector<uint8_t> const expected
{
0xd1,
static_cast<uint8_t>((i >> 8) & 0xff),
static_cast<uint8_t>(i & 0xff),
};
// compare result + size
const auto result = json::to_msgpack(j);
@ -451,7 +464,7 @@ TEST_CASE("MessagePack")
// check individual bytes
CHECK(result[0] == 0xd1);
auto restored = static_cast<int16_t>((result[1] << 8) + result[2]);
auto const restored = static_cast<int16_t>((result[1] << 8) + result[2]);
CHECK(restored == i);
// roundtrip
@ -462,29 +475,33 @@ TEST_CASE("MessagePack")
SECTION("-32769..-2147483648")
{
std::vector<int32_t> numbers;
numbers.push_back(-32769);
numbers.push_back(-65536);
numbers.push_back(-77777);
numbers.push_back(-1048576);
numbers.push_back(-2147483648LL);
std::vector<int32_t> const numbers
{
-32769,
-65536,
-77777,
-1048576,
-2147483648LL,
};
for (auto i : numbers)
{
CAPTURE(i)
// create JSON value with integer number
json j = i;
json const j = i;
// check type
CHECK(j.is_number_integer());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0xd2);
expected.push_back(static_cast<uint8_t>((i >> 24) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 16) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(i & 0xff));
std::vector<uint8_t> const expected
{
0xd2,
static_cast<uint8_t>((i >> 24) & 0xff),
static_cast<uint8_t>((i >> 16) & 0xff),
static_cast<uint8_t>((i >> 8) & 0xff),
static_cast<uint8_t>(i & 0xff),
};
// compare result + size
const auto result = json::to_msgpack(j);
@ -493,10 +510,10 @@ TEST_CASE("MessagePack")
// check individual bytes
CHECK(result[0] == 0xd2);
uint32_t restored = (static_cast<uint32_t>(result[1]) << 030) +
(static_cast<uint32_t>(result[2]) << 020) +
(static_cast<uint32_t>(result[3]) << 010) +
static_cast<uint32_t>(result[4]);
uint32_t const restored = (static_cast<uint32_t>(result[1]) << 030) +
(static_cast<uint32_t>(result[2]) << 020) +
(static_cast<uint32_t>(result[3]) << 010) +
static_cast<uint32_t>(result[4]);
CHECK(static_cast<std::int32_t>(restored) == i);
// roundtrip
@ -507,30 +524,34 @@ TEST_CASE("MessagePack")
SECTION("-9223372036854775808..-2147483649 (int 64)")
{
std::vector<int64_t> numbers;
numbers.push_back(INT64_MIN);
numbers.push_back(-2147483649LL);
std::vector<int64_t> const numbers
{
INT64_MIN,
-2147483649LL,
};
for (auto i : numbers)
{
CAPTURE(i)
// create JSON value with unsigned integer number
json j = i;
json const j = i;
// check type
CHECK(j.is_number_integer());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0xd3);
expected.push_back(static_cast<uint8_t>((i >> 070) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 060) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 050) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 040) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 030) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 020) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 010) & 0xff));
expected.push_back(static_cast<uint8_t>(i & 0xff));
std::vector<uint8_t> const expected
{
0xd3,
static_cast<uint8_t>((i >> 070) & 0xff),
static_cast<uint8_t>((i >> 060) & 0xff),
static_cast<uint8_t>((i >> 050) & 0xff),
static_cast<uint8_t>((i >> 040) & 0xff),
static_cast<uint8_t>((i >> 030) & 0xff),
static_cast<uint8_t>((i >> 020) & 0xff),
static_cast<uint8_t>((i >> 010) & 0xff),
static_cast<uint8_t>(i & 0xff),
};
// compare result + size
const auto result = json::to_msgpack(j);
@ -539,14 +560,14 @@ TEST_CASE("MessagePack")
// check individual bytes
CHECK(result[0] == 0xd3);
int64_t restored = (static_cast<int64_t>(result[1]) << 070) +
(static_cast<int64_t>(result[2]) << 060) +
(static_cast<int64_t>(result[3]) << 050) +
(static_cast<int64_t>(result[4]) << 040) +
(static_cast<int64_t>(result[5]) << 030) +
(static_cast<int64_t>(result[6]) << 020) +
(static_cast<int64_t>(result[7]) << 010) +
static_cast<int64_t>(result[8]);
int64_t const restored = (static_cast<int64_t>(result[1]) << 070) +
(static_cast<int64_t>(result[2]) << 060) +
(static_cast<int64_t>(result[3]) << 050) +
(static_cast<int64_t>(result[4]) << 040) +
(static_cast<int64_t>(result[5]) << 030) +
(static_cast<int64_t>(result[6]) << 020) +
(static_cast<int64_t>(result[7]) << 010) +
static_cast<int64_t>(result[8]);
CHECK(restored == i);
// roundtrip
@ -565,14 +586,13 @@ TEST_CASE("MessagePack")
CAPTURE(i)
// create JSON value with unsigned integer number
json j = i;
json const j = i;
// check type
CHECK(j.is_number_unsigned());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(i));
std::vector<uint8_t> const expected{static_cast<uint8_t>(i)};
// compare result + size
const auto result = json::to_msgpack(j);
@ -595,15 +615,17 @@ TEST_CASE("MessagePack")
CAPTURE(i)
// create JSON value with unsigned integer number
json j = i;
json const j = i;
// check type
CHECK(j.is_number_unsigned());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0xcc);
expected.push_back(static_cast<uint8_t>(i));
std::vector<uint8_t> const expected
{
0xcc,
static_cast<uint8_t>(i),
};
// compare result + size
const auto result = json::to_msgpack(j);
@ -612,7 +634,7 @@ TEST_CASE("MessagePack")
// check individual bytes
CHECK(result[0] == 0xcc);
auto restored = static_cast<uint8_t>(result[1]);
auto const restored = static_cast<uint8_t>(result[1]);
CHECK(restored == i);
// roundtrip
@ -628,16 +650,18 @@ TEST_CASE("MessagePack")
CAPTURE(i)
// create JSON value with unsigned integer number
json j = i;
json const j = i;
// check type
CHECK(j.is_number_unsigned());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0xcd);
expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(i & 0xff));
std::vector<uint8_t> const expected
{
0xcd,
static_cast<uint8_t>((i >> 8) & 0xff),
static_cast<uint8_t>(i & 0xff),
};
// compare result + size
const auto result = json::to_msgpack(j);
@ -646,7 +670,7 @@ TEST_CASE("MessagePack")
// check individual bytes
CHECK(result[0] == 0xcd);
auto restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
auto const restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
CHECK(restored == i);
// roundtrip
@ -657,7 +681,7 @@ TEST_CASE("MessagePack")
SECTION("65536..4294967295 (uint 32)")
{
for (uint32_t i :
for (const uint32_t i :
{
65536u, 77777u, 1048576u, 4294967295u
})
@ -665,18 +689,20 @@ TEST_CASE("MessagePack")
CAPTURE(i)
// create JSON value with unsigned integer number
json j = i;
json const j = i;
// check type
CHECK(j.is_number_unsigned());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0xce);
expected.push_back(static_cast<uint8_t>((i >> 24) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 16) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(i & 0xff));
std::vector<uint8_t> const expected
{
0xce,
static_cast<uint8_t>((i >> 24) & 0xff),
static_cast<uint8_t>((i >> 16) & 0xff),
static_cast<uint8_t>((i >> 8) & 0xff),
static_cast<uint8_t>(i & 0xff),
};
// compare result + size
const auto result = json::to_msgpack(j);
@ -685,10 +711,10 @@ TEST_CASE("MessagePack")
// check individual bytes
CHECK(result[0] == 0xce);
uint32_t restored = (static_cast<uint32_t>(result[1]) << 030) +
(static_cast<uint32_t>(result[2]) << 020) +
(static_cast<uint32_t>(result[3]) << 010) +
static_cast<uint32_t>(result[4]);
uint32_t const restored = (static_cast<uint32_t>(result[1]) << 030) +
(static_cast<uint32_t>(result[2]) << 020) +
(static_cast<uint32_t>(result[3]) << 010) +
static_cast<uint32_t>(result[4]);
CHECK(restored == i);
// roundtrip
@ -699,7 +725,7 @@ TEST_CASE("MessagePack")
SECTION("4294967296..18446744073709551615 (uint 64)")
{
for (uint64_t i :
for (const uint64_t i :
{
4294967296LU, 18446744073709551615LU
})
@ -707,22 +733,24 @@ TEST_CASE("MessagePack")
CAPTURE(i)
// create JSON value with unsigned integer number
json j = i;
json const j = i;
// check type
CHECK(j.is_number_unsigned());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0xcf);
expected.push_back(static_cast<uint8_t>((i >> 070) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 060) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 050) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 040) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 030) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 020) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 010) & 0xff));
expected.push_back(static_cast<uint8_t>(i & 0xff));
std::vector<uint8_t> const expected
{
0xcf,
static_cast<uint8_t>((i >> 070) & 0xff),
static_cast<uint8_t>((i >> 060) & 0xff),
static_cast<uint8_t>((i >> 050) & 0xff),
static_cast<uint8_t>((i >> 040) & 0xff),
static_cast<uint8_t>((i >> 030) & 0xff),
static_cast<uint8_t>((i >> 020) & 0xff),
static_cast<uint8_t>((i >> 010) & 0xff),
static_cast<uint8_t>(i & 0xff),
};
// compare result + size
const auto result = json::to_msgpack(j);
@ -731,14 +759,14 @@ TEST_CASE("MessagePack")
// check individual bytes
CHECK(result[0] == 0xcf);
uint64_t restored = (static_cast<uint64_t>(result[1]) << 070) +
(static_cast<uint64_t>(result[2]) << 060) +
(static_cast<uint64_t>(result[3]) << 050) +
(static_cast<uint64_t>(result[4]) << 040) +
(static_cast<uint64_t>(result[5]) << 030) +
(static_cast<uint64_t>(result[6]) << 020) +
(static_cast<uint64_t>(result[7]) << 010) +
static_cast<uint64_t>(result[8]);
uint64_t const restored = (static_cast<uint64_t>(result[1]) << 070) +
(static_cast<uint64_t>(result[2]) << 060) +
(static_cast<uint64_t>(result[3]) << 050) +
(static_cast<uint64_t>(result[4]) << 040) +
(static_cast<uint64_t>(result[5]) << 030) +
(static_cast<uint64_t>(result[6]) << 020) +
(static_cast<uint64_t>(result[7]) << 010) +
static_cast<uint64_t>(result[8]);
CHECK(restored == i);
// roundtrip
@ -752,9 +780,9 @@ TEST_CASE("MessagePack")
{
SECTION("3.1415925")
{
double v = 3.1415925;
json j = v;
std::vector<uint8_t> expected =
double const v = 3.1415925;
json const j = v;
std::vector<uint8_t> const expected =
{
0xcb, 0x40, 0x09, 0x21, 0xfb, 0x3f, 0xa6, 0xde, 0xfc
};
@ -769,9 +797,9 @@ TEST_CASE("MessagePack")
SECTION("1.0")
{
double v = 1.0;
json j = v;
std::vector<uint8_t> expected =
double const v = 1.0;
json const j = v;
std::vector<uint8_t> const expected =
{
0xca, 0x3f, 0x80, 0x00, 0x00
};
@ -786,9 +814,9 @@ TEST_CASE("MessagePack")
SECTION("128.128")
{
double v = 128.1280059814453125;
json j = v;
std::vector<uint8_t> expected =
double const v = 128.1280059814453125;
json const j = v;
std::vector<uint8_t> const expected =
{
0xca, 0x43, 0x00, 0x20, 0xc5
};
@ -822,7 +850,7 @@ TEST_CASE("MessagePack")
// create JSON value with string containing of N * 'x'
const auto s = std::string(N, 'x');
json j = s;
json const j = s;
// create expected byte vector
std::vector<uint8_t> expected;
@ -859,7 +887,7 @@ TEST_CASE("MessagePack")
// create JSON value with string containing of N * 'x'
const auto s = std::string(N, 'x');
json j = s;
json const j = s;
// create expected byte vector
std::vector<uint8_t> expected;
@ -894,7 +922,7 @@ TEST_CASE("MessagePack")
// create JSON value with string containing of N * 'x'
const auto s = std::string(N, 'x');
json j = s;
json const j = s;
// create expected byte vector (hack: create string first)
std::vector<uint8_t> expected(N, 'x');
@ -927,7 +955,7 @@ TEST_CASE("MessagePack")
// create JSON value with string containing of N * 'x'
const auto s = std::string(N, 'x');
json j = s;
json const j = s;
// create expected byte vector (hack: create string first)
std::vector<uint8_t> expected(N, 'x');
@ -956,8 +984,8 @@ TEST_CASE("MessagePack")
{
SECTION("empty")
{
json j = json::array();
std::vector<uint8_t> expected = {0x90};
json const j = json::array();
std::vector<uint8_t> const expected = {0x90};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
@ -968,8 +996,8 @@ TEST_CASE("MessagePack")
SECTION("[null]")
{
json j = {nullptr};
std::vector<uint8_t> expected = {0x91, 0xc0};
json const j = {nullptr};
std::vector<uint8_t> const expected = {0x91, 0xc0};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
@ -980,8 +1008,8 @@ TEST_CASE("MessagePack")
SECTION("[1,2,3,4,5]")
{
json j = json::parse("[1,2,3,4,5]");
std::vector<uint8_t> expected = {0x95, 0x01, 0x02, 0x03, 0x04, 0x05};
json const j = json::parse("[1,2,3,4,5]");
std::vector<uint8_t> const expected = {0x95, 0x01, 0x02, 0x03, 0x04, 0x05};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
@ -992,8 +1020,8 @@ TEST_CASE("MessagePack")
SECTION("[[[[]]]]")
{
json j = json::parse("[[[[]]]]");
std::vector<uint8_t> expected = {0x91, 0x91, 0x91, 0x90};
json const j = json::parse("[[[[]]]]");
std::vector<uint8_t> const expected = {0x91, 0x91, 0x91, 0x90};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
@ -1046,8 +1074,8 @@ TEST_CASE("MessagePack")
{
SECTION("empty")
{
json j = json::object();
std::vector<uint8_t> expected = {0x80};
json const j = json::object();
std::vector<uint8_t> const expected = {0x80};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
@ -1058,8 +1086,8 @@ TEST_CASE("MessagePack")
SECTION("{\"\":null}")
{
json j = {{"", nullptr}};
std::vector<uint8_t> expected = {0x81, 0xa0, 0xc0};
json const j = {{"", nullptr}};
std::vector<uint8_t> const expected = {0x81, 0xa0, 0xc0};
const auto result = json::to_msgpack(j);
CHECK(result == expected);
@ -1070,8 +1098,8 @@ TEST_CASE("MessagePack")
SECTION("{\"a\": {\"b\": {\"c\": {}}}}")
{
json j = json::parse(R"({"a": {"b": {"c": {}}}})");
std::vector<uint8_t> expected =
json const j = json::parse(R"({"a": {"b": {"c": {}}}})");
std::vector<uint8_t> const expected =
{
0x81, 0xa1, 0x61, 0x81, 0xa1, 0x62, 0x81, 0xa1, 0x63, 0x80
};
@ -1085,7 +1113,7 @@ TEST_CASE("MessagePack")
SECTION("map 16")
{
json j = R"({"00": null, "01": null, "02": null, "03": null,
json const j = R"({"00": null, "01": null, "02": null, "03": null,
"04": null, "05": null, "06": null, "07": null,
"08": null, "09": null, "10": null, "11": null,
"12": null, "13": null, "14": null, "15": null})"_json;
@ -1150,7 +1178,7 @@ TEST_CASE("MessagePack")
// create JSON value with byte array containing of N * 'x'
const auto s = std::vector<uint8_t>(N, 'x');
json j = json::binary(s);
std::uint8_t subtype = 42;
std::uint8_t const subtype = 42;
j.get_binary().set_subtype(subtype);
// create expected byte vector
@ -1225,7 +1253,7 @@ TEST_CASE("MessagePack")
// create JSON value with string containing of N * 'x'
const auto s = std::vector<uint8_t>(N, 'x');
json j = json::binary(s);
std::uint8_t subtype = 42;
std::uint8_t const subtype = 42;
j.get_binary().set_subtype(subtype);
// create expected byte vector (hack: create string first)
@ -1261,7 +1289,7 @@ TEST_CASE("MessagePack")
// create JSON value with string containing of N * 'x'
const auto s = std::vector<uint8_t>(N, 'x');
json j = json::binary(s);
std::uint8_t subtype = 42;
std::uint8_t const subtype = 42;
j.get_binary().set_subtype(subtype);
// create expected byte vector (hack: create string first)
@ -1298,7 +1326,7 @@ TEST_CASE("MessagePack")
// create JSON value with byte array containing of N * 'x'
const auto s = std::vector<uint8_t>(N, 'x');
json j = json::binary(s);
json const j = json::binary(s);
// create expected byte vector
std::vector<std::uint8_t> expected;
@ -1336,7 +1364,7 @@ TEST_CASE("MessagePack")
// create JSON value with string containing of N * 'x'
const auto s = std::vector<std::uint8_t>(N, 'x');
json j = json::binary(s);
json const j = json::binary(s);
// create expected byte vector (hack: create string first)
std::vector<std::uint8_t> expected(N, 'x');
@ -1369,7 +1397,7 @@ TEST_CASE("MessagePack")
// create JSON value with string containing of N * 'x'
const auto s = std::vector<std::uint8_t>(N, 'x');
json j = json::binary(s);
json const j = json::binary(s);
// create expected byte vector (hack: create string first)
std::vector<uint8_t> expected(N, 'x');
@ -1398,7 +1426,7 @@ TEST_CASE("MessagePack")
SECTION("from float32")
{
auto given = std::vector<uint8_t>({0xca, 0x41, 0xc8, 0x00, 0x01});
json j = json::from_msgpack(given);
json const j = json::from_msgpack(given);
CHECK(j.get<double>() == Approx(25.0000019073486));
}
@ -1511,7 +1539,7 @@ TEST_CASE("MessagePack")
SECTION("strict mode")
{
std::vector<uint8_t> vec = {0xc0, 0xc0};
std::vector<uint8_t> const vec = {0xc0, 0xc0};
SECTION("non-strict mode")
{
const auto result = json::from_msgpack(vec, false);
@ -1531,21 +1559,21 @@ TEST_CASE("MessagePack")
{
SECTION("start_array(len)")
{
std::vector<uint8_t> v = {0x93, 0x01, 0x02, 0x03};
std::vector<uint8_t> const v = {0x93, 0x01, 0x02, 0x03};
SaxCountdown scp(0);
CHECK(!json::sax_parse(v, &scp, json::input_format_t::msgpack));
}
SECTION("start_object(len)")
{
std::vector<uint8_t> v = {0x81, 0xa3, 0x66, 0x6F, 0x6F, 0xc2};
std::vector<uint8_t> const v = {0x81, 0xa3, 0x66, 0x6F, 0x6F, 0xc2};
SaxCountdown scp(0);
CHECK(!json::sax_parse(v, &scp, json::input_format_t::msgpack));
}
SECTION("key()")
{
std::vector<uint8_t> v = {0x81, 0xa3, 0x66, 0x6F, 0x6F, 0xc2};
std::vector<uint8_t> const v = {0x81, 0xa3, 0x66, 0x6F, 0x6F, 0xc2};
SaxCountdown scp(1);
CHECK(!json::sax_parse(v, &scp, json::input_format_t::msgpack));
}
@ -1557,7 +1585,7 @@ TEST_CASE("single MessagePack roundtrip")
{
SECTION("sample.json")
{
std::string filename = TEST_DATA_DIRECTORY "/json_testsuite/sample.json";
std::string const filename = TEST_DATA_DIRECTORY "/json_testsuite/sample.json";
// parse JSON file
std::ifstream f_json(filename);
@ -1817,7 +1845,7 @@ TEST_CASE("MessagePack roundtrips" * doctest::skip())
INFO_WITH_TEMP(filename + ": output to output adapters");
// parse JSON file
std::ifstream f_json(filename);
json j1 = json::parse(f_json);
json const j1 = json::parse(f_json);
// parse MessagePack file
auto packed = utils::read_binary_file(filename + ".msgpack");

View File

@ -46,7 +46,7 @@ TEST_CASE("ordered_json")
CHECK(oj.dump() == "{\"element3\":3,\"element2\":2}");
// There are no dup keys cause constructor calls emplace...
json multi {{"z", 1}, {"m", 2}, {"m", 3}, {"y", 4}, {"m", 5}};
json const multi {{"z", 1}, {"m", 2}, {"m", 3}, {"y", 4}, {"m", 5}};
CHECK(multi.size() == 3);
CHECK(multi.dump() == "{\"m\":2,\"y\":4,\"z\":1}");

View File

@ -19,7 +19,7 @@ TEST_CASE("ordered_map")
SECTION("constructor from iterator range")
{
std::map<std::string, std::string> m {{"eins", "one"}, {"zwei", "two"}, {"drei", "three"}};
ordered_map<std::string, std::string> om(m.begin(), m.end());
ordered_map<std::string, std::string> const om(m.begin(), m.end());
CHECK(om.size() == 3);
}
@ -281,8 +281,8 @@ TEST_CASE("ordered_map")
SECTION("const value_type&")
{
ordered_map<std::string, std::string>::value_type vt1 {"eins", "1"};
ordered_map<std::string, std::string>::value_type vt4 {"vier", "four"};
ordered_map<std::string, std::string>::value_type const vt1 {"eins", "1"};
ordered_map<std::string, std::string>::value_type const vt4 {"vier", "four"};
auto res1 = om.insert(vt1);
CHECK(res1.first == om.begin());

View File

@ -33,7 +33,7 @@ TEST_CASE("README" * doctest::skip())
{
// redirect std::cout for the README file
auto* old_cout_buffer = std::cout.rdbuf();
std::ostringstream new_stream;
std::ostringstream const new_stream;
std::cout.rdbuf(new_stream.rdbuf());
{
// create an empty structure (null)
@ -61,7 +61,7 @@ TEST_CASE("README" * doctest::skip())
j["object"] = { {"currency", "USD"}, {"value", 42.99} };
// instead, you could also write (which looks very similar to the JSON above)
json j2 =
json const j2 =
{
{"pi", 3.141},
{"happy", true},
@ -84,13 +84,13 @@ TEST_CASE("README" * doctest::skip())
{
// ways to express the empty array []
json empty_array_implicit = {{}};
json const empty_array_implicit = {{}};
CHECK(empty_array_implicit.is_array());
json empty_array_explicit = json::array();
json const empty_array_explicit = json::array();
CHECK(empty_array_explicit.is_array());
// a way to express the empty object {}
json empty_object_explicit = json::object();
json const empty_object_explicit = json::object();
CHECK(empty_object_explicit.is_object());
// a way to express an _array_ of key/value pairs [["currency", "USD"], ["value", 42.99]]
@ -103,7 +103,7 @@ TEST_CASE("README" * doctest::skip())
{
// create object from string literal
json j = "{ \"happy\": true, \"pi\": 3.141 }"_json; // NOLINT(modernize-raw-string-literal)
json const j = "{ \"happy\": true, \"pi\": 3.141 }"_json; // NOLINT(modernize-raw-string-literal)
// or even nicer with a raw string literal
auto j2 = R"({
@ -115,7 +115,7 @@ TEST_CASE("README" * doctest::skip())
auto j3 = json::parse(R"({"happy": true, "pi": 3.141})");
// explicit conversion to string
std::string s = j.dump(); // {\"happy\":true,\"pi\":3.141}
std::string const s = j.dump(); // {\"happy\":true,\"pi\":3.141}
// serialization with pretty printing
// pass in the amount of spaces to indent
@ -178,82 +178,82 @@ TEST_CASE("README" * doctest::skip())
}
{
std::vector<int> c_vector {1, 2, 3, 4};
json j_vec(c_vector);
std::vector<int> const c_vector {1, 2, 3, 4};
json const j_vec(c_vector);
// [1, 2, 3, 4]
std::deque<float> c_deque {1.2f, 2.3f, 3.4f, 5.6f};
json j_deque(c_deque);
std::deque<float> const c_deque {1.2f, 2.3f, 3.4f, 5.6f};
json const j_deque(c_deque);
// [1.2, 2.3, 3.4, 5.6]
std::list<bool> c_list {true, true, false, true};
json j_list(c_list);
std::list<bool> const c_list {true, true, false, true};
json const j_list(c_list);
// [true, true, false, true]
std::forward_list<int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543};
json j_flist(c_flist);
std::forward_list<int64_t> const c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543};
json const j_flist(c_flist);
// [12345678909876, 23456789098765, 34567890987654, 45678909876543]
std::array<unsigned long, 4> c_array {{1, 2, 3, 4}};
json j_array(c_array);
std::array<unsigned long, 4> const c_array {{1, 2, 3, 4}};
json const j_array(c_array);
// [1, 2, 3, 4]
std::set<std::string> c_set {"one", "two", "three", "four", "one"};
json j_set(c_set); // only one entry for "one" is used
std::set<std::string> const c_set {"one", "two", "three", "four", "one"};
json const j_set(c_set); // only one entry for "one" is used
// ["four", "one", "three", "two"]
std::unordered_set<std::string> c_uset {"one", "two", "three", "four", "one"};
json j_uset(c_uset); // only one entry for "one" is used
std::unordered_set<std::string> const c_uset {"one", "two", "three", "four", "one"};
json const j_uset(c_uset); // only one entry for "one" is used
// maybe ["two", "three", "four", "one"]
std::multiset<std::string> c_mset {"one", "two", "one", "four"};
json j_mset(c_mset); // both entries for "one" are used
std::multiset<std::string> const c_mset {"one", "two", "one", "four"};
json const j_mset(c_mset); // both entries for "one" are used
// maybe ["one", "two", "one", "four"]
std::unordered_multiset<std::string> c_umset {"one", "two", "one", "four"};
json j_umset(c_umset); // both entries for "one" are used
std::unordered_multiset<std::string> const c_umset {"one", "two", "one", "four"};
json const j_umset(c_umset); // both entries for "one" are used
// maybe ["one", "two", "one", "four"]
}
{
std::map<std::string, int> c_map { {"one", 1}, {"two", 2}, {"three", 3} };
json j_map(c_map);
std::map<std::string, int> const c_map { {"one", 1}, {"two", 2}, {"three", 3} };
json const j_map(c_map);
// {"one": 1, "two": 2, "three": 3}
std::unordered_map<const char*, float> c_umap { {"one", 1.2f}, {"two", 2.3f}, {"three", 3.4f} };
json j_umap(c_umap);
std::unordered_map<const char*, float> const c_umap { {"one", 1.2f}, {"two", 2.3f}, {"three", 3.4f} };
json const j_umap(c_umap);
// {"one": 1.2, "two": 2.3, "three": 3.4}
std::multimap<std::string, bool> c_mmap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };
json j_mmap(c_mmap); // only one entry for key "three" is used
std::multimap<std::string, bool> const c_mmap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };
json const j_mmap(c_mmap); // only one entry for key "three" is used
// maybe {"one": true, "two": true, "three": true}
std::unordered_multimap<std::string, bool> c_ummap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };
json j_ummap(c_ummap); // only one entry for key "three" is used
std::unordered_multimap<std::string, bool> const c_ummap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };
json const j_ummap(c_ummap); // only one entry for key "three" is used
// maybe {"one": true, "two": true, "three": true}
}
{
// strings
std::string s1 = "Hello, world!";
json js = s1;
std::string const s1 = "Hello, world!";
json const js = s1;
auto s2 = js.get<std::string>();
// Booleans
bool b1 = true;
json jb = b1;
bool const b1 = true;
json const jb = b1;
bool b2{jb};
CHECK(b2 == true);
// numbers
int i = 42;
json jn = i;
int const i = 42;
json const jn = i;
double f{jn};
CHECK(f == 42);
// etc.
std::string vs = js.get<std::string>();
std::string const vs = js.get<std::string>();
bool vb = jb.get<bool>();
CHECK(vb == true);
int vi = jn.get<int>();
@ -274,14 +274,14 @@ TEST_CASE("README" * doctest::skip())
// "two"
// a JSON patch (RFC 6902)
json j_patch = R"([
json const j_patch = R"([
{ "op": "replace", "path": "/baz", "value": "boo" },
{ "op": "add", "path": "/hello", "value": ["world"] },
{ "op": "remove", "path": "/foo"}
])"_json;
// apply the patch
json j_result = j_original.patch(j_patch);
json const j_result = j_original.patch(j_patch);
// {
// "baz": "boo",
// "hello": ["world"]

View File

@ -14,7 +14,7 @@ using nlohmann::json;
TEST_CASE("reference access")
{
// create a JSON value with different types
json json_types =
const json json_types =
{
{"boolean", true},
{

View File

@ -143,27 +143,27 @@ TEST_CASE("regression tests 1")
// to null), but are serialized as null.
SECTION("NAN value")
{
json j1 = NAN;
json const j1 = NAN;
CHECK(j1.is_number_float());
json::number_float_t f1{j1};
json::number_float_t const f1{j1};
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());
json::number_float_t f2{j2};
json::number_float_t const f2{j2};
CHECK(std::isnan(f2));
}
SECTION("infinity")
{
json j1 = INFINITY;
json const j1 = INFINITY;
CHECK(j1.is_number_float());
json::number_float_t f1{j1};
json::number_float_t const f1{j1};
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());
json::number_float_t f2{j2};
json::number_float_t const f2{j2};
CHECK(!std::isfinite(f2));
}
}
@ -200,7 +200,7 @@ TEST_CASE("regression tests 1")
fields["three"] = std::string("three \"four\"");
// 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);
// check individual fields to match both objects
@ -288,7 +288,7 @@ TEST_CASE("regression tests 1")
}
{
json a = {1, 2, 3};
json::reverse_iterator rit = ++a.rbegin();
json::reverse_iterator const rit = ++a.rbegin();
CHECK(*rit == 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")
{
int64_t number = 10;
std::string bytes{"\x00" "asdf\n", 6};
int64_t const number = 10;
std::string const bytes{"\x00" "asdf\n", 6};
json j;
j["int64"] = number;
j["binary string"] = bytes;
@ -352,7 +352,7 @@ TEST_CASE("regression tests 1")
SECTION("issue #111 - subsequent unicode chars")
{
std::string bytes{0x7, 0x7};
std::string const bytes{0x7, 0x7};
json j;
j["string"] = bytes;
CHECK(j["string"] == "\u0007\u0007");
@ -465,33 +465,33 @@ TEST_CASE("regression tests 1")
// create JSON class with nonstandard float number type
// 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;
CHECK(j_float.get<float>() == 1.23e25f);
// 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;
CHECK(j_double.get<double>() == 1.23e35);
// 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);
}
SECTION("issue #228 - double values are serialized with commas as decimal points")
{
json j1a = 2312.42;
json j1b = json::parse("2312.42");
json const j1a = 2312.42;
json const j1b = json::parse("2312.42");
json j2a = 2342e-2;
json const j2a = 2342e-2;
//issue #230
//json j2b = json::parse("2342e-2");
json j3a = 10E3;
json j3b = json::parse("10E3");
json j3c = json::parse("10e3");
json const j3a = 10E3;
json const j3b = json::parse("10E3");
json const j3c = json::parse("10e3");
// class to create a locale that would use a comma for decimals
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")
{
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;
// check roundtrip
@ -610,7 +610,7 @@ TEST_CASE("regression tests 1")
// code triggered a "warning: unused variable" warning and is left
// here to avoid the warning in the future
json object;
json patch = json::array();
json const patch = json::array();
object = object.patch(patch);
}
@ -837,7 +837,7 @@ TEST_CASE("regression tests 1")
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");
@ -875,23 +875,23 @@ TEST_CASE("regression tests 1")
SECTION("issue #389 - Integer-overflow (OSS-Fuzz issue 267)")
{
// original test case
json j1 = json::parse("-9223372036854775808");
json const j1 = json::parse("-9223372036854775808");
CHECK(j1.is_number_integer());
CHECK(j1.get<json::number_integer_t>() == INT64_MIN);
// edge case (+1; still an integer)
json j2 = json::parse("-9223372036854775807");
json const j2 = json::parse("-9223372036854775807");
CHECK(j2.is_number_integer());
CHECK(j2.get<json::number_integer_t>() == INT64_MIN + 1);
// edge case (-1; overflow -> floats)
json j3 = json::parse("-9223372036854775809");
json const j3 = json::parse("-9223372036854775809");
CHECK(j3.is_number_float());
}
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.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)")
{
// 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 _;
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 _;
// 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&);
// 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&);
// 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&);
// 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&);
// 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&);
}
@ -934,7 +934,7 @@ TEST_CASE("regression tests 1")
json _;
// 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&);
// 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
})
{
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&);
}
@ -961,12 +961,12 @@ TEST_CASE("regression tests 1")
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&);
}
// 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_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 _;
// 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&);
// 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&);
// 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&);
}
SECTION("issue #412 - Heap-buffer-overflow (OSS-Fuzz issue 367)")
{
// original test case
std::vector<uint8_t> vec
std::vector<uint8_t> const vec
{
0xab, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98,
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&);
// 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&);
// 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&);
// 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&);
}
@ -1049,7 +1049,7 @@ TEST_CASE("regression tests 1")
SECTION("issue #416 - Use-of-uninitialized-value (OSS-Fuzz issue 377)")
{
// original test case
std::vector<uint8_t> vec1
std::vector<uint8_t> const vec1
{
0x94, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa,
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&);
// related test case: double-precision
std::vector<uint8_t> vec2
std::vector<uint8_t> const vec2
{
0x94, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa,
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)")
{
std::vector<uint8_t> vec = {'-', '0', '1', '2', '2', '7', '4'};
std::vector<uint8_t> const vec = {'-', '0', '1', '2', '2', '7', '4'};
json _;
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")
{
json j1 = json::parse("1000000000000000010E5");
json const j1 = json::parse("1000000000000000010E5");
std::string s1 = j1.dump();
json j2 = json::parse(s1);
json const j2 = json::parse(s1);
std::string s2 = j2.dump();
CHECK(s1 == s2);
}
@ -1110,15 +1110,15 @@ TEST_CASE("regression tests 1")
#if JSON_USE_IMPLICIT_CONVERSIONS
SECTION("issue #473 - inconsistent behavior in conversion to array type")
{
json j_array = {1, 2, 3, 4};
json j_number = 42;
json j_null = nullptr;
json const j_array = {1, 2, 3, 4};
json const j_number = 42;
json const j_null = nullptr;
SECTION("std::vector")
{
auto create = [](const json & j)
{
std::vector<int> v = j;
std::vector<int> const v = j;
};
CHECK_NOTHROW(create(j_array));
@ -1130,7 +1130,7 @@ TEST_CASE("regression tests 1")
{
auto create = [](const json & j)
{
std::list<int> v = j;
std::list<int> const v = j;
};
CHECK_NOTHROW(create(j_array));
@ -1142,7 +1142,7 @@ TEST_CASE("regression tests 1")
{
auto create = [](const json & j)
{
std::forward_list<int> v = j;
std::forward_list<int> const v = j;
};
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")
{
std::vector<bool> boolVector = {false, true, false, false};
std::vector<bool> const boolVector = {false, true, false, false};
json j;
j["bool_vector"] = boolVector;
@ -1170,14 +1170,14 @@ TEST_CASE("regression tests 1")
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};
json j1 = json::from_cbor(vec1, false);
std::vector<uint8_t> const vec1 = {0xf9, 0xff, 0xff, 0x4a, 0x3a, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x37, 0x02, 0x38};
json const j1 = json::from_cbor(vec1, false);
// step 2: round trip
std::vector<uint8_t> vec2 = json::to_cbor(j1);
// parse serialization
json j2 = json::from_cbor(vec2);
json const j2 = json::from_cbor(vec2);
// NaN is dumped to "null"
CHECK(j2.is_number_float());
@ -1226,7 +1226,7 @@ TEST_CASE("regression tests 1")
SECTION("issue #575 - heap-buffer-overflow (OSS-Fuzz 1400)")
{
json _;
std::vector<uint8_t> vec = {'"', '\\', '"', 'X', '"', '"'};
std::vector<uint8_t> const vec = {'"', '\\', '"', 'X', '"', '"'};
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}};
// create and print a JSON from the map
json j = m1;
json const j = m1;
// get the map out of JSON
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"}};
// create and print a JSON from the map
json j = m1;
json const j = m1;
// get the map out of JSON
std::map<std::string, std::string> m2 = j;
@ -1277,7 +1277,7 @@ TEST_CASE("regression tests 1")
{
SECTION("original example")
{
std::valarray<double> v;
std::valarray<double> const v;
nlohmann::json j;
j["test"] = v;
}
@ -1364,8 +1364,8 @@ TEST_CASE("regression tests 1")
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::string key1_str(reinterpret_cast<char*>(key1.data()));
json j = key1_str;
std::string const key1_str(reinterpret_cast<char*>(key1.data()));
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&);
}
@ -1405,7 +1405,7 @@ TEST_CASE("regression tests 1")
SECTION("issue #961 - incorrect parsing of indefinite length CBOR strings")
{
std::vector<uint8_t> v_cbor =
std::vector<uint8_t> const v_cbor =
{
0x7F,
0x64,
@ -1453,7 +1453,7 @@ TEST_CASE("regression tests 1")
)";
// 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"
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")
{
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")
@ -1478,7 +1478,7 @@ TEST_CASE("regression tests 1")
CHECK(lj.size() == 1);
CHECK(lj["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

@ -348,7 +348,7 @@ TEST_CASE("regression tests 2")
]
})";
json::parser_callback_t cb = [&](int /*level*/, json::parse_event_t event, json & parsed) noexcept
const json::parser_callback_t cb = [&](int /*level*/, json::parse_event_t event, json & parsed) noexcept
{
// skip uninteresting events
if (event == json::parse_event_t::value && !parsed.is_primitive())
@ -440,7 +440,7 @@ TEST_CASE("regression tests 2")
SECTION("issue #1299 - compile error in from_json converting to container "
"with std::pair")
{
json j =
const json j =
{
{"1", {{"a", "testa_1"}, {"b", "testb_1"}}},
{"2", {{"a", "testa_2"}, {"b", "testb_2"}}},
@ -501,7 +501,7 @@ TEST_CASE("regression tests 2")
{109, 108, 103, 125, -122, -53, 115, 18, 3, 0, 102, 19, 1, 15, -110, 13, -3, -1, -81, 32, 2, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -80, 2, 0, 0, 96, -118, 46, -116, 46, 109, -84, -87, 108, 14, 109, -24, -83, 13, -18, -51, -83, -52, -115, 14, 6, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 3, 0, 0, 0, 35, -74, -73, 55, 57, -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, -96, -54, -28, -26}
};
std::string s;
for (int i : data)
for (const int i : data)
{
s += static_cast<char>(i);
}
@ -512,20 +512,20 @@ TEST_CASE("regression tests 2")
SECTION("issue #1447 - Integer Overflow (OSS-Fuzz 12506)")
{
json j = json::parse("[-9223372036854775808]");
const json j = json::parse("[-9223372036854775808]");
CHECK(j.dump() == "[-9223372036854775808]");
}
SECTION("issue #1708 - minimum value of int64_t can be outputted")
{
constexpr auto smallest = (std::numeric_limits<int64_t>::min)();
json j = smallest;
const json j = smallest;
CHECK(j.dump() == std::to_string(smallest));
}
SECTION("issue #1727 - Contains with non-const lvalue json_pointer picks the wrong overload")
{
json j = {{"root", {{"settings", {{"logging", true}}}}}};
const json j = {{"root", {{"settings", {{"logging", true}}}}}};
auto jptr1 = "/root/settings/logging"_json_pointer;
auto jptr2 = json::json_pointer{"/root/settings/logging"};
@ -539,7 +539,7 @@ TEST_CASE("regression tests 2")
// does not compile on ICPC when targeting C++20
#if !(defined(__INTEL_COMPILER) && __cplusplus >= 202000)
{
json j;
const json j;
NonDefaultFromJsonStruct x(j);
NonDefaultFromJsonStruct y;
CHECK(x == y);
@ -548,27 +548,27 @@ TEST_CASE("regression tests 2")
auto val = nlohmann::json("one").get<for_1647>();
CHECK(val == for_1647::one);
json j = val;
const json j = val;
}
SECTION("issue #1715 - json::from_cbor does not respect allow_exceptions = false when input is string literal")
{
SECTION("string literal")
{
json cbor = json::from_cbor("B", true, false);
const json cbor = json::from_cbor("B", true, false);
CHECK(cbor.is_discarded());
}
SECTION("string array")
{
const std::array<char, 2> input = {{'B', 0x00}};
json cbor = json::from_cbor(input, true, false);
const json cbor = json::from_cbor(input, true, false);
CHECK(cbor.is_discarded());
}
SECTION("std::string")
{
json cbor = json::from_cbor(std::string("B"), true, false);
const json cbor = json::from_cbor(std::string("B"), true, false);
CHECK(cbor.is_discarded());
}
}
@ -597,7 +597,7 @@ TEST_CASE("regression tests 2")
SECTION("issue #2067 - cannot serialize binary data to text JSON")
{
const std::array<unsigned char, 23> data = {{0x81, 0xA4, 0x64, 0x61, 0x74, 0x61, 0xC4, 0x0F, 0x33, 0x30, 0x30, 0x32, 0x33, 0x34, 0x30, 0x31, 0x30, 0x37, 0x30, 0x35, 0x30, 0x31, 0x30}};
json j = json::from_msgpack(data.data(), data.size());
const json j = json::from_msgpack(data.data(), data.size());
CHECK_NOTHROW(
j.dump(4, // Indent
' ', // Indent char
@ -609,15 +609,15 @@ TEST_CASE("regression tests 2")
SECTION("PR #2181 - regression bug with lvalue")
{
// see https://github.com/nlohmann/json/pull/2181#issuecomment-653326060
json j{{"x", "test"}};
std::string defval = "default value";
const json j{{"x", "test"}};
const std::string defval = "default value";
auto val = j.value("x", defval);
auto val2 = j.value("y", defval);
}
SECTION("issue #2293 - eof doesn't cause parsing to stop")
{
std::vector<uint8_t> data =
const std::vector<uint8_t> data =
{
0x7B,
0x6F,
@ -630,23 +630,23 @@ TEST_CASE("regression tests 2")
0x4F,
0x42
};
json result = json::from_cbor(data, true, false);
const json result = json::from_cbor(data, true, false);
CHECK(result.is_discarded());
}
SECTION("issue #2315 - json.update and vector<pair>does not work with ordered_json")
{
nlohmann::ordered_json jsonAnimals = {{"animal", "dog"}};
nlohmann::ordered_json jsonCat = {{"animal", "cat"}};
const nlohmann::ordered_json jsonCat = {{"animal", "cat"}};
jsonAnimals.update(jsonCat);
CHECK(jsonAnimals["animal"] == "cat");
auto jsonAnimals_parsed = nlohmann::ordered_json::parse(jsonAnimals.dump());
CHECK(jsonAnimals == jsonAnimals_parsed);
std::vector<std::pair<std::string, int64_t>> intData = {std::make_pair("aaaa", 11),
std::make_pair("bbb", 222)
};
const std::vector<std::pair<std::string, int64_t>> intData = {std::make_pair("aaaa", 11),
std::make_pair("bbb", 222)
};
nlohmann::ordered_json jsonObj;
for (const auto& data : intData)
{
@ -658,8 +658,8 @@ TEST_CASE("regression tests 2")
SECTION("issue #2330 - ignore_comment=true fails on multiple consecutive lines starting with comments")
{
std::string ss = "//\n//\n{\n}\n";
json j = json::parse(ss, nullptr, true, true);
const std::string ss = "//\n//\n{\n}\n";
const json j = json::parse(ss, nullptr, true, true);
CHECK(j.dump() == "{}");
}
@ -668,7 +668,7 @@ TEST_CASE("regression tests 2")
{
const char DATA[] = R"("Hello, world!")"; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
const auto s = std::as_bytes(std::span(DATA));
json j = json::parse(s);
const json j = json::parse(s);
CHECK(j.dump() == "\"Hello, world!\"");
}
#endif
@ -678,14 +678,14 @@ TEST_CASE("regression tests 2")
SECTION("std::array")
{
{
json j = {7, 4};
const json j = {7, 4};
auto arr = j.get<std::array<NonDefaultConstructible, 2>>();
CHECK(arr[0].x == 7);
CHECK(arr[1].x == 4);
}
{
json j = 7;
const json j = 7;
CHECK_THROWS_AS((j.get<std::array<NonDefaultConstructible, 1>>()), json::type_error);
}
}
@ -693,28 +693,28 @@ TEST_CASE("regression tests 2")
SECTION("std::pair")
{
{
json j = {3, 8};
const json j = {3, 8};
auto p = j.get<std::pair<NonDefaultConstructible, NonDefaultConstructible>>();
CHECK(p.first.x == 3);
CHECK(p.second.x == 8);
}
{
json j = {4, 1};
const json j = {4, 1};
auto p = j.get<std::pair<int, NonDefaultConstructible>>();
CHECK(p.first == 4);
CHECK(p.second.x == 1);
}
{
json j = {6, 7};
const json j = {6, 7};
auto p = j.get<std::pair<NonDefaultConstructible, int>>();
CHECK(p.first.x == 6);
CHECK(p.second == 7);
}
{
json j = 7;
const json j = 7;
CHECK_THROWS_AS((j.get<std::pair<NonDefaultConstructible, int>>()), json::type_error);
}
}
@ -722,13 +722,13 @@ TEST_CASE("regression tests 2")
SECTION("std::tuple")
{
{
json j = {9};
const json j = {9};
auto t = j.get<std::tuple<NonDefaultConstructible>>();
CHECK(std::get<0>(t).x == 9);
}
{
json j = {9, 8, 7};
const json j = {9, 8, 7};
auto t = j.get<std::tuple<NonDefaultConstructible, int, NonDefaultConstructible>>();
CHECK(std::get<0>(t).x == 9);
CHECK(std::get<1>(t) == 8);
@ -736,7 +736,7 @@ TEST_CASE("regression tests 2")
}
{
json j = 7;
const json j = 7;
CHECK_THROWS_AS((j.get<std::tuple<NonDefaultConstructible>>()), json::type_error);
}
}
@ -747,7 +747,7 @@ TEST_CASE("regression tests 2")
// the code below is expected to not leak memory
{
nlohmann::json o;
std::string s = "bar";
const std::string s = "bar";
nlohmann::to_json(o["foo"], s);
@ -759,7 +759,7 @@ TEST_CASE("regression tests 2")
{
nlohmann::json o;
std::string s = "bar";
const std::string s = "bar";
nlohmann::to_json(o["foo"], s);
@ -785,7 +785,7 @@ TEST_CASE("regression tests 2")
SECTION("issue #2958 - Inserting in unordered json using a pointer retains the leading slash")
{
std::string p = "/root";
const std::string p = "/root";
json test1;
test1[json::json_pointer(p)] = json::object();
@ -816,7 +816,7 @@ TEST_CASE("regression tests 2")
SECTION("issue #3070 - Version 3.10.3 breaks backward-compatibility with 3.10.2 ")
{
nlohmann::detail::std_fs::path text_path("/tmp/text.txt");
json j(text_path);
const json j(text_path);
const auto j_path = j.get<nlohmann::detail::std_fs::path>();
CHECK(j_path == text_path);
@ -867,7 +867,7 @@ TEST_CASE("regression tests 2")
SECTION("issue #3171 - if class is_constructible from std::string wrong from_json overload is being selected, compilation failed")
{
json j{{ "str", "value"}};
const json j{{ "str", "value"}};
// failed with: error: no match for operator= (operand types are for_3171_derived and const nlohmann::basic_json<>::string_t
// {aka const std::__cxx11::basic_string<char>})
@ -881,7 +881,7 @@ TEST_CASE("regression tests 2")
SECTION("issue #3312 - Parse to custom class from unordered_json breaks on G++11.2.0 with C++20")
{
// see test for #3171
ordered_json j = {{"name", "class"}};
const ordered_json j = {{"name", "class"}};
for_3312 obj{};
j.get_to(obj);
@ -893,8 +893,8 @@ TEST_CASE("regression tests 2")
#if defined(JSON_HAS_CPP_17) && JSON_USE_IMPLICIT_CONVERSIONS
SECTION("issue #3428 - Error occurred when converting nlohmann::json to std::any")
{
json j;
std::any a1 = j;
const json j;
const std::any a1 = j;
std::any&& a2 = j;
CHECK(a1.type() == typeid(j));

View File

@ -21,7 +21,7 @@ TEST_CASE("serialization")
SECTION("no given width")
{
std::stringstream ss;
json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
const json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
ss << j;
CHECK(ss.str() == "[\"foo\",1,2,3,false,{\"one\":1}]");
}
@ -29,7 +29,7 @@ TEST_CASE("serialization")
SECTION("given width")
{
std::stringstream ss;
json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
const json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
ss << std::setw(4) << j;
CHECK(ss.str() ==
"[\n \"foo\",\n 1,\n 2,\n 3,\n false,\n {\n \"one\": 1\n }\n]");
@ -38,7 +38,7 @@ TEST_CASE("serialization")
SECTION("given fill")
{
std::stringstream ss;
json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
const json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
ss << std::setw(1) << std::setfill('\t') << j;
CHECK(ss.str() ==
"[\n\t\"foo\",\n\t1,\n\t2,\n\t3,\n\tfalse,\n\t{\n\t\t\"one\": 1\n\t}\n]");
@ -50,7 +50,7 @@ TEST_CASE("serialization")
SECTION("no given width")
{
std::stringstream ss;
json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
const json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
j >> ss;
CHECK(ss.str() == "[\"foo\",1,2,3,false,{\"one\":1}]");
}
@ -58,7 +58,7 @@ TEST_CASE("serialization")
SECTION("given width")
{
std::stringstream ss;
json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
const json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
ss.width(4);
j >> ss;
CHECK(ss.str() ==
@ -68,7 +68,7 @@ TEST_CASE("serialization")
SECTION("given fill")
{
std::stringstream ss;
json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
const json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
ss.width(1);
ss.fill('\t');
j >> ss;
@ -81,7 +81,7 @@ TEST_CASE("serialization")
{
SECTION("invalid character")
{
json j = "ä\xA9ü";
const json j = "ä\xA9ü";
CHECK_THROWS_WITH_AS(j.dump(), "[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9", json::type_error&);
CHECK_THROWS_WITH_AS(j.dump(1, ' ', false, json::error_handler_t::strict), "[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9", json::type_error&);
@ -92,7 +92,7 @@ TEST_CASE("serialization")
SECTION("ending with incomplete character")
{
json j = "123\xC2";
const json j = "123\xC2";
CHECK_THROWS_WITH_AS(j.dump(), "[json.exception.type_error.316] incomplete UTF-8 string; last byte: 0xC2", json::type_error&);
CHECK_THROWS_AS(j.dump(1, ' ', false, json::error_handler_t::strict), json::type_error&);
@ -103,7 +103,7 @@ TEST_CASE("serialization")
SECTION("unexpected character")
{
json j = "123\xF1\xB0\x34\x35\x36";
const json j = "123\xF1\xB0\x34\x35\x36";
CHECK_THROWS_WITH_AS(j.dump(), "[json.exception.type_error.316] invalid UTF-8 byte at index 5: 0x34", json::type_error&);
CHECK_THROWS_AS(j.dump(1, ' ', false, json::error_handler_t::strict), json::type_error&);
@ -120,7 +120,7 @@ TEST_CASE("serialization")
auto test = [&](std::string const & input, std::string const & expected)
{
json j = input;
const json j = input;
CHECK(j.dump(-1, ' ', true, json::error_handler_t::replace) == "\"" + expected + "\"");
};
@ -154,7 +154,7 @@ TEST_CASE("serialization")
auto test = [&](std::string const & input, std::string const & expected)
{
using std::to_string;
json j = input;
const json j = input;
CHECK(to_string(j) == "\"" + expected + "\"");
};
@ -170,14 +170,14 @@ TEST_CASE_TEMPLATE("serialization for extreme integer values", T, int32_t, uint3
SECTION("minimum")
{
constexpr auto minimum = (std::numeric_limits<T>::min)();
json j = minimum;
const json j = minimum;
CHECK(j.dump() == std::to_string(minimum));
}
SECTION("maximum")
{
constexpr auto maximum = (std::numeric_limits<T>::max)();
json j = maximum;
const json j = maximum;
CHECK(j.dump() == std::to_string(maximum));
}
}
@ -189,15 +189,15 @@ TEST_CASE("dump with binary values")
auto binary_with_subtype = json::binary({1, 2, 3, 4}, 128);
auto binary_empty_with_subtype = json::binary({}, 128);
json object = {{"key", binary}};
json object_empty = {{"key", binary_empty}};
json object_with_subtype = {{"key", binary_with_subtype}};
json object_empty_with_subtype = {{"key", binary_empty_with_subtype}};
const json object = {{"key", binary}};
const json object_empty = {{"key", binary_empty}};
const json object_with_subtype = {{"key", binary_with_subtype}};
const json object_empty_with_subtype = {{"key", binary_empty_with_subtype}};
json array = {"value", 1, binary};
json array_empty = {"value", 1, binary_empty};
json array_with_subtype = {"value", 1, binary_with_subtype};
json array_empty_with_subtype = {"value", 1, binary_empty_with_subtype};
const json array = {"value", 1, binary};
const json array_empty = {"value", 1, binary_empty};
const json array_with_subtype = {"value", 1, binary_with_subtype};
const json array_empty_with_subtype = {"value", 1, binary_empty_with_subtype};
SECTION("normal")
{

View File

@ -302,7 +302,7 @@ TEST_CASE("compliance tests from nativejson-benchmark")
(std::istreambuf_iterator<char>()) );
CAPTURE(json_string)
json j = json::parse(json_string);
const json j = json::parse(json_string);
CHECK(j.dump() == json_string);
}
}
@ -363,35 +363,35 @@ TEST_CASE("json.org examples")
}
SECTION("FILE 1.json")
{
std::unique_ptr<std::FILE, decltype(&std::fclose)> f(std::fopen(TEST_DATA_DIRECTORY "/json.org/1.json", "r"), &std::fclose);
const std::unique_ptr<std::FILE, decltype(&std::fclose)> f(std::fopen(TEST_DATA_DIRECTORY "/json.org/1.json", "r"), &std::fclose);
json _;
CHECK_NOTHROW(_ = json::parse(f.get()));
}
SECTION("FILE 2.json")
{
std::unique_ptr<std::FILE, decltype(&std::fclose)> f(std::fopen(TEST_DATA_DIRECTORY "/json.org/2.json", "r"), &std::fclose);
const std::unique_ptr<std::FILE, decltype(&std::fclose)> f(std::fopen(TEST_DATA_DIRECTORY "/json.org/2.json", "r"), &std::fclose);
json _;
CHECK_NOTHROW(_ = json::parse(f.get()));
}
SECTION("FILE 3.json")
{
std::unique_ptr<std::FILE, decltype(&std::fclose)> f(std::fopen(TEST_DATA_DIRECTORY "/json.org/3.json", "r"), &std::fclose);
const std::unique_ptr<std::FILE, decltype(&std::fclose)> f(std::fopen(TEST_DATA_DIRECTORY "/json.org/3.json", "r"), &std::fclose);
json _;
CHECK_NOTHROW(_ = json::parse(f.get()));
}
SECTION("FILE 4.json")
{
std::unique_ptr<std::FILE, decltype(&std::fclose)> f(std::fopen(TEST_DATA_DIRECTORY "/json.org/4.json", "r"), &std::fclose);
const std::unique_ptr<std::FILE, decltype(&std::fclose)> f(std::fopen(TEST_DATA_DIRECTORY "/json.org/4.json", "r"), &std::fclose);
json _;
CHECK_NOTHROW(_ = json::parse(f.get()));
}
SECTION("FILE 5.json")
{
std::unique_ptr<std::FILE, decltype(&std::fclose)> f(std::fopen(TEST_DATA_DIRECTORY "/json.org/5.json", "r"), &std::fclose);
const std::unique_ptr<std::FILE, decltype(&std::fclose)> f(std::fopen(TEST_DATA_DIRECTORY "/json.org/5.json", "r"), &std::fclose);
json _;
CHECK_NOTHROW(_ = json::parse(f.get()));
}
@ -1340,12 +1340,12 @@ std::string trim(const std::string& str);
// from https://stackoverflow.com/a/25829178/266378
std::string trim(const std::string& str)
{
size_t first = str.find_first_not_of(' ');
const size_t first = str.find_first_not_of(' ');
if (std::string::npos == first)
{
return str;
}
size_t last = str.find_last_not_of(' ');
const size_t last = str.find_last_not_of(' ');
return str.substr(first, (last - first + 1));
}
} // namespace
@ -1384,7 +1384,7 @@ TEST_CASE("Big List of Naughty Strings")
// check roundtrip
CAPTURE(line)
json j = json::parse(line);
const json j = json::parse(line);
CHECK(j.dump() == line);
}
}

View File

@ -61,11 +61,11 @@ float make_float(uint64_t f, int e)
e--;
}
uint64_t biased_exponent = (e == kDenormalExponent && (f & kHiddenBit) == 0)
? 0
: static_cast<uint64_t>(e + kExponentBias);
const uint64_t biased_exponent = (e == kDenormalExponent && (f & kHiddenBit) == 0)
? 0
: static_cast<uint64_t>(e + kExponentBias);
uint64_t bits = (f & kSignificandMask) | (biased_exponent << kPhysicalSignificandSize);
const uint64_t bits = (f & kSignificandMask) | (biased_exponent << kPhysicalSignificandSize);
return reinterpret_bits<float>(static_cast<uint32_t>(bits));
}
@ -113,11 +113,11 @@ double make_double(uint64_t f, int e)
e--;
}
uint64_t biased_exponent = (e == kDenormalExponent && (f & kHiddenBit) == 0)
? 0
: static_cast<uint64_t>(e + kExponentBias);
const uint64_t biased_exponent = (e == kDenormalExponent && (f & kHiddenBit) == 0)
? 0
: static_cast<uint64_t>(e + kExponentBias);
uint64_t bits = (f & kSignificandMask) | (biased_exponent << kPhysicalSignificandSize);
const uint64_t bits = (f & kSignificandMask) | (biased_exponent << kPhysicalSignificandSize);
return reinterpret_bits<double>(bits);
}
} // namespace
@ -458,7 +458,7 @@ TEST_CASE("formatting")
{
auto check_integer = [](std::int64_t number, const std::string & expected)
{
nlohmann::json j = number;
const nlohmann::json j = number;
CHECK(j.dump() == expected);
};

File diff suppressed because it is too large Load Diff

View File

@ -418,15 +418,15 @@ TEST_CASE("adl_serializer specialization" * doctest::test_suite("udt"))
{
SECTION("to_json")
{
udt::legacy_type lt{"4242"};
udt::legacy_type const lt{"4242"};
json j = lt;
json const j = lt;
CHECK(j.get<int>() == 4242);
}
SECTION("from_json")
{
json j = 4242;
json const j = 4242;
auto lt = j.get<udt::legacy_type>();
CHECK(lt.number == "4242");
}
@ -459,7 +459,7 @@ struct adl_serializer<std::vector<float>>
TEST_CASE("even supported types can be specialized" * doctest::test_suite("udt"))
{
json j = std::vector<float> {1.0, 2.0, 3.0};
json const j = std::vector<float> {1.0, 2.0, 3.0};
CHECK(j.dump() == R"("hijacked!")");
auto f = j.get<std::vector<float>>();
// the single argument from_json method is preferred
@ -644,14 +644,14 @@ TEST_CASE("custom serializer for pods" * doctest::test_suite("udt"))
std::int64_t, std::uint64_t, double, std::allocator, pod_serializer>;
auto p = udt::small_pod{42, '/', 42};
custom_json j = p;
custom_json const j = p;
auto p2 = j.get<udt::small_pod>();
CHECK(p == p2);
auto np = udt::non_pod{{"non-pod"}};
custom_json j2 = np;
custom_json const j2 = np;
auto np2 = j2.get<udt::non_pod>();
CHECK(np == np2);
}
@ -681,8 +681,8 @@ TEST_CASE("custom serializer that does adl by default" * doctest::test_suite("ud
{
auto me = udt::person{{23}, {"theo"}, udt::country::france};
json j = me;
custom_json cj = me;
json const j = me;
custom_json const cj = me;
CHECK(j.dump() == cj.dump());
@ -694,21 +694,21 @@ TEST_CASE("different basic_json types conversions")
{
SECTION("null")
{
json j;
json const j;
custom_json cj = j;
CHECK(cj == nullptr);
}
SECTION("boolean")
{
json j = true;
json const j = true;
custom_json cj = j;
CHECK(cj == true);
}
SECTION("discarded")
{
json j(json::value_t::discarded);
json const j(json::value_t::discarded);
custom_json cj;
CHECK_NOTHROW(cj = j);
CHECK(cj.type() == custom_json::value_t::discarded);
@ -716,35 +716,35 @@ TEST_CASE("different basic_json types conversions")
SECTION("array")
{
json j = {1, 2, 3};
custom_json cj = j;
json const j = {1, 2, 3};
custom_json const cj = j;
CHECK((cj == std::vector<int> {1, 2, 3}));
}
SECTION("integer")
{
json j = 42;
json const j = 42;
custom_json cj = j;
CHECK(cj == 42);
}
SECTION("float")
{
json j = 42.0;
json const j = 42.0;
custom_json cj = j;
CHECK(cj == 42.0);
}
SECTION("unsigned")
{
json j = 42u;
json const j = 42u;
custom_json cj = j;
CHECK(cj == 42u);
}
SECTION("string")
{
json j = "forty-two";
json const j = "forty-two";
custom_json cj = j;
CHECK(cj == "forty-two");
}
@ -761,7 +761,7 @@ TEST_CASE("different basic_json types conversions")
SECTION("object")
{
json j = {{"forty", "two"}};
json const j = {{"forty", "two"}};
custom_json cj = j;
auto m = j.get<std::map<std::string, std::string>>();
CHECK(cj == m);
@ -769,7 +769,7 @@ TEST_CASE("different basic_json types conversions")
SECTION("get<custom_json>")
{
json j = 42;
json const j = 42;
custom_json cj = j.get<custom_json>();
CHECK(cj == 42);
}
@ -857,8 +857,8 @@ class no_iterator_type
TEST_CASE("compatible array type, without iterator type alias")
{
no_iterator_type vec{1, 2, 3};
json j = vec;
no_iterator_type const vec{1, 2, 3};
json const j = vec;
}
DOCTEST_GCC_SUPPRESS_WARNING_POP

View File

@ -349,7 +349,7 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv
{
{
T obj1;
nlohmann::json j = obj1; //via json object
nlohmann::json const j = obj1; //via json object
T obj2;
j.get_to(obj2);
bool ok = (obj1 == obj2);
@ -358,9 +358,9 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv
{
T obj1;
nlohmann::json j1 = obj1; //via json string
std::string s = j1.dump();
nlohmann::json j2 = nlohmann::json::parse(s);
nlohmann::json const j1 = obj1; //via json string
std::string const s = j1.dump();
nlohmann::json const j2 = nlohmann::json::parse(s);
T obj2;
j2.get_to(obj2);
bool ok = (obj1 == obj2);
@ -369,9 +369,9 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv
{
T obj1;
nlohmann::json j1 = obj1; //via msgpack
std::vector<uint8_t> buf = nlohmann::json::to_msgpack(j1);
nlohmann::json j2 = nlohmann::json::from_msgpack(buf);
nlohmann::json const j1 = obj1; //via msgpack
std::vector<uint8_t> const buf = nlohmann::json::to_msgpack(j1);
nlohmann::json const j2 = nlohmann::json::from_msgpack(buf);
T obj2;
j2.get_to(obj2);
bool ok = (obj1 == obj2);
@ -380,9 +380,9 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv
{
T obj1;
nlohmann::json j1 = obj1; //via bson
std::vector<uint8_t> buf = nlohmann::json::to_bson(j1);
nlohmann::json j2 = nlohmann::json::from_bson(buf);
nlohmann::json const j1 = obj1; //via bson
std::vector<uint8_t> const buf = nlohmann::json::to_bson(j1);
nlohmann::json const j2 = nlohmann::json::from_bson(buf);
T obj2;
j2.get_to(obj2);
bool ok = (obj1 == obj2);
@ -391,9 +391,9 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv
{
T obj1;
nlohmann::json j1 = obj1; //via cbor
std::vector<uint8_t> buf = nlohmann::json::to_cbor(j1);
nlohmann::json j2 = nlohmann::json::from_cbor(buf);
nlohmann::json const j1 = obj1; //via cbor
std::vector<uint8_t> const buf = nlohmann::json::to_cbor(j1);
nlohmann::json const j2 = nlohmann::json::from_cbor(buf);
T obj2;
j2.get_to(obj2);
bool ok = (obj1 == obj2);
@ -402,9 +402,9 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv
{
T obj1;
nlohmann::json j1 = obj1; //via ubjson
std::vector<uint8_t> buf = nlohmann::json::to_ubjson(j1);
nlohmann::json j2 = nlohmann::json::from_ubjson(buf);
nlohmann::json const j1 = obj1; //via ubjson
std::vector<uint8_t> const buf = nlohmann::json::to_ubjson(j1);
nlohmann::json const j2 = nlohmann::json::from_ubjson(buf);
T obj2;
j2.get_to(obj2);
bool ok = (obj1 == obj2);

View File

@ -233,9 +233,9 @@ void roundtrip(bool success_expected, const std::string& s)
json _;
// create JSON string value
json j = s;
const json j = s;
// create JSON text
std::string ps = std::string("\"") + s + "\"";
const std::string ps = std::string("\"") + s + "\"";
if (success_expected)
{
@ -253,7 +253,7 @@ void roundtrip(bool success_expected, const std::string& s)
CHECK_NOTHROW(_ = json::parse(j.dump()));
// after roundtrip, the same string is stored
json jr = json::parse(j.dump());
const json jr = json::parse(j.dump());
CHECK(jr.get<std::string>() == s);
}
else

View File

@ -45,7 +45,7 @@ const char* end(const MyContainer& c)
TEST_CASE("Custom container non-member begin/end")
{
MyContainer data{"[1,2,3,4]"};
const MyContainer data{"[1,2,3,4]"};
json as_json = json::parse(data);
CHECK(as_json.at(0) == 1);
CHECK(as_json.at(1) == 2);
@ -71,7 +71,7 @@ TEST_CASE("Custom container member begin/end")
}
};
MyContainer2 data{"[1,2,3,4]"};
const MyContainer2 data{"[1,2,3,4]"};
json as_json = json::parse(data);
CHECK(as_json.at(0) == 1);
CHECK(as_json.at(1) == 2);
@ -117,8 +117,8 @@ TEST_CASE("Custom iterator")
CHECK(std::is_same<MyIterator::reference, const char&>::value);
CHECK(std::is_same<MyIterator::iterator_category, std::input_iterator_tag>::value);
MyIterator begin{raw_data};
MyIterator end{raw_data + strlen(raw_data)}; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
const MyIterator begin{raw_data};
const MyIterator end{raw_data + strlen(raw_data)}; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
json as_json = json::parse(begin, end);
CHECK(as_json.at(0) == 1);

View File

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

View File

@ -20,7 +20,7 @@
// doctest doesn't support THROWS_WITH for std::string out of the box (has to include <string>...)
#define CHECK_THROWS_WITH_STD_STR_IMPL(expr, str, var_name) \
do { \
std::string var_name = str; \
const std::string var_name = str; \
CHECK_THROWS_WITH(expr, var_name.c_str()); \
} while (false)
#define CHECK_THROWS_WITH_STD_STR(expr, str) \