Tests: cover the remaining lines of the library headers

Add tests for the lines of include/nlohmann that were not covered: the
binary readers and writers, the lexer and locale paths, the json_view
modules, the custom number, key and array types, json_pointer, json_patch,
diff and the iterators.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-11-01 22:32:42 +01:00
1 parent d7b9472173
commit 056ba3b7c1
33 files changed
+1627 -1

No files matched your search

+46
View File
@@ -19,11 +19,13 @@
using nlohmann::json;
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <fstream>
#include <istream>
#include <limits>
#include <random>
#include <sstream>
#include <streambuf>
@@ -93,6 +95,25 @@ class chunked_streambuf : public std::streambuf
std::size_t m_unbuffered;
};
#if TEST_STREAM_BULK_SCAN
// A streambuf that claims a get area of several gigabytes, of which only the
// first bytes exist. It is only used to move the read position of the adapter,
// never to read beyond them, so that skipping more than gbump() can bump at
// once can be tested without allocating that much.
class oversized_get_area_streambuf : public std::streambuf
{
public:
explicit oversized_get_area_streambuf(std::size_t claimed_size)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
setg(m_data.data(), m_data.data(), m_data.data() + claimed_size);
}
private:
std::array<char, 8> m_data{{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}};
};
#endif
// how a test reads from the stream
enum class read_mode
{
@@ -433,6 +454,31 @@ TEST_CASE("parsing from a std::istream scans the get area in bulk")
ia.copy_token(token, false);
CHECK(token.empty());
}
SECTION("skipping more characters than gbump() takes at once")
{
// gbump() takes an int, but a get area can be larger
if (sizeof(std::size_t) < 8)
{
MESSAGE("a get area larger than an int needs a 64-bit size_t");
}
else
{
const std::size_t claimed = std::size_t{3} << 30u;
oversized_get_area_streambuf buf(claimed);
std::istream is(&buf);
nlohmann::detail::input_stream_adapter ia(is);
CHECK(ia.get_character() == 'a');
CHECK(ia.bulk_remaining() == claimed - 1);
const std::size_t skipped = static_cast<std::size_t>((std::numeric_limits<int>::max)()) + 10;
ia.bulk_skip(skipped);
CHECK(ia.bulk_remaining() == claimed - 1 - skipped);
ia.bulk_skip(ia.bulk_remaining());
CHECK(ia.bulk_remaining() == 0);
}
}
#endif
SECTION("SAX parsing from a stream")