mirror of
https://github.com/nlohmann/json.git
synced 2026-10-10 16:37:14 +00:00
Merge branch 'json-view/22-view-dump-fast' into json-view/15-view-bench
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
68 files changed
+3734
-964
No files matched your search
+4
-1
@@ -6,7 +6,10 @@ Each parser of the library (JSON, BJData, BON8, BSON, CBOR, MessagePack, and UBJ
|
||||
Additionally, `parse_json_view_fuzzer` (`tests/src/fuzzer-parse_json_view.cpp`) cross-checks `json_document`/`json_view`
|
||||
(the zero-copy, read-only view declared in `json_view.hpp`) against `basic_json` on the same JSON text: it asserts that
|
||||
`json_document::accept` agrees with `json::accept`, that an accepted input materializes to the same value `json::parse`
|
||||
produces, and that a rejected input makes both parsers throw with an identical `what()`. It takes plain JSON text, so it
|
||||
produces, and that a rejected input makes both parsers throw with an identical `what()`. It checks this for a
|
||||
`std::string` input (borrowed, with a NUL after the last byte) and for an exact-size `std::vector<std::uint8_t>`
|
||||
(borrowed, with nothing after the last byte), and for the `ignore_comments` and `ignore_trailing_commas` options, which
|
||||
are taken from the low bits of the first input byte (the byte stays part of the text). It takes plain JSON text, so it
|
||||
reuses the `corpus_json` corpus rather than a format of its own.
|
||||
|
||||
`json_view_image_fuzzer` (`tests/src/fuzzer-json_view_image.cpp`) tests the images of `json_document` (`save()` and
|
||||
|
||||
@@ -9,7 +9,9 @@
|
||||
/*
|
||||
This file implements a parser test suitable for fuzz testing. It checks that
|
||||
json_document (the zero-copy, read-only view of a parsed JSON text declared in
|
||||
json_view.hpp) agrees with basic_json on every input:
|
||||
json_view.hpp) agrees with basic_json on every input, for the parse options
|
||||
selected by the low bits of the first input byte (bit 0: ignore_comments, bit 1:
|
||||
ignore_trailing_commas; the byte stays part of the text):
|
||||
|
||||
- json_document::accept(data) must equal json::accept(data)
|
||||
- if the input is accepted, json_document::parse(data).root().materialize()
|
||||
@@ -18,12 +20,20 @@ json_view.hpp) agrees with basic_json on every input:
|
||||
enabled) must throw a json::parse_error or json::out_of_range whose what()
|
||||
is identical to the one json::parse(data) throws
|
||||
|
||||
This is checked for two kinds of input: a std::string, which the document
|
||||
borrows and which ends in the NUL the parser uses as sentinel, and an
|
||||
exact-size byte vector, which has no NUL after its last byte and takes the
|
||||
parser's bounds-checked path (AddressSanitizer reports any read past the end).
|
||||
|
||||
The provided function `LLVMFuzzerTestOneInput` can be used in different fuzzer
|
||||
drivers.
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
@@ -35,65 +45,110 @@ drivers.
|
||||
using json = nlohmann::json;
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
// see http://llvm.org/docs/LibFuzzer.html
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
|
||||
namespace
|
||||
{
|
||||
// json_document::accept only has a single-argument overload; wrap the raw
|
||||
// bytes in a (borrowed) std::string so the same bytes can be handed to it
|
||||
const std::string input(reinterpret_cast<const char*>(data), size); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
|
||||
// what json::parse does with a text: the value, or the message of the exception
|
||||
struct reference_result
|
||||
{
|
||||
bool accepted = false;
|
||||
json value{};
|
||||
std::string what{}; // NOLINT(readability-redundant-member-init)
|
||||
};
|
||||
|
||||
const bool accepted_by_json = json::accept(data, data + size);
|
||||
const bool accepted_by_view = json_document::accept(input);
|
||||
reference_result parse_reference(const std::uint8_t* data, std::size_t size, bool comments, bool trailing_commas)
|
||||
{
|
||||
reference_result r;
|
||||
r.accepted = json::accept(data, data + size, comments, trailing_commas);
|
||||
bool json_threw = false;
|
||||
try
|
||||
{
|
||||
r.value = json::parse(data, data + size, nullptr, true, comments, trailing_commas);
|
||||
}
|
||||
catch (const json::parse_error& e)
|
||||
{
|
||||
r.what = e.what();
|
||||
json_threw = true;
|
||||
}
|
||||
catch (const json::out_of_range& e)
|
||||
{
|
||||
r.what = e.what();
|
||||
json_threw = true;
|
||||
}
|
||||
// json::accept and json::parse must agree
|
||||
assert(json_threw == !r.accepted);
|
||||
static_cast<void>(json_threw);
|
||||
return r;
|
||||
}
|
||||
|
||||
// json_document must agree with the reference for this input (a container
|
||||
// that json_document::parse borrows)
|
||||
template<typename Input>
|
||||
void check_input(const Input& input, const reference_result& expected, bool comments, bool trailing_commas)
|
||||
{
|
||||
// json_document::accept must agree with json::accept on every input
|
||||
assert(accepted_by_json == accepted_by_view);
|
||||
const bool accepted_by_view = json_document::accept(input, comments, trailing_commas);
|
||||
assert(expected.accepted == accepted_by_view);
|
||||
static_cast<void>(accepted_by_view);
|
||||
|
||||
if (accepted_by_json)
|
||||
if (expected.accepted)
|
||||
{
|
||||
// both parsers must agree on the resulting value
|
||||
json const j1 = json::parse(data, data + size);
|
||||
json_document const doc = json_document::parse(input);
|
||||
json_document const doc = json_document::parse(input, true, comments, trailing_commas);
|
||||
assert(!doc.is_discarded());
|
||||
json const j2 = doc.root().materialize();
|
||||
assert(j1 == j2);
|
||||
assert(expected.value == j2);
|
||||
static_cast<void>(j2);
|
||||
|
||||
// (without exceptions, the same document)
|
||||
json_document const quiet = json_document::parse(input, false, comments, trailing_commas);
|
||||
assert(!quiet.is_discarded());
|
||||
assert(quiet.node_count() == doc.node_count());
|
||||
}
|
||||
else
|
||||
{
|
||||
// both parsers must reject the input the same way when exceptions are used
|
||||
std::string expected_what;
|
||||
bool json_threw = false;
|
||||
try
|
||||
{
|
||||
static_cast<void>(json::parse(data, data + size));
|
||||
}
|
||||
catch (const json::parse_error& e)
|
||||
{
|
||||
expected_what = e.what();
|
||||
json_threw = true;
|
||||
}
|
||||
catch (const json::out_of_range& e)
|
||||
{
|
||||
expected_what = e.what();
|
||||
json_threw = true;
|
||||
}
|
||||
assert(json_threw);
|
||||
|
||||
bool view_threw = false;
|
||||
try
|
||||
{
|
||||
static_cast<void>(json_document::parse(input));
|
||||
static_cast<void>(json_document::parse(input, true, comments, trailing_commas));
|
||||
}
|
||||
catch (const json::parse_error& e)
|
||||
{
|
||||
assert(e.what() == expected_what);
|
||||
assert(e.what() == expected.what);
|
||||
view_threw = true;
|
||||
}
|
||||
catch (const json::out_of_range& e)
|
||||
{
|
||||
assert(e.what() == expected_what);
|
||||
assert(e.what() == expected.what);
|
||||
view_threw = true;
|
||||
}
|
||||
assert(view_threw);
|
||||
static_cast<void>(view_threw);
|
||||
|
||||
// and without exceptions, the document is discarded
|
||||
json_document const quiet = json_document::parse(input, false, comments, trailing_commas);
|
||||
assert(quiet.is_discarded());
|
||||
static_cast<void>(quiet);
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// see http://llvm.org/docs/LibFuzzer.html
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
|
||||
{
|
||||
// the parse options are taken from the low bits of the first byte
|
||||
const bool comments = size > 0 && (data[0] & 1U) != 0;
|
||||
const bool trailing_commas = size > 0 && (data[0] & 2U) != 0;
|
||||
|
||||
const reference_result expected = parse_reference(data, size, comments, trailing_commas);
|
||||
|
||||
// a std::string: borrowed, with the NUL of std::string as sentinel
|
||||
const std::string input(reinterpret_cast<const char*>(data), size); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
|
||||
check_input(input, expected, comments, trailing_commas);
|
||||
|
||||
// an exact-size byte vector: borrowed, with nothing after its last byte
|
||||
const std::vector<std::uint8_t> exact(data, data + size);
|
||||
check_input(exact, expected, comments, trailing_commas);
|
||||
|
||||
// return 0 - non-zero return values are reserved for future use
|
||||
return 0;
|
||||
|
||||
@@ -1792,4 +1792,11 @@ TEST_CASE("string scanning kernels")
|
||||
CHECK(nlohmann::detail::count_trailing_zeros(bit) == k);
|
||||
CHECK(nlohmann::detail::count_trailing_zeros(bit | (bit << 1u) | 0x8000000000000000u) == k);
|
||||
}
|
||||
|
||||
// eight bytes as a little-endian word, at any alignment
|
||||
const unsigned char bytes[16] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF};
|
||||
CHECK(nlohmann::detail::read_eight_bytes(bytes) == 0x0807060504030201u);
|
||||
CHECK(nlohmann::detail::read_eight_bytes(bytes + 1) == 0x0908070605040302u);
|
||||
CHECK(nlohmann::detail::read_eight_bytes(bytes + 8) == 0xFF0F0E0D0C0B0A09u);
|
||||
CHECK(nlohmann::detail::read_eight_bytes(reinterpret_cast<const char*>(bytes) + 3) == 0x0B0A090807060504u);
|
||||
}
|
||||
+585
-50
@@ -19,16 +19,19 @@ using nlohmann::ordered_json_view;
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -39,6 +42,55 @@ using nlohmann::ordered_json_view;
|
||||
|
||||
namespace
|
||||
{
|
||||
// the value of a text, through a named document: the views of a temporary
|
||||
// document would dangle (root() of an rvalue document does not compile)
|
||||
template<typename Document, typename... Args>
|
||||
auto materialized(Args&& ... args) -> decltype(std::declval<typename Document::view_type>().materialize())
|
||||
{
|
||||
const Document d = Document::parse(std::forward<Args>(args)...);
|
||||
return d.root().materialize();
|
||||
}
|
||||
|
||||
template<typename Document, typename Input>
|
||||
auto materialized_copy(Input&& input) -> decltype(std::declval<typename Document::view_type>().materialize())
|
||||
{
|
||||
const Document d = Document::parse_copy(std::forward<Input>(input));
|
||||
return d.root().materialize();
|
||||
}
|
||||
|
||||
// a "byte container" that claims to hold `size` bytes, to reach the limit on
|
||||
// the size of the input without allocating gigabytes; nothing past the first
|
||||
// bytes is ever read, because the size is checked before the parse starts
|
||||
struct oversized_input
|
||||
{
|
||||
using value_type = char;
|
||||
std::size_t claimed;
|
||||
|
||||
const char* data() const
|
||||
{
|
||||
return "[1]";
|
||||
}
|
||||
|
||||
std::size_t size() const
|
||||
{
|
||||
return claimed;
|
||||
}
|
||||
};
|
||||
|
||||
// detection of calls that must not compile
|
||||
template<typename... Args>
|
||||
using parse_call_t = decltype(json_document::parse(std::declval<Args>()...));
|
||||
template<typename... Args>
|
||||
using parse_copy_call_t = decltype(json_document::parse_copy(std::declval<Args>()...));
|
||||
template<typename... Args>
|
||||
using accept_call_t = decltype(json_document::accept(std::declval<Args>()...));
|
||||
template<typename... Args>
|
||||
using read_call_t = decltype(std::declval<json_document&>().read(std::declval<Args>()...));
|
||||
template<typename Document>
|
||||
using root_call_t = decltype(std::declval<Document>().root());
|
||||
template<typename View>
|
||||
using bool_conversion_t = decltype(static_cast<bool>(std::declval<View>()));
|
||||
|
||||
#if !defined(JSON_NOEXCEPTION)
|
||||
// the exception parse() throws for a text, or "" if it accepts it
|
||||
std::string parse_exception(const std::string& text, bool comments = false, bool trailing_commas = false)
|
||||
@@ -152,7 +204,6 @@ TEST_CASE("json_view")
|
||||
CHECK(v.is_primitive() == j.is_primitive());
|
||||
CHECK(v.is_structured() == j.is_structured());
|
||||
CHECK(!v.is_discarded());
|
||||
CHECK(static_cast<bool>(v));
|
||||
CHECK(v.size() == j.size());
|
||||
CHECK(v.empty() == j.empty());
|
||||
CHECK(v.materialize() == j);
|
||||
@@ -160,7 +211,6 @@ TEST_CASE("json_view")
|
||||
|
||||
const json_view invalid{};
|
||||
CHECK(invalid.is_discarded());
|
||||
CHECK(!static_cast<bool>(invalid));
|
||||
CHECK(invalid.type() == json::value_t::discarded);
|
||||
CHECK(invalid.size() == 0);
|
||||
CHECK(invalid.empty());
|
||||
@@ -176,19 +226,19 @@ TEST_CASE("json_view")
|
||||
std::string text;
|
||||
g.value(text, 0);
|
||||
CAPTURE(text)
|
||||
CHECK(json_document::parse(text).root().materialize() == json::parse(text));
|
||||
CHECK(materialized<json_document>(text) == json::parse(text));
|
||||
// member order as ordered_json::parse keeps it
|
||||
CHECK(ordered_json_document::parse(text).root().materialize().dump() == ordered_json::parse(text).dump());
|
||||
CHECK(materialized<ordered_json_document>(text).dump() == ordered_json::parse(text).dump());
|
||||
}
|
||||
// duplicate keys: the last value, at the position of the first key
|
||||
CHECK(json_document::parse(R"({"a":1,"b":2,"a":3})").root().materialize() == json::parse(R"({"a":1,"b":2,"a":3})"));
|
||||
CHECK(ordered_json_document::parse(R"({"a":1,"b":2,"a":3})").root().materialize().dump() == R"({"a":3,"b":2})");
|
||||
CHECK(materialized<json_document>(R"({"a":1,"b":2,"a":3})") == json::parse(R"({"a":1,"b":2,"a":3})"));
|
||||
CHECK(materialized<ordered_json_document>(R"({"a":1,"b":2,"a":3})").dump() == R"({"a":3,"b":2})");
|
||||
// very deep nesting (iterative, as parse())
|
||||
const std::string deep = std::string(100000, '[') + std::string(100000, ']');
|
||||
CHECK(json_document::parse(deep).root().materialize() == json::parse(deep));
|
||||
CHECK(materialized<json_document>(deep) == json::parse(deep));
|
||||
#if JSON_DIAGNOSTICS
|
||||
// the parents are set, so errors name the path
|
||||
const json m = json_document::parse(R"({"a":{"b":[1]}})").root().materialize();
|
||||
const json m = materialized<json_document>(R"({"a":{"b":[1]}})");
|
||||
CHECK_THROWS_WITH_AS(m.at("a").at("b").at(0).at("x"), "[json.exception.type_error.304] (/a/b/0) cannot use at() with number", json::type_error&);
|
||||
#endif
|
||||
}
|
||||
@@ -261,7 +311,7 @@ TEST_CASE("json_view")
|
||||
CHECK(json_document::accept(text));
|
||||
if (accepted)
|
||||
{
|
||||
CHECK(float_document::parse(text).root().materialize() == json_float::parse(text));
|
||||
CHECK(materialized<float_document>(text) == json_float::parse(text));
|
||||
}
|
||||
}
|
||||
float_document f;
|
||||
@@ -275,7 +325,7 @@ TEST_CASE("json_view")
|
||||
CHECK(json_document::accept(with_nul) == json::accept(with_nul));
|
||||
const std::string nul_in_comment("[1, // c\0\n2]", 12);
|
||||
CHECK(json_document::accept(nul_in_comment, true) == json::accept(nul_in_comment, true));
|
||||
CHECK(json_document::parse("\xEF\xBB\xBF[1]").root().materialize() == json::parse("\xEF\xBB\xBF[1]"));
|
||||
CHECK(materialized<json_document>("\xEF\xBB\xBF[1]") == json::parse("\xEF\xBB\xBF[1]"));
|
||||
#if !defined(JSON_NOEXCEPTION)
|
||||
CHECK(view_exception("\xEF\xBB") == parse_exception("\xEF\xBB"));
|
||||
#endif
|
||||
@@ -291,18 +341,18 @@ TEST_CASE("json_view")
|
||||
CHECK(!borrowed.owns_source());
|
||||
CHECK(borrowed.source().data() == text.data());
|
||||
CHECK(borrowed.root().materialize() == expected);
|
||||
CHECK(json_document::parse(text.c_str()).root().materialize() == expected);
|
||||
CHECK(json_document::parse(R"([1, "two", {"three": 3.5}])").root().materialize() == expected);
|
||||
CHECK(json_document::parse(text.data(), text.data() + text.size()).root().materialize() == expected);
|
||||
CHECK(materialized<json_document>(text.c_str()) == expected);
|
||||
CHECK(materialized<json_document>(R"([1, "two", {"three": 3.5}])") == expected);
|
||||
CHECK(materialized<json_document>(text.data(), text.data() + text.size()) == expected);
|
||||
const std::vector<char> chars(text.begin(), text.end());
|
||||
CHECK(!json_document::parse(chars).owns_source());
|
||||
CHECK(json_document::parse(chars).root().materialize() == expected);
|
||||
CHECK(materialized<json_document>(chars) == expected);
|
||||
const std::vector<std::uint8_t> bytes(text.begin(), text.end());
|
||||
CHECK(json_document::parse(bytes).root().materialize() == expected);
|
||||
CHECK(materialized<json_document>(bytes) == expected);
|
||||
#ifdef JSON_HAS_CPP_17
|
||||
const std::string_view sv = text;
|
||||
CHECK(!json_document::parse(sv).owns_source());
|
||||
CHECK(json_document::parse(sv).root().materialize() == expected);
|
||||
CHECK(materialized<json_document>(sv) == expected);
|
||||
#endif
|
||||
|
||||
// owned
|
||||
@@ -311,15 +361,28 @@ TEST_CASE("json_view")
|
||||
CHECK(from_rvalue.owns_source());
|
||||
CHECK(from_rvalue.root().materialize() == expected);
|
||||
CHECK(json_document::parse(std::vector<char>(text.begin(), text.end())).owns_source());
|
||||
// a const rvalue cannot be moved from, and is not borrowed (it may be a
|
||||
// temporary): it is copied, as is a const rvalue of any container
|
||||
const std::string const_text = text;
|
||||
const json_document from_const_rvalue = json_document::parse(std::move(const_text)); // NOLINT(performance-move-const-arg,hicpp-move-const-arg)
|
||||
CHECK(from_const_rvalue.owns_source());
|
||||
CHECK(from_const_rvalue.source().data() != const_text.data());
|
||||
CHECK(from_const_rvalue.root().materialize() == expected);
|
||||
json_document read_const_rvalue;
|
||||
read_const_rvalue.read(std::move(const_text)); // NOLINT(performance-move-const-arg,hicpp-move-const-arg)
|
||||
CHECK(read_const_rvalue.owns_source());
|
||||
CHECK(read_const_rvalue.root().materialize() == expected);
|
||||
const std::vector<char> const_chars(text.begin(), text.end());
|
||||
CHECK(json_document::parse(std::move(const_chars)).owns_source()); // NOLINT(performance-move-const-arg,hicpp-move-const-arg)
|
||||
CHECK(json_document::parse_copy(text).owns_source());
|
||||
CHECK(json_document::parse_copy(text).root().materialize() == expected);
|
||||
CHECK(materialized_copy<json_document>(text) == expected);
|
||||
std::istringstream stream(text);
|
||||
const json_document from_stream = json_document::parse(stream);
|
||||
CHECK(from_stream.owns_source());
|
||||
CHECK(from_stream.root().materialize() == expected);
|
||||
const std::list<char> list(text.begin(), text.end());
|
||||
CHECK(json_document::parse(list.begin(), list.end()).owns_source());
|
||||
CHECK(json_document::parse(list.begin(), list.end()).root().materialize() == expected);
|
||||
CHECK(materialized<json_document>(list.begin(), list.end()) == expected);
|
||||
|
||||
// iterator pairs: pointers are borrowed, and so are contiguous library
|
||||
// iterators where the input adapter detects them (C++20)
|
||||
@@ -330,14 +393,85 @@ TEST_CASE("json_view")
|
||||
CHECK((from_iterators.source().data() == chars.data()) == contiguous);
|
||||
CHECK(from_iterators.root().materialize() == expected);
|
||||
const std::string padded = "x" + text + "x";
|
||||
CHECK(json_document::parse(padded.begin() + 1, padded.end() - 1).root().materialize() == expected);
|
||||
CHECK(materialized<json_document>(padded.begin() + 1, padded.end() - 1) == expected);
|
||||
CHECK(json_document::parse(chars.cbegin(), chars.cbegin(), false).is_discarded());
|
||||
const std::wstring wide = L"[\"\u00e4\u20ac\", 1]";
|
||||
CHECK(json_document::parse(wide).root().materialize() == json::parse(wide));
|
||||
CHECK(materialized<json_document>(wide) == json::parse(wide));
|
||||
CHECK(json_document::parse(static_cast<const char*>(nullptr), false).is_discarded());
|
||||
CHECK(json_document::parse("", false).is_discarded());
|
||||
}
|
||||
|
||||
SECTION("integer arguments do not compile")
|
||||
{
|
||||
using nlohmann::detail::is_detected;
|
||||
|
||||
// a length is not a flag: parse(ptr, len) would convert len to
|
||||
// allow_exceptions and read ptr as a C string, which need not end
|
||||
static_assert(is_detected<parse_call_t, const char*, bool>::value, "parse(ptr, bool) is valid");
|
||||
static_assert(is_detected<parse_call_t, const char*, bool, bool, bool>::value, "parse(ptr, bool, bool, bool) is valid");
|
||||
static_assert(is_detected<parse_call_t, const char*, const char*>::value, "parse(first, last) is valid");
|
||||
static_assert(is_detected<parse_call_t, const char*, const char*, bool>::value, "parse(first, last, bool) is valid");
|
||||
static_assert(!is_detected<parse_call_t, const char*, std::size_t>::value, "parse(ptr, len) must not compile");
|
||||
static_assert(!is_detected<parse_call_t, const char*, int>::value, "parse(ptr, int) must not compile");
|
||||
static_assert(!is_detected<parse_call_t, const char*, char>::value, "parse(ptr, char) must not compile");
|
||||
static_assert(!is_detected<parse_call_t, const char*, std::size_t, bool>::value, "parse(ptr, len, bool) must not compile");
|
||||
static_assert(!is_detected<parse_call_t, const std::string&, std::size_t>::value, "parse(string, len) must not compile");
|
||||
static_assert(!is_detected<parse_call_t, const std::vector<char>&, std::size_t>::value, "parse(vector, len) must not compile");
|
||||
|
||||
static_assert(is_detected<parse_copy_call_t, const char*, bool>::value, "parse_copy(ptr, bool) is valid");
|
||||
static_assert(!is_detected<parse_copy_call_t, const char*, std::size_t>::value, "parse_copy(ptr, len) must not compile");
|
||||
|
||||
static_assert(is_detected<accept_call_t, const char*, bool>::value, "accept(ptr, bool) is valid");
|
||||
static_assert(!is_detected<accept_call_t, const char*, std::size_t>::value, "accept(ptr, len) must not compile");
|
||||
|
||||
static_assert(is_detected<read_call_t, const char*, bool>::value, "read(ptr, bool) is valid");
|
||||
static_assert(!is_detected<read_call_t, const char*, std::size_t>::value, "read(ptr, len) must not compile");
|
||||
|
||||
// json_view has no conversion to bool: unlike basic_json's, it would
|
||||
// mean "exists", not "is not null"; use is_discarded()
|
||||
static_assert(!is_detected<bool_conversion_t, json_view>::value, "json_view must not convert to bool");
|
||||
|
||||
// the valid calls still work
|
||||
const char* const text = "[1]";
|
||||
CHECK(materialized<json_document>(text, true) == json::parse(text));
|
||||
CHECK(json_document::accept(text, true, true));
|
||||
}
|
||||
|
||||
SECTION("root of a temporary document does not compile")
|
||||
{
|
||||
using nlohmann::detail::is_detected;
|
||||
|
||||
// the view would dangle: auto v = json_document::parse(text).root();
|
||||
static_assert(is_detected<root_call_t, json_document&>::value, "root() of an lvalue is valid");
|
||||
static_assert(is_detected<root_call_t, const json_document&>::value, "root() of a const lvalue is valid");
|
||||
static_assert(!is_detected<root_call_t, json_document>::value, "root() of an rvalue must not compile");
|
||||
static_assert(!is_detected < root_call_t, json_document && >::value, "root() of an rvalue must not compile");
|
||||
static_assert(!is_detected < root_call_t, const json_document && >::value, "root() of a const rvalue must not compile");
|
||||
static_assert(!is_detected<root_call_t, ordered_json_document>::value, "root() of an rvalue must not compile");
|
||||
|
||||
// a named document is fine, also after a move
|
||||
json_document d = json_document::parse("[1]");
|
||||
CHECK(d.root().size() == 1);
|
||||
const json_document moved = std::move(d);
|
||||
CHECK(moved.root().size() == 1);
|
||||
}
|
||||
|
||||
SECTION("input size limit")
|
||||
{
|
||||
// 32-bit offsets: the limit is 4 GiB minus 16 bytes (a margin below 2^32),
|
||||
// which is what the exception message and the documentation say
|
||||
const std::size_t limit = nlohmann::detail::view::max_input_size;
|
||||
CHECK(limit == std::size_t{4294967279u});
|
||||
|
||||
const oversized_input input{limit + 1};
|
||||
CHECK(!json_document::accept(input));
|
||||
CHECK(json_document::parse(input, false).is_discarded());
|
||||
#if !defined(JSON_NOEXCEPTION)
|
||||
json_document d;
|
||||
CHECK_THROWS_WITH_AS(d = json_document::parse(input), "[json.exception.out_of_range.416] input of 4294967280 bytes or more is not supported by json_document", json::out_of_range&);
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("document lifetime and reuse")
|
||||
{
|
||||
json_document d;
|
||||
@@ -443,7 +577,7 @@ std::string exception_of(F f)
|
||||
|
||||
// compares a view with the ordered_json value materialize() gives for it:
|
||||
// types, sizes, elements and members (by index, key, and iteration), in
|
||||
// document order; duplicate keys are found as their first occurrence
|
||||
// document order; duplicate keys are found as their last occurrence
|
||||
void check_access(const ordered_json_view& v, const ordered_json& j)
|
||||
{
|
||||
REQUIRE(v.type() == j.type());
|
||||
@@ -460,7 +594,7 @@ void check_access(const ordered_json_view& v, const ordered_json& j)
|
||||
++i;
|
||||
}
|
||||
CHECK(i == v.size());
|
||||
CHECK(!v[v.size()]);
|
||||
CHECK(v[v.size()].is_discarded());
|
||||
std::size_t index = 0;
|
||||
for (const auto& item : v.items())
|
||||
{
|
||||
@@ -484,11 +618,23 @@ void check_access(const ordered_json_view& v, const ordered_json& j)
|
||||
const std::string key(it.key().data(), it.key().size());
|
||||
CHECK(v.contains(key));
|
||||
CHECK(v.count(key) == 1);
|
||||
if (std::find(keys.begin(), keys.end(), key) != keys.end())
|
||||
if (std::find(keys.begin(), keys.end(), key) == keys.end())
|
||||
{
|
||||
continue; // a duplicate: lookups find the first one
|
||||
keys.push_back(key);
|
||||
}
|
||||
// lookups find the last member with the key, which is this one if
|
||||
// there is no later one
|
||||
auto next = it;
|
||||
++next;
|
||||
bool is_last = true;
|
||||
for (; next != v.end(); ++next)
|
||||
{
|
||||
is_last = is_last && next.key() != it.key();
|
||||
}
|
||||
if (!is_last)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
keys.push_back(key);
|
||||
CHECK(v.find(key) == it);
|
||||
CHECK(v[key].materialize() == it->materialize());
|
||||
CHECK(v.at(key).materialize() == it.value().materialize());
|
||||
@@ -514,7 +660,7 @@ void check_access(const ordered_json_view& v, const ordered_json& j)
|
||||
CHECK(v.back().materialize() == j.back());
|
||||
}
|
||||
}
|
||||
CHECK(!v["not a key in the generated documents"]);
|
||||
CHECK(v["not a key in the generated documents"].is_discarded());
|
||||
CHECK(v.find("not a key in the generated documents") == v.end());
|
||||
}
|
||||
else
|
||||
@@ -579,15 +725,35 @@ TEST_CASE("json_view element access and iteration")
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("duplicate keys: lookups find the first member, iteration all")
|
||||
SECTION("duplicate keys: lookups find the last member, iteration all")
|
||||
{
|
||||
const json_document d = json_document::parse(R"({"a":1,"b":2,"a":3})");
|
||||
const json_view v = d.root();
|
||||
CHECK(v.size() == 3);
|
||||
CHECK(v["a"].materialize() == 1);
|
||||
CHECK(v.at("a").materialize() == 1);
|
||||
CHECK(v.find("a") == v.begin());
|
||||
CHECK(v["a"].materialize() == 3);
|
||||
CHECK(v.at("a").materialize() == 3);
|
||||
CHECK(v.find("a") == std::next(v.begin(), 2));
|
||||
CHECK(v.find("a").value().materialize() == 3);
|
||||
CHECK(v.find("b") == std::next(v.begin()));
|
||||
CHECK(v.count("a") == 1);
|
||||
CHECK(v.contains("a"));
|
||||
CHECK(v.value("a", 0) == 3);
|
||||
CHECK(v["a"].materialize() == v.materialize()["a"]); // as materialize()
|
||||
// keys of every length class (the 16-byte short compare and memcmp)
|
||||
for (const std::size_t n :
|
||||
{
|
||||
0u, 1u, 3u, 7u, 8u, 15u, 16u, 17u, 40u
|
||||
})
|
||||
{
|
||||
const std::string key(n, 'k');
|
||||
const json_document dk = json_document::parse("{\"" + key + "\":1,\"" + key + "x\":2,\"" + key + "\":3,\"" + key + "\":4}");
|
||||
CAPTURE(n)
|
||||
CHECK(dk.root()[key].materialize() == 4);
|
||||
CHECK(dk.root().at(key).materialize() == 4);
|
||||
CHECK(dk.root().find(key) == std::next(dk.root().begin(), 3));
|
||||
CHECK(dk.root().value(key, 0) == 4);
|
||||
CHECK(dk.root()[key + "x"].materialize() == 2);
|
||||
}
|
||||
std::string order;
|
||||
for (auto it = v.begin(); it != v.end(); ++it)
|
||||
{
|
||||
@@ -638,14 +804,131 @@ TEST_CASE("json_view element access and iteration")
|
||||
|
||||
// where basic_json has undefined behavior, the view answers safely
|
||||
const json_document d = json_document::parse(R"({"a":[]})");
|
||||
CHECK(!d.root()["b"]);
|
||||
CHECK(!d.root()["a"][0]);
|
||||
CHECK(d.root()["b"].is_discarded());
|
||||
CHECK(d.root()["a"][0].is_discarded());
|
||||
CHECK_THROWS_WITH_AS(d.root()["a"].front(), "[json.exception.invalid_iterator.214] cannot get value", json::invalid_iterator&);
|
||||
CHECK_THROWS_WITH_AS(d.root()["a"].back(), "[json.exception.invalid_iterator.214] cannot get value", json::invalid_iterator&);
|
||||
const json_view invalid{};
|
||||
CHECK(invalid.begin() == invalid.end());
|
||||
CHECK(std::string(invalid.type_name()) == "discarded");
|
||||
CHECK_THROWS_WITH_AS(invalid["a"], "[json.exception.type_error.305] cannot use operator[] with a string argument with discarded", json::type_error&);
|
||||
}
|
||||
|
||||
SECTION("chained access is safe: operator[] of a discarded view is discarded")
|
||||
{
|
||||
const json_document d = json_document::parse(R"({"a":{"b":[10,20]},"s":"str"})");
|
||||
const json_view v = d.root();
|
||||
// missing keys and indexes
|
||||
CHECK(v["x"].is_discarded());
|
||||
CHECK(v["x"]["y"].is_discarded());
|
||||
CHECK(v["x"]["y"]["z"].is_discarded());
|
||||
CHECK(v["x"][0].is_discarded());
|
||||
CHECK(v["x"][0u][1L].is_discarded());
|
||||
CHECK(v["a"]["b"][2].is_discarded());
|
||||
CHECK(v["a"]["b"][2]["c"].is_discarded());
|
||||
CHECK(v["a"]["b"][2][json_view::json_pointer("/c")].is_discarded());
|
||||
CHECK(v["x"][json_view::json_pointer("/a/b")].is_discarded());
|
||||
CHECK(v["x"][json_view::json_pointer("")].is_discarded());
|
||||
CHECK(v["x"]["y"].is_discarded());
|
||||
CHECK(v["x"][std::string("y")].is_discarded());
|
||||
// a resolvable path still resolves
|
||||
CHECK(v["a"]["b"][1].materialize() == 20);
|
||||
CHECK(v[json_view::json_pointer("/a/b/1")].materialize() == 20);
|
||||
// the discarded view of an unresolved pointer is discarded too
|
||||
CHECK(v[json_view::json_pointer("/x/y")]["z"].is_discarded());
|
||||
CHECK(v[json_view::json_pointer("/a/b/5")][0].is_discarded());
|
||||
#if !defined(JSON_NOEXCEPTION)
|
||||
// type errors on values that are not discarded stay
|
||||
CHECK_THROWS_WITH_AS(v[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with object", json::type_error&);
|
||||
CHECK_THROWS_WITH_AS(v["a"]["b"]["c"], "[json.exception.type_error.305] cannot use operator[] with a string argument with array", json::type_error&);
|
||||
CHECK_THROWS_WITH_AS(v["s"]["c"], "[json.exception.type_error.305] cannot use operator[] with a string argument with string", json::type_error&);
|
||||
CHECK_THROWS_WITH_AS(v["s"][0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with string", json::type_error&);
|
||||
CHECK_THROWS_AS(v["a"]["b"][0]["c"], json::type_error&);
|
||||
CHECK_THROWS_AS(v["s"][json_view::json_pointer("/x")], json::out_of_range&);
|
||||
// at() keeps throwing on a discarded view
|
||||
const json_view invalid{};
|
||||
CHECK(invalid["a"].is_discarded());
|
||||
CHECK(invalid[0].is_discarded());
|
||||
CHECK(invalid[json_view::json_pointer("/a")].is_discarded());
|
||||
CHECK_THROWS_WITH_AS(invalid.at("a"), "[json.exception.type_error.304] cannot use at() with discarded", json::type_error&);
|
||||
CHECK_THROWS_WITH_AS(invalid.at(0), "[json.exception.type_error.304] cannot use at() with discarded", json::type_error&);
|
||||
CHECK_THROWS_AS(v.at("x").at("y"), json::out_of_range&);
|
||||
CHECK_THROWS_AS(v["x"].at("y"), json::type_error&);
|
||||
CHECK_THROWS_AS(v.at(json_view::json_pointer("/x/y")), json::out_of_range&);
|
||||
CHECK_THROWS_AS(v["x"].at(json_view::json_pointer("/y")), json::out_of_range&);
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("integer types as array indexes")
|
||||
{
|
||||
const json_document d = json_document::parse("[10,20,30]");
|
||||
const json_view v = d.root();
|
||||
const json j = v.materialize();
|
||||
// (compile-time: no overload is ambiguous)
|
||||
CHECK(v[0].materialize() == 10);
|
||||
CHECK(v[1].materialize() == 20);
|
||||
CHECK(v[0u].materialize() == 10);
|
||||
CHECK(v[1u].materialize() == 20);
|
||||
CHECK(v[1L].materialize() == 20);
|
||||
CHECK(v[2UL].materialize() == 30);
|
||||
CHECK(v[1LL].materialize() == 20);
|
||||
CHECK(v[2ULL].materialize() == 30);
|
||||
CHECK(v[static_cast<short>(1)].materialize() == 20);
|
||||
CHECK(v[static_cast<unsigned short>(2)].materialize() == 30);
|
||||
CHECK(v[static_cast<signed char>(1)].materialize() == 20);
|
||||
CHECK(v[static_cast<unsigned char>(2)].materialize() == 30);
|
||||
CHECK(v[std::int8_t(1)].materialize() == 20);
|
||||
CHECK(v[std::int16_t(2)].materialize() == 30);
|
||||
CHECK(v[std::int32_t(1)].materialize() == 20);
|
||||
CHECK(v[std::int64_t(2)].materialize() == 30);
|
||||
CHECK(v[std::uint32_t(0)].materialize() == 10);
|
||||
CHECK(v[std::uint64_t(1)].materialize() == 20);
|
||||
CHECK(v[std::size_t(2)].materialize() == 30);
|
||||
CHECK(v[std::ptrdiff_t(1)].materialize() == 20);
|
||||
CHECK(j[0u] == 10); // as basic_json
|
||||
|
||||
CHECK(v.at(0).materialize() == 10);
|
||||
CHECK(v.at(1u).materialize() == 20);
|
||||
CHECK(v.at(1L).materialize() == 20);
|
||||
CHECK(v.at(2LL).materialize() == 30);
|
||||
CHECK(v.at(2ULL).materialize() == 30);
|
||||
CHECK(v.at(static_cast<short>(1)).materialize() == 20);
|
||||
CHECK(v.at(static_cast<unsigned short>(2)).materialize() == 30);
|
||||
CHECK(v.at(std::int32_t(0)).materialize() == 10);
|
||||
CHECK(v.at(std::uint32_t(0)).materialize() == 10);
|
||||
CHECK(v.at(std::int64_t(0)).materialize() == 10);
|
||||
CHECK(v.at(std::uint64_t(1)).materialize() == 20);
|
||||
CHECK(v.at(std::size_t(2)).materialize() == 30);
|
||||
CHECK(j.at(std::uint32_t(0)) == 10); // as basic_json
|
||||
|
||||
// out of range, including negative values (no wrap-around)
|
||||
CHECK(v[3].is_discarded());
|
||||
CHECK(v[3u].is_discarded());
|
||||
CHECK(v[3L].is_discarded());
|
||||
CHECK(v[-1].is_discarded());
|
||||
CHECK(v[-1L].is_discarded());
|
||||
CHECK(v[-1LL].is_discarded());
|
||||
CHECK(v[static_cast<short>(-1)].is_discarded());
|
||||
CHECK(v[std::int64_t(-3)].is_discarded());
|
||||
CHECK(v[(std::numeric_limits<std::int64_t>::min)()].is_discarded());
|
||||
CHECK(v[(std::numeric_limits<std::uint64_t>::max)()].is_discarded());
|
||||
CHECK(v[(std::numeric_limits<std::size_t>::max)()].is_discarded());
|
||||
CHECK(v[std::numeric_limits<int>::max()].is_discarded());
|
||||
#if !defined(JSON_NOEXCEPTION)
|
||||
CHECK_THROWS_WITH_AS(v.at(3), "[json.exception.out_of_range.401] array index 3 is out of range", json::out_of_range&);
|
||||
CHECK_THROWS_WITH_AS(v.at(3u), "[json.exception.out_of_range.401] array index 3 is out of range", json::out_of_range&);
|
||||
CHECK_THROWS_WITH_AS(v.at(std::int64_t(3)), "[json.exception.out_of_range.401] array index 3 is out of range", json::out_of_range&);
|
||||
CHECK_THROWS_AS(v.at(-1), json::out_of_range&);
|
||||
CHECK_THROWS_AS(v.at(-1L), json::out_of_range&);
|
||||
CHECK_THROWS_AS(v.at(std::int64_t(-1)), json::out_of_range&);
|
||||
CHECK_THROWS_AS(v.at((std::numeric_limits<std::int64_t>::min)()), json::out_of_range&);
|
||||
CHECK_THROWS_AS(v.at((std::numeric_limits<std::uint64_t>::max)()), json::out_of_range&);
|
||||
// not an array
|
||||
const json_document o = json_document::parse("{}");
|
||||
CHECK_THROWS_AS(o.root()[0u], json::type_error&);
|
||||
CHECK_THROWS_AS(o.root()[1L], json::type_error&);
|
||||
CHECK_THROWS_AS(o.root().at(std::uint32_t(0)), json::type_error&);
|
||||
CHECK_THROWS_AS(o.root().at(std::int64_t(0)), json::type_error&);
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("iterators")
|
||||
@@ -919,10 +1202,12 @@ TEST_CASE("json_view values")
|
||||
CAPTURE(token)
|
||||
const std::string text = "[" + token + "]";
|
||||
const double b = json::parse(text)[0].get<double>();
|
||||
CHECK(bits(json_document::parse(text).root()[0].get<double>()) == bits(b));
|
||||
const json_document dd = json_document::parse(text);
|
||||
CHECK(bits(dd.root()[0].get<double>()) == bits(b));
|
||||
if (std::abs(b) < 1e38)
|
||||
{
|
||||
CHECK(bits(nlohmann::basic_json_document<json_float>::parse(text).root()[0].get<float>()) == bits(json_float::parse(text)[0].get<float>()));
|
||||
const nlohmann::basic_json_document<json_float> df = nlohmann::basic_json_document<json_float>::parse(text);
|
||||
CHECK(bits(df.root()[0].get<float>()) == bits(json_float::parse(text)[0].get<float>()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -979,7 +1264,8 @@ TEST_CASE("json_view values")
|
||||
CHECK(count == 3);
|
||||
|
||||
// a duplicate key: the last value, as parse()
|
||||
CHECK((json_document::parse(R"({"a":1,"a":2})").root().get<std::map<std::string, int>>() == std::map<std::string, int> {{"a", 2}}));
|
||||
const json_document dup = json_document::parse(R"({"a":1,"a":2})");
|
||||
CHECK((dup.root().get<std::map<std::string, int>>() == std::map<std::string, int> {{"a", 2}}));
|
||||
|
||||
const json_view invalid{};
|
||||
CHECK_THROWS_WITH_AS(invalid.get<int>(), "[json.exception.type_error.302] type must be number, but is discarded", json::type_error&);
|
||||
@@ -1070,7 +1356,7 @@ TEST_CASE("json_view JSON pointers")
|
||||
else if (at_error.find("out_of_range.401") != std::string::npos || at_error.find("out_of_range.403") != std::string::npos) // NOLINT(abseil-string-find-str-contains)
|
||||
{
|
||||
// undefined behavior for const basic_json::operator[]
|
||||
CHECK(!v[p]);
|
||||
CHECK(v[p].is_discarded());
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1198,7 +1484,8 @@ TEST_CASE("json_view dump")
|
||||
many_tokens += (i != 0 ? "," : "") + token;
|
||||
}
|
||||
many_tokens += ']';
|
||||
CHECK(json_document::parse(many_tokens).root().dump() == json::parse(many_tokens).dump());
|
||||
const json_document many_doc = json_document::parse(many_tokens);
|
||||
CHECK(many_doc.root().dump() == json::parse(many_tokens).dump());
|
||||
}
|
||||
|
||||
// random doubles, written as parse() and dump() would
|
||||
@@ -1215,10 +1502,12 @@ TEST_CASE("json_view dump")
|
||||
}
|
||||
}
|
||||
many += ']';
|
||||
CHECK(json_document::parse(many).root().dump() == json::parse(many).dump());
|
||||
const json_document many_document = json_document::parse(many);
|
||||
CHECK(many_document.root().dump() == json::parse(many).dump());
|
||||
|
||||
using json_float = nlohmann::basic_json<std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t, float>;
|
||||
CHECK(nlohmann::basic_json_document<json_float>::parse("[0.1, 1.5e10, 3.4028235e38]").root().dump() == json_float::parse("[0.1, 1.5e10, 3.4028235e38]").dump());
|
||||
const nlohmann::basic_json_document<json_float> float_document = nlohmann::basic_json_document<json_float>::parse("[0.1, 1.5e10, 3.4028235e38]");
|
||||
CHECK(float_document.root().dump() == json_float::parse("[0.1, 1.5e10, 3.4028235e38]").dump());
|
||||
}
|
||||
|
||||
SECTION("members in document order, all of them")
|
||||
@@ -1231,7 +1520,42 @@ TEST_CASE("json_view dump")
|
||||
SECTION("deep nesting")
|
||||
{
|
||||
const std::string deep = std::string(100000, '[') + std::string(100000, ']');
|
||||
CHECK(json_document::parse(deep).root().dump() == deep);
|
||||
const json_document deep_document = json_document::parse(deep);
|
||||
CHECK(deep_document.root().dump() == deep);
|
||||
}
|
||||
|
||||
SECTION("the output buffer of a small value is small")
|
||||
{
|
||||
// an escaped key after the value: its node lies in the arena, so the
|
||||
// source extent of the value cannot be read from the next node
|
||||
const std::string big(100000, 'a');
|
||||
const std::string text = R"({"small":1,"list":[1,2,3],"k\n":")" + big + R"("})";
|
||||
const json_document d = json_document::parse(text);
|
||||
const auto small = d.root()["small"].dump();
|
||||
CHECK(small == "1");
|
||||
CHECK(small.capacity() < 4096);
|
||||
const auto list = d.root()["list"].dump();
|
||||
CHECK(list == "[1,2,3]");
|
||||
CHECK(list.capacity() < 4096);
|
||||
CHECK(d.root()["list"].dump(2).capacity() < 4096);
|
||||
|
||||
// the whole document and the large value are unaffected
|
||||
CHECK(d.root().dump() == ordered_json::parse(text).dump());
|
||||
CHECK(d.root()["k\n"].dump() == "\"" + big + "\"");
|
||||
}
|
||||
|
||||
SECTION("output that outgrows the estimate")
|
||||
{
|
||||
// ensure_ascii writes six bytes for each two-byte character
|
||||
std::string chars;
|
||||
for (int i = 0; i < 5000; ++i)
|
||||
{
|
||||
chars += "\xC3\xA9";
|
||||
}
|
||||
const json_document d = json_document::parse("{\"a\":\"" + chars + R"(","k\n":1})");
|
||||
const json expected = json::parse("\"" + chars + "\"");
|
||||
CHECK(d.root()["a"].dump(-1, ' ', true) == expected.dump(-1, ' ', true));
|
||||
CHECK(d.root()["a"].dump(-1, ' ', true).size() == 2 + 5000 * 6);
|
||||
}
|
||||
|
||||
SECTION("streams and discarded views")
|
||||
@@ -1293,7 +1617,9 @@ TEST_CASE("json_view comparison")
|
||||
{
|
||||
const auto same = [](const char* x, const char* y)
|
||||
{
|
||||
return json_document::parse(x).root() == json_document::parse(y).root();
|
||||
const json_document dx = json_document::parse(x);
|
||||
const json_document dy = json_document::parse(y);
|
||||
return dx.root() == dy.root();
|
||||
};
|
||||
CHECK(same("1", "1.0"));
|
||||
CHECK(same("[1, -1, 2.5]", "[1.0, -1.0, 25e-1]"));
|
||||
@@ -1308,15 +1634,20 @@ TEST_CASE("json_view comparison")
|
||||
CHECK(same("\"\\u00e9\"", "\"\xc3\xa9\""));
|
||||
CHECK(!same("null", "false"));
|
||||
CHECK(!same("[]", "{}"));
|
||||
CHECK(ordered_json_document::parse(R"({"a": 1, "b": 2, "a": 3})").root() == ordered_json_document::parse(R"({"a": 3, "b": 2})").root());
|
||||
CHECK(ordered_json_document::parse(R"({"a": 1, "b": 2})").root() != ordered_json_document::parse(R"({"b": 2, "a": 1})").root());
|
||||
const ordered_json_document dup = ordered_json_document::parse(R"({"a": 1, "b": 2, "a": 3})");
|
||||
const ordered_json_document last = ordered_json_document::parse(R"({"a": 3, "b": 2})");
|
||||
CHECK(dup.root() == last.root());
|
||||
const ordered_json_document ab = ordered_json_document::parse(R"({"a": 1, "b": 2})");
|
||||
const ordered_json_document ba = ordered_json_document::parse(R"({"b": 2, "a": 1})");
|
||||
CHECK(ab.root() != ba.root());
|
||||
|
||||
// discarded values compare as basic_json's do
|
||||
const json discarded(json::value_t::discarded);
|
||||
CHECK((json_view() == json_view()) == (discarded == discarded)); // NOLINT(readability-container-size-empty): operator== is tested
|
||||
CHECK((json_view() == discarded) == (discarded == discarded));
|
||||
CHECK(!(json_view() == json_document::parse("null").root())); // NOLINT(readability-container-size-empty)
|
||||
CHECK(!(json_document::parse("null").root() == discarded));
|
||||
const json_document null_document = json_document::parse("null");
|
||||
CHECK(!(json_view() == null_document.root())); // NOLINT(readability-container-size-empty)
|
||||
CHECK(!(null_document.root() == discarded));
|
||||
}
|
||||
|
||||
SECTION("deep nesting")
|
||||
@@ -1327,7 +1658,8 @@ TEST_CASE("json_view comparison")
|
||||
CHECK(a.root() == b.root());
|
||||
CHECK(a.root() == json::parse(deep));
|
||||
const std::string other = std::string(100000, '[') + "1" + std::string(100000, ']');
|
||||
CHECK(a.root() != json_document::parse(other).root());
|
||||
const json_document c = json_document::parse(other);
|
||||
CHECK(a.root() != c.root());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1352,20 +1684,223 @@ TEST_CASE("json_view large objects")
|
||||
for (std::size_t i = 0; i < members; ++i)
|
||||
{
|
||||
const std::string key = std::string(i % 23, 'k') + std::to_string(i) + (i % 7 == 0 ? "\n" : "");
|
||||
CHECK(v[key].get<std::size_t>() == i);
|
||||
CHECK(v.contains(key));
|
||||
CHECK(v.find(key).key() == key);
|
||||
CHECK(v.at(key).get<std::size_t>() == i);
|
||||
CHECK(!v.contains(key + "x"));
|
||||
if (key == "k1")
|
||||
{
|
||||
continue; // repeated below: the last member wins
|
||||
}
|
||||
CHECK(v[key].get<std::size_t>() == i);
|
||||
CHECK(v.at(key).get<std::size_t>() == i);
|
||||
}
|
||||
CHECK(v[""].get_string() == "empty key");
|
||||
CHECK(v["k1"].get<int>() == 1); // the first of duplicate keys, as for small objects
|
||||
CHECK(v["k1"].get_string() == "a duplicate of an earlier key"); // the last of duplicate keys, as for small objects
|
||||
CHECK(v.at("k1").get_string() == "a duplicate of an earlier key");
|
||||
CHECK(!v.contains("missing"));
|
||||
CHECK_THROWS_WITH_AS(v.at("missing"), "[json.exception.out_of_range.403] key 'missing' not found", json::out_of_range&);
|
||||
CHECK(v == j);
|
||||
CHECK(v.materialize() == j);
|
||||
}
|
||||
|
||||
SECTION("duplicate keys: the last member wins, with and without a table")
|
||||
{
|
||||
// an object of `total` members: the keys "k0".."k<n-1>" in order, then
|
||||
// three keys repeated twice more (one copy in the middle, one at the
|
||||
// end), and two keys repeated once; the value of a member is its
|
||||
// position, so that the last member of a key can be told apart
|
||||
struct member
|
||||
{
|
||||
std::string key;
|
||||
std::size_t position;
|
||||
};
|
||||
const auto make_members = [](std::size_t total)
|
||||
{
|
||||
std::vector<std::string> keys;
|
||||
for (std::size_t i = 0; i + 8 < total; ++i)
|
||||
{
|
||||
keys.push_back("k" + std::to_string(i));
|
||||
}
|
||||
const std::size_t n = keys.size();
|
||||
const std::array<std::size_t, 3> triple = {{3, 17, n - 1}};
|
||||
const std::array<std::size_t, 2> twice = {{5, n / 2}};
|
||||
std::vector<std::string> ordered = keys;
|
||||
for (const std::size_t i : triple)
|
||||
{
|
||||
ordered.insert(ordered.begin() + static_cast<std::ptrdiff_t>(ordered.size() / 2), keys[i]);
|
||||
}
|
||||
for (const std::size_t i : twice)
|
||||
{
|
||||
ordered.insert(ordered.begin() + static_cast<std::ptrdiff_t>(ordered.size() / 3), keys[i]);
|
||||
}
|
||||
for (const std::size_t i : triple)
|
||||
{
|
||||
ordered.push_back(keys[i]);
|
||||
}
|
||||
std::vector<member> result;
|
||||
for (std::size_t i = 0; i < ordered.size(); ++i)
|
||||
{
|
||||
result.push_back({ordered[i], i});
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
// 100 and 127 members: no table; 128 members and more: a table
|
||||
for (const std::size_t total :
|
||||
{
|
||||
100u, 127u, 128u, 200u, 5000u
|
||||
})
|
||||
{
|
||||
CAPTURE(total)
|
||||
const std::vector<member> members = make_members(total);
|
||||
REQUIRE(members.size() >= total);
|
||||
REQUIRE(members.size() >= 100);
|
||||
std::string text = "{";
|
||||
std::map<std::string, std::size_t> last;
|
||||
for (const member& m : members)
|
||||
{
|
||||
text += (text.size() > 1 ? ",\"" : "\"") + m.key + "\":" + std::to_string(m.position);
|
||||
last[m.key] = m.position;
|
||||
}
|
||||
text += '}';
|
||||
REQUIRE(last.size() < members.size());
|
||||
|
||||
const json_document d = json_document::parse(text);
|
||||
const json_view v = d.root();
|
||||
const json j = json::parse(text);
|
||||
CHECK(v.size() == members.size()); // every occurrence is visited
|
||||
for (const auto& entry : last)
|
||||
{
|
||||
CAPTURE(entry.first)
|
||||
const std::size_t expected = entry.second;
|
||||
CHECK(v[entry.first].get<std::size_t>() == expected);
|
||||
CHECK(v.at(entry.first).get<std::size_t>() == expected);
|
||||
CHECK(v.find(entry.first).value().get<std::size_t>() == expected);
|
||||
CHECK(v.value(entry.first, std::size_t{0}) == expected);
|
||||
CHECK(v.contains(entry.first));
|
||||
CHECK(v.count(entry.first) == 1);
|
||||
const std::string pointer = "/" + entry.first;
|
||||
CHECK(v[json::json_pointer(pointer)].get<std::size_t>() == expected);
|
||||
CHECK(v.at(json::json_pointer(pointer)).get<std::size_t>() == expected);
|
||||
CHECK(v.value(json::json_pointer(pointer), std::size_t{0}) == expected);
|
||||
CHECK(v.contains(json::json_pointer(pointer)));
|
||||
CHECK(j[entry.first].get<std::size_t>() == expected); // as materialize() and parse() keep it
|
||||
}
|
||||
CHECK(!v.contains("k"));
|
||||
CHECK(v["missing"].is_discarded());
|
||||
CHECK(v.materialize() == j);
|
||||
CHECK(v == j);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("colliding keys")
|
||||
{
|
||||
// the hash is not seeded, so keys that all land in one place must not
|
||||
// make the table build quadratic: such an object gets no table and is
|
||||
// searched linearly
|
||||
constexpr std::size_t members = 300;
|
||||
constexpr std::size_t slots = 1024; // the table size for 300 members: the next power of two >= 600
|
||||
std::vector<std::string> colliding;
|
||||
std::vector<std::string> spread;
|
||||
for (std::uint64_t counter = 0; colliding.size() < members || spread.size() < members; ++counter)
|
||||
{
|
||||
std::string key(8, 'a');
|
||||
for (std::uint64_t x = counter, i = 0; i < 8; ++i, x /= 26)
|
||||
{
|
||||
key[i] = static_cast<char>('a' + (x % 26));
|
||||
}
|
||||
const bool lands_in_slot_zero = (nlohmann::detail::view::key_hash(key.data(), key.size()) & (slots - 1)) == 0;
|
||||
if (lands_in_slot_zero && colliding.size() < members)
|
||||
{
|
||||
colliding.push_back(key);
|
||||
}
|
||||
else if (!lands_in_slot_zero && spread.size() < members)
|
||||
{
|
||||
spread.push_back(key);
|
||||
}
|
||||
}
|
||||
|
||||
const auto make_text = [](const std::vector<std::string>& keys)
|
||||
{
|
||||
std::string text = "{";
|
||||
for (std::size_t i = 0; i < keys.size(); ++i)
|
||||
{
|
||||
text += (i != 0 ? ",\"" : "\"") + keys[i] + "\":" + std::to_string(i % 10);
|
||||
}
|
||||
return text + "}";
|
||||
};
|
||||
const std::string colliding_text = make_text(colliding);
|
||||
const std::string spread_text = make_text(spread);
|
||||
|
||||
json_document with_collisions = json_document::parse(colliding_text);
|
||||
json_document without_collisions = json_document::parse(spread_text);
|
||||
for (const auto* pair :
|
||||
{
|
||||
&colliding, &spread
|
||||
})
|
||||
{
|
||||
const json_view v = (pair == &colliding ? with_collisions : without_collisions).root();
|
||||
for (std::size_t i = 0; i < members; ++i)
|
||||
{
|
||||
CAPTURE(i)
|
||||
CHECK(v[(*pair)[i]].get<std::size_t>() == i % 10);
|
||||
CHECK(v.at((*pair)[i]).get<std::size_t>() == i % 10);
|
||||
CHECK(v.find((*pair)[i]).key() == (*pair)[i]);
|
||||
CHECK(!v.contains((*pair)[i] + "x"));
|
||||
}
|
||||
CHECK(!v.contains("missing"));
|
||||
}
|
||||
CHECK(with_collisions.root() == json::parse(colliding_text));
|
||||
|
||||
// only the object with the spread keys got a table (both texts have the
|
||||
// same length, so the tables are the only difference)
|
||||
with_collisions.shrink_to_fit();
|
||||
without_collisions.shrink_to_fit();
|
||||
CHECK(without_collisions.memory_usage() >= with_collisions.memory_usage() + (slots * sizeof(std::uint32_t)));
|
||||
}
|
||||
|
||||
SECTION("shrink_to_fit releases the tables' spare capacity")
|
||||
{
|
||||
const auto make_text = [](int objects, int members)
|
||||
{
|
||||
std::string text = "[";
|
||||
for (int object = 0; object < objects; ++object)
|
||||
{
|
||||
text += object != 0 ? ",{" : "{";
|
||||
for (int i = 0; i < members + object; ++i)
|
||||
{
|
||||
text += (i != 0 ? ",\"" : "\"") + std::to_string(i) + "\":" + std::to_string(i);
|
||||
}
|
||||
text += '}';
|
||||
}
|
||||
return text + "]";
|
||||
};
|
||||
const std::string small_text = make_text(5, 150);
|
||||
const std::string big_text = make_text(40, 400);
|
||||
|
||||
// reading a big text, and then a small one, leaves the spare capacity
|
||||
// of the big one: shrink_to_fit() brings the document to the size of
|
||||
// one parsed from the small text alone
|
||||
json_document d = json_document::parse(big_text);
|
||||
const std::size_t big = d.memory_usage();
|
||||
d.read(small_text);
|
||||
CHECK(d.memory_usage() >= big);
|
||||
d.shrink_to_fit();
|
||||
json_document fresh = json_document::parse(small_text);
|
||||
fresh.shrink_to_fit();
|
||||
CHECK(d.memory_usage() == fresh.memory_usage());
|
||||
CHECK(d.memory_usage() < big / 2);
|
||||
CHECK(d.root() == json::parse(small_text));
|
||||
for (int object = 0; object < 5; ++object)
|
||||
{
|
||||
const json_view v = d.root()[static_cast<std::size_t>(object)];
|
||||
for (int i = 0; i < 150 + object; ++i)
|
||||
{
|
||||
CHECK(v[std::to_string(i)].get<int>() == i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("nested, reused, and in arrays")
|
||||
{
|
||||
std::string inner = "{";
|
||||
|
||||
@@ -20,8 +20,10 @@ using nlohmann::json;
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <new>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
@@ -506,3 +508,89 @@ TEST_CASE("json_view builder: strings across vector blocks")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("json_view node integer bits")
|
||||
{
|
||||
using nlohmann::detail::view::integer_bits;
|
||||
using nlohmann::detail::view::set_integer_bits;
|
||||
|
||||
// an integer lives in len (low half) and next (high half), on any byte
|
||||
// order; a big-endian target must not store the native word over both
|
||||
SECTION("set_integer_bits and integer_bits")
|
||||
{
|
||||
node n = {};
|
||||
for (const std::uint64_t v :
|
||||
{
|
||||
std::uint64_t{0}, std::uint64_t{1}, std::uint64_t{0xFFFFFFFFu}, std::uint64_t{0x100000000u},
|
||||
std::uint64_t{0x0000000200000003u}, std::uint64_t{0x0123456789ABCDEFu}, std::uint64_t{0xFFFFFFFFFFFFFFFEu}
|
||||
})
|
||||
{
|
||||
CAPTURE(v)
|
||||
n.kind = 0x5A;
|
||||
n.flags = 0xA5;
|
||||
n.extra = 0x1234;
|
||||
n.off = 0x89ABCDEFu;
|
||||
set_integer_bits(n, v);
|
||||
CHECK(integer_bits(n) == v);
|
||||
CHECK(n.len == static_cast<std::uint32_t>(v));
|
||||
CHECK(n.next == static_cast<std::uint32_t>(v >> 32));
|
||||
// the other fields are untouched
|
||||
CHECK(n.kind == 0x5A);
|
||||
CHECK(n.flags == 0xA5);
|
||||
CHECK(n.extra == 0x1234);
|
||||
CHECK(n.off == 0x89ABCDEFu);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("parsed integers")
|
||||
{
|
||||
struct integer_case
|
||||
{
|
||||
const char* text;
|
||||
std::uint64_t bits;
|
||||
};
|
||||
for (const integer_case c :
|
||||
{
|
||||
integer_case{"[8589934595]", 0x0000000200000003u}, integer_case{"[4294967296]", 0x100000000u}, integer_case{"[4294967295]", 0xFFFFFFFFu},
|
||||
integer_case{"[-2]", 0xFFFFFFFFFFFFFFFEu}, integer_case{"[-4294967297]", 0xFFFFFFFEFFFFFFFFu}, integer_case{"[18446744073709551615]", 0xFFFFFFFFFFFFFFFFu},
|
||||
integer_case{"[7]", 7u}
|
||||
})
|
||||
{
|
||||
CAPTURE(c.text)
|
||||
const built b = build(c.text, false, false, true);
|
||||
REQUIRE(b.ok);
|
||||
const node& n = b.data->tape[1];
|
||||
CHECK(integer_bits(n) == c.bits);
|
||||
CHECK(n.len == static_cast<std::uint32_t>(c.bits));
|
||||
CHECK(n.next == static_cast<std::uint32_t>(c.bits >> 32));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("json_view node array size limit")
|
||||
{
|
||||
// a node array larger than the address space is refused, not wrapped to a
|
||||
// small allocation (the size computation overflows on 32-bit targets, and
|
||||
// for absurd counts everywhere)
|
||||
std::unique_ptr<document_data, document_data::deleter> d(document_data::create(0));
|
||||
d->reserve(8);
|
||||
REQUIRE(d->tape_cap >= 8);
|
||||
d->tape_size = 2;
|
||||
const std::size_t cap = d->tape_cap;
|
||||
node* const tape = d->tape;
|
||||
|
||||
#if !defined(JSON_NOEXCEPTION)
|
||||
const std::size_t too_many = document_data::max_nodes() + 1;
|
||||
CHECK_THROWS_AS(d->reserve(too_many), std::bad_alloc&);
|
||||
CHECK_THROWS_AS(d->reserve((std::numeric_limits<std::size_t>::max)()), std::bad_alloc&);
|
||||
// the array is unchanged
|
||||
CHECK(d->tape == tape);
|
||||
CHECK(d->tape_cap == cap);
|
||||
CHECK(d->tape_size == 2);
|
||||
#endif
|
||||
|
||||
// the largest count that fits is not refused by the check (nothing is
|
||||
// allocated for a count that is already there)
|
||||
d->reserve(cap);
|
||||
CHECK(d->tape == tape);
|
||||
}
|
||||
@@ -181,6 +181,104 @@ void compare(const ordered_json_editable_view& v, const ordered_json& j)
|
||||
}
|
||||
}
|
||||
|
||||
// the text of j, in which some objects repeat a key of theirs: before their
|
||||
// members (the real member is then the last), or after (the repeat is)
|
||||
std::string text_with_duplicates(const ordered_json& j)
|
||||
{
|
||||
if (j.is_object())
|
||||
{
|
||||
std::vector<std::string> keys;
|
||||
for (const auto& kv : j.items())
|
||||
{
|
||||
keys.push_back(kv.key());
|
||||
}
|
||||
const auto repeated = [&keys]()
|
||||
{
|
||||
return ordered_json(keys[static_cast<std::size_t>(r(static_cast<int>(keys.size())))]).dump() + ":" + random_value(2).dump();
|
||||
};
|
||||
std::string text = "{";
|
||||
if (!keys.empty() && r(4) == 0)
|
||||
{
|
||||
text += repeated() + ",";
|
||||
}
|
||||
bool first = true;
|
||||
for (const auto& kv : j.items())
|
||||
{
|
||||
text += (first ? "" : ",") + ordered_json(kv.key()).dump() + ":" + text_with_duplicates(kv.value());
|
||||
first = false;
|
||||
}
|
||||
for (int i = keys.empty() ? 0 : r(3); i > 0; --i)
|
||||
{
|
||||
text += "," + repeated();
|
||||
}
|
||||
return text + "}";
|
||||
}
|
||||
if (j.is_array())
|
||||
{
|
||||
std::string text = "[";
|
||||
for (std::size_t i = 0; i < j.size(); ++i)
|
||||
{
|
||||
text += (i != 0 ? "," : "") + text_with_duplicates(j[i]);
|
||||
}
|
||||
return text + "]";
|
||||
}
|
||||
return j.dump();
|
||||
}
|
||||
|
||||
// every lookup of the edited view finds what j holds, although the view may
|
||||
// have several members for a key (j has the last value, at the position of the
|
||||
// first member: what parse() and materialize() make of it)
|
||||
void check_lookups(const ordered_json_editable_view& v, const ordered_json& j)
|
||||
{
|
||||
REQUIRE(v.type() == j.type());
|
||||
if (j.is_object())
|
||||
{
|
||||
for (const auto& kv : j.items())
|
||||
{
|
||||
const std::string& key = kv.key();
|
||||
const ptr_t ptr = ptr_t() / key;
|
||||
CAPTURE(key)
|
||||
const ordered_json_editable_view m = v[key];
|
||||
REQUIRE(!m.is_discarded());
|
||||
CHECK(m.materialize() == kv.value());
|
||||
CHECK(v.at(key).materialize() == kv.value());
|
||||
CHECK(v[ptr].materialize() == kv.value());
|
||||
CHECK(v.at(ptr).materialize() == kv.value());
|
||||
CHECK(v.contains(key));
|
||||
CHECK(v.contains(ptr));
|
||||
CHECK(v.count(key) == 1);
|
||||
const auto it = v.find(key);
|
||||
REQUIRE(it != v.end());
|
||||
CHECK((*it).materialize() == kv.value());
|
||||
CHECK(v.value(ptr, ordered_json(nullptr)) == kv.value());
|
||||
if (kv.value().is_string())
|
||||
{
|
||||
CHECK(v.value(key, std::string("-")) == kv.value().get<std::string>());
|
||||
}
|
||||
check_lookups(m, kv.value());
|
||||
}
|
||||
CHECK(v["missing#key"].is_discarded());
|
||||
CHECK(!v.contains("missing#key"));
|
||||
CHECK(v.find("missing#key") == v.end());
|
||||
}
|
||||
else if (j.is_array())
|
||||
{
|
||||
for (std::size_t i = 0; i < j.size(); ++i)
|
||||
{
|
||||
check_lookups(v[i], j[i]);
|
||||
check_lookups(v.at(i), j[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// (for documents whose objects repeat keys: dump(), size(), and iteration
|
||||
// list all members, so only the lookups and materialize() are compared)
|
||||
void check_duplicates(const ordered_json_editable_document& d, const ordered_json& j)
|
||||
{
|
||||
CHECK(d.root().materialize() == j);
|
||||
check_lookups(d.root(), j);
|
||||
}
|
||||
|
||||
void check_all(const ordered_json_editable_document& d, const ordered_json& j, bool deep)
|
||||
{
|
||||
const std::string text = d.root().dump();
|
||||
@@ -204,14 +302,16 @@ TEST_CASE("json_view edits: differential")
|
||||
// random edits are applied to an ordered_json_editable_document and to the
|
||||
// ordered_json parse() produces; after every edit both must serialize,
|
||||
// materialize, and read back the same
|
||||
for (int n = 0; n < 150; ++n)
|
||||
for (int n = 0; n < 250; ++n)
|
||||
{
|
||||
// the last 100 documents repeat keys in some of their objects
|
||||
const bool duplicates = n >= 150;
|
||||
ordered_json j = random_value(0);
|
||||
if (r(4) == 0)
|
||||
{
|
||||
j = ordered_json::object({{"a", random_value(1)}, {"b", random_value(1)}});
|
||||
}
|
||||
const std::string text = j.dump(r(2) == 0 ? -1 : 2);
|
||||
const std::string text = duplicates ? text_with_duplicates(j) : j.dump(r(2) == 0 ? -1 : 2);
|
||||
CAPTURE(text)
|
||||
ordered_json_editable_document d = ordered_json_editable_document::parse(text);
|
||||
j = ordered_json::parse(text);
|
||||
@@ -340,7 +440,14 @@ TEST_CASE("json_view edits: differential")
|
||||
}
|
||||
CAPTURE(p.to_string())
|
||||
CAPTURE(op)
|
||||
check_all(d, j, e % 8 == 7 || e == edits - 1);
|
||||
if (duplicates)
|
||||
{
|
||||
check_duplicates(d, j);
|
||||
}
|
||||
else
|
||||
{
|
||||
check_all(d, j, e % 8 == 7 || e == edits - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -517,7 +624,7 @@ TEST_CASE("json_view edits: views and values")
|
||||
SECTION("duplicate keys")
|
||||
{
|
||||
json_editable_document d = json_editable_document::parse(R"({"a": 1, "b": 2, "a": 3})");
|
||||
d.set(d.root(), "a", 4); // the first member is assigned, the others dropped
|
||||
d.set(d.root(), "a", 4); // the last member is assigned (at the position of the first), the others dropped
|
||||
CHECK(d.root().dump() == R"({"a":4,"b":2})");
|
||||
d = json_editable_document::parse(R"({"a": 1, "b": 2, "a": 3})");
|
||||
CHECK(d.erase(d.root(), "a") == 2);
|
||||
@@ -577,3 +684,393 @@ TEST_CASE("json_view edits: views and values")
|
||||
CHECK(d.root().dump() == "[true,false]");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("json_view edits: deeply nested values")
|
||||
{
|
||||
// copying a value into a document must not recurse per nesting level
|
||||
const std::size_t depth = 100000;
|
||||
const std::string brackets = std::string(depth, '[') + std::string(depth, ']');
|
||||
std::string braces;
|
||||
for (std::size_t i = 0; i < depth; ++i)
|
||||
{
|
||||
braces += "{\"a\":";
|
||||
}
|
||||
braces += '1';
|
||||
braces += std::string(depth, '}');
|
||||
|
||||
SECTION("a view of a read-only document")
|
||||
{
|
||||
const json_document source = json_document::parse(brackets);
|
||||
json_editable_document d = json_editable_document::parse("[]");
|
||||
d.push_back(d.root(), source.root());
|
||||
CHECK(d.root().dump() == "[" + brackets + "]");
|
||||
}
|
||||
|
||||
SECTION("a view of an editable document")
|
||||
{
|
||||
const json_editable_document source = json_editable_document::parse(braces);
|
||||
json_editable_document d = json_editable_document::parse("{}");
|
||||
d.set(d.root(), "deep", source.root());
|
||||
CHECK(d.root().dump() == "{\"deep\":" + braces + "}");
|
||||
}
|
||||
|
||||
SECTION("a view of an edited document (values behind links)")
|
||||
{
|
||||
const json_document source = json_document::parse(brackets);
|
||||
json_editable_document edited = json_editable_document::parse("[[]]");
|
||||
edited.push_back(edited.root()[0], source.root());
|
||||
edited.push_back(edited.root(), source.root());
|
||||
json_editable_document d = json_editable_document::parse("null");
|
||||
d.set(d.root(), edited.root());
|
||||
CHECK(d.root().dump() == "[[" + brackets + "]," + brackets + "]");
|
||||
}
|
||||
|
||||
SECTION("a basic_json value")
|
||||
{
|
||||
json deep = json::array();
|
||||
json* inner = &deep;
|
||||
for (std::size_t i = 1; i < depth; ++i)
|
||||
{
|
||||
inner->push_back(json::array());
|
||||
inner = &inner->back();
|
||||
}
|
||||
json_editable_document d = json_editable_document::parse("[]");
|
||||
d.push_back(d.root(), deep);
|
||||
CHECK(d.root().dump() == "[" + brackets + "]");
|
||||
}
|
||||
|
||||
SECTION("a basic_json value with objects")
|
||||
{
|
||||
json deep = 1;
|
||||
for (std::size_t i = 0; i < depth; ++i)
|
||||
{
|
||||
json outer = json::object();
|
||||
outer["a"] = std::move(deep);
|
||||
deep = std::move(outer);
|
||||
}
|
||||
json_editable_document d = json_editable_document::parse("{}");
|
||||
d.set(d.root(), "deep", deep);
|
||||
CHECK(d.root().dump() == "{\"deep\":" + braces + "}");
|
||||
}
|
||||
}
|
||||
|
||||
#if !defined(JSON_NOEXCEPTION)
|
||||
TEST_CASE("json_view edits: pointers below a null value")
|
||||
{
|
||||
// a null value on the way becomes what basic_json makes of it: an array
|
||||
// for "-" and for digits, an object otherwise
|
||||
struct test_case
|
||||
{
|
||||
const char* document;
|
||||
const char* pointer;
|
||||
};
|
||||
const std::array<test_case, 16> cases =
|
||||
{
|
||||
{
|
||||
{R"({"a":null})", "/a/0"},
|
||||
{R"({"a":null})", "/a/-"},
|
||||
{R"({"a":null})", "/a/3"},
|
||||
{R"({"a":null})", "/a/x"},
|
||||
{R"({"a":null})", "/a/+1"},
|
||||
{R"({"a":null})", "/a/01"},
|
||||
{R"({"a":null})", "/a/"},
|
||||
{R"({"a":{"b":null}})", "/a/b/1"},
|
||||
{R"({"a":{"b":null}})", "/a/b/-"},
|
||||
{R"({"a":[null]})", "/a/0/0"},
|
||||
{R"({"a":[null,null]})", "/a/1/k"},
|
||||
{R"([null])", "/0"},
|
||||
{"null", "/0"},
|
||||
{"null", "/-"},
|
||||
{"null", "/k"},
|
||||
{"null", ""},
|
||||
}
|
||||
};
|
||||
for (const test_case& c : cases)
|
||||
{
|
||||
CAPTURE(c.document)
|
||||
CAPTURE(c.pointer)
|
||||
json expected = json::parse(c.document);
|
||||
const std::string error = exception_of_call([&]
|
||||
{
|
||||
expected[json::json_pointer(c.pointer)] = 1;
|
||||
});
|
||||
json_editable_document d = json_editable_document::parse(c.document);
|
||||
if (error.empty())
|
||||
{
|
||||
d.set(json::json_pointer(c.pointer), 1);
|
||||
CHECK(d.root().dump() == expected.dump());
|
||||
CHECK(d.root().materialize() == expected);
|
||||
}
|
||||
else
|
||||
{
|
||||
// the same error, and the document is not changed
|
||||
CHECK(exception_of_call([&] { d.set(json::json_pointer(c.pointer), 1); }) == error);
|
||||
CHECK(d.root().dump() == json::parse(c.document).dump());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("json_view edits: strings of other documents are checked")
|
||||
{
|
||||
// A document borrows the text it was parsed from, and sees later changes
|
||||
// of the text: a way to get ill-formed UTF-8 into a view. Copying it into
|
||||
// an editable document is an error, as for any other string.
|
||||
std::string text = R"({"key":"abc","list":["abc"]})";
|
||||
const json_document source = json_document::parse(text);
|
||||
const auto message_of = [](const std::string & bad)
|
||||
{
|
||||
return exception_of_call([&]
|
||||
{
|
||||
const std::string dumped = json(bad).dump();
|
||||
static_cast<void>(dumped);
|
||||
});
|
||||
};
|
||||
json_editable_document d = json_editable_document::parse("[1]");
|
||||
d.push_back(d.root(), source.root());
|
||||
CHECK(d.root().dump() == R"([1,{"key":"abc","list":["abc"]}])");
|
||||
|
||||
text[text.find("abc") + 1] = '\xC3'; // "a\xC3c"
|
||||
text[text.rfind("abc") + 1] = '\xC3';
|
||||
const std::string bad_value = message_of(std::string("a\xC3" "c"));
|
||||
CHECK(!bad_value.empty());
|
||||
CHECK(exception_of_call([&] { d.push_back(d.root(), source.root()["key"]); }) == bad_value);
|
||||
CHECK(exception_of_call([&] { d.set(d.root()[0], source.root()["list"][0]); }) == bad_value);
|
||||
CHECK(exception_of_call([&] { d.set(d.root(), 0, source.root()["list"]); }) == bad_value);
|
||||
CHECK(exception_of_call([&] { d.set(d.root(), 0, source.root()); }) == bad_value);
|
||||
|
||||
text[text.find("key") + 1] = '\xC3'; // a key is checked as well
|
||||
const std::string bad_key = message_of(std::string("k\xC3" "y"));
|
||||
CHECK(exception_of_call([&] { d.set(d.root(), 0, source.root()); }) == bad_key);
|
||||
CHECK(exception_of_call([&] { d.push_back(d.root(), source.root()); }) == bad_key);
|
||||
|
||||
// nothing of the failed edits is visible
|
||||
CHECK(d.root().dump() == R"([1,{"key":"abc","list":["abc"]}])");
|
||||
}
|
||||
|
||||
TEST_CASE("json_view edits: the size of a text arena")
|
||||
{
|
||||
using nlohmann::detail::view::text_capacity;
|
||||
constexpr std::size_t limit = 0xFFFFFFFFu;
|
||||
// grows by doubling, or to what is needed (plus some room)
|
||||
CHECK(text_capacity(0, 0, 10) == 266);
|
||||
CHECK(text_capacity(1000, 990, 20) == 2000);
|
||||
CHECK(text_capacity(100, 100, 5000) == 5356);
|
||||
// an arena beyond 2 GiB: doubling is clamped to 4 GiB - 1
|
||||
CHECK(text_capacity(0x90000000u, 0x8FFFFFFFu, 2) == limit);
|
||||
CHECK(text_capacity(limit, limit - 10, 10) == limit);
|
||||
// exactly what fits is accepted, without room to spare
|
||||
CHECK(text_capacity(100, 90, limit - 90) == limit);
|
||||
CHECK(text_capacity(limit - 100, limit - 100, 100) == limit);
|
||||
// what does not fit is an error
|
||||
CHECK_THROWS_WITH_AS(text_capacity(100, 90, limit - 89), "[json.exception.out_of_range.416] edits of 4 GiB or more are not supported by json_document", json::out_of_range&);
|
||||
CHECK_THROWS_WITH_AS(text_capacity(limit, limit, 1), "[json.exception.out_of_range.416] edits of 4 GiB or more are not supported by json_document", json::out_of_range&);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_CASE("json_view edits: replacing arrays and objects by scalars")
|
||||
{
|
||||
// the first assignment switches the parent to links; the later ones do
|
||||
// not need to look for the parent again
|
||||
std::string text = "[";
|
||||
json expected = json::array();
|
||||
for (int i = 0; i < 300; ++i)
|
||||
{
|
||||
text += (i != 0 ? ",[" : "[") + std::to_string(i) + ",{\"k\":" + std::to_string(i) + "}]";
|
||||
expected.push_back(json::array({i, json{{"k", i}}}));
|
||||
}
|
||||
text += ']';
|
||||
json_editable_document d = json_editable_document::parse(text);
|
||||
CHECK(d.root().dump() == expected.dump());
|
||||
for (int i = 0; i < 300; i += 2)
|
||||
{
|
||||
d.set(d.root()[static_cast<std::size_t>(i)], i);
|
||||
expected[static_cast<std::size_t>(i)] = i;
|
||||
}
|
||||
CHECK(d.root().dump() == expected.dump());
|
||||
for (int i = 1; i < 300; i += 2) // (elements that are still in the parsed layout of their parent)
|
||||
{
|
||||
d.set(d.root()[static_cast<std::size_t>(i)][1], "x"); // replaces an object
|
||||
expected[static_cast<std::size_t>(i)][1] = "x";
|
||||
}
|
||||
CHECK(d.root().dump() == expected.dump());
|
||||
|
||||
// new values, and values in new values
|
||||
d.push_back(d.root(), json::parse(R"([[1,2],{"a":[3]}])"));
|
||||
expected.push_back(json::parse(R"([[1,2],{"a":[3]}])"));
|
||||
d.set(d.root()[300][0], 7);
|
||||
expected[300][0] = 7;
|
||||
d.insert(d.root(), 0, json::array({1, 2}));
|
||||
expected.insert(expected.begin(), json::array({1, 2}));
|
||||
d.set(d.root()[0], nullptr);
|
||||
expected[0] = nullptr;
|
||||
d.set(d.root()[301], 5);
|
||||
expected[301] = 5;
|
||||
CHECK(d.root().dump() == expected.dump());
|
||||
CHECK(d.root().materialize() == expected);
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
// reads of "a" in an object that repeats it: the last member wins, however the
|
||||
// object is stored
|
||||
template<typename View>
|
||||
void check_last_wins(const View& o, int expected)
|
||||
{
|
||||
CAPTURE(expected)
|
||||
CHECK(o["a"].template get<int>() == expected);
|
||||
CHECK(o.at("a").template get<int>() == expected);
|
||||
CHECK(o.find("a")->template get<int>() == expected);
|
||||
CHECK(o.value("a", -1) == expected);
|
||||
CHECK(o[json::json_pointer("/a")].template get<int>() == expected);
|
||||
CHECK(o.at(json::json_pointer("/a")).template get<int>() == expected);
|
||||
CHECK(o.value(json::json_pointer("/a"), -1) == expected);
|
||||
CHECK(o.contains("a"));
|
||||
CHECK(o.contains(json::json_pointer("/a")));
|
||||
CHECK(o.count("a") == 1);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("json_view edits: duplicate keys")
|
||||
{
|
||||
SECTION("an object in its parsed layout, then moved by edits")
|
||||
{
|
||||
json_editable_document d = json_editable_document::parse(R"({"a": 1, "b": 2, "a": 3})");
|
||||
check_last_wins(d.root(), 3);
|
||||
CHECK(d.root().size() == 3);
|
||||
CHECK(d.root().dump() == R"({"a":1,"b":2,"a":3})");
|
||||
|
||||
// an edit of a value does not move the object
|
||||
d.set(d.root()["b"], 5);
|
||||
check_last_wins(d.root(), 3);
|
||||
d.set(d.root()["a"], 4); // the member that reads find
|
||||
check_last_wins(d.root(), 4);
|
||||
CHECK(d.root().dump() == R"({"a":1,"b":5,"a":4})");
|
||||
|
||||
// an appended member moves it
|
||||
d.set(d.root(), "c", true);
|
||||
check_last_wins(d.root(), 4);
|
||||
CHECK(d.root().size() == 4);
|
||||
CHECK(d.root().dump() == R"({"a":1,"b":5,"a":4,"c":true})");
|
||||
d.set(d.root()["a"], 6);
|
||||
check_last_wins(d.root(), 6);
|
||||
CHECK(d.root().dump() == R"({"a":1,"b":5,"a":6,"c":true})");
|
||||
d.set(json::json_pointer("/a"), 7);
|
||||
check_last_wins(d.root(), 7);
|
||||
}
|
||||
|
||||
SECTION("set assigns the member that reads find")
|
||||
{
|
||||
// the key keeps the position of its first occurrence (as in materialize()), the later members are dropped
|
||||
ordered_json_editable_document d = ordered_json_editable_document::parse(R"({"a": 1, "b": 2, "a": 3, "c": 4, "a": 5})");
|
||||
const ordered_json_editable_view held = d.root()["a"];
|
||||
CHECK(held.get<int>() == 5);
|
||||
const ordered_json_editable_view assigned = d.set(d.root(), "a", "x");
|
||||
CHECK(held.get<std::string>() == "x");
|
||||
CHECK(assigned.get<std::string>() == "x");
|
||||
CHECK(d.root().dump() == R"({"a":"x","b":2,"c":4})");
|
||||
CHECK(d.root().materialize() == ordered_json::parse(R"({"a": "x", "b": 2, "c": 4})"));
|
||||
CHECK(d.root().size() == 3);
|
||||
|
||||
// as for the object that parse() makes of the text
|
||||
ordered_json j = ordered_json::parse(R"({"a": 1, "b": 2, "a": 3, "c": 4, "a": 5})");
|
||||
j["a"] = "x";
|
||||
CHECK(d.root().materialize().dump() == j.dump());
|
||||
|
||||
// through a pointer, below a duplicate
|
||||
d = ordered_json_editable_document::parse(R"({"a": {"x": 1}, "b": 2, "a": {"x": 3}})");
|
||||
d.set(ptr_t("/a/x"), 4);
|
||||
CHECK(d.root().dump() == R"({"a":{"x":1},"b":2,"a":{"x":4}})");
|
||||
d.set(ptr_t("/a"), 0);
|
||||
CHECK(d.root().dump() == R"({"a":0,"b":2})");
|
||||
}
|
||||
|
||||
SECTION("erase removes every member")
|
||||
{
|
||||
json_editable_document d = json_editable_document::parse(R"({"a": 1, "b": 2, "a": 3})");
|
||||
d.set(d.root(), "c", 4);
|
||||
check_last_wins(d.root(), 3);
|
||||
CHECK(d.erase(d.root(), "a") == 2);
|
||||
CHECK(d.root().dump() == R"({"b":2,"c":4})");
|
||||
CHECK(!d.root().contains("a"));
|
||||
CHECK(d.root()["a"].is_discarded());
|
||||
CHECK(d.erase(d.root(), "a") == 0);
|
||||
d = json_editable_document::parse(R"({"a": 1, "b": 2, "a": 3})");
|
||||
CHECK(d.erase(json::json_pointer("/a")) == 2);
|
||||
CHECK(d.root().dump() == R"({"b":2})");
|
||||
}
|
||||
|
||||
SECTION("a large object with an index")
|
||||
{
|
||||
std::string text = R"({"a":0,"k7":"first")";
|
||||
for (int i = 0; i < 200; ++i)
|
||||
{
|
||||
text += ",\"k" + std::to_string(i) + "\":" + std::to_string(i);
|
||||
}
|
||||
text += R"(,"a":1,"k7":"last","a":2})";
|
||||
json_editable_document d = json_editable_document::parse(text);
|
||||
check_last_wins(d.root(), 2);
|
||||
CHECK(d.root()["k7"].get_string() == "last");
|
||||
CHECK(d.root()["k199"].get<int>() == 199);
|
||||
|
||||
// a value assigned in place: the index stays in use
|
||||
d.set(d.root()["a"], 3);
|
||||
check_last_wins(d.root(), 3);
|
||||
d.set(d.root()["k7"], "changed");
|
||||
CHECK(d.root()["k7"].get_string() == "changed");
|
||||
CHECK(d.root()["k199"].get<int>() == 199);
|
||||
|
||||
// an appended member moves the members: the lookup scans them
|
||||
d.set(d.root(), "new", 1);
|
||||
check_last_wins(d.root(), 3);
|
||||
CHECK(d.root()["k7"].get_string() == "changed");
|
||||
CHECK(d.root()["k7"].get_string() == d.root().at("k7").get_string());
|
||||
CHECK(d.root()["k199"].get<int>() == 199);
|
||||
CHECK(d.root()["new"].get<int>() == 1);
|
||||
|
||||
// a value replaced by a container, in an object that was not moved
|
||||
d = json_editable_document::parse(text);
|
||||
d.set(d.root()["a"], json{{"x", 1}});
|
||||
CHECK(d.root()["a"]["x"].get<int>() == 1);
|
||||
CHECK(d.root()["k7"].get_string() == "last");
|
||||
CHECK(d.erase(d.root(), "k7") == 3); // (the key occurs three times)
|
||||
CHECK(d.root()["k7"].is_discarded());
|
||||
CHECK(d.root()["a"]["x"].get<int>() == 1);
|
||||
}
|
||||
|
||||
SECTION("objects in moved arrays and in new values")
|
||||
{
|
||||
json_editable_document d = json_editable_document::parse(R"([{"a": 1, "a": 2}, {"a": 3, "b": 0, "a": 4}])");
|
||||
check_last_wins(d.root()[0], 2);
|
||||
check_last_wins(d.root()[1], 4);
|
||||
d.insert(d.root(), 0, json::parse(R"({"a": 0})"));
|
||||
d.push_back(d.root(), json::parse(R"({"a": 5, "a": 6})")); // (a basic_json value has one member)
|
||||
check_last_wins(d.root()[1], 2);
|
||||
check_last_wins(d.root()[2], 4);
|
||||
CHECK(d.root()[3]["a"].get<int>() == 6);
|
||||
d.set(d.root()[1], "a", 8);
|
||||
check_last_wins(d.root()[1], 8);
|
||||
CHECK(d.root()[1].dump() == R"({"a":8})");
|
||||
d.erase(d.root(), 0);
|
||||
check_last_wins(d.root()[1], 4);
|
||||
CHECK(d.root().dump() == R"([{"a":8},{"a":3,"b":0,"a":4},{"a":6}])");
|
||||
}
|
||||
|
||||
SECTION("values of other documents")
|
||||
{
|
||||
const json_document source = json_document::parse(R"({"list": [{"a": 1, "a": 2}], "o": {"b": {"a": 3, "a": 4}, "a": 5, "a": 6}})");
|
||||
json_editable_document d = json_editable_document::parse("{}");
|
||||
d.set(d.root(), "copy", source.root()["list"]);
|
||||
d.set(d.root(), "o", source.root()["o"]);
|
||||
// (the copies keep the repeated members)
|
||||
CHECK(d.root().dump() == R"({"copy":[{"a":1,"a":2}],"o":{"b":{"a":3,"a":4},"a":5,"a":6}})");
|
||||
check_last_wins(d.root()["copy"][0], 2);
|
||||
check_last_wins(d.root()["o"]["b"], 4);
|
||||
check_last_wins(d.root()["o"], 6);
|
||||
d.set(d.root()["o"]["b"], "c", 0);
|
||||
check_last_wins(d.root()["o"]["b"], 4);
|
||||
d.set(d.root()["o"], "d", 0);
|
||||
check_last_wins(d.root()["o"], 6);
|
||||
CHECK(d.root()["o"]["b"]["a"].get<int>() == 4);
|
||||
CHECK(d.root()["o"]["d"].get<int>() == 0);
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ using nlohmann::ordered_json_editable_document;
|
||||
using image_check = json_document::image_check;
|
||||
using nlohmann::detail::view::node;
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
@@ -61,6 +62,14 @@ std::string read_file(const std::string& name)
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
// the dump of a loaded image, through a named document (root() of a temporary
|
||||
// document does not compile, and its views would dangle)
|
||||
std::string loaded_dump(const std::vector<std::uint8_t>& image, image_check check = image_check::full)
|
||||
{
|
||||
const json_document d = json_document::load(image, check);
|
||||
return d.root().dump();
|
||||
}
|
||||
|
||||
// the offsets of the parts of an image
|
||||
constexpr std::size_t header_size = 64;
|
||||
|
||||
@@ -191,7 +200,8 @@ TEST_CASE("json_view images: round trips")
|
||||
const json_document d = json_document::parse(text);
|
||||
check_round_trip(d);
|
||||
// what a loaded document reads is what parse() produces
|
||||
CHECK(json_document::load(d.save()).root().materialize() == json::parse(text));
|
||||
const json_document l = json_document::load(d.save());
|
||||
CHECK(l.root().materialize() == json::parse(text));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -231,7 +241,7 @@ TEST_CASE("json_view images: round trips")
|
||||
{
|
||||
CHECK(l.root()["k" + std::to_string(i)] == d.root()["k" + std::to_string(i)]);
|
||||
}
|
||||
CHECK(l.root()["k7"].get<int>() == 7); // the first of duplicate keys
|
||||
CHECK(l.root()["k7"] == "a duplicate"); // the last of duplicate keys
|
||||
CHECK(l.root()["inner"]["m199"].get<int>() == -199);
|
||||
CHECK(!l.root().contains("k1000"));
|
||||
// the index is not part of the image
|
||||
@@ -240,6 +250,155 @@ TEST_CASE("json_view images: round trips")
|
||||
// the nodes of objects in the image do not carry the number of an index
|
||||
CHECK(node_at(image, 0).extra == 0);
|
||||
}
|
||||
|
||||
SECTION("duplicate keys of a large object: lookups return the last member")
|
||||
{
|
||||
std::string text = "{";
|
||||
for (int i = 0; i < 200; ++i)
|
||||
{
|
||||
text += (i != 0 ? ",\"k" : "\"k") + std::to_string(i) + "\":" + std::to_string(i);
|
||||
}
|
||||
// three members for k5 (the third is the last), and duplicates of k0 and k199
|
||||
text += R"(,"k5":"two","k0":null,"k199":[],"k5":"three"})";
|
||||
const json_document d = json_document::parse(text);
|
||||
REQUIRE(d.root().size() == 204);
|
||||
CHECK(d.root()["k5"] == "three");
|
||||
const std::vector<std::uint8_t> image = d.save();
|
||||
for (const image_check check :
|
||||
{
|
||||
image_check::full, image_check::bounds, image_check::none
|
||||
})
|
||||
{
|
||||
const json_document l = json_document::load(image, check);
|
||||
CHECK(l.root().size() == 204);
|
||||
CHECK(l.root()["k5"] == "three");
|
||||
CHECK(l.root().at("k5") == "three");
|
||||
CHECK(l.root().find("k5").value() == "three");
|
||||
CHECK(l.root()["k0"].is_null());
|
||||
CHECK(l.root()["k199"] == json::array());
|
||||
CHECK(l.root()["k100"] == 100);
|
||||
CHECK(l.root().materialize() == d.root().materialize());
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("objects with colliding keys")
|
||||
{
|
||||
// the hash is not seeded: keys can be found that land in one slot of
|
||||
// a table (see json_view: "colliding keys")
|
||||
const auto keys_for = [](std::size_t slots, std::size_t colliding_count, std::size_t spread_count)
|
||||
{
|
||||
std::vector<std::string> colliding;
|
||||
std::vector<std::string> spread;
|
||||
for (std::uint64_t counter = 0; colliding.size() < colliding_count || spread.size() < spread_count; ++counter)
|
||||
{
|
||||
std::string key(8, 'a');
|
||||
for (std::uint64_t x = counter, i = 0; i < 8; ++i, x /= 26)
|
||||
{
|
||||
key[i] = static_cast<char>('a' + (x % 26));
|
||||
}
|
||||
const bool lands_in_slot_zero = (nlohmann::detail::view::key_hash(key.data(), key.size()) & (slots - 1)) == 0;
|
||||
if (lands_in_slot_zero && colliding.size() < colliding_count)
|
||||
{
|
||||
colliding.push_back(key);
|
||||
}
|
||||
else if (!lands_in_slot_zero && spread.size() < spread_count)
|
||||
{
|
||||
spread.push_back(key);
|
||||
}
|
||||
}
|
||||
colliding.insert(colliding.end(), spread.begin(), spread.end());
|
||||
return colliding;
|
||||
};
|
||||
const auto make_text = [](const std::vector<std::string>& keys)
|
||||
{
|
||||
std::string text = "{";
|
||||
for (std::size_t i = 0; i < keys.size(); ++i)
|
||||
{
|
||||
text += (i != 0 ? ",\"" : "\"") + keys[i] + "\":" + std::to_string(i);
|
||||
}
|
||||
return text + "}";
|
||||
};
|
||||
|
||||
// 300 keys in one slot (the table is 1024 slots): the table would
|
||||
// exceed the probe limit, so the object has none and is searched
|
||||
// linearly; 40 of 200 keys in one slot (512 slots): a table with a
|
||||
// long chain
|
||||
const struct
|
||||
{
|
||||
std::size_t slots;
|
||||
std::size_t colliding;
|
||||
std::size_t spread;
|
||||
} cases[] = {{1024, 300, 0}, {512, 40, 160}};
|
||||
for (const auto& c : cases)
|
||||
{
|
||||
CAPTURE(c.slots)
|
||||
std::vector<std::string> keys = keys_for(c.slots, c.colliding, c.spread);
|
||||
const std::size_t members = keys.size();
|
||||
// a duplicate of a colliding key, of the first and of a spread key
|
||||
const std::vector<std::string> duplicated = {keys[c.colliding - 1], keys[0], keys[members - 1]};
|
||||
std::string text = make_text(keys);
|
||||
text.pop_back();
|
||||
for (const std::string& key : duplicated)
|
||||
{
|
||||
text += ",\"" + key + "\":\"last\"";
|
||||
}
|
||||
text += "}";
|
||||
const json_document d = json_document::parse(text);
|
||||
const std::vector<std::uint8_t> image = d.save();
|
||||
for (const image_check check :
|
||||
{
|
||||
image_check::full, image_check::bounds, image_check::none
|
||||
})
|
||||
{
|
||||
const json_document l = json_document::load(image, check);
|
||||
REQUIRE(l.root().size() == members + duplicated.size());
|
||||
for (std::size_t i = 0; i < members; ++i)
|
||||
{
|
||||
CAPTURE(i)
|
||||
const bool duplicate = std::find(duplicated.begin(), duplicated.end(), keys[i]) != duplicated.end();
|
||||
if (duplicate)
|
||||
{
|
||||
CHECK(l.root()[keys[i]] == "last");
|
||||
}
|
||||
else
|
||||
{
|
||||
CHECK(l.root()[keys[i]] == i);
|
||||
}
|
||||
CHECK(l.root().at(keys[i]) == l.root()[keys[i]]);
|
||||
CHECK(l.root().find(keys[i]).key() == keys[i]);
|
||||
CHECK(!l.root().contains(keys[i] + "x"));
|
||||
}
|
||||
CHECK(!l.root().contains("missing"));
|
||||
CHECK(l.root().materialize() == json::parse(text));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("loading releases the list of large objects")
|
||||
{
|
||||
std::string text = "[";
|
||||
for (int object = 0; object < 400; ++object)
|
||||
{
|
||||
text += object != 0 ? ",{" : "{";
|
||||
for (int i = 0; i < 128; ++i)
|
||||
{
|
||||
text += (i != 0 ? ",\"" : "\"") + std::to_string(i) + "\":" + std::to_string(i);
|
||||
}
|
||||
text += '}';
|
||||
}
|
||||
text += "]";
|
||||
json_document parsed = json_document::parse(text);
|
||||
const std::vector<std::uint8_t> image = parsed.save();
|
||||
json_document loaded = json_document::load(image);
|
||||
CHECK(loaded.root()[399]["127"] == 127);
|
||||
// Parsing and loading build the same tables, and keep nothing else:
|
||||
// not the positions of the objects to index (2 KiB here). The slack
|
||||
// covers the nodes in the header of the document, which are sized
|
||||
// differently.
|
||||
parsed.shrink_to_fit();
|
||||
loaded.shrink_to_fit();
|
||||
CHECK(loaded.memory_usage() <= parsed.memory_usage() + 512);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("json_view images: edited documents")
|
||||
@@ -305,6 +464,153 @@ TEST_CASE("json_view images: edited documents")
|
||||
CHECK(l.root()["list"].size() == 4);
|
||||
}
|
||||
|
||||
SECTION("the targets of links do not reach the image")
|
||||
{
|
||||
// Edits that make an entry of a moved sequence link to a value mark the
|
||||
// value as linked (node_flags::linked). An image has no links and no
|
||||
// such flag: the full and the bounds check reject it.
|
||||
const std::string nested = R"({"a": [1, 2, {"x": [3]}, "s", 1.5, true, null, [4, 5]], "b": {"c": 1, "d": [1, 2, 3], "e": {"f": "g"}}, "h": "str"})";
|
||||
const auto check_image = [](const json_editable_document & d)
|
||||
{
|
||||
const std::vector<std::uint8_t> image = d.save();
|
||||
for (std::size_t i = 0; i < node_count(image); ++i)
|
||||
{
|
||||
CAPTURE(i)
|
||||
CHECK((node_at(image, i).flags & nlohmann::detail::view::node_flags::linked) == 0);
|
||||
CHECK(node_at(image, i).kind != nlohmann::detail::view::kind_link);
|
||||
}
|
||||
for (const image_check check :
|
||||
{
|
||||
image_check::full, image_check::bounds
|
||||
})
|
||||
{
|
||||
CHECK(load_result(image, check).empty());
|
||||
}
|
||||
check_round_trip(d);
|
||||
// a loaded document is edited and saved again
|
||||
json_editable_document e = json_editable_document::load(image);
|
||||
CHECK(e.root().dump() == d.root().dump());
|
||||
e.set(e.root(), "after", 1);
|
||||
CHECK(loaded_dump(e.save()) == e.root().dump());
|
||||
};
|
||||
|
||||
SECTION("a container replaced by a scalar after an earlier edit")
|
||||
{
|
||||
json_editable_document d = json_editable_document::parse(nested);
|
||||
d.set(d.root()["a"][0], 9);
|
||||
d.set(d.root()["a"][2], 5);
|
||||
check_image(d);
|
||||
d.set(d.root()["a"][2], "now a string");
|
||||
check_image(d);
|
||||
d.set(d.root()["a"][7], 0.25);
|
||||
check_image(d);
|
||||
d.set(d.root()["b"]["e"], true);
|
||||
d.set(d.root()["b"]["d"], 7);
|
||||
check_image(d);
|
||||
}
|
||||
|
||||
SECTION("insert and push_back into parsed arrays")
|
||||
{
|
||||
json_editable_document d = json_editable_document::parse(nested);
|
||||
d.push_back(d.root()["a"], 6);
|
||||
check_image(d);
|
||||
d.insert(d.root()["a"], 0, "first");
|
||||
check_image(d);
|
||||
d.insert(d.root()["b"]["d"], 1, json::array({1, 2}));
|
||||
d.push_back(d.root()["b"]["d"], json::object({{"k", nullptr}}));
|
||||
check_image(d);
|
||||
// links to values that are replaced afterwards
|
||||
d.set(d.root()["a"][3], json::object());
|
||||
d.set(d.root()["a"][4], false);
|
||||
d.set(d.root()["a"][5], "replaced");
|
||||
d.set(d.root()["a"][9], 1e300);
|
||||
check_image(d);
|
||||
}
|
||||
|
||||
SECTION("members set in parsed objects")
|
||||
{
|
||||
json_editable_document d = json_editable_document::parse(nested);
|
||||
d.set(d.root(), "new", 1);
|
||||
d.set(d.root()["b"], "c", "changed");
|
||||
d.set(d.root()["b"], "e", json::array({1, {{"z", 2}}}));
|
||||
d.set(d.root()["b"], "more", d.root()["a"]);
|
||||
check_image(d);
|
||||
d.set(d.root()["a"][2], 3); // a link target in a copied subtree
|
||||
d.erase(d.root()["b"], "c");
|
||||
d.set(d.root(), "h", json::object());
|
||||
check_image(d);
|
||||
}
|
||||
|
||||
SECTION("erase")
|
||||
{
|
||||
json_editable_document d = json_editable_document::parse(nested);
|
||||
d.push_back(d.root()["a"], 6);
|
||||
d.erase(d.root()["a"], 0);
|
||||
d.erase(d.root()["a"], 1);
|
||||
d.set(d.root()["a"][0], 1);
|
||||
d.erase(d.root()["b"], "d");
|
||||
check_image(d);
|
||||
}
|
||||
}
|
||||
|
||||
#if !defined(JSON_NOEXCEPTION)
|
||||
SECTION("invalid UTF-8 of a loaded image is not copied into an editable document")
|
||||
{
|
||||
// loading with the bounds check (or none) does not look at the encoding
|
||||
// of strings: dump() of such a view throws, and an editable document
|
||||
// must not take the string over
|
||||
const auto patched = [](const std::string & json_text)
|
||||
{
|
||||
std::vector<std::uint8_t> image = json_document::parse(json_text).save();
|
||||
bool found = false;
|
||||
for (std::size_t i = 0; i + 1 < image.size(); ++i)
|
||||
{
|
||||
if (image[i] == 'Q' && image[i + 1] == 'Z')
|
||||
{
|
||||
image[i] = 0xC3;
|
||||
image[i + 1] = 0x28; // an invalid sequence
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
REQUIRE(found);
|
||||
return image;
|
||||
};
|
||||
|
||||
for (const image_check check :
|
||||
{
|
||||
image_check::bounds, image_check::none
|
||||
})
|
||||
{
|
||||
// as a value
|
||||
{
|
||||
const json_document loaded = json_document::load(patched(R"(["abQZ"])"), check);
|
||||
json_editable_document e = json_editable_document::parse("[]");
|
||||
CHECK_THROWS_WITH_AS(e.push_back(e.root(), loaded.root()[0]), "[json.exception.type_error.316] invalid UTF-8 byte at index 3: 0x28", json::type_error&);
|
||||
CHECK_THROWS_WITH_AS(e.insert(e.root(), 0, loaded.root()[0]), "[json.exception.type_error.316] invalid UTF-8 byte at index 3: 0x28", json::type_error&);
|
||||
CHECK_THROWS_WITH_AS(e.set(e.root(), loaded.root()[0]), "[json.exception.type_error.316] invalid UTF-8 byte at index 3: 0x28", json::type_error&);
|
||||
CHECK_THROWS_WITH_AS(e.push_back(e.root(), loaded.root()), "[json.exception.type_error.316] invalid UTF-8 byte at index 3: 0x28", json::type_error&);
|
||||
// nothing changed
|
||||
CHECK(e.root().dump() == "[]");
|
||||
CHECK(e.root().size() == 0);
|
||||
CHECK(loaded_dump(e.save()) == "[]");
|
||||
// the valid part of the same document can be copied
|
||||
e.push_back(e.root(), loaded.root().size());
|
||||
CHECK(e.root().dump() == "[1]");
|
||||
}
|
||||
// as a key, and in a nested value
|
||||
{
|
||||
const json_document loaded = json_document::load(patched(R"([{"abQZ": 1}, [["x", "QZ"]]])"), check);
|
||||
json_editable_document e = json_editable_document::parse(R"({"keep": [1]})");
|
||||
CHECK_THROWS_WITH_AS(e.set(e.root(), "k", loaded.root()[0]), "[json.exception.type_error.316] invalid UTF-8 byte at index 3: 0x28", json::type_error&);
|
||||
CHECK_THROWS_WITH_AS(e.set(e.root(), "k", loaded.root()[1]), "[json.exception.type_error.316] invalid UTF-8 byte at index 1: 0x28", json::type_error&);
|
||||
CHECK_THROWS_WITH_AS(e.push_back(e.root()["keep"], loaded.root()[1]), "[json.exception.type_error.316] invalid UTF-8 byte at index 1: 0x28", json::type_error&);
|
||||
CHECK(e.root().dump() == R"({"keep":[1]})");
|
||||
CHECK(loaded_dump(e.save()) == R"({"keep":[1]})");
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("the root replaced")
|
||||
{
|
||||
json_editable_document d = json_editable_document::parse(text);
|
||||
@@ -530,6 +836,15 @@ TEST_CASE("json_view images: check")
|
||||
{
|
||||
n.extra = 1; // a hash index
|
||||
}), true);
|
||||
// the flag of the targets of links (editable documents) is not part of an image
|
||||
for (std::size_t i = 0; i < node_count(image); ++i)
|
||||
{
|
||||
CAPTURE(i)
|
||||
rejected(corrupted(image, i, [](node & n)
|
||||
{
|
||||
n.flags = static_cast<std::uint8_t>(n.flags | nlohmann::detail::view::node_flags::linked);
|
||||
}), true);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("bounds")
|
||||
@@ -625,7 +940,7 @@ TEST_CASE("json_view images: check")
|
||||
});
|
||||
CHECK(load_result(as_array, image_check::full).empty());
|
||||
const std::string expected_dump = R"({"s":"x\"y","i":-12,"u":7,"f":1.5e+300,"b":true,"n":null,"a":["t",[]]})";
|
||||
CHECK(json_document::load(as_array).root().dump() == expected_dump);
|
||||
CHECK(loaded_dump(as_array) == expected_dump);
|
||||
}
|
||||
|
||||
SECTION("strings")
|
||||
@@ -680,7 +995,8 @@ TEST_CASE("json_view images: check")
|
||||
n.extra = 0;
|
||||
});
|
||||
CHECK(load_result(positive, image_check::full).empty());
|
||||
CHECK(json_document::load(positive).root()["u"].is_number_integer());
|
||||
const json_document pos = json_document::load(positive);
|
||||
CHECK(pos.root()["u"].is_number_integer());
|
||||
rejected(corrupted(image, 8, [](node & n)
|
||||
{
|
||||
n.kind = 6; // a float token as integer
|
||||
@@ -712,6 +1028,136 @@ TEST_CASE("json_view images: check")
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("float tokens with fewer digits than the layout records")
|
||||
{
|
||||
// The bytes of the token are not digits (they read as zeros or as
|
||||
// other values), so the value has fewer (or more) digits than the
|
||||
// layout says: dump() must still write a number.
|
||||
for (const auto& source : std::vector<std::pair<std::string, std::string>>
|
||||
{
|
||||
{"123456789012345678.5", std::string("@") + std::string(16, '0') + "1.1"}, // 19 digits
|
||||
{"1234.5", "@001.1"}, // 5 digits
|
||||
{"1234.5", "9??.??"} // more than 5 digits
|
||||
})
|
||||
{
|
||||
CAPTURE(source.second)
|
||||
const std::vector<std::uint8_t> img = json_document::parse("[" + source.first + "]").save();
|
||||
const node n = node_at(img, 1);
|
||||
REQUIRE(source.second.size() == n.len);
|
||||
std::vector<std::uint8_t> b = img;
|
||||
std::memcpy(b.data() + text_at(img) + n.off, source.second.data(), n.len);
|
||||
const json_document d = json_document::load(b, image_check::bounds);
|
||||
const std::string dumped = d.root().dump();
|
||||
CAPTURE(dumped)
|
||||
CHECK(json::parse(dumped)[0].is_number());
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("nodes that share a range")
|
||||
{
|
||||
// [big string, then n strings made to point to the big string]: every
|
||||
// node shares the one range (the check reads it once)
|
||||
const std::size_t n = 200;
|
||||
std::string long_text = "[\"" + std::string(5000, 'a') + "\"";
|
||||
for (std::size_t k = 0; k < n; ++k)
|
||||
{
|
||||
long_text += ",\"x\"";
|
||||
}
|
||||
long_text += "]";
|
||||
const std::vector<std::uint8_t> img = json_document::parse(long_text).save();
|
||||
const node big = node_at(img, 1);
|
||||
std::vector<std::uint8_t> b = img;
|
||||
for (std::size_t k = 0; k < n; ++k)
|
||||
{
|
||||
set_node(b, 2 + k, big);
|
||||
}
|
||||
CHECK(load_result(b, image_check::full).empty());
|
||||
const json_document d = json_document::load(b);
|
||||
CHECK(d.root().size() == n + 1);
|
||||
CHECK(d.root()[n].get<std::string>() == std::string(5000, 'a'));
|
||||
CHECK(d.root().dump() == loaded_dump(b, image_check::none));
|
||||
|
||||
// the same for decoded strings and float tokens, with a node that
|
||||
// records another digit layout than its token
|
||||
const std::vector<std::uint8_t> img2 = json_document::parse(R"(["a\"b", "a\"b", 1.25, 1.25, 1.25e3])").save();
|
||||
CHECK(load_result(img2, image_check::full).empty());
|
||||
std::vector<std::uint8_t> same = img2;
|
||||
set_node(same, 2, node_at(img2, 1));
|
||||
set_node(same, 4, node_at(img2, 3));
|
||||
CHECK(load_result(same, image_check::full).empty());
|
||||
CHECK(loaded_dump(same) == R"(["a\"b","a\"b",1.25,1.25,1250.0])");
|
||||
std::vector<std::uint8_t> layout = same;
|
||||
node f4 = node_at(layout, 4);
|
||||
f4.extra = 0x0100u; // the layout of "1.", and not that of "1.25"
|
||||
set_node(layout, 4, f4);
|
||||
rejected(layout, false);
|
||||
f4.extra = 0xFFFFu; // "many" digits: fine, as compaction writes it
|
||||
set_node(layout, 4, f4);
|
||||
CHECK(load_result(layout, image_check::full).empty());
|
||||
}
|
||||
|
||||
SECTION("ranges that overlap")
|
||||
{
|
||||
// save() writes every string and every token to a place of its own
|
||||
// (nodes that share a value share the whole range): ranges that
|
||||
// overlap without being identical are a damaged image, though each
|
||||
// range is a valid string or token
|
||||
// nodes: 0 [ 1 "abcdef" 2 "ghijkl" 3 1.2525 4 9.9 5 "a\"bcd" (decoded) 6 "e\"fgh" (decoded)
|
||||
const std::vector<std::uint8_t> img = json_document::parse(R"(["abcdef", "ghijkl", 1.2525, 9.9, "a\"bcd", "e\"fgh"])").save();
|
||||
REQUIRE(load_result(img, image_check::full).empty());
|
||||
// node i with the range (off of node of + shift, length), and extra
|
||||
const auto aliased = [&](std::size_t i, std::size_t of, std::uint32_t shift, std::uint32_t length, std::uint16_t extra)
|
||||
{
|
||||
std::vector<std::uint8_t> b = img;
|
||||
node n = node_at(b, of);
|
||||
n.off += shift;
|
||||
n.len = length;
|
||||
n.extra = extra;
|
||||
set_node(b, i, n);
|
||||
return b;
|
||||
};
|
||||
// source strings
|
||||
rejected(aliased(2, 1, 0, 4, 0), false); // "abcd": the start of another string
|
||||
rejected(aliased(2, 1, 1, 4, 0), false); // "bcde": inside
|
||||
rejected(aliased(2, 1, 2, 4, 0), false); // "cdef": the end
|
||||
CHECK(load_result(aliased(2, 1, 0, 6, 0), image_check::full).empty()); // the whole range: identical
|
||||
// decoded strings: inside, and partially overlapping (the arena holds a"bcde"fgh)
|
||||
rejected(aliased(6, 5, 1, 3, 0), false);
|
||||
rejected(aliased(6, 5, 3, 5, 0), false);
|
||||
CHECK(load_result(aliased(6, 5, 0, 5, 0), image_check::full).empty());
|
||||
// float tokens: "1.25" and "525" (an integer token as a float) inside "1.2525"
|
||||
rejected(aliased(4, 3, 0, 4, 0x0201u), false);
|
||||
rejected(aliased(4, 3, 3, 3, 0x0003u), false);
|
||||
CHECK(load_result(aliased(4, 3, 0, 6, 0x0401u), image_check::full).empty());
|
||||
// a string and a float token may use the same bytes (each is checked by its own kind)
|
||||
std::vector<std::uint8_t> shared = img;
|
||||
node as_string = node_at(img, 2);
|
||||
as_string.off = node_at(img, 3).off;
|
||||
as_string.len = 6;
|
||||
set_node(shared, 2, as_string);
|
||||
CHECK(load_result(shared, image_check::full).empty());
|
||||
// empty strings inside others are not ranges
|
||||
CHECK(load_result(aliased(2, 1, 2, 0, 0), image_check::full).empty());
|
||||
}
|
||||
|
||||
SECTION("copies within an edited document")
|
||||
{
|
||||
// copies within a document share the value: identical ranges
|
||||
json_editable_document d = json_editable_document::parse(R"({"s": "ab", "e": "x\"y", "f": 1.5, "g": 1.5e300, "a": [1.5, "ab"]})");
|
||||
d.set(d.root(), "c1", d.root()["a"]);
|
||||
d.set(d.root(), "c2", d.root()["a"]);
|
||||
d.set(d.root(), "c3", d.root());
|
||||
d.push_back(d.root()["a"], d.root()["e"]);
|
||||
d.push_back(d.root()["a"], d.root()["g"]);
|
||||
d.push_back(d.root()["a"], d.root()["g"]);
|
||||
d.set(d.root()["s"], "changed");
|
||||
d.set(d.root()["f"], 7.25);
|
||||
const std::vector<std::uint8_t> saved = d.save();
|
||||
CHECK(load_result(saved, image_check::full).empty());
|
||||
CHECK(loaded_dump(saved) == d.root().dump());
|
||||
check_round_trip(d);
|
||||
}
|
||||
|
||||
SECTION("integer ranges")
|
||||
{
|
||||
// tokens of many digits, which the parser stores as floats
|
||||
|
||||
+154
-5
@@ -15,6 +15,7 @@
|
||||
#include <nlohmann/json.hpp>
|
||||
using nlohmann::detail::dtoa_impl::reinterpret_bits;
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
@@ -666,13 +667,24 @@ void check_shortest(double v)
|
||||
const std::string text(buf.data(), end);
|
||||
CAPTURE(text)
|
||||
CHECK(parse_double(text) == v);
|
||||
// the layout is that of format_buffer() for the same digits
|
||||
// the layout is that of format_buffer() for the digits of Zmij
|
||||
const auto sd = nlohmann::detail::zmij::to_shortest(reinterpret_bits<std::uint64_t>(v));
|
||||
const std::uint64_t significand = sd.has_digit ? (sd.integral * 10) + sd.digit : sd.integral;
|
||||
int exponent = sd.has_digit ? sd.exponent : sd.exponent + 1;
|
||||
std::string significand_digits = std::to_string(significand);
|
||||
while (significand_digits.size() > 1 && significand_digits.back() == '0')
|
||||
{
|
||||
significand_digits.pop_back();
|
||||
++exponent;
|
||||
}
|
||||
std::array<char, 64> reference{};
|
||||
int len = 0;
|
||||
int exponent = 0;
|
||||
nlohmann::detail::dtoa_impl::shortest_digits(reference.data(), len, exponent, v);
|
||||
const char* const reference_end = nlohmann::detail::dtoa_impl::format_buffer(reference.data(), len, exponent, -4, 15);
|
||||
std::copy(significand_digits.begin(), significand_digits.end(), reference.begin());
|
||||
const char* const reference_end = nlohmann::detail::dtoa_impl::format_buffer(reference.data(), static_cast<int>(significand_digits.size()), exponent, -4, 15);
|
||||
CHECK(text == std::string(reference.data(), static_cast<std::size_t>(reference_end - reference.data())));
|
||||
// and write_positive() is what to_chars() calls
|
||||
std::array<char, 64> positive{};
|
||||
const char* const positive_end = nlohmann::detail::dtoa_impl::write_positive(positive.data(), positive.data() + positive.size(), v);
|
||||
CHECK(text == std::string(positive.data(), static_cast<std::size_t>(positive_end - positive.data())));
|
||||
const auto de = digits_and_exponent(text);
|
||||
const std::string& digits = de.first;
|
||||
if (digits.size() > 1)
|
||||
@@ -785,3 +797,140 @@ TEST_CASE("shortest digits of doubles")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("choice of the conversion")
|
||||
{
|
||||
using nlohmann::detail::dtoa_impl::is_binary64;
|
||||
|
||||
SECTION("by the format of the type")
|
||||
{
|
||||
// Zmij needs binary64 numbers; everything else uses Grisu2
|
||||
static_assert(!is_binary64<float>::value, "float is not binary64");
|
||||
static_assert(is_binary64<double>::value == (std::numeric_limits<double>::is_iec559 && std::numeric_limits<double>::digits == 53),
|
||||
"double is binary64 where it is IEEE 754 with 53 digits");
|
||||
static_assert(!is_binary64<int>::value, "integers are not binary64");
|
||||
static_assert(is_binary64<long double>::value == (std::numeric_limits<long double>::is_iec559 && std::numeric_limits<long double>::digits == 53 && sizeof(long double) == 8),
|
||||
"long double is binary64 where it has the format of a double");
|
||||
CHECK(!is_binary64<float>::value);
|
||||
CHECK(is_binary64<double>::value);
|
||||
}
|
||||
|
||||
SECTION("float: Grisu2, double: Zmij")
|
||||
{
|
||||
// 5.3165205877497296e+16 is one of the doubles for which Grisu2 does not find the shortest digits
|
||||
constexpr double value = 5.3165205877497296e+16;
|
||||
std::array<char, 64> buf{};
|
||||
const char* const last = buf.data() + buf.size();
|
||||
|
||||
char* end = nlohmann::detail::dtoa_impl::write_positive(buf.data(), last, value);
|
||||
CHECK(std::string(buf.data(), end) == "5.31652058774973e+16");
|
||||
end = nlohmann::detail::dtoa_impl::write_positive_grisu2(buf.data(), last, value);
|
||||
CHECK(std::string(buf.data(), end) == "5.3165205877497296e+16");
|
||||
|
||||
constexpr float f = 1.1754944e-38f;
|
||||
end = nlohmann::detail::dtoa_impl::write_positive(buf.data(), last, f);
|
||||
const std::string dispatched(buf.data(), end);
|
||||
end = nlohmann::detail::dtoa_impl::write_positive_grisu2(buf.data(), last, f);
|
||||
CHECK(dispatched == std::string(buf.data(), end));
|
||||
}
|
||||
|
||||
SECTION("long double with the format of a double: Zmij")
|
||||
{
|
||||
// (on platforms where long double is wider, Grisu2 does not apply either: the snprintf fallback does)
|
||||
if (std::numeric_limits<long double>::digits == 53 && std::numeric_limits<long double>::is_iec559)
|
||||
{
|
||||
using long_double_json = nlohmann::json::with_float_t<long double>;
|
||||
for (const double d :
|
||||
{
|
||||
5.3165205877497296e+16, 1.0, 0.1, 123456.789, 2.2250738585072014e-308, 1.7976931348623157e+308, -5.3165205877497296e+16
|
||||
})
|
||||
{
|
||||
CAPTURE(d)
|
||||
CHECK(long_double_json(static_cast<long double>(d)).dump() == nlohmann::json(d).dump());
|
||||
}
|
||||
CHECK(long_double_json(5.3165205877497296e+16L).dump() == "5.31652058774973e+16");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("short decimals")
|
||||
{
|
||||
// write_short_decimal() writes digits * 10^exp for the digits of a double
|
||||
// that need no conversion (at most 15, the first not 0): as to_chars()
|
||||
// writes the (positive) double that has these digits
|
||||
const auto written = [](std::uint64_t digits, int exp)
|
||||
{
|
||||
std::array<char, 64> buf{}; // (up to 41 bytes are written)
|
||||
char* const end = nlohmann::detail::dtoa_impl::write_short_decimal(buf.data(), digits, exp);
|
||||
return std::string(buf.data(), end);
|
||||
};
|
||||
const auto written_counted = [](std::uint64_t digits, int count, int exp)
|
||||
{
|
||||
std::array<char, 64> buf{};
|
||||
char* const end = nlohmann::detail::dtoa_impl::write_short_decimal(buf.data(), digits, count, exp);
|
||||
return std::string(buf.data(), end);
|
||||
};
|
||||
const auto expected = [](std::uint64_t digits, int exp)
|
||||
{
|
||||
const double value = std::strtod((std::to_string(digits) + "e" + std::to_string(exp)).c_str(), nullptr);
|
||||
std::array<char, 64> buf{};
|
||||
char* const end = nlohmann::detail::to_chars(buf.data(), buf.data() + 32, value);
|
||||
return std::string(buf.data(), end);
|
||||
};
|
||||
|
||||
SECTION("powers of ten")
|
||||
{
|
||||
const auto& powers = nlohmann::detail::dtoa_impl::powers_of_ten_16();
|
||||
std::uint64_t power = 1;
|
||||
for (const std::uint64_t p : powers)
|
||||
{
|
||||
CHECK(p == power);
|
||||
power *= 10;
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("examples")
|
||||
{
|
||||
CHECK(written(1, 0) == "1.0");
|
||||
CHECK(written(15, -1) == "1.5");
|
||||
CHECK(written(125, -2) == "1.25");
|
||||
CHECK(written(1, 22) == "1e+22");
|
||||
CHECK(written(123456789012345, -2) == "1234567890123.45");
|
||||
CHECK(written(999999999999999, -15) == "0.999999999999999");
|
||||
CHECK(written(5, -324) == "5e-324");
|
||||
CHECK(written_counted(1, 1, 0) == "1.0");
|
||||
CHECK(written_counted(125, 3, -2) == "1.25");
|
||||
CHECK(written_counted(100, 3, -2) == "1.0");
|
||||
CHECK(written_counted(999999999999999, 15, -15) == "0.999999999999999");
|
||||
}
|
||||
|
||||
SECTION("random digits, exponents and trailing zeros")
|
||||
{
|
||||
std::mt19937_64 rng(1170); // NOLINT(cert-msc32-c,cert-msc51-cpp,bugprone-random-generator-seed)
|
||||
for (int i = 0; i < 100000; ++i)
|
||||
{
|
||||
// 1 to 15 digits, the first not 0, and up to 14 of them trailing zeros
|
||||
std::uint64_t count = 1 + (rng() % 15u);
|
||||
std::uint64_t power = 1;
|
||||
for (std::uint64_t k = 1; k < count; ++k)
|
||||
{
|
||||
power *= 10;
|
||||
}
|
||||
std::uint64_t digits = power + (rng() % (9 * power));
|
||||
const std::uint64_t zeros = (rng() % 3u == 0) ? (rng() % count) : 0;
|
||||
for (std::uint64_t k = 0; k < zeros; ++k)
|
||||
{
|
||||
digits = (digits / 10) * 10;
|
||||
}
|
||||
// (a value between 1e-300 and 1e300)
|
||||
const int exp = static_cast<int>(rng() % 560u) - 300 - static_cast<int>(count);
|
||||
|
||||
CAPTURE(digits)
|
||||
CAPTURE(count)
|
||||
CAPTURE(exp)
|
||||
const std::string want = expected(digits, exp);
|
||||
CHECK(written(digits, exp) == want);
|
||||
CHECK(written_counted(digits, static_cast<int>(count), exp) == want);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user