File: tst_async.cpp

package info (click to toggle)
qtcreator 18.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 261,240 kB
  • sloc: cpp: 1,581,554; ansic: 528,781; python: 48,245; xml: 34,678; javascript: 15,495; sh: 2,670; asm: 2,049; perl: 1,039; lex: 737; java: 695; objc: 409; makefile: 111; yacc: 86; cs: 41; ruby: 33; sed: 22
file content (635 lines) | stat: -rw-r--r-- 22,417 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <utils/algorithm.h>
#include <utils/async.h>

#include <QTest>

using namespace Utils;

using namespace std::chrono_literals;

class tst_Async : public QObject
{
    Q_OBJECT

private slots:
    void runAsync();
    void crefFunction();
    void onResultReady();
    void futureSynchonizer();
    void taskTree();
    void mapReduce_data();
    void mapReduce();
private:
    QThreadPool m_threadPool;
};

void report3(QPromise<int> &promise)
{
    promise.addResult(0);
    promise.addResult(2);
    promise.addResult(1);
}

static void staticReport3(QPromise<int> &promise)
{
    promise.addResult(0);
    promise.addResult(2);
    promise.addResult(1);
}

void reportN(QPromise<double> &promise, int n)
{
    for (int i = 0; i < n; ++i)
        promise.addResult(0);
}

static void staticReportN(QPromise<double> &promise, int n)
{
    for (int i = 0; i < n; ++i)
        promise.addResult(0);
}

void reportString1(QPromise<QString> &promise, const QString &s)
{
    promise.addResult(s);
}

void reportString2(QPromise<QString> &promise, QString s)
{
    promise.addResult(s);
}

class Functor {
public:
    void operator()(QPromise<double> &promise, int n) const
    {
        for (int i = 0; i < n; ++i)
            promise.addResult(0);
    }
};

class MyObject {
public:
    static void staticMember0(QPromise<double> &promise)
    {
        promise.addResult(0);
        promise.addResult(2);
        promise.addResult(1);
    }

    static void staticMember1(QPromise<double> &promise, int n)
    {
        for (int i = 0; i < n; ++i)
            promise.addResult(0);
    }

    void member0(QPromise<double> &promise) const
    {
        promise.addResult(0);
        promise.addResult(2);
        promise.addResult(1);
    }

    void member1(QPromise<double> &promise, int n) const
    {
        for (int i = 0; i < n; ++i)
            promise.addResult(0);
    }

    void memberString1(QPromise<QString> &promise, const QString &s) const
    {
        promise.addResult(s);
    }

    void memberString2(QPromise<QString> &promise, QString s) const
    {
        promise.addResult(s);
    }

    void nonConstMember(QPromise<double> &promise)
    {
        promise.addResult(0);
        promise.addResult(2);
        promise.addResult(1);
    }
};

template <typename...>
struct FutureArgType;

template <typename Arg>
struct FutureArgType<QFuture<Arg>>
{
    using Type = Arg;
};

template <typename...>
struct ConcurrentResultType;

template<typename Function, typename ...Args>
struct ConcurrentResultType<Function, Args...>
{
    using Type = typename FutureArgType<decltype(QtConcurrent::run(
        std::declval<Function>(), std::declval<Args>()...))>::Type;
};

template <typename Function, typename ...Args,
          typename ResultType = typename ConcurrentResultType<Function, Args...>::Type>
std::shared_ptr<Async<ResultType>> createAsyncTask(Function &&function, Args &&...args)
{
    auto asyncTask = std::make_shared<Async<ResultType>>();
    asyncTask->setConcurrentCallData(std::forward<Function>(function), std::forward<Args>(args)...);
    asyncTask->start();
    return asyncTask;
}

