starter.cpp

 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
#include <felspar/coro/start.hpp>
#include <felspar/exceptions.hpp>
#include <felspar/test.hpp>


namespace {


    felspar::coro::task<void> co_throw(felspar::source_location loc) {
        throw felspar::stdexcept::runtime_error{"A test exception", loc};
        co_return;
    }
    felspar::coro::task<bool> co_true() { co_return true; }
    felspar::coro::task<bool> co_false() { co_return false; }
    felspar::coro::task<bool> co_throw_bool(felspar::source_location loc) {
        throw felspar::stdexcept::runtime_error{"A test exception", loc};
        co_return true;
    }


    auto const s = felspar::testsuite(
            "starter",
            [](auto check) {
                felspar::coro::starter<> s;
                s.post(co_throw, felspar::source_location::current());
                check(s.size()) == 1u;
                s.garbage_collect_completed();
                check(s.size()) == 0u;
            },
            [](auto check) {
                felspar::coro::starter<felspar::coro::task<bool>> s;
                s.post(co_true);
                check(s.size()) == 1u;
                s.garbage_collect_completed();
                check(s.size()) == 0u;
            },
            [](auto check) {
                felspar::coro::starter<> s;
                s.post(co_throw, felspar::source_location::current());
                check(s.size()) == 1u;
                check([&]() { s.wait_for_all().get(); })
                        .throws(felspar::stdexcept::runtime_error{
                                "A test exception"});
                check(s.size()) == 0u;
            },
            [](auto check) {
                felspar::coro::starter<felspar::coro::task<bool>> s;
                s.post(co_true);
                check(s.size()) == 1u;
                check(s.wait_for_all().get()) == 1u;
                check(s.size()) == 0u;
            },
            [](auto check) {
                felspar::coro::starter<felspar::coro::task<bool>> s;
                s.post(co_true);
                s.post(co_throw_bool, felspar::source_location::current());
                s.post(co_false);
                check(s.size()) == 3u;
                check(s.next().get()) == false;
                check([&]() { s.next().get(); })
                        .throws(felspar::stdexcept::runtime_error{
                                "A test exception"});
                check(s.next().get()) == true;
                check(s.size()) == 0u;
            });


}