Skip to content

Commit

Permalink
Extending QScopedPointer test case
Browse files Browse the repository at this point in the history
... 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
  • Loading branch information
biochimia authored and David Boddie committed Nov 11, 2009
1 parent 84e4fed commit a5b8ede
Showing 1 changed file with 139 additions and 17 deletions.
156 changes: 139 additions & 17 deletions tests/auto/qscopedpointer/tst_qscopedpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a5b8ede

Please sign in to comment.