void tst_Async::runAsync()
{
    // tesing QtConcurrent::run()
    QCOMPARE(QtConcurrent::run(&report3).results(),
             QList<int>({0, 2, 1}));
    QCOMPARE(QtConcurrent::run(report3).results(),
             QList<int>({0, 2, 1}));
    QCOMPARE(QtConcurrent::run(&staticReport3).results(),
             QList<int>({0, 2, 1}));
    QCOMPARE(QtConcurrent::run(staticReport3).results(),
             QList<int>({0, 2, 1}));
    QCOMPARE(QtConcurrent::run(&reportN, 2).results(),
             QList<double>({0, 0}));
    QCOMPARE(QtConcurrent::run(reportN, 2).results(),
             QList<double>({0, 0}));
    QCOMPARE(QtConcurrent::run(&staticReportN, 2).results(),
             QList<double>({0, 0}));
    QCOMPARE(QtConcurrent::run(staticReportN, 2).results(),
             QList<double>({0, 0}));

    // free function pointer
    QCOMPARE(createAsyncTask(&report3)->results(),
             QList<int>({0, 2, 1}));
    QCOMPARE(Utils::asyncRun(&report3).results(),
             QList<int>({0, 2, 1}));
    QCOMPARE(createAsyncTask(report3)->results(),
             QList<int>({0, 2, 1}));
    QCOMPARE(Utils::asyncRun(report3).results(),
             QList<int>({0, 2, 1}));
    QCOMPARE(createAsyncTask(&staticReport3)->results(),
             QList<int>({0, 2, 1}));
    QCOMPARE(Utils::asyncRun(&staticReport3).results(),
             QList<int>({0, 2, 1}));
    QCOMPARE(createAsyncTask(staticReport3)->results(),
             QList<int>({0, 2, 1}));
    QCOMPARE(Utils::asyncRun(staticReport3).results(),
             QList<int>({0, 2, 1}));

    QCOMPARE(createAsyncTask(&reportN, 2)->results(),
             QList<double>({0, 0}));
    QCOMPARE(Utils::asyncRun(&reportN, 2).results(),
             QList<double>({0, 0}));
    QCOMPARE(createAsyncTask(reportN, 2)->results(),
             QList<double>({0, 0}));
    QCOMPARE(Utils::asyncRun(reportN, 2).results(),
             QList<double>({0, 0}));
    QCOMPARE(createAsyncTask(&staticReportN, 2)->results(),
             QList<double>({0, 0}));
    QCOMPARE(Utils::asyncRun(&staticReportN, 2).results(),
             QList<double>({0, 0}));
    QCOMPARE(createAsyncTask(staticReportN, 2)->results(),
             QList<double>({0, 0}));
    QCOMPARE(Utils::asyncRun(staticReportN, 2).results(),
             QList<double>({0, 0}));

    QString s = QLatin1String("string");
    const QString &crs = QLatin1String("cr string");
    const QString cs = QLatin1String("c string");

    QCOMPARE(createAsyncTask(reportString1, s)->results(),
             QStringList({s}));
    QCOMPARE(Utils::asyncRun(reportString1, s).results(),
             QStringList({s}));
    QCOMPARE(createAsyncTask(reportString1, crs)->results(),
             QStringList({crs}));
    QCOMPARE(Utils::asyncRun(reportString1, crs).results(),
             QStringList({crs}));
    QCOMPARE(createAsyncTask(reportString1, cs)->results(),
             QStringList({cs}));
    QCOMPARE(Utils::asyncRun(reportString1, cs).results(),
             QStringList({cs}));
    QCOMPARE(createAsyncTask(reportString1, QString(QLatin1String("rvalue")))->results(),
             QStringList({QString(QLatin1String("rvalue"))}));
    QCOMPARE(Utils::asyncRun(reportString1, QString(QLatin1String("rvalue"))).results(),
             QStringList({QString(QLatin1String("rvalue"))}));

    QCOMPARE(createAsyncTask(reportString2, s)->results(),
             QStringList({s}));
    QCOMPARE(Utils::asyncRun(reportString2, s).results(),
             QStringList({s}));
    QCOMPARE(createAsyncTask(reportString2, crs)->results(),
             QStringList({crs}));
    QCOMPARE(Utils::asyncRun(reportString2, crs).results(),
             QStringList({crs}));
    QCOMPARE(createAsyncTask(reportString2, cs)->results(),
             QStringList({cs}));
    QCOMPARE(Utils::asyncRun(reportString2, cs).results(),
             QStringList({cs}));
    QCOMPARE(createAsyncTask(reportString2, QString(QLatin1String("rvalue")))->results(),
             QStringList({QString(QLatin1String("rvalue"))}));
    QCOMPARE(Utils::asyncRun(reportString2, QString(QLatin1String("rvalue"))).results(),
             QStringList({QString(QLatin1String("rvalue"))}));

    // lambda
    QCOMPARE(createAsyncTask([](QPromise<double> &promise, int n) {
                 for (int i = 0; i < n; ++i)
                     promise.addResult(0);
             }, 3)->results(),
             QList<double>({0, 0, 0}));
    QCOMPARE(Utils::asyncRun([](QPromise<double> &promise, int n) {
                 for (int i = 0; i < n; ++i)
                     promise.addResult(0);
             }, 3).results(),
             QList<double>({0, 0, 0}));

    // std::function
    const std::function<void(QPromise<double>&,int)> fun = [](QPromise<double> &promise, int n) {
        for (int i = 0; i < n; ++i)
            promise.addResult(0);
    };
    QCOMPARE(createAsyncTask(fun, 2)->results(),
             QList<double>({0, 0}));
    QCOMPARE(Utils::asyncRun(fun, 2).results(),
             QList<double>({0, 0}));

    // operator()
    QCOMPARE(createAsyncTask(Functor(), 3)->results(),
             QList<double>({0, 0, 0}));
    QCOMPARE(Utils::asyncRun(Functor(), 3).results(),
             QList<double>({0, 0, 0}));
    const Functor c{};
    QCOMPARE(createAsyncTask(c, 2)->results(),
             QList<double>({0, 0}));
    QCOMPARE(Utils::asyncRun(c, 2).results(),
             QList<double>({0, 0}));

    // static member functions
    QCOMPARE(createAsyncTask(&MyObject::staticMember0)->results(),
             QList<double>({0, 2, 1}));
    QCOMPARE(Utils::asyncRun(&MyObject::staticMember0).results(),
             QList<double>({0, 2, 1}));
    QCOMPARE(createAsyncTask(&MyObject::staticMember1, 2)->results(),
             QList<double>({0, 0}));
    QCOMPARE(Utils::asyncRun(&MyObject::staticMember1, 2).results(),
             QList<double>({0, 0}));

    // member functions
    const MyObject obj{};
    QCOMPARE(createAsyncTask(&MyObject::member0, &obj)->results(),
             QList<double>({0, 2, 1}));
    QCOMPARE(Utils::asyncRun(&MyObject::member0, &obj).results(),
             QList<double>({0, 2, 1}));
    QCOMPARE(createAsyncTask(&MyObject::member1, &obj, 4)->results(),
             QList<double>({0, 0, 0, 0}));
    QCOMPARE(Utils::asyncRun(&MyObject::member1, &obj, 4).results(),
             QList<double>({0, 0, 0, 0}));
    QCOMPARE(createAsyncTask(&MyObject::memberString1, &obj, s)->results(),
             QStringList({s}));
    QCOMPARE(Utils::asyncRun(&MyObject::memberString1, &obj, s).results(),
             QStringList({s}));
    QCOMPARE(createAsyncTask(&MyObject::memberString1, &obj, crs)->results(),
             QStringList({crs}));
    QCOMPARE(Utils::asyncRun(&MyObject::memberString1, &obj, crs).results(),
             QStringList({crs}));
    QCOMPARE(createAsyncTask(&MyObject::memberString1, &obj, cs)->results(),
             QStringList({cs}));
    QCOMPARE(Utils::asyncRun(&MyObject::memberString1, &obj, cs).results(),
             QStringList({cs}));
    QCOMPARE(createAsyncTask(&MyObject::memberString1, &obj, QString(QLatin1String("rvalue")))->results(),
             QStringList({QString(QLatin1String("rvalue"))}));
    QCOMPARE(Utils::asyncRun(&MyObject::memberString1, &obj, QString(QLatin1String("rvalue"))).results(),
             QStringList({QString(QLatin1String("rvalue"))}));
    QCOMPARE(createAsyncTask(&MyObject::memberString2, &obj, s)->results(),
             QStringList({s}));
    QCOMPARE(Utils::asyncRun(&MyObject::memberString2, &obj, s).results(),
             QStringList({s}));
    QCOMPARE(createAsyncTask(&MyObject::memberString2, &obj, crs)->results(),
             QStringList({crs}));
    QCOMPARE(Utils::asyncRun(&MyObject::memberString2, &obj, crs).results(),
             QStringList({crs}));
    QCOMPARE(createAsyncTask(&MyObject::memberString2, &obj, cs)->results(),
             QStringList({cs}));
    QCOMPARE(Utils::asyncRun(&MyObject::memberString2, &obj, cs).results(),
             QStringList({cs}));
    QCOMPARE(createAsyncTask(&MyObject::memberString2, &obj, QString(QLatin1String("rvalue")))->results(),
             QStringList({QString(QLatin1String("rvalue"))}));
    QCOMPARE(Utils::asyncRun(&MyObject::memberString2, &obj, QString(QLatin1String("rvalue"))).results(),
             QStringList({QString(QLatin1String("rvalue"))}));
    MyObject nonConstObj{};
    QCOMPARE(createAsyncTask(&MyObject::nonConstMember, &nonConstObj)->results(),
             QList<double>({0, 2, 1}));
    QCOMPARE(Utils::asyncRun(&MyObject::nonConstMember, &nonConstObj).results(),
             QList<double>({0, 2, 1}));
}

