Test that reset() and move assignment handles self-assignment

Calling reset() with the owned handle should maintain ownership and not
leak resources. Move assignment should also handle self-assignment
without leaks. The new tests verifies that this holds true.

Task-number: QTBUG-132507
Change-Id: Icdfa4d67b06b71a74e810593e57449b51982c98c
Reviewed-by: Tim Blechmann <tim.blechmann@qt.io>
(cherry picked from commit 03bd9491491881529ad28cd6d672edfdda9a0065)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 33f37ff6376835dc8add4e5d8273f3dc5e7b086d)
This commit is contained in:
Jøger Hansegård 2025-01-03 17:40:32 +01:00 committed by Qt Cherry-pick Bot
parent b3b245cb74
commit c803b1f534

View File

@ -146,6 +146,15 @@ private slots:
QVERIFY(GlobalResource::isOpen(dest.get()));
}
void moveAssignment_maintainsOwnershipWhenSelfAssigning() const
{
Handle resource{ GlobalResource::open() };
resource = std::move(resource); // NOLINT(clang-diagnostic-self-move)
QVERIFY(resource.isValid());
QVERIFY(GlobalResource::isOpen(resource.get()));
}
void isValid_returnsFalse_onlyWhenHandleIsInvalid() const
{
const Handle invalid;
@ -209,6 +218,17 @@ private slots:
QVERIFY(!h);
}
void reset_maintainsOwnership_whenCalledWithOwnedHandle() const
{
const auto resource = GlobalResource::open();
Handle h{ resource };
h.reset(resource);
QVERIFY(GlobalResource::isOpen(resource));
QVERIFY(h);
}
void reset_doesNothing_whenCalledOnInvalidHandle() const
{
Handle h{ };