| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the test suite 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> | 
| 30 |  | 
| 31 | #include <QtCore/qfuturesynchronizer.h> | 
| 32 | #include <QtCore/qfuture.h> | 
| 33 |  | 
| 34 | class tst_QFutureSynchronizer : public QObject | 
| 35 | { | 
| 36 |     Q_OBJECT | 
| 37 |  | 
| 38 |  | 
| 39 | private Q_SLOTS: | 
| 40 |     void construction(); | 
| 41 |     void addFuture(); | 
| 42 |     void cancelOnWait(); | 
| 43 |     void clearFutures(); | 
| 44 |     void futures(); | 
| 45 |     void setFuture(); | 
| 46 |     void waitForFinished(); | 
| 47 | }; | 
| 48 |  | 
| 49 |  | 
| 50 | void tst_QFutureSynchronizer::construction() | 
| 51 | { | 
| 52 |  | 
| 53 |     QFuture<void> future; | 
| 54 |     QFutureSynchronizer<void> synchronizer; | 
| 55 |     QFutureSynchronizer<void> synchronizerWithFuture(future); | 
| 56 |  | 
| 57 |     QCOMPARE(synchronizer.futures().size(), 0); | 
| 58 |     QCOMPARE(synchronizerWithFuture.futures().size(), 1); | 
| 59 | } | 
| 60 |  | 
| 61 | void tst_QFutureSynchronizer::addFuture() | 
| 62 | { | 
| 63 |     QFutureSynchronizer<void> synchronizer; | 
| 64 |  | 
| 65 |     synchronizer.addFuture(future: QFuture<void>()); | 
| 66 |     QFuture<void> future; | 
| 67 |     synchronizer.addFuture(future); | 
| 68 |     synchronizer.addFuture(future); | 
| 69 |  | 
| 70 |     QCOMPARE(synchronizer.futures().size(), 3); | 
| 71 | } | 
| 72 |  | 
| 73 | void tst_QFutureSynchronizer::cancelOnWait() | 
| 74 | { | 
| 75 |     QFutureSynchronizer<void> synchronizer; | 
| 76 |     QVERIFY(!synchronizer.cancelOnWait()); | 
| 77 |     synchronizer.setCancelOnWait(true); | 
| 78 |     QVERIFY(synchronizer.cancelOnWait()); | 
| 79 |     synchronizer.setCancelOnWait(false); | 
| 80 |     QVERIFY(!synchronizer.cancelOnWait()); | 
| 81 |     synchronizer.setCancelOnWait(true); | 
| 82 |     QVERIFY(synchronizer.cancelOnWait()); | 
| 83 | } | 
| 84 |  | 
| 85 | void tst_QFutureSynchronizer::clearFutures() | 
| 86 | { | 
| 87 |     QFutureSynchronizer<void> synchronizer; | 
| 88 |     synchronizer.clearFutures(); | 
| 89 |     QVERIFY(synchronizer.futures().isEmpty()); | 
| 90 |  | 
| 91 |     synchronizer.addFuture(future: QFuture<void>()); | 
| 92 |     QFuture<void> future; | 
| 93 |     synchronizer.addFuture(future); | 
| 94 |     synchronizer.addFuture(future); | 
| 95 |     synchronizer.clearFutures(); | 
| 96 |     QVERIFY(synchronizer.futures().isEmpty()); | 
| 97 | } | 
| 98 |  | 
| 99 | void tst_QFutureSynchronizer::futures() | 
| 100 | { | 
| 101 |     QFutureSynchronizer<void> synchronizer; | 
| 102 |  | 
| 103 |     QList<QFuture<void> > futures; | 
| 104 |     for (int i=0; i<100; i++) { | 
| 105 |         QFuture<void> future; | 
| 106 |         futures.append(t: future); | 
| 107 |         synchronizer.addFuture(future); | 
| 108 |     } | 
| 109 |  | 
| 110 |     QCOMPARE(futures, synchronizer.futures()); | 
| 111 | } | 
| 112 |  | 
| 113 | void tst_QFutureSynchronizer::setFuture() | 
| 114 | { | 
| 115 |     QFutureSynchronizer<void> synchronizer; | 
| 116 |  | 
| 117 |     for (int i=0; i<100; i++) { | 
| 118 |         synchronizer.addFuture(future: QFuture<void>()); | 
| 119 |     } | 
| 120 |     QCOMPARE(synchronizer.futures().size(), 100); | 
| 121 |  | 
| 122 |     QFuture<void> future; | 
| 123 |     synchronizer.setFuture(future); | 
| 124 |     QCOMPARE(synchronizer.futures().size(), 1); | 
| 125 |     QCOMPARE(synchronizer.futures().first(), future); | 
| 126 | } | 
| 127 |  | 
| 128 | void tst_QFutureSynchronizer::waitForFinished() | 
| 129 | { | 
| 130 |     QFutureSynchronizer<void> synchronizer; | 
| 131 |  | 
| 132 |     for (int i=0; i<100; i++) { | 
| 133 |         synchronizer.addFuture(future: QFuture<void>()); | 
| 134 |     } | 
| 135 |     synchronizer.waitForFinished(); | 
| 136 |     const QList<QFuture<void> > futures = synchronizer.futures(); | 
| 137 |  | 
| 138 |     for (int i=0; i<100; i++) { | 
| 139 |         QVERIFY(futures.at(i).isFinished()); | 
| 140 |     } | 
| 141 | } | 
| 142 |  | 
| 143 | QTEST_MAIN(tst_QFutureSynchronizer) | 
| 144 |  | 
| 145 | #include "tst_qfuturesynchronizer.moc" | 
| 146 |  |