void tst_Async::crefFunction()
{
    // free function pointer with promise
    auto fun = &report3;
    QCOMPARE(createAsyncTask(std::cref(fun))->results(),
             QList<int>({0, 2, 1}));
    QCOMPARE(Utils::asyncRun(std::cref(fun)).results(),
             QList<int>({0, 2, 1}));

    // lambda with promise
    auto lambda = [](QPromise<double> &promise, int n) {
        for (int i = 0; i < n; ++i)
            promise.addResult(0);
    };
    QCOMPARE(createAsyncTask(std::cref(lambda), 3)->results(),
             QList<double>({0, 0, 0}));
    QCOMPARE(Utils::asyncRun(std::cref(lambda), 3).results(),
             QList<double>({0, 0, 0}));

    // std::function with promise
    const std::function<void(QPromise<double>&,int)> funObj = [](QPromise<double> &promise, int n) {
        for (int i = 0; i < n; ++i)
            promise.addResult(0);
    };
    QCOMPARE(createAsyncTask(std::cref(funObj), 2)->results(),
             QList<double>({0, 0}));
    QCOMPARE(Utils::asyncRun(std::cref(funObj), 2).results(),
             QList<double>({0, 0}));

    // callable with promise
    const Functor c{};
    QCOMPARE(createAsyncTask(std::cref(c), 2)->results(),
             QList<double>({0, 0}));
    QCOMPARE(Utils::asyncRun(std::cref(c), 2).results(),
             QList<double>({0, 0}));

    // member functions with promise
    auto member = &MyObject::member0;
    const MyObject obj{};
    QCOMPARE(createAsyncTask(std::cref(member), &obj)->results(),
             QList<double>({0, 2, 1}));
    QCOMPARE(Utils::asyncRun(std::cref(member), &obj).results(),
             QList<double>({0, 2, 1}));
}

