00001 /* 00002 Copyright © 2010 yan Verdavaine 00003 00004 This file is part of QExtend. 00005 00006 QExtend is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 any later version. 00010 00011 QExtend is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with QExtend. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #include <QtTest/QtTest> 00021 #include <QExtend/Core/ObjectPtr> 00022 00023 QString const memberObjectName = QString::fromUtf8("OBJ_TEST"); 00024 00025 class testObjectPtr: public QObject 00026 { 00027 Q_OBJECT 00028 QExtend::ObjectPtr<QObject> m_obj; 00029 private slots: 00030 //set name of the QObject member 00031 void initTestCase() 00032 { 00033 m_obj->setObjectName( memberObjectName ); 00034 } 00035 00036 //verify if m_obj object is created 00037 void memberAutoCreate() 00038 { 00039 QVERIFY( ! m_obj.isNull() ); 00040 } 00041 00042 //test pointer validity by a cast to bool in a if 00043 void castToBool() 00044 { 00045 if(m_obj) 00046 { 00047 QVERIFY( true); 00048 } 00049 else 00050 { 00051 QVERIFY( false); 00052 } 00053 } 00054 00055 //test cast to pointer 00056 void castToPointer() 00057 { 00058 QVERIFY( static_cast<QObject *>(m_obj) != 0 ); 00059 } 00060 00061 //test using like a pointer 00062 void useLikePointer() 00063 { 00064 QVERIFY(m_obj->objectName() == memberObjectName); 00065 } 00066 00067 //test pointer deferencement 00068 void dereferencement() 00069 { 00070 QVERIFY((*m_obj).objectName() == memberObjectName); 00071 } 00072 00073 //test SCOPE (default) policy 00074 void scopePolicy() 00075 { 00076 QWeakPointer<QObject> weakPtr; 00077 { 00078 QExtend::ObjectPtr<QObject> tmp; 00079 weakPtr = static_cast<QObject *>(tmp); 00080 00081 }//tmp should delete pointer 00082 00083 QVERIFY( weakPtr.isNull() ); 00084 } 00085 //test SCOPE (default) policy if object have been ever deleted. 00086 void scopePolicyWithObjectDeleted() 00087 { 00088 QWeakPointer<QObject> weakPtr; 00089 { 00090 QExtend::ObjectPtr<QObject> tmp; 00091 weakPtr = static_cast<QObject *>(tmp); 00092 00093 delete static_cast<QObject *>(tmp); 00094 00095 //tmp become null 00096 QVERIFY( tmp.isNull() ); 00097 }//tmp destruction do nothing 00098 QVERIFY( weakPtr.isNull() ); 00099 } 00100 00101 //test NOPARENT (default) policy when object haven't a parent 00102 void noparentPolicyWithoutParent() 00103 { 00104 QWeakPointer<QObject> weakPtr; 00105 { 00106 QExtend::ObjectPtr<QObject , QExtend::NOPARENT> tmp; 00107 weakPtr = static_cast<QObject *>(tmp); 00108 }//tmp should delete the object 00109 00110 QVERIFY( weakPtr.isNull() ); 00111 } 00112 00113 //test NOPARENT (default) policy when object have a parent 00114 void noparentPolicyWithParent() 00115 { 00116 QObject parent; 00117 QWeakPointer<QObject> weakPtr; 00118 { 00119 QExtend::ObjectPtr< QObject , QExtend::NOPARENT > tmp(&parent); 00120 weakPtr = static_cast< QObject * >(tmp); 00121 00122 }//tmp shouldn't delete obj 00123 QVERIFY( ! weakPtr.isNull() ); 00124 00125 } 00126 00127 //test SCOPE (default) policy if object have been ever deleted. 00128 void noparentPolicyWithObjectDeleted() 00129 { 00130 QWeakPointer<QObject> weakPtr; 00131 { 00132 QExtend::ObjectPtr<QObject , QExtend::NOPARENT> tmp; 00133 weakPtr = static_cast<QObject *>(tmp); 00134 00135 delete static_cast<QObject *>(tmp); 00136 00137 //tmp become null 00138 QVERIFY( tmp.isNull() ); 00139 }//tmp destruction do nothing 00140 QVERIFY( weakPtr.isNull() ); 00141 } 00142 00143 }; 00144 00145 00146 00147 QTEST_MAIN(testObjectPtr) 00148 #include "objectptr.moc"
Les sources présentées sur cette page sont libres de droits et vous pouvez les utiliser à votre convenance. Par contre, la page de présentation constitue une œuvre intellectuelle protégée par les droits d'auteur. Copyright © 2010 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site ni de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon vous encourez selon la loi jusqu'à trois ans de prison et jusqu'à 300 000 € de dommages et intérêts.