Skip to content

Commit

Permalink
Add regression test for nlohmann#3810 (nlohmann#4608)
Browse files Browse the repository at this point in the history
* ✅ add regression test for nlohmann#3810

Signed-off-by: Niels Lohmann <[email protected]>

* ✅ add regression test for nlohmann#3810

Signed-off-by: Niels Lohmann <[email protected]>

* ✅ add regression test for nlohmann#3810

Signed-off-by: Niels Lohmann <[email protected]>

* ✅ add regression test for nlohmann#3810

Signed-off-by: Niels Lohmann <[email protected]>

* ✅ add regression test for nlohmann#3810

Signed-off-by: Niels Lohmann <[email protected]>

---------

Signed-off-by: Niels Lohmann <[email protected]>
  • Loading branch information
nlohmann authored Feb 15, 2025
1 parent a43350c commit 0b6881a
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tests/src/unit-regression2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,19 @@ inline for_3333::for_3333(const json& j)
: for_3333(j.value("x", 0), j.value("y", 0))
{}

/////////////////////////////////////////////////////////////////////
// for #3810
/////////////////////////////////////////////////////////////////////

struct Example_3810
{
int bla{};

Example_3810() = default;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Example_3810, bla);

TEST_CASE("regression tests 2")
{
SECTION("issue #1001 - Fix memory leak during parser callback")
Expand Down Expand Up @@ -1004,6 +1017,26 @@ TEST_CASE("regression tests 2")
CHECK(p.x == 1);
CHECK(p.y == 2);
}

SECTION("issue #3810 - ordered_json doesn't support construction from C array of custom type")
{
Example_3810 states[45]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)

// fix "not used" warning
states[0].bla = 1;

const auto* const expected = R"([{"bla":1},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0}])";

// This works:
nlohmann::json j;
j["test"] = states;
CHECK(j["test"].dump() == expected);

// This doesn't compile:
nlohmann::ordered_json oj;
oj["test"] = states;
CHECK(oj["test"].dump() == expected);
}
}

DOCTEST_CLANG_SUPPRESS_WARNING_POP

0 comments on commit 0b6881a

Please sign in to comment.