class ObjWithProperty : public QObject
{
    Q_OBJECT

public slots:
    void setValue(const QString &s)
    {
        value = s;
    }

public:
    QString value;
};

void tst_Async::onResultReady()
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
    { // lambda
        QObject context;
        QFuture<QString> f = Utils::asyncRun([](QPromise<QString> &fi) {
            fi.addResult("Hi");
            fi.addResult("there");
        });
        int count = 0;
        QString res;
        Utils::onResultReady(f, &context, [&count, &res](const QString &s) {
            ++count;
            res = s;
        });
        f.waitForFinished();
        QCoreApplication::processEvents();
        QCOMPARE(count, 2);
        QCOMPARE(res, QString("there"));
    }
    { // lambda with guard
        QFuture<QString> f = Utils::asyncRun([](QPromise<QString> &fi) {
            fi.addResult("Hi");
            fi.addResult("there");
        });
        int count = 0;
        ObjWithProperty obj;
        Utils::onResultReady(f, &obj, [&count, &obj](const QString &s) {
            ++count;
            obj.setValue(s);
        });
        f.waitForFinished();
        QCoreApplication::processEvents();
        QCOMPARE(count, 2);
        QCOMPARE(obj.value, QString("there"));
    }
#endif
    { // member
        QFuture<QString> f = Utils::asyncRun([] { return QString("Hi"); });
        ObjWithProperty obj;
        Utils::onResultReady(f, &obj, &ObjWithProperty::setValue);
        f.waitForFinished();
        QCoreApplication::processEvents();
        QCOMPARE(obj.value, QString("Hi"));
    }
}

