lazy.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <felspar/coro/lazy.hpp>
#include <felspar/memory/slab.storage.hpp>
#include <felspar/test.hpp>


namespace {


    auto const lazy =
            felspar::testsuite("lazy")
                    .test("unevaluated",
                          [](auto check) {
                              bool run = false;
                              auto const notrun =
                                      [&]() -> felspar::coro::lazy<int> {
                                  run = true;
                                  co_return 0;
                              };
                              auto c = notrun();
                              check(run).is_falsey();
                          })
                    .test("evaluated_by_get",
                          [](auto check) {
                              std::size_t runs = 0;
                              auto const notrun =
                                      [&]() -> felspar::coro::lazy<int> {
                                  ++runs;
                                  co_return 42;
                              };
                              auto c = notrun();
                              check(runs) == 0u;
                              check(c()) == 42;
                              check(runs) == 1u;
                              check(c()) == 42;
                              check(runs) == 1u;
                              check(c()) == 42;
                              check(runs) == 1u;
                          })
                    .test("throws", [](auto check) {
                        auto const thrower = []() -> felspar::coro::lazy<int> {
                            throw std::runtime_error{"Test exception"};
                        };
                        check([&]() {
                            thrower()();
                        }).throws(std::runtime_error{"Test exception"});
                    });


#ifndef NDEBUG
    felspar::coro::lazy<int, felspar::memory::slab_storage<>>
            track_run(felspar::memory::slab_storage<> &, bool &r) {
        r = true;
        co_return 1234;
    }
    auto const af = lazy.test("allocator/function", [](auto check) {
        felspar::memory::slab_storage<> slab;
        auto const start_free = slab.free();
        auto run = false;

        auto lazy = track_run(slab, run);
        check(slab.free()) < start_free;
        check(run) == false;

        check(lazy()) == 1234;
        check(run) == true;
    });
    auto const am = lazy.test("allocator/member", [](auto check) {
        felspar::memory::slab_storage<> slab;
        auto const start_free = slab.free();
        auto run = false;

        auto const coro = [](auto &, bool &r)
                -> felspar::coro::lazy<int, felspar::memory::slab_storage<>> {
            r = true;
            co_return 42;
        };

        auto lazy = coro(slab, run);
        check(slab.free()) < start_free;
        check(run) == false;

        check(lazy()) == 42;
        check(run) == true;
    });
#endif


}