mirror of
https://github.com/nlohmann/json.git
synced 2026-09-06 00:08:00 +00:00
test: verify move semantics of byte_container_with_subtype's rvalue constructors (#5423)
The two rvalue-reference constructors were never distinguished from their const-lvalue-reference twins by any test. Add a "move semantics" section that constructs from an rvalue std::vector, checks the resulting container keeps the exact same buffer address as the source (a stronger check than just observing the source ended up empty, since a copy-then-clear could do that too), and confirms the source vector was left empty. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -42,6 +42,39 @@ TEST_CASE("byte_container_with_subtype")
|
||||
CHECK(container.subtype() == static_cast<subtype_type>(-1));
|
||||
}
|
||||
|
||||
SECTION("move semantics")
|
||||
{
|
||||
// the rvalue-reference constructor (without a subtype) must actually move
|
||||
// the passed-in container rather than copy it; comparing the buffer address
|
||||
// before and after is a stronger check than just observing the source is
|
||||
// empty afterward, since a copy-then-clear could also leave it empty
|
||||
{
|
||||
std::vector<std::uint8_t> bytes = {{0xCA, 0xFE, 0xBA, 0xBE}};
|
||||
const auto* const data_ptr = bytes.data();
|
||||
|
||||
nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> container(std::move(bytes));
|
||||
|
||||
CHECK(container.size() == 4);
|
||||
CHECK(container.data() == data_ptr);
|
||||
CHECK(!container.has_subtype());
|
||||
CHECK(bytes.empty()); // NOLINT(bugprone-use-after-move,clang-analyzer-cplusplus.Move,hicpp-invalid-access-moved)
|
||||
}
|
||||
|
||||
// same check for the rvalue-reference constructor that also takes a subtype
|
||||
{
|
||||
std::vector<std::uint8_t> bytes = {{0xCA, 0xFE, 0xBA, 0xBE}};
|
||||
const auto* const data_ptr = bytes.data();
|
||||
|
||||
nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> container(std::move(bytes), 42);
|
||||
|
||||
CHECK(container.size() == 4);
|
||||
CHECK(container.data() == data_ptr);
|
||||
CHECK(container.has_subtype());
|
||||
CHECK(container.subtype() == 42);
|
||||
CHECK(bytes.empty()); // NOLINT(bugprone-use-after-move,clang-analyzer-cplusplus.Move,hicpp-invalid-access-moved)
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("comparisons")
|
||||
{
|
||||
std::vector<std::uint8_t> const bytes = {{0xCA, 0xFE, 0xBA, 0xBE}};
|
||||
|
||||
Reference in New Issue
Block a user