void tst_Async::futureSynchonizer()
{
    auto lambda = [](QPromise<int> &promise) {
        while (true) {
            if (promise.isCanceled()) {
                promise.future().cancel();
                promise.finish();
                return;
            }
            QThread::msleep(100);
        }
    };

    FutureSynchronizer synchronizer;
    synchronizer.setCancelOnWait(false);
    {
        Async<int> task;
        task.setConcurrentCallData(lambda);
        task.setFutureSynchronizer(&synchronizer);
        task.start();
        QThread::msleep(10);
        // We assume here that worker thread will still work for about 90 ms.
        QVERIFY(!task.isCanceled());
        QVERIFY(!task.isDone());
    }
    synchronizer.flushFinishedFutures();
    QVERIFY(!synchronizer.isEmpty());
    // The destructor of synchronizer should wait for about 90 ms for worker thread to be canceled
}

void multiplyBy2(QPromise<int> &promise, int input) { promise.addResult(input * 2); }

void tst_Async::taskTree()
{
    using namespace Tasking;

    int value = 1;

    const auto setupIntAsync = [&](Async<int> &task) {
        task.setConcurrentCallData(multiplyBy2, value);
    };
    const auto handleIntAsync = [&](const Async<int> &task) {
        value = task.result();
    };

    const Group recipe {
        AsyncTask<int>(setupIntAsync, handleIntAsync, CallDone::OnSuccess),
        AsyncTask<int>(setupIntAsync, handleIntAsync, CallDone::OnSuccess),
        AsyncTask<int>(setupIntAsync, handleIntAsync, CallDone::OnSuccess),
        AsyncTask<int>(setupIntAsync, handleIntAsync, CallDone::OnSuccess),
    };

    QCOMPARE(TaskTree::runBlocking(recipe.withTimeout(1000ms)), DoneWith::Success);
    QCOMPARE(value, 16);
}

static int returnxx(int x)
{
    return x * x;
}

static void returnxxWithPromise(QPromise<int> &promise, int x)
{
    promise.addResult(x * x);
}

static double s_sum = 0;
static QList<double> s_results;

