File: qcorolazytask.cpp

package info (click to toggle)
qcoro 0.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,700 kB
  • sloc: cpp: 8,573; python: 32; xml: 26; makefile: 23; sh: 15
file content (175 lines) | stat: -rw-r--r-- 4,884 bytes parent folder | download | duplicates (2)
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
// SPDX-FileCopyrightText: 2024 Daniel Vrátil <dvratil@kde.org>
//
// SPDX-License-Identifier: MIT

#include "qcorotest.h"
#include "testobject.h"
#include "qcorotimer.h"
#include <qobject.h>
#include "qcorolazytask.h"

using namespace std::chrono_literals;

class QCoroLazyTaskTest : public QCoro::TestObject<QCoroLazyTaskTest>
{
    Q_OBJECT

private:
    QCoro::Task<> testSyncLazyCoroutineStarts_coro(QCoro::TestContext context) {
        context.setShouldNotSuspend();

        constexpr auto coro = [](bool &started) -> QCoro::LazyTask<> {
            started = true;
            co_return;
        };

        bool started = false;
        const auto task = coro(started);
        QCORO_VERIFY(!started);

        co_await task;

        QCORO_VERIFY(started);
    }

    QCoro::Task<> testLazyCoroutineStarts_coro(QCoro::TestContext) {
        constexpr auto coro = [](bool &started, bool &resumed) -> QCoro::LazyTask<> {
            started = true;
            co_await QCoro::sleepFor(1ms);
            resumed = true;
        };

        bool started = false;
        bool resumed = false;
        const auto task = coro(started, resumed);
        QCORO_VERIFY(!started);

        co_await task;

        QCORO_VERIFY(started);
        QCORO_VERIFY(resumed);
    }

    QCoro::Task<> testNonVoidSyncLazyCoroutineStarts_coro(QCoro::TestContext context) {
        context.setShouldNotSuspend();

        constexpr auto coro = [](bool &started) -> QCoro::LazyTask<int> {
            started = true;
            co_return 42;
        };

        bool started = false;
        const auto task = coro(started);
        QCORO_VERIFY(!started);

        const auto result = co_await task;

        QCORO_VERIFY(started);
        QCORO_COMPARE(result, 42);
    }

    QCoro::Task<> testNonVoidLazyCoroutineStarts_coro(QCoro::TestContext) {
        constexpr auto coro = [](bool &started, bool &resumed) -> QCoro::LazyTask<int> {
            started = true;
            co_await QCoro::sleepFor(1ms);
            resumed = true;
            co_return 42;
        };

        bool started = false;
        bool resumed = false;
        const auto task = coro(started, resumed);
        QCORO_VERIFY(!started);

        const auto result = co_await task;

        QCORO_VERIFY(started);
        QCORO_VERIFY(resumed);
        QCORO_COMPARE(result, 42);
    }

    QCoro::Task<> testEagerInsideLazy_coro(QCoro::TestContext) {
        constexpr auto coro = []() -> QCoro::LazyTask<int> {
            co_return co_await []() -> QCoro::Task<int> {
                co_await QCoro::sleepFor(1ms);
                co_return 42;
            }();
        };

        const auto task = coro();

        const auto result = co_await task;

        QCORO_COMPARE(result, 42);
    }

    QCoro::Task<> testThenLazyContinuation_coro(QCoro::TestContext) {
        constexpr auto coro = []() -> QCoro::LazyTask<int> {
            co_await QCoro::sleepFor(1ms);
            co_return 42;
        };

        const auto task = coro().then([](int result) -> QCoro::LazyTask<QString> {
            co_await QCoro::sleepFor(1ms);
            co_return QString::number(result);
        });

        const auto result = co_await task;
        QCORO_COMPARE(result, QStringLiteral("42"));
    }

    QCoro::Task<> testThenEagerContinuation_coro(QCoro::TestContext) {
        constexpr auto coro = []() -> QCoro::LazyTask<int> {
            co_await QCoro::sleepFor(1ms);
            co_return 42;
        };

        const auto task = coro().then([](int result) -> QCoro::Task<int> {
            co_await QCoro::sleepFor(1ms);
            co_return result;
        });

        const auto result = co_await task;
        QCORO_COMPARE(result, 42);
    }

    QCoro::Task<> testThenNonCoroutineContinuation_coro(QCoro::TestContext) {
        constexpr auto coro = []() -> QCoro::LazyTask<int> {
            co_await QCoro::sleepFor(1ms);
            co_return 42;
        };

        const auto task = coro().then([](int result) {
            return QString::number(result); 
        });
        static_assert(std::is_same_v<decltype(task), const QCoro::LazyTask<QString>>);

        const auto result = co_await task;
        QCORO_COMPARE(result, QStringLiteral("42"));
    }

private Q_SLOTS:
    addTest(SyncLazyCoroutineStarts)
    addTest(LazyCoroutineStarts)
    addTest(NonVoidSyncLazyCoroutineStarts)
    addTest(NonVoidLazyCoroutineStarts)
    addTest(EagerInsideLazy)
    addTest(ThenLazyContinuation)
    addTest(ThenEagerContinuation)
    addTest(ThenNonCoroutineContinuation)

    void testWaitFor() {
        auto coro = []() -> QCoro::LazyTask<int> {
            co_await QCoro::sleepFor(1ms);
            co_return 42;
        };

        const auto result = QCoro::waitFor(coro());
        QCOMPARE(result, 42);
    }
};


QTEST_GUILESS_MAIN(QCoroLazyTaskTest)

#include "qcorolazytask.moc"