From c803b1f5346687aa17b2eec84fe8b0955ea32044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8ger=20Hanseg=C3=A5rd?= Date: Fri, 3 Jan 2025 17:40:32 +0100 Subject: [PATCH] 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 (cherry picked from commit 03bd9491491881529ad28cd6d672edfdda9a0065) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit 33f37ff6376835dc8add4e5d8273f3dc5e7b086d) --- .../tools/quniquehandle/tst_quniquehandle.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/auto/corelib/tools/quniquehandle/tst_quniquehandle.cpp b/tests/auto/corelib/tools/quniquehandle/tst_quniquehandle.cpp index 55647f3ee4b..ffcaddb1112 100644 --- a/tests/auto/corelib/tools/quniquehandle/tst_quniquehandle.cpp +++ b/tests/auto/corelib/tools/quniquehandle/tst_quniquehandle.cpp @@ -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{ };