From b44541d8a42b7fc0b24cde4086b0030623280cc0 Mon Sep 17 00:00:00 2001 From: Rym Bouabid Date: Wed, 3 Jul 2024 17:51:29 +0200 Subject: [PATCH] QExplicitlySharedDataPointerV2: Add unit-tests The tests are needed for the next patch where we modernize comparisons and wrap the pointer, which involves changing most of the functions. Pick-to: 6.8 6.7 6.5 6.2 Change-Id: If635538bfca33e410e19472d32b9666d258f96a9 Reviewed-by: Ivan Solovev --- tests/auto/corelib/tools/CMakeLists.txt | 1 + .../CMakeLists.txt | 17 ++ .../tst_qexplicitlyshareddatapointerv2.cpp | 214 ++++++++++++++++++ 3 files changed, 232 insertions(+) create mode 100644 tests/auto/corelib/tools/qexplicitlyshareddatapointerv2/CMakeLists.txt create mode 100644 tests/auto/corelib/tools/qexplicitlyshareddatapointerv2/tst_qexplicitlyshareddatapointerv2.cpp diff --git a/tests/auto/corelib/tools/CMakeLists.txt b/tests/auto/corelib/tools/CMakeLists.txt index ed8c27a5f5d..ddf26f85e1c 100644 --- a/tests/auto/corelib/tools/CMakeLists.txt +++ b/tests/auto/corelib/tools/CMakeLists.txt @@ -16,6 +16,7 @@ add_subdirectory(qcryptographichash) add_subdirectory(qduplicatetracker) add_subdirectory(qeasingcurve) add_subdirectory(qexplicitlyshareddatapointer) +add_subdirectory(qexplicitlyshareddatapointerv2) add_subdirectory(qflatmap) if(QT_FEATURE_private_tests) add_subdirectory(qfreelist) diff --git a/tests/auto/corelib/tools/qexplicitlyshareddatapointerv2/CMakeLists.txt b/tests/auto/corelib/tools/qexplicitlyshareddatapointerv2/CMakeLists.txt new file mode 100644 index 00000000000..553450907e4 --- /dev/null +++ b/tests/auto/corelib/tools/qexplicitlyshareddatapointerv2/CMakeLists.txt @@ -0,0 +1,17 @@ +# Copyright (C) 2024 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qexplicitlyshareddatapointerv2 Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qexplicitlyshareddatapointerv2 LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qexplicitlyshareddatapointerv2 + SOURCES + tst_qexplicitlyshareddatapointerv2.cpp +) diff --git a/tests/auto/corelib/tools/qexplicitlyshareddatapointerv2/tst_qexplicitlyshareddatapointerv2.cpp b/tests/auto/corelib/tools/qexplicitlyshareddatapointerv2/tst_qexplicitlyshareddatapointerv2.cpp new file mode 100644 index 00000000000..d8493306250 --- /dev/null +++ b/tests/auto/corelib/tools/qexplicitlyshareddatapointerv2/tst_qexplicitlyshareddatapointerv2.cpp @@ -0,0 +1,214 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include + +#include + +class tst_QExplicitlySharedDataPointerv2 : public QObject +{ + Q_OBJECT + + template using QESDP_V2 = QtPrivate::QExplicitlySharedDataPointerV2; + +private slots: + void copyConstructor() const; + void moveConstructor() const; + void copyAssignment() const; + void moveAssignment() const; + void compare() const; + void mutability() const; + void data() const; + void reset() const; + void swap() const; + void take() const; +}; + +class MyClass : public QSharedData +{ +public: + MyClass() = default; + MyClass(int v) : m_value(v) { ref.ref(); }; + int m_value; + void mutating() {} + void notMutating() const {} + MyClass &operator=(const MyClass &) { return *this; } +}; + +void tst_QExplicitlySharedDataPointerv2::copyConstructor() const +{ + const QESDP_V2 pointer(new MyClass()); + const QESDP_V2 copy(pointer); + QCOMPARE_EQ(pointer, copy); +} + +void tst_QExplicitlySharedDataPointerv2::moveConstructor() const +{ + QESDP_V2 pointer(new MyClass()); + const QESDP_V2 moved(std::move(pointer)); + QCOMPARE_NE(moved.data(), static_cast(0)); + QCOMPARE_EQ(pointer.data(), static_cast(0)); +} + +void tst_QExplicitlySharedDataPointerv2::copyAssignment() const +{ + const QESDP_V2 pointer(new MyClass()); + const QESDP_V2 copy = pointer; + QCOMPARE_EQ(pointer, copy); +} + +void tst_QExplicitlySharedDataPointerv2::moveAssignment() const +{ + QESDP_V2 pointer(new MyClass()); + const QESDP_V2 moved = std::move(pointer); + QCOMPARE_NE(moved.data(), static_cast(0)); + QCOMPARE_EQ(pointer.data(), static_cast(0)); +} + +void tst_QExplicitlySharedDataPointerv2::compare() const +{ + const QESDP_V2 ptr; + const QESDP_V2 ptr2; + QCOMPARE_EQ(ptr.data(), static_cast(0)); + QCOMPARE_EQ(ptr2.data(), static_cast(0)); + QCOMPARE_EQ(ptr, ptr2); + + const QESDP_V2 copy(ptr); + QCOMPARE_EQ(ptr, copy); + + const QESDP_V2 new_ptr(new MyClass()); + QCOMPARE_NE(new_ptr.data(), static_cast(0)); + QCOMPARE_NE(ptr, new_ptr); + + std::array myArray {MyClass(2), MyClass(1), MyClass(0)}; + const QESDP_V2 val0(&myArray[0]); + const QESDP_V2 val1(&myArray[1]); + QCOMPARE_NE(val1, val0); +} + +void tst_QExplicitlySharedDataPointerv2::mutability() const +{ + /* Pointer itself is const & points to const type. */ + { + const QESDP_V2 pointer(new MyClass()); + pointer->notMutating(); + } + + /* Pointer itself is mutable & points to const type. */ + { + QESDP_V2 pointer(new MyClass()); + pointer->notMutating(); + } + + /* Pointer itself is const & points to mutable type. */ + { + const QESDP_V2 pointer(new MyClass()); + pointer->notMutating(); + } + + /* Pointer itself is mtable & points to mutable type. */ + { + QESDP_V2 pointer(new MyClass()); + pointer->notMutating(); + pointer->mutating(); + *pointer = MyClass(); + } +} + +void tst_QExplicitlySharedDataPointerv2::data() const +{ + /* Check default value. */ + { + QESDP_V2 pointer; + QCOMPARE_EQ(pointer.data(), static_cast(0)); + } + + { + const QESDP_V2 pointer(new MyClass()); + /* Check that this cast is possible. */ + Q_UNUSED(static_cast(pointer.data())); + } + + { + QESDP_V2 pointer(new MyClass()); + /* Check that this cast is possible. */ + Q_UNUSED(static_cast(pointer.data())); + } + + { + const QESDP_V2 pointer(new MyClass()); + /* Check that this cast is possible. */ + Q_UNUSED(static_cast(pointer.data())); + } + + { + QESDP_V2 pointer(new MyClass()); + /* Check that these casts are possible. */ + Q_UNUSED(static_cast(pointer.data())); + Q_UNUSED(static_cast(pointer.data())); + } +} + +void tst_QExplicitlySharedDataPointerv2::reset() const +{ + /* Reset a default constructed shared data object: reference count is equal to 0. */ + { + QESDP_V2 pointer; + QCOMPARE_EQ(pointer.data(), static_cast(0)); + + pointer.reset(); + QCOMPARE_EQ(pointer.data(), static_cast(0)); + } + + /* Reset a shared data object where the reference count is equal to 1. */ + { + QESDP_V2 pointer(new MyClass()); + QCOMPARE_NE(pointer.data(), static_cast(0)); + + pointer.reset(); + QCOMPARE_EQ(pointer.data(), static_cast(0)); + } + + /* Reset a shared data object where the reference count is greater than 1. */ + { + QESDP_V2 pointer(new MyClass()); + QCOMPARE_NE(pointer.data(), static_cast(0)); + QESDP_V2 pointer2(pointer); + QCOMPARE_NE(pointer2.data(), static_cast(0)); + + pointer.reset(); + QCOMPARE_EQ(pointer.data(), static_cast(0)); + QCOMPARE_NE(pointer2.data(), static_cast(0)); + } +} + +void tst_QExplicitlySharedDataPointerv2::swap() const +{ + QESDP_V2 p1(0), p2(new MyClass()); + QVERIFY(!p1.data()); + QVERIFY(p2.data()); + + p1.swap(p2); + QVERIFY(p1.data()); + QVERIFY(!p2.data()); + + p1.swap(p2); + QVERIFY(!p1.data()); + QVERIFY(p2.data()); + + qSwap(p1, p2); + QVERIFY(p1.data()); + QVERIFY(!p2.data()); +} + +void tst_QExplicitlySharedDataPointerv2::take() const +{ + QESDP_V2 pointer(new MyClass()); + QCOMPARE_NE(pointer.data(), static_cast(0)); + delete pointer.take(); + QCOMPARE_EQ(pointer.data(), static_cast(0)); +} + +QTEST_MAIN(tst_QExplicitlySharedDataPointerv2) + +#include "tst_qexplicitlyshareddatapointerv2.moc"