diff options
Diffstat (limited to 'tests/auto/corelib/tools/qscopedpointer')
-rw-r--r-- | tests/auto/corelib/tools/qscopedpointer/CMakeLists.txt | 9 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qscopedpointer/tst_qscopedpointer.cpp | 97 |
2 files changed, 58 insertions, 48 deletions
diff --git a/tests/auto/corelib/tools/qscopedpointer/CMakeLists.txt b/tests/auto/corelib/tools/qscopedpointer/CMakeLists.txt index df2d2aa509..7bfcfdebbf 100644 --- a/tests/auto/corelib/tools/qscopedpointer/CMakeLists.txt +++ b/tests/auto/corelib/tools/qscopedpointer/CMakeLists.txt @@ -1,9 +1,16 @@ -# Generated from qscopedpointer.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_qscopedpointer Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qscopedpointer LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qscopedpointer SOURCES tst_qscopedpointer.cpp diff --git a/tests/auto/corelib/tools/qscopedpointer/tst_qscopedpointer.cpp b/tests/auto/corelib/tools/qscopedpointer/tst_qscopedpointer.cpp index 967a3ccf55..3468c97f42 100644 --- a/tests/auto/corelib/tools/qscopedpointer/tst_qscopedpointer.cpp +++ b/tests/auto/corelib/tools/qscopedpointer/tst_qscopedpointer.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> #include <QtCore/QScopedPointer> @@ -61,6 +36,11 @@ private Q_SLOTS: void comparison(); void array(); // TODO instanciate on const object + + // Tests for deprecated APIs +#if QT_DEPRECATED_SINCE(6, 1) + void deprecatedTake(); +#endif // QT_DEPRECATED_SINCE(6, 1) }; void tst_QScopedPointer::defaultConstructor() @@ -367,51 +347,57 @@ void scopedPointerComparisonTest(const A1 &a1, const A2 &a2, const B &b) QVERIFY(a2 != b); } +// tst_QScopedPointer::comparison creates two QScopedPointers referring to the +// same memory. This will lead to double-deletion error during cleanup if we +// use a default QScopedPointer{Array}Deleter. This DummyDeleter does nothing, +// so we can safely reference the same memory from multiple QScopedPointer +// instances, and manage the memory manually. +// That is fine for the comparison() test, because its goal is to check the +// object's (in)equality, not the memory management +struct DummyDeleter +{ + static inline void cleanup(RefCounted *) noexcept {} + void operator()(RefCounted *pointer) const noexcept + { + cleanup(pointer); + } +}; + void tst_QScopedPointer::comparison() { QCOMPARE( RefCounted::instanceCount.loadRelaxed(), 0 ); { - RefCounted *a = new RefCounted; - RefCounted *b = new RefCounted; + auto a = std::make_unique<RefCounted>(); + auto b = std::make_unique<RefCounted>(); QCOMPARE( RefCounted::instanceCount.loadRelaxed(), 2 ); - QScopedPointer<RefCounted> pa1(a); - QScopedPointer<RefCounted> pa2(a); - QScopedPointer<RefCounted> pb(b); + QScopedPointer<RefCounted, DummyDeleter> pa1(a.get()); + QScopedPointer<RefCounted, DummyDeleter> pa2(a.get()); + QScopedPointer<RefCounted, DummyDeleter> pb(b.get()); scopedPointerComparisonTest(pa1, pa1, pb); scopedPointerComparisonTest(pa2, pa2, pb); scopedPointerComparisonTest(pa1, pa2, pb); -QT_WARNING_PUSH -QT_WARNING_DISABLE_DEPRECATED - pa2.take(); -QT_WARNING_POP - QCOMPARE( RefCounted::instanceCount.loadRelaxed(), 2 ); } QCOMPARE( RefCounted::instanceCount.loadRelaxed(), 0 ); { - RefCounted *a = new RefCounted[42]; - RefCounted *b = new RefCounted[43]; + auto a = std::make_unique<RefCounted[]>(42); + auto b = std::make_unique<RefCounted[]>(43); QCOMPARE( RefCounted::instanceCount.loadRelaxed(), 85 ); - QScopedArrayPointer<RefCounted> pa1(a); - QScopedArrayPointer<RefCounted> pa2(a); - QScopedArrayPointer<RefCounted> pb(b); + QScopedArrayPointer<RefCounted, DummyDeleter> pa1(a.get()); + QScopedArrayPointer<RefCounted, DummyDeleter> pa2(a.get()); + QScopedArrayPointer<RefCounted, DummyDeleter> pb(b.get()); scopedPointerComparisonTest(pa1, pa2, pb); -QT_WARNING_PUSH -QT_WARNING_DISABLE_DEPRECATED - pa2.take(); -QT_WARNING_POP - QCOMPARE( RefCounted::instanceCount.loadRelaxed(), 85 ); } @@ -459,6 +445,23 @@ void tst_QScopedPointer::array() QCOMPARE(instCount, RefCounted::instanceCount.loadRelaxed()); } +#if QT_DEPRECATED_SINCE(6, 1) +void tst_QScopedPointer::deprecatedTake() +{ + RefCounted *a = new RefCounted; + + QScopedPointer<RefCounted> pa1(a); + QScopedPointer<RefCounted> pa2(a); + + QCOMPARE(RefCounted::instanceCount.loadRelaxed(), 1); + + QT_IGNORE_DEPRECATIONS(pa2.take();) + + // check that pa2 holds nullptr, but the memory was not released + QVERIFY(pa2.isNull()); + QCOMPARE(RefCounted::instanceCount.loadRelaxed(), 1); +} +#endif // QT_DEPRECATED_SINCE(6, 1) QTEST_MAIN(tst_QScopedPointer) #include "tst_qscopedpointer.moc" |