1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
43 #include <QtCore/QScopedPointer>
46 \class tst_QScopedPointer
49 \brief Tests class QScopedPointer.
52 class tst_QScopedPointer
: public QObject
57 void defaultConstructor();
58 void dataOnDefaultConstructed();
59 void useSubClassInConstructor();
63 void dereferenceOperator();
64 void dereferenceOperatorSignature();
65 void pointerOperator();
66 void pointerOperatorSignature();
67 void negationOperator();
68 void negationOperatorSignature();
70 void operatorBoolSignature();
72 void isNullSignature();
75 // TODO instanciate on const object
78 void tst_QScopedPointer::defaultConstructor()
80 /* Check that the members, one, is correctly initialized. */
81 QScopedPointer
<int> p
;
82 QCOMPARE(p
.data(), static_cast<int *>(0));
85 void tst_QScopedPointer::dataOnDefaultConstructed()
87 QScopedPointer
<int> p
;
89 QCOMPARE(p
.data(), static_cast<int *>(0));
96 class MySubClass
: public MyClass
100 void tst_QScopedPointer::useSubClassInConstructor()
102 /* Use a syntax which users typically would do. */
103 QScopedPointer
<MyClass
> p(new MyClass());
106 void tst_QScopedPointer::dataOnValue()
108 int *const rawPointer
= new int(5);
109 QScopedPointer
<int> p(rawPointer
);
111 QCOMPARE(p
.data(), rawPointer
);
114 void tst_QScopedPointer::dataSignature()
116 const QScopedPointer
<int> p
;
117 /* data() should be const. */
121 void tst_QScopedPointer::reset()
123 /* Call reset() on a default constructed value. */
125 QScopedPointer
<int> p
;
127 QCOMPARE(p
.data(), static_cast<int *>(0));
130 /* Call reset() on an active value. */
132 QScopedPointer
<int> p(new int(3));
134 QCOMPARE(p
.data(), static_cast<int *>(0));
137 /* Call reset() with a value, on an active value. */
139 QScopedPointer
<int> p(new int(3));
141 int *const value
= new int(9);
143 QCOMPARE(*p
.data(), 9);
146 /* Call reset() with a value, on default constructed value. */
148 QScopedPointer
<int> p
;
150 int *const value
= new int(9);
152 QCOMPARE(*p
.data(), 9);
159 virtual ~AbstractClass()
163 virtual int member() const = 0;
166 class SubClass
: public AbstractClass
169 virtual int member() const
175 void tst_QScopedPointer::dereferenceOperator()
177 /* Dereference a basic value. */
179 QScopedPointer
<int> p(new int(5));
181 const int value2
= *p
;
185 /* Dereference a pointer to an abstract class. This verifies
186 * that the operator returns a reference, when compiling
189 QScopedPointer
<AbstractClass
> p(new SubClass());
191 QCOMPARE((*p
).member(), 5);
195 void tst_QScopedPointer::dereferenceOperatorSignature()
197 /* The operator should be const. */
199 const QScopedPointer
<int> p(new int(5));
203 /* A reference should be returned, not a value. */
205 const QScopedPointer
<int> p(new int(5));
206 Q_UNUSED(static_cast<int &>(*p
));
209 /* Instantiated on a const object, the returned object is a const reference. */
211 const QScopedPointer
<const int> p(new int(5));
212 Q_UNUSED(static_cast<const int &>(*p
));
222 void tst_QScopedPointer::pointerOperator()
224 QScopedPointer
<AnyForm
> p(new AnyForm());
227 QCOMPARE(p
->value
, 5);
230 void tst_QScopedPointer::pointerOperatorSignature()
232 /* The operator should be const. */
233 const QScopedPointer
<AnyForm
> p(new AnyForm
);
239 void tst_QScopedPointer::negationOperator()
241 /* Invoke on default constructed value. */
243 QScopedPointer
<int> p
;
247 /* Invoke on a value. */
249 QScopedPointer
<int> p(new int(2));
254 void tst_QScopedPointer::negationOperatorSignature()
256 /* The signature should be const. */
257 const QScopedPointer
<int> p
;
260 /* The return value should be bool. */
261 static_cast<bool>(!p
);
264 void tst_QScopedPointer::operatorBool()
266 /* Invoke on default constructed value. */
268 QScopedPointer
<int> p
;
269 QCOMPARE(bool(p
), false);
272 /* Invoke on active value. */
274 QScopedPointer
<int> p(new int(3));
279 void tst_QScopedPointer::operatorBoolSignature()
281 /* The signature should be const and return bool. */
282 const QScopedPointer
<int> p
;
284 (void)static_cast<bool>(p
);
287 void tst_QScopedPointer::isNull()
289 /* Invoke on default constructed value. */
291 QScopedPointer
<int> p
;
295 /* Invoke on a set value. */
297 QScopedPointer
<int> p(new int(69));
298 QVERIFY(!p
.isNull());
302 void tst_QScopedPointer::isNullSignature()
304 const QScopedPointer
<int> p(new int(69));
306 /* The signature should be const and return bool. */
307 static_cast<bool>(p
.isNull());
310 void tst_QScopedPointer::objectSize()
312 /* The size of QScopedPointer should be the same as one pointer. */
313 QCOMPARE(sizeof(QScopedPointer
<int>), sizeof(void *));
324 RefCounted(RefCounted
const &)
333 instanceCount
.deref();
336 RefCounted
&operator=(RefCounted
const &)
343 static QAtomicInt instanceCount
;
346 QAtomicInt
RefCounted::instanceCount
= 0;
348 template <class A1
, class A2
, class B
>
349 void scopedPointerComparisonTest(const A1
&a1
, const A2
&a2
, const B
&b
)
351 // test equality on equal pointers
355 // test inequality on equal pointers
356 QVERIFY(!(a1
!= a2
));
357 QVERIFY(!(a2
!= a1
));
359 // test equality on unequal pointers
365 // test inequality on unequal pointers
372 void tst_QScopedPointer::comparison()
374 QCOMPARE( int(RefCounted::instanceCount
), 0 );
377 RefCounted
*a
= new RefCounted
;
378 RefCounted
*b
= new RefCounted
;
380 QCOMPARE( int(RefCounted::instanceCount
), 2 );
382 QScopedPointer
<RefCounted
> pa1(a
);
383 QScopedPointer
<RefCounted
> pa2(a
);
384 QScopedPointer
<RefCounted
> pb(b
);
386 scopedPointerComparisonTest(pa1
, pa1
, pb
);
387 scopedPointerComparisonTest(pa2
, pa2
, pb
);
388 scopedPointerComparisonTest(pa1
, pa2
, pb
);
392 QCOMPARE( int(RefCounted::instanceCount
), 2 );
395 QCOMPARE( int(RefCounted::instanceCount
), 0 );
398 RefCounted
*a
= new RefCounted
[42];
399 RefCounted
*b
= new RefCounted
[43];
401 QCOMPARE( int(RefCounted::instanceCount
), 85 );
403 QScopedArrayPointer
<RefCounted
> pa1(a
);
404 QScopedArrayPointer
<RefCounted
> pa2(a
);
405 QScopedArrayPointer
<RefCounted
> pb(b
);
407 scopedPointerComparisonTest(pa1
, pa2
, pb
);
411 QCOMPARE( int(RefCounted::instanceCount
), 85 );
414 QCOMPARE( int(RefCounted::instanceCount
), 0 );
417 // QScopedSharedPointer is an internal helper class -- it is unsupported!
419 RefCounted
*a
= new RefCounted
;
420 RefCounted
*b
= new RefCounted
;
422 QCOMPARE( int(RefCounted::instanceCount
), 2 );
424 QSharedDataPointer
<RefCounted
> pa1(a
);
425 QSharedDataPointer
<RefCounted
> pa2(a
);
426 QSharedDataPointer
<RefCounted
> pb(b
);
428 QCOMPARE( int(a
->ref
), 2 );
429 QCOMPARE( int(b
->ref
), 1 );
430 QCOMPARE( int(RefCounted::instanceCount
), 2 );
432 scopedPointerComparisonTest(pa1
, pa2
, pb
);
434 QCOMPARE( int(RefCounted::instanceCount
), 2 );
437 QCOMPARE( int(RefCounted::instanceCount
), 0 );
440 QTEST_MAIN(tst_QScopedPointer
)
441 #include "tst_qscopedpointer.moc"