QPointer: add a test for const QPointer<X>

People use this, so make sure there's a test for it.

I don't expect this test to fail, but static and
dynamic checkers should be presented with this
use-case, so they have a chance of warning, because
certain implementation strategies of QPointer may
make this code undefined.

Change-Id: I334bd73204ba4e186c4098fc6b7188917407e020
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Marc Mutz 2016-04-01 09:24:51 +02:00
parent a5456b23f4
commit b7c7beacda

View File

@ -59,6 +59,7 @@ private slots:
void qvariantCast();
void constPointer();
void constQPointer();
};
void tst_QPointer::constructors()
@ -402,6 +403,21 @@ void tst_QPointer::constPointer()
delete fp.data();
}
void tst_QPointer::constQPointer()
{
// Check that const QPointers work. It's a bit weird to mark a pointer
// const if its value can change, but the shallow-const principle in C/C++
// allows this, and people use it, so document it with a test.
//
// It's unlikely that this test will fail in and out of itself, but it
// presents the use-case to static and dynamic checkers that can raise
// a warning (hopefully) should this become an issue.
QObject *o = new QObject(this);
const QPointer<QObject> p = o;
delete o;
QVERIFY(!p);
}
QTEST_MAIN(tst_QPointer)
#include "tst_qpointer.moc"