1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include <QtTest/QtTest>
45 #include "private/qautoptr_p.h"
47 using namespace QPatternist;
53 \brief Tests class QAutoPtr.
56 class tst_QAutoPtr : public QObject
61 void defaultConstructor() const;
62 void copyConstructor() const;
63 void assignmentOperator() const;
65 void dataSignature() const;
68 void onConstObject() const;
69 void dereferenceOperator() const;
70 void pointerOperator() const;
71 void pointerOperatorSignature() const;
72 void negationOperator() const;
73 void negationOperatorSignature() const;
74 void operatorBool() const;
75 void operatorBoolSignature() const;
78 - Test all the type hierarchy operators/constructors
79 - No code is at all calling AutoPtr& operator=(AutoPtrRef<T> ref) currently. Is it needed?
80 - No code is at all calling AutoPtrRef stuff. Is it needed?
81 - Equalness/unequalness operators?
82 - Test AutoPtr& operator=(AutoPtrRef<T> ref)
86 void tst_QAutoPtr::defaultConstructor() const
88 /* Check that the members, one, is correctly initialized. */
90 QCOMPARE(p.data(), static_cast<int *>(0));
93 void tst_QAutoPtr::copyConstructor() const
95 /* Copy default constructed value. */
99 QCOMPARE(p2.data(), static_cast<int *>(0));
102 /* Copy active value. */
104 AutoPtr<int> p1(new int(7));
106 QCOMPARE(*p2.data(), 7);
107 QCOMPARE(p1.data(), static_cast<int *>(0));
111 void tst_QAutoPtr::assignmentOperator() const
113 /* Assign to self, a default constructed value. */
121 /* Assign to a default constructed value. */
130 /* Assign to an active value. */
132 AutoPtr<int> p1(new int(6));
139 /* Assign from an active value. */
141 int *const ptr = new int(6);
142 AutoPtr<int> p1(ptr);
146 QCOMPARE(p2.data(), ptr);
147 /* p1 should have reset. */
148 QCOMPARE(p1.data(), static_cast<int *>(0));
152 void tst_QAutoPtr::data() const
156 QCOMPARE(p.data(), static_cast<int *>(0));
159 void tst_QAutoPtr::dataSignature() const
161 const AutoPtr<int> p;
162 /* data() should be const. */
166 void tst_QAutoPtr::release() const
168 /* Call release() on a default constructed value. */
171 QCOMPARE(p.release(), static_cast<int *>(0));
174 /* Call release() on an active value, it shouldn't delete. */
177 AutoPtr<int> p(&value);
182 void tst_QAutoPtr::reset() const
184 /* Call reset() on a default constructed value. */
190 /* Call reset() on an active value. */
192 AutoPtr<int> p(new int(3));
196 /* Call reset() with a value, on an active value. */
198 AutoPtr<int> p(new int(3));
200 int *const value = new int(9);
202 QCOMPARE(*p.data(), 9);
205 /* Call reset() with a value, on default constructed value. */
209 int *const value = new int(9);
211 QCOMPARE(*p.data(), 9);
215 void tst_QAutoPtr::onConstObject() const
217 /* Instansiate on a const object. */
218 AutoPtr<const int> p(new int(3));
228 virtual ~AbstractClass()
232 virtual int member() const = 0;
235 class SubClass : public AbstractClass
238 virtual int member() const
244 void tst_QAutoPtr::dereferenceOperator() const
246 /* Dereference a basic value. */
249 AutoPtr<int> p(&value);
251 const int value2 = *p;
256 /* Dereference a pointer to an abstract class. This verifies
257 * that the operator returns a reference, when compiling
260 AutoPtr<AbstractClass> p(new SubClass());
262 QCOMPARE((*p).member(), 5);
272 void tst_QAutoPtr::pointerOperator() const
276 AutoPtr<AnyForm> p(&af);
278 QCOMPARE(p->value, 5);
282 void tst_QAutoPtr::pointerOperatorSignature() const
284 /* The operator should be const. */
285 const AutoPtr<AnyForm> p(new AnyForm);
291 void tst_QAutoPtr::negationOperator() const
293 /* Invoke on default constructed value. */
300 void tst_QAutoPtr::negationOperatorSignature() const
302 /* The signature should be const. */
303 const AutoPtr<int> p;
306 /* The return value should be bool. */
310 void tst_QAutoPtr::operatorBool() const
312 /* Invoke on default constructed value. */
315 QCOMPARE(bool(p), false);
318 /* Invoke on active value. */
320 AutoPtr<int> p(new int(3));
325 void tst_QAutoPtr::operatorBoolSignature() const
327 /* The signature should be const. */
328 const AutoPtr<int> p;
329 QCOMPARE(bool(p), false);
332 QTEST_MAIN(tst_QAutoPtr)
334 #include "tst_qautoptr.moc"