File indexing completed on 2025-02-16 04:23:35
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 "constpropagatingrawpointer.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 // Assuming you have a member variable in your class: 0025 PerceptualColor::ConstPropagatingRawPointer<QRectF> pointerToQRectF{new QRectF}; 0026 0027 // Now, you access this member variable from a method within your class: 0028 0029 // Helper variables 0030 QRectF myRectF; 0031 qreal myHeight; 0032 QRectF *normalCppPointerToQRectF; 0033 0034 // The following code works within both, const and non-const contexts: 0035 myHeight = pointerToQRectF->height(); 0036 myRectF = *pointerToQRectF; 0037 0038 // The following code works only within non-const contexts. 0039 // Within cost contexts, you will get an error at compile time. 0040 pointerToQRectF->setHeight(5); 0041 *pointerToQRectF = myRectF; 0042 normalCppPointerToQRectF = pointerToQRectF; 0043 delete pointerToQRectF; 0044 //! [example] 0045 Q_UNUSED(myRectF) 0046 Q_UNUSED(myHeight) 0047 Q_UNUSED(normalCppPointerToQRectF) 0048 } 0049 0050 namespace PerceptualColor 0051 { 0052 class TestConstPropagatingRawPointer : public QObject 0053 { 0054 Q_OBJECT 0055 0056 public: 0057 explicit TestConstPropagatingRawPointer(QObject *parent = nullptr) 0058 : QObject(parent) 0059 , pointerToQRectF(new QRectF) 0060 , pointerToTestDelete(new QRectF) 0061 { 0062 } 0063 0064 private: 0065 ConstPropagatingRawPointer<QRectF> pointerToQRectF; 0066 ConstPropagatingRawPointer<QRectF> pointerToTestDelete; 0067 0068 private Q_SLOTS: 0069 0070 void initTestCase() 0071 { 0072 // Called before the first test function is executed 0073 } 0074 0075 void cleanupTestCase() 0076 { 0077 // Called after the last test function was executed 0078 delete pointerToQRectF; 0079 } 0080 0081 void init() 0082 { 0083 // Called before each test function is executed 0084 } 0085 void cleanup() 0086 { 0087 // Called after every test function 0088 } 0089 0090 void testConstructorDestructor() 0091 { 0092 ConstPropagatingRawPointer<QObject> test; 0093 } 0094 0095 void testDefaultConstructor() 0096 { 0097 ConstPropagatingRawPointer<QObject> test; 0098 QVERIFY2(!test, "Verify that default constructor produced an invalid pointer."); 0099 } 0100 0101 // NOTE Should break on compile time if the method is const. 0102 void testDelete() 0103 { 0104 delete pointerToTestDelete; 0105 } 0106 0107 // NOTE Should break on compile time if the method is const. 0108 void testNonConstAccess() 0109 { 0110 pointerToQRectF->setHeight(5); 0111 } 0112 0113 // NOTE Should break on compile time if the method is const. 0114 void testBackCopy01() 0115 { 0116 QRectF temp; 0117 *pointerToQRectF = temp; 0118 } 0119 0120 // NOTE Should break on compile time if the method is const. 0121 void testCastToNormalRawPointer() 0122 { 0123 QRectF *temp; 0124 temp = pointerToQRectF; 0125 Q_UNUSED(temp) 0126 } 0127 0128 void testCastToNormalRawPointerToConstObjectInConstContext() const 0129 { 0130 const QRectF *temp; 0131 temp = pointerToQRectF.toPointerToConstObject(); 0132 Q_UNUSED(temp) 0133 } 0134 0135 void testCastToNormalRawPointerToConstObjectInNonConstContext() 0136 { 0137 const QRectF *temp; 0138 temp = pointerToQRectF.toPointerToConstObject(); 0139 Q_UNUSED(temp) 0140 } 0141 0142 void testConstAccess01() const 0143 { 0144 // The following line should not break 0145 qreal height = pointerToQRectF->height(); 0146 Q_UNUSED(height) 0147 } 0148 0149 void testConstAccess02() 0150 { 0151 // The following line should not break 0152 qreal height = pointerToQRectF->height(); 0153 Q_UNUSED(height) 0154 } 0155 0156 void testCopy01() const 0157 { 0158 QRectF temp = *pointerToQRectF; 0159 Q_UNUSED(temp) 0160 } 0161 0162 void testCopy02() 0163 { 0164 QRectF temp = *pointerToQRectF; 0165 Q_UNUSED(temp) 0166 } 0167 0168 void testSnippet01() 0169 { 0170 snippet01(); 0171 } 0172 }; 0173 0174 } // namespace PerceptualColor 0175 0176 QTEST_MAIN(PerceptualColor::TestConstPropagatingRawPointer) 0177 // The following “include” is necessary because we do not use a header file: 0178 #include "testconstpropagatingrawpointer.moc"