void tst_Async::mapReduce_data()
{
    using namespace Tasking;

    QTest::addColumn<Group>("recipe");
    QTest::addColumn<double>("sum");
    QTest::addColumn<QList<double>>("results");

    const auto initTree = [] {
        s_sum = 0;
        s_results.append(s_sum);
    };
    const auto setupAsync = [](Async<int> &task, int input) {
        task.setConcurrentCallData(returnxx, input);
    };
    const auto setupAsyncWithFI = [](Async<int> &task, int input) {
        task.setConcurrentCallData(returnxxWithPromise, input);
    };
    const auto setupAsyncWithTP = [this](Async<int> &task, int input) {
        task.setConcurrentCallData(returnxx, input);
        task.setThreadPool(&m_threadPool);
    };
    const auto handleAsync = [](const Async<int> &task) {
        s_sum += task.result();
        s_results.append(task.result());
    };
    const auto handleTreeParallel = [] {
        s_sum /= 2;
        s_results.append(s_sum);
        Utils::sort(s_results); // mapping order is undefined
    };
    const auto handleTreeSequential = [] {
        s_sum /= 2;
        s_results.append(s_sum);
    };

    using namespace Tasking;
    using namespace std::placeholders;

    using SetupHandler = std::function<void(Async<int> &task, int input)>;
    using DoneHandler = std::function<void()>;

    const auto createTask = [=](const GroupItem &executeMode,
                                const SetupHandler &setupHandler,
                                const DoneHandler &doneHandler) {
        return Group {
            executeMode,
            onGroupSetup(initTree),
            AsyncTask<int>(std::bind(setupHandler, _1, 1), handleAsync, CallDone::OnSuccess),
            AsyncTask<int>(std::bind(setupHandler, _1, 2), handleAsync, CallDone::OnSuccess),
            AsyncTask<int>(std::bind(setupHandler, _1, 3), handleAsync, CallDone::OnSuccess),
            AsyncTask<int>(std::bind(setupHandler, _1, 4), handleAsync, CallDone::OnSuccess),
            AsyncTask<int>(std::bind(setupHandler, _1, 5), handleAsync, CallDone::OnSuccess),
            onGroupDone(doneHandler, CallDone::OnSuccess)
        };
    };

    const Group parallelRoot = createTask(parallel, setupAsync, handleTreeParallel);
    const Group parallelRootWithFI = createTask(parallel, setupAsyncWithFI, handleTreeParallel);
    const Group parallelRootWithTP = createTask(parallel, setupAsyncWithTP, handleTreeParallel);
    const Group sequentialRoot = createTask(sequential, setupAsync, handleTreeSequential);
    const Group sequentialRootWithFI = createTask(sequential, setupAsyncWithFI, handleTreeSequential);
    const Group sequentialRootWithTP = createTask(sequential, setupAsyncWithTP, handleTreeSequential);

    const double defaultSum = 27.5;
    const QList<double> defaultResult{0., 1., 4., 9., 16., 25., 27.5};

    QTest::newRow("Parallel") << parallelRoot << defaultSum << defaultResult;
    QTest::newRow("ParallelWithFutureInterface") << parallelRootWithFI << defaultSum << defaultResult;
    QTest::newRow("ParallelWithThreadPool") << parallelRootWithTP << defaultSum << defaultResult;
    QTest::newRow("Sequential") << sequentialRoot << defaultSum << defaultResult;
    QTest::newRow("SequentialWithFutureInterface") << sequentialRootWithFI << defaultSum << defaultResult;
    QTest::newRow("SequentialWithThreadPool") << sequentialRootWithTP << defaultSum << defaultResult;

    const auto setupSimpleAsync = [](Async<int> &task, int input) {
        task.setConcurrentCallData([](int input) { return input * 2; }, input);
    };
    const auto handleSimpleAsync = [](const Async<int> &task) {
        s_sum += task.result() / 4.;
        s_results.append(s_sum);
    };
    const Group simpleRoot = {
        sequential,
        onGroupSetup([] { s_sum = 0; }),
        AsyncTask<int>(std::bind(setupSimpleAsync, _1, 1), handleSimpleAsync, CallDone::OnSuccess),
        AsyncTask<int>(std::bind(setupSimpleAsync, _1, 2), handleSimpleAsync, CallDone::OnSuccess),
        AsyncTask<int>(std::bind(setupSimpleAsync, _1, 3), handleSimpleAsync, CallDone::OnSuccess)
    };
    QTest::newRow("Simple") << simpleRoot << 3.0 << QList<double>({.5, 1.5, 3.});

    const auto setupStringAsync = [](Async<int> &task, const QString &input) {
        task.setConcurrentCallData([](const QString &input) -> int { return input.size(); }, input);
    };
    const auto handleStringAsync = [](const Async<int> &task) {
        s_sum /= task.result();
    };
    const Group stringRoot = {
        parallel,
        onGroupSetup([] { s_sum = 90.0; }),
        AsyncTask<int>(std::bind(setupStringAsync, _1, "blubb"), handleStringAsync, CallDone::OnSuccess),
        AsyncTask<int>(std::bind(setupStringAsync, _1, "foo"), handleStringAsync, CallDone::OnSuccess),
        AsyncTask<int>(std::bind(setupStringAsync, _1, "blah"), handleStringAsync, CallDone::OnSuccess)
    };
    QTest::newRow("String") << stringRoot << 1.5 << QList<double>({});
}

void tst_Async::mapReduce()
{
    QThreadPool pool;

    s_sum = 0;
    s_results.clear();

    using namespace Tasking;

    QFETCH(Group, recipe);
    QFETCH(double, sum);
    QFETCH(QList<double>, results);

    QCOMPARE(TaskTree::runBlocking(recipe.withTimeout(1000ms)), DoneWith::Success);
    QCOMPARE(s_results, results);
    QCOMPARE(s_sum, sum);
}

QTEST_GUILESS_MAIN(tst_Async)

#include "tst_async.moc"