diff -r 56cd8111b7f7 -r 41300fa6a67c tests/auto/qscopedpointer/tst_qscopedpointer.cpp --- a/tests/auto/qscopedpointer/tst_qscopedpointer.cpp Tue Jan 26 12:42:25 2010 +0200 +++ b/tests/auto/qscopedpointer/tst_qscopedpointer.cpp Tue Feb 02 00:43:10 2010 +0200 @@ -313,30 +313,128 @@ QCOMPARE(sizeof(QScopedPointer), sizeof(void *)); } +struct RefCounted +{ + RefCounted() + : ref(0) + { + instanceCount.ref(); + } + + RefCounted(RefCounted const &) + : ref(0) + { + instanceCount.ref(); + } + + ~RefCounted() + { + QVERIFY( ref == 0 ); + instanceCount.deref(); + } + + RefCounted &operator=(RefCounted const &) + { + return *this; + } + + QAtomicInt ref; + + static QAtomicInt instanceCount; +}; + +QAtomicInt RefCounted::instanceCount = 0; + +template +void scopedPointerComparisonTest(const A1 &a1, const A2 &a2, const B &b) +{ + // test equality on equal pointers + 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() { - int *a = new int(42); - int *b = new int(43); + 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); - QScopedPointer pa(a); - QScopedPointer pa2(a); - QScopedPointer pb(b); + scopedPointerComparisonTest(pa1, pa1, pb); + scopedPointerComparisonTest(pa2, pa2, pb); + scopedPointerComparisonTest(pa1, pa2, pb); + + pa2.take(); - // test equality on equal pointers - QVERIFY(pa == pa2); - QVERIFY(pa2 == pa); + QCOMPARE( int(RefCounted::instanceCount), 2 ); + } + + QCOMPARE( int(RefCounted::instanceCount), 0 ); + + { + RefCounted *a = new RefCounted[42]; + RefCounted *b = new RefCounted[43]; + + QCOMPARE( int(RefCounted::instanceCount), 85 ); - // test unequality on equal pointers - QVERIFY(!(pa != pa2)); - QVERIFY(!(pa2 != pa)); + 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 ); + + { + // QScopedSharedPointer is an internal helper class -- it is unsupported! - // test on unequal pointers - QVERIFY(!(pa == pb)); - QVERIFY(!(pb == pa)); - QVERIFY(pb != pa); - QVERIFY(pa != pb); + RefCounted *a = new RefCounted; + RefCounted *b = new RefCounted; + + QCOMPARE( int(RefCounted::instanceCount), 2 ); + + QSharedDataPointer pa1(a); + QSharedDataPointer pa2(a); + QSharedDataPointer pb(b); - pa2.take(); + QCOMPARE( int(a->ref), 2 ); + QCOMPARE( int(b->ref), 1 ); + QCOMPARE( int(RefCounted::instanceCount), 2 ); + + scopedPointerComparisonTest(pa1, pa2, pb); + + QCOMPARE( int(RefCounted::instanceCount), 2 ); + } + + QCOMPARE( int(RefCounted::instanceCount), 0 ); } QTEST_MAIN(tst_QScopedPointer)