File indexing completed on 2024-07-14 04:37:04

0001 // SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com>
0002 // SPDX-License-Identifier: BSD-2-Clause OR MIT
0003 
0004 // First included header is the public header of the class we are testing;
0005 // this forces the header to be self-contained.
0006 #include "constpropagatinguniquepointer.h"
0007 
0008 #include <qglobal.h>
0009 #include <qobject.h>
0010 #include <qrect.h>
0011 #include <qtest.h>
0012 #include <qtestcase.h>
0013 
0014 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0015 #include <qtmetamacros.h>
0016 #else
0017 #include <qobjectdefs.h>
0018 #include <qstring.h>
0019 #endif
0020 
0021 static void snippet01()
0022 {
0023     //! [example]
0024     // A ConstPropagatingUniquePointer pointing to a new QObject
0025     PerceptualColor::ConstPropagatingUniquePointer<QObject> myPointer(new QObject());
0026     //! [example]
0027 }
0028 
0029 namespace PerceptualColor
0030 {
0031 class TestConstPropagatingUniquePointer : public QObject
0032 {
0033     Q_OBJECT
0034 
0035 public:
0036     explicit TestConstPropagatingUniquePointer(QObject *parent = nullptr)
0037         : QObject(parent)
0038         , pointerToQRectF(new QRectF)
0039     {
0040     }
0041 
0042 private:
0043     ConstPropagatingUniquePointer<QRectF> pointerToQRectF;
0044 
0045 private Q_SLOTS:
0046 
0047     void initTestCase()
0048     {
0049         // Called before the first test function is executed
0050     }
0051 
0052     void cleanupTestCase()
0053     {
0054         // Called after the last test function was executed
0055     }
0056 
0057     void init()
0058     {
0059         // Called before each test function is executed
0060     }
0061     void cleanup()
0062     {
0063         // Called after every test function
0064     }
0065 
0066     void testConstructorDestructor()
0067     {
0068         ConstPropagatingUniquePointer<QObject> test;
0069     }
0070 
0071     void testDefaultConstructor()
0072     {
0073         ConstPropagatingUniquePointer<QObject> test;
0074         QCOMPARE(test.get(), nullptr);
0075     }
0076 
0077     // NOTE Should break on compile time when the function is const.
0078     void testNonConstAccess()
0079     {
0080         // The following line should not break
0081         pointerToQRectF->setHeight(5);
0082     }
0083 
0084     // NOTE Should break on compile time when the function is const.
0085     void testBackCopy01()
0086     {
0087         QRectF temp;
0088         *pointerToQRectF = temp;
0089     }
0090 
0091     void testConstAccess01() const
0092     {
0093         // The following line should not break
0094         qreal height = pointerToQRectF->height();
0095         Q_UNUSED(height)
0096     }
0097 
0098     void testConstAccess02()
0099     {
0100         // The following line should not break
0101         qreal height = pointerToQRectF->height();
0102         Q_UNUSED(height)
0103     }
0104 
0105     void testCopy01() const
0106     {
0107         QRectF temp = *pointerToQRectF;
0108         Q_UNUSED(temp)
0109     }
0110 
0111     void testCopy02()
0112     {
0113         QRectF temp = *pointerToQRectF;
0114         Q_UNUSED(temp)
0115     }
0116 
0117     void testReset()
0118     {
0119         ConstPropagatingUniquePointer<int> ptr(new int(42));
0120         QCOMPARE(*ptr, 42);
0121 
0122         ptr.reset(new int(23));
0123         QCOMPARE(*ptr, 23);
0124 
0125         ptr.reset();
0126         QCOMPARE(ptr.operator->(), nullptr);
0127         QCOMPARE(ptr.get(), nullptr);
0128     }
0129 
0130     void testSwap1()
0131     {
0132         ConstPropagatingUniquePointer<int> ptr1(new int(42));
0133         ConstPropagatingUniquePointer<int> ptr2(new int(23));
0134         QCOMPARE(*ptr1, 42);
0135         QCOMPARE(*ptr2, 23);
0136 
0137         ptr1.swap(ptr2);
0138         QCOMPARE(*ptr1, 23);
0139         QCOMPARE(*ptr2, 42);
0140     }
0141 
0142     void testSwap2()
0143     {
0144         ConstPropagatingUniquePointer<int> ptr1(new int(42));
0145         ConstPropagatingUniquePointer<int> ptr2;
0146         QCOMPARE(*ptr1, 42);
0147         QCOMPARE(ptr2.operator->(), nullptr);
0148 
0149         ptr1.swap(ptr2);
0150         QCOMPARE(ptr1.operator->(), nullptr);
0151         QCOMPARE(*ptr2, 42);
0152     }
0153 
0154     void testGet()
0155     {
0156         ConstPropagatingUniquePointer<int> ptr(new int(42));
0157         QCOMPARE(*ptr.get(), 42);
0158         QVERIFY(ptr.get() != nullptr);
0159     }
0160 
0161     void testGet2()
0162     {
0163         ConstPropagatingUniquePointer<int> ptr;
0164         QCOMPARE(ptr.get(), nullptr);
0165     }
0166 
0167     void testSnippet01()
0168     {
0169         snippet01();
0170     }
0171 };
0172 
0173 } // namespace PerceptualColor
0174 
0175 QTEST_MAIN(PerceptualColor::TestConstPropagatingUniquePointer)
0176 // The following “include” is necessary because we do not use a header file:
0177 #include "testconstpropagatinguniquepointer.moc"