| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 Paul Lemire |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include <QtTest/QTest> |
| 30 | #include <Qt3DRender/private/genericlambdajob_p.h> |
| 31 | #include <functional> |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | int randomTestValue = 0; |
| 36 | |
| 37 | void freeStandingFunction() |
| 38 | { |
| 39 | randomTestValue = 883; |
| 40 | } |
| 41 | |
| 42 | } // anonymous |
| 43 | |
| 44 | class tst_GenericLambdaJob : public QObject |
| 45 | { |
| 46 | Q_OBJECT |
| 47 | private Q_SLOTS: |
| 48 | void lambdaTest() |
| 49 | { |
| 50 | // GIVEN |
| 51 | int a = 0; |
| 52 | auto incremeneterLambda = [&] () { ++a; }; |
| 53 | Qt3DRender::Render::GenericLambdaJob<decltype(incremeneterLambda)> job(incremeneterLambda); |
| 54 | |
| 55 | // THEN |
| 56 | QCOMPARE(a, 0); |
| 57 | |
| 58 | // WHEN |
| 59 | job.run(); |
| 60 | |
| 61 | // THEN |
| 62 | QCOMPARE(a, 1); |
| 63 | |
| 64 | // WHEN |
| 65 | job.run(); |
| 66 | QCOMPARE(a, 2); |
| 67 | } |
| 68 | |
| 69 | void functionTest() |
| 70 | { |
| 71 | // GIVEN |
| 72 | Qt3DRender::Render::GenericLambdaJob<decltype(&freeStandingFunction)> job(&freeStandingFunction); |
| 73 | |
| 74 | // THEN |
| 75 | QCOMPARE(randomTestValue, 0); |
| 76 | |
| 77 | // WHEN |
| 78 | job.run(); |
| 79 | |
| 80 | // THEN |
| 81 | QCOMPARE(randomTestValue, 883); |
| 82 | } |
| 83 | |
| 84 | void stdFunctionTest() |
| 85 | { |
| 86 | // GIVEN |
| 87 | int u = 1584; |
| 88 | auto decrementerLambda = [&] () { u = 0; }; |
| 89 | std::function<void ()> func = decrementerLambda; |
| 90 | Qt3DRender::Render::GenericLambdaJob<decltype(func)> job(func); |
| 91 | |
| 92 | // THEN |
| 93 | QCOMPARE(u, 1584); |
| 94 | |
| 95 | // WHEN |
| 96 | job.run(); |
| 97 | |
| 98 | // THEN |
| 99 | QCOMPARE(u, 0); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | QTEST_APPLESS_MAIN(tst_GenericLambdaJob) |
| 104 | |
| 105 | #include "tst_genericlambdajob.moc" |
| 106 | |