diff options
author | Sean Harmer <sean.harmer@kdab.com> | 2014-04-04 16:20:33 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-04-08 19:46:46 +0200 |
commit | a77d3d61ad7b4497707ecf49d1d0ceaec0dba34e (patch) | |
tree | 0e57cdd99cfd90b3d52703e28c0a0eece8817ffb /tests | |
parent | 379ba861ec3b11f0aef94eafab6de8ea481a8bd8 (diff) |
Add QHandleManager template and test
With: Paul Lemire
Change-Id: Id269fd051a6a6b4f23a733c8123f11bb63e80aa7
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/core/core.pro | 3 | ||||
-rw-r--r-- | tests/auto/core/handlemanager/handlemanager.pro | 7 | ||||
-rw-r--r-- | tests/auto/core/handlemanager/tst_handlemanager.cpp | 197 |
3 files changed, 206 insertions, 1 deletions
diff --git a/tests/auto/core/core.pro b/tests/auto/core/core.pro index 28959d11c..f57b98daa 100644 --- a/tests/auto/core/core.pro +++ b/tests/auto/core/core.pro @@ -1,4 +1,5 @@ TEMPLATE = subdirs SUBDIRS = \ - handle + handle \ + handlemanager diff --git a/tests/auto/core/handlemanager/handlemanager.pro b/tests/auto/core/handlemanager/handlemanager.pro new file mode 100644 index 000000000..e95f1d433 --- /dev/null +++ b/tests/auto/core/handlemanager/handlemanager.pro @@ -0,0 +1,7 @@ +TARGET = tst_handlemanager +CONFIG += testcase +TEMPLATE = app + +SOURCES += tst_handlemanager.cpp + +QT += testlib 3dcore diff --git a/tests/auto/core/handlemanager/tst_handlemanager.cpp b/tests/auto/core/handlemanager/tst_handlemanager.cpp new file mode 100644 index 000000000..8d603b6ab --- /dev/null +++ b/tests/auto/core/handlemanager/tst_handlemanager.cpp @@ -0,0 +1,197 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> +#include <Qt3DCore/qhandlemanager.h> +#include <Qt3DCore/qhandle.h> + +class tst_HandleManager : public QObject +{ + Q_OBJECT +public: + tst_HandleManager() {} + ~tst_HandleManager() {} + +private slots: + void construction(); + void correctPointer(); + void correctPointers(); + void correctConstPointer(); + void nullForRemovedEntry(); + void validHandleForReplacementEntry(); + void updateChangesValue(); + void resetRemovesAllEntries(); +}; + +class SimpleResource +{ +public: + SimpleResource() + : m_value(0) + {} + + int m_value; +}; + +typedef Qt3D::QHandle<SimpleResource> Handle; + +void tst_HandleManager::construction() +{ + Qt3D::QHandleManager<SimpleResource> manager; + QVERIFY(manager.activeEntries() == 0); +} + +void tst_HandleManager::correctPointer() +{ + Qt3D::QHandleManager<SimpleResource> manager; + SimpleResource *p1 = (SimpleResource *)0xdeadbeef; + const Handle h = manager.acquire(p1); + + bool ok = false; + SimpleResource *p2 = manager.data(h, &ok); + QVERIFY(ok == true); + QVERIFY(p1 == p2); +} + +void tst_HandleManager::correctPointers() +{ + Qt3D::QHandleManager<SimpleResource> manager; + SimpleResource *p[3]; + p[0] = (SimpleResource *)0xdeadbeef; + p[1] = (SimpleResource *)0x11111111; + p[2] = (SimpleResource *)0x22222222; + + for (int i = 0; i < 3; ++i) { + const Handle h = manager.acquire(p[i]); + + bool ok = false; + SimpleResource *q = manager.data(h, &ok); + QVERIFY(ok == true); + QVERIFY(p[i] == q); + } + + QVERIFY(manager.activeEntries() == 3); +} + +void tst_HandleManager::correctConstPointer() +{ + Qt3D::QHandleManager<SimpleResource> manager; + QSharedPointer<SimpleResource> p1(new SimpleResource); + const Handle h = manager.acquire(p1.data()); + + bool ok = false; + const SimpleResource *p2 = manager.constData(h, &ok); + QVERIFY(ok == true); + QVERIFY(p1.data() == p2); +} + +void tst_HandleManager::nullForRemovedEntry() +{ + Qt3D::QHandleManager<SimpleResource> manager; + QSharedPointer<SimpleResource> p1(new SimpleResource); + const Handle h = manager.acquire(p1.data()); + manager.release(h); + + bool ok = false; + SimpleResource *p2 = manager.data(h, &ok); + QVERIFY(ok == false); + QVERIFY(p2 == Q_NULLPTR); +} + +void tst_HandleManager::validHandleForReplacementEntry() +{ + Qt3D::QHandleManager<SimpleResource> manager; + QSharedPointer<SimpleResource> p1(new SimpleResource); + const Handle h = manager.acquire(p1.data()); + QVERIFY(manager.activeEntries() == 1); + manager.release(h); + QVERIFY(manager.activeEntries() == 0); + + QSharedPointer<SimpleResource> p2(new SimpleResource); + const Handle h2 = manager.acquire(p2.data()); + QVERIFY(h2.isNull() == false); + QVERIFY(h2.counter() == 2); + QVERIFY(manager.activeEntries() == 1); + + bool ok = false; + SimpleResource *p3 = manager.data(h2, &ok); + QVERIFY(ok == true); + QVERIFY(p3 == p2); +} + +void tst_HandleManager::updateChangesValue() +{ + Qt3D::QHandleManager<SimpleResource> manager; + QSharedPointer<SimpleResource> p1(new SimpleResource); + const Handle h = manager.acquire(p1.data()); + + QSharedPointer<SimpleResource> p2(new SimpleResource); + manager.update(h, p2.data()); + QVERIFY(manager.activeEntries() == 1); + + bool ok = false; + SimpleResource *p3 = manager.data(h, &ok); + QVERIFY(ok == true); + QVERIFY(p3 == p2); +} + +void tst_HandleManager::resetRemovesAllEntries() +{ + Qt3D::QHandleManager<SimpleResource> manager; + for (int i = 0; i < 100; ++i) { + SimpleResource *p = (SimpleResource *) 0xdead0000 + i; + const Handle h = manager.acquire(p); + + bool ok = false; + SimpleResource *q = manager.data(h, &ok); + QVERIFY(ok == true); + QVERIFY(p == q); + } + + QVERIFY(manager.activeEntries() == 100); + + manager.reset(); + QVERIFY(manager.activeEntries() == 0); +} + +QTEST_APPLESS_MAIN(tst_HandleManager) + +#include "tst_handlemanager.moc" |