void tst_QFuture::cancel() { { QFuture<void> f; QFutureInterface<void> result; result.reportStarted(); f = result.future(); QVERIFY(!f.isCanceled()); result.reportCanceled(); QVERIFY(f.isCanceled()); result.reportFinished(); QVERIFY(f.isCanceled()); f.waitForFinished(); QVERIFY(f.isCanceled()); } // Cancel from the QFuture side and test if the result // interface detects it. { QFutureInterface<void> result; QFuture<void> f; QVERIFY(f.isStarted()); result.reportStarted(); f = result.future(); QVERIFY(f.isStarted()); QVERIFY(!result.isCanceled()); f.cancel(); QVERIFY(result.isCanceled()); result.reportFinished(); } // Test that finished futures can be canceled. { QFutureInterface<void> result; QFuture<void> f; QVERIFY(f.isStarted()); result.reportStarted(); f = result.future(); QVERIFY(f.isStarted()); result.reportFinished(); f.cancel(); QVERIFY(result.isCanceled()); QVERIFY(f.isCanceled()); } // Results reported after canceled is called should not be propagated. { QFutureInterface<int> futureInterface; futureInterface.reportStarted(); QFuture<int> f = futureInterface.future(); int result = 0; futureInterface.reportResult(&result); result = 1; futureInterface.reportResult(&result); f.cancel(); result = 2; futureInterface.reportResult(&result); result = 3; futureInterface.reportResult(&result); futureInterface.reportFinished(); QCOMPARE(f.results(), QList<int>()); } }
/* Test out-of-order result reporting using indexes */ void tst_QFuture::indexedResults() { { QFutureInterface<QChar> Interface; QFuture<QChar> f; QVERIFY(f.isStarted()); Interface.reportStarted(); f = Interface.future(); QVERIFY(f.isStarted()); QChar result; result = 'B'; Interface.reportResult(&result, 1); QCOMPARE(f.resultAt(1), result); result = 'A'; Interface.reportResult(&result, 0); QCOMPARE(f.resultAt(0), result); result = 'C'; Interface.reportResult(&result); // no index QCOMPARE(f.resultAt(2), result); Interface.reportFinished(); QCOMPARE(f.results(), QList<QChar>() << 'A' << 'B' << 'C'); } { // Test result reporting with a missing result in the middle QFutureInterface<int> Interface; Interface.reportStarted(); QFuture<int> f = Interface.future(); int result; result = 0; Interface.reportResult(&result, 0); QVERIFY(f.isResultReadyAt(0)); QCOMPARE(f.resultAt(0), 0); result = 3; Interface.reportResult(&result, 3); QVERIFY(f.isResultReadyAt(3)); QCOMPARE(f.resultAt(3), 3); result = 2; Interface.reportResult(&result, 2); QVERIFY(f.isResultReadyAt(2)); QCOMPARE(f.resultAt(2), 2); result = 4; Interface.reportResult(&result); // no index QVERIFY(f.isResultReadyAt(4)); QCOMPARE(f.resultAt(4), 4); Interface.reportFinished(); QCOMPARE(f.results(), QList<int>() << 0 << 2 << 3 << 4); } }
void tst_QFuture::futureInterface() { { QFuture<void> future; { QFutureInterface<void> i; i.reportStarted(); future = i.future(); i.reportFinished(); } } { QFuture<int> future; { QFutureInterface<int> i; i.reportStarted(); i.reportResult(10); future = i.future(); i.reportFinished(); } QCOMPARE(future.resultAt(0), 10); } { QFuture<int> intFuture; QCOMPARE(intFuture.isStarted(), true); QCOMPARE(intFuture.isFinished(), true); IntResult result; result.reportStarted(); intFuture = result.future(); QCOMPARE(intFuture.isStarted(), true); QCOMPARE(intFuture.isFinished(), false); result.reportFinished(&value); QCOMPARE(intFuture.isStarted(), true); QCOMPARE(intFuture.isFinished(), true); int e = intFuture.result(); QCOMPARE(intFuture.isStarted(), true); QCOMPARE(intFuture.isFinished(), true); QCOMPARE(intFuture.isCanceled(), false); QCOMPARE(e, value); intFuture.waitForFinished(); IntResult intAlgo; intFuture = intAlgo.run(); QFuture<int> intFuture2(intFuture); QCOMPARE(intFuture.result(), value); QCOMPARE(intFuture2.result(), value); intFuture.waitForFinished(); VoidResult a; a.run().waitForFinished(); } }