From a5b8ede4938809f6a513a76edc3659db3c9658cf Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jo=C3=A3o=20Abecasis?= Date: Fri, 6 Nov 2009 17:35:10 +0100 Subject: [PATCH] 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 --- tests/auto/qscopedpointer/tst_qscopedpointer.cpp | 156 ++++++++++++++++++++--- 1 file changed, 139 insertions(+), 17 deletions(-) diff --git a/tests/auto/qscopedpointer/tst_qscopedpointer.cpp b/tests/auto/qscopedpointer/tst_qscopedpointer.cpp index aeead150e9..f319891f46 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), 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 pa(a); - QScopedPointer pa2(a); - QScopedPointer pb(b); +QAtomicInt RefCounted::instanceCount = 0; +template +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 pa1(a); + QScopedPointer pa2(a); + QScopedPointer 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 pa1(a); + QScopedArrayPointer pa2(a); + QScopedArrayPointer 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 pa1(a); + QCustomScopedPointer pa2(a); + QCustomScopedPointer 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 pa1(a); + a->ref.ref(); + QScopedSharedPointer pa2(a); + b->ref.ref(); + QScopedSharedPointer 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) -- 2.11.4.GIT