diff options
author | João Abecasis <joao@abecasis.name> | 2009-11-06 16:35:10 (GMT) |
---|---|---|
committer | Marius Storm-Olsen <marius.storm-olsen@nokia.com> | 2009-11-10 09:13:37 (GMT) |
commit | 7bfdec392df263b15fb5661ab7d4f66f1cd8fbae (patch) | |
tree | db265d81ce2f1341575e0ebfc6be969d1fbf38d1 /tests/auto/qscopedpointer | |
parent | d5db58127b16f3c2508975661ee7328f569b9518 (diff) | |
download | Qt-7bfdec392df263b15fb5661ab7d4f66f1cd8fbae.zip Qt-7bfdec392df263b15fb5661ab7d4f66f1cd8fbae.tar.gz Qt-7bfdec392df263b15fb5661ab7d4f66f1cd8fbae.tar.bz2 |
Extending QScopedPointer test case
... to also test QScopedArrayPointer, QCustomScopedPointer and
QScopedSharedPointer.
Added one level of indirection to comparison test case to avoid
double-delete in case of test failure. The test also tests other aspects
of Q*Scoped*Pointer behavior.
Reviewed-by: Olivier Goffart
Diffstat (limited to 'tests/auto/qscopedpointer')
-rw-r--r-- | tests/auto/qscopedpointer/tst_qscopedpointer.cpp | 156 |
1 files changed, 139 insertions, 17 deletions
diff --git a/tests/auto/qscopedpointer/tst_qscopedpointer.cpp b/tests/auto/qscopedpointer/tst_qscopedpointer.cpp index aeead15..f319891 100644 --- a/tests/auto/qscopedpointer/tst_qscopedpointer.cpp +++ b/tests/auto/qscopedpointer/tst_qscopedpointer.cpp @@ -313,30 +313,152 @@ void tst_QScopedPointer::objectSize() QCOMPARE(sizeof(QScopedPointer<int>), sizeof(void *)); } -void tst_QScopedPointer::comparison() +struct RefCounted { - int *a = new int(42); - int *b = new int(43); + RefCounted() + : ref(0) + { + instanceCount.ref(); + } + + RefCounted(RefCounted const &other) + : ref(0) + { + instanceCount.ref(); + } + + ~RefCounted() + { + QVERIFY( ref == 0 ); + instanceCount.deref(); + } + + RefCounted &operator=(RefCounted const &) + { + } + + QAtomicInt ref; + + static QAtomicInt instanceCount; +}; - QScopedPointer<int> pa(a); - QScopedPointer<int> pa2(a); - QScopedPointer<int> pb(b); +QAtomicInt RefCounted::instanceCount = 0; +template <class A1, class A2, class B> +void scopedPointerComparisonTest(const A1 &a1, const A2 &a2, const B &b) +{ // test equality on equal pointers - QVERIFY(pa == pa2); - QVERIFY(pa2 == pa); + QVERIFY(a1 == a2); + QVERIFY(a2 == a1); + + // test inequality on equal pointers + QVERIFY(!(a1 != a2)); + QVERIFY(!(a2 != a1)); + + // test equality on unequal pointers + QVERIFY(!(a1 == b)); + QVERIFY(!(a2 == b)); + QVERIFY(!(b == a1)); + QVERIFY(!(b == a2)); + + // test inequality on unequal pointers + QVERIFY(b != a1); + QVERIFY(b != a2); + QVERIFY(a1 != b); + QVERIFY(a2 != b); +} + +void tst_QScopedPointer::comparison() +{ + QCOMPARE( int(RefCounted::instanceCount), 0 ); + + { + RefCounted *a = new RefCounted; + RefCounted *b = new RefCounted; + + QCOMPARE( int(RefCounted::instanceCount), 2 ); + + QScopedPointer<RefCounted> pa1(a); + QScopedPointer<RefCounted> pa2(a); + QScopedPointer<RefCounted> pb(b); + + scopedPointerComparisonTest(pa1, pa1, pb); + scopedPointerComparisonTest(pa2, pa2, pb); + scopedPointerComparisonTest(pa1, pa2, pb); + + pa2.take(); + + QCOMPARE( int(RefCounted::instanceCount), 2 ); + } - // test unequality on equal pointers - QVERIFY(!(pa != pa2)); - QVERIFY(!(pa2 != pa)); + QCOMPARE( int(RefCounted::instanceCount), 0 ); - // test on unequal pointers - QVERIFY(!(pa == pb)); - QVERIFY(!(pb == pa)); - QVERIFY(pb != pa); - QVERIFY(pa != pb); + { + RefCounted *a = new RefCounted[42]; + RefCounted *b = new RefCounted[43]; + + QCOMPARE( int(RefCounted::instanceCount), 85 ); + + QScopedArrayPointer<RefCounted> pa1(a); + QScopedArrayPointer<RefCounted> pa2(a); + QScopedArrayPointer<RefCounted> pb(b); + + scopedPointerComparisonTest(pa1, pa2, pb); + + pa2.take(); + + QCOMPARE( int(RefCounted::instanceCount), 85 ); + } + + QCOMPARE( int(RefCounted::instanceCount), 0 ); + + { + // QCustomScopedPointer is an internal helper class -- it is unsupported! + + RefCounted *a = new RefCounted; + RefCounted *b = new RefCounted; + + QCOMPARE( int(RefCounted::instanceCount), 2 ); + + QCustomScopedPointer<RefCounted> pa1(a); + QCustomScopedPointer<RefCounted> pa2(a); + QCustomScopedPointer<RefCounted> pb(b); + + scopedPointerComparisonTest(pa1, pa2, pb); + + pa2.take(); + + QCOMPARE( int(RefCounted::instanceCount), 2 ); + } + + QCOMPARE( int(RefCounted::instanceCount), 0 ); + + { + // QScopedSharedPointer is an internal helper class -- it is unsupported! + + RefCounted *a = new RefCounted; + RefCounted *b = new RefCounted; + + QCOMPARE( int(RefCounted::instanceCount), 2 ); + + a->ref.ref(); + QScopedSharedPointer<RefCounted> pa1(a); + a->ref.ref(); + QScopedSharedPointer<RefCounted> pa2(a); + b->ref.ref(); + QScopedSharedPointer<RefCounted> pb(b); + + + QCOMPARE( int(a->ref), 2 ); + QCOMPARE( int(b->ref), 1 ); + QCOMPARE( int(RefCounted::instanceCount), 2 ); + + scopedPointerComparisonTest(pa1, pa2, pb); + + QCOMPARE( int(RefCounted::instanceCount), 2 ); + } - pa2.take(); + QCOMPARE( int(RefCounted::instanceCount), 0 ); } QTEST_MAIN(tst_QScopedPointer) |