Mark the code of json_view that tests cannot reach

The 4 GiB limit and the fallback for an input that parse() accepts but
the view rejects (a bug) are excluded from the coverage; shrink_to_fit()
of an empty document is tested.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann
2026-09-30 20:58:47 +02:00
parent 723cf14be4
commit 06bebe2af6
4 changed files with 15 additions and 2 deletions
+4
View File
@@ -55,9 +55,12 @@ template<typename BasicJsonType>
{ {
if (f.code == error_code::input_too_large) if (f.code == error_code::input_too_large)
{ {
// LCOV_EXCL_START (4 GiB)
NLOHMANN_VIEW_THROW(out_of_range::create(416, "input of 4 GiB or more is not supported by json_document", nullptr)); NLOHMANN_VIEW_THROW(out_of_range::create(416, "input of 4 GiB or more is not supported by json_document", nullptr));
// LCOV_EXCL_STOP
} }
const BasicJsonType accepted = BasicJsonType::parse(src, src + size, nullptr, true, ignore_comments, ignore_trailing_commas); const BasicJsonType accepted = BasicJsonType::parse(src, src + size, nullptr, true, ignore_comments, ignore_trailing_commas);
// LCOV_EXCL_START (only if parse() accepts what the view rejects: a bug)
static_cast<void>(accepted); static_cast<void>(accepted);
position_t pos; position_t pos;
@@ -74,6 +77,7 @@ template<typename BasicJsonType>
} }
pos.chars_read_current_line = off + 1 - line_start; pos.chars_read_current_line = off + 1 - line_start;
NLOHMANN_VIEW_THROW(parse_error::create(101, pos, "syntax error while parsing value", nullptr)); NLOHMANN_VIEW_THROW(parse_error::create(101, pos, "syntax error while parsing value", nullptr));
// LCOV_EXCL_STOP
} }
} // namespace view } // namespace view
+1 -1
View File
@@ -459,7 +459,7 @@ class basic_json_document
bool ok = false; bool ok = false;
if (NLOHMANN_VIEW_UNLIKELY(size >= 0xFFFFFFF0u)) if (NLOHMANN_VIEW_UNLIKELY(size >= 0xFFFFFFF0u))
{ {
failure.code = detail::view::error_code::input_too_large; failure.code = detail::view::error_code::input_too_large; // LCOV_EXCL_LINE (4 GiB)
} }
else else
{ {
+5 -1
View File
@@ -1635,9 +1635,12 @@ template<typename BasicJsonType>
{ {
if (f.code == error_code::input_too_large) if (f.code == error_code::input_too_large)
{ {
// LCOV_EXCL_START (4 GiB)
NLOHMANN_VIEW_THROW(out_of_range::create(416, "input of 4 GiB or more is not supported by json_document", nullptr)); NLOHMANN_VIEW_THROW(out_of_range::create(416, "input of 4 GiB or more is not supported by json_document", nullptr));
// LCOV_EXCL_STOP
} }
const BasicJsonType accepted = BasicJsonType::parse(src, src + size, nullptr, true, ignore_comments, ignore_trailing_commas); const BasicJsonType accepted = BasicJsonType::parse(src, src + size, nullptr, true, ignore_comments, ignore_trailing_commas);
// LCOV_EXCL_START (only if parse() accepts what the view rejects: a bug)
static_cast<void>(accepted); static_cast<void>(accepted);
position_t pos; position_t pos;
@@ -1654,6 +1657,7 @@ template<typename BasicJsonType>
} }
pos.chars_read_current_line = off + 1 - line_start; pos.chars_read_current_line = off + 1 - line_start;
NLOHMANN_VIEW_THROW(parse_error::create(101, pos, "syntax error while parsing value", nullptr)); NLOHMANN_VIEW_THROW(parse_error::create(101, pos, "syntax error while parsing value", nullptr));
// LCOV_EXCL_STOP
} }
} // namespace view } // namespace view
@@ -2486,7 +2490,7 @@ class basic_json_document
bool ok = false; bool ok = false;
if (NLOHMANN_VIEW_UNLIKELY(size >= 0xFFFFFFF0u)) if (NLOHMANN_VIEW_UNLIKELY(size >= 0xFFFFFFF0u))
{ {
failure.code = detail::view::error_code::input_too_large; failure.code = detail::view::error_code::input_too_large; // LCOV_EXCL_LINE (4 GiB)
} }
else else
{ {
+5
View File
@@ -382,6 +382,11 @@ TEST_CASE("json_view")
CHECK(reused.memory_usage() < grown); CHECK(reused.memory_usage() < grown);
CHECK(reused.root().materialize() == json::parse(smaller)); CHECK(reused.root().materialize() == json::parse(smaller));
// an empty document has nothing to release
json_document empty;
empty.shrink_to_fit();
CHECK(empty.memory_usage() == 0);
// a small document stays in the storage block of the header // a small document stays in the storage block of the header
json_document small = json_document::parse("[1,[2,3],{\"a\":\"b\\n\"}]"); // NOLINT(modernize-raw-string-literal) json_document small = json_document::parse("[1,[2,3],{\"a\":\"b\\n\"}]"); // NOLINT(modernize-raw-string-literal)
small.shrink_to_fit(); small.shrink_to_fit();