File: transducer.cpp

package info (click to toggle)
zug 0.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,076 kB
  • sloc: cpp: 6,209; makefile: 203; sh: 88; python: 62
file content (80 lines) | stat: -rw-r--r-- 2,139 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
#include <zug/compose.hpp>
#include <zug/reducing/last.hpp>
#include <zug/transduce.hpp>
#include <zug/transducer/map.hpp>
#include <zug/transducer/transducer.hpp>

#include <catch2/catch.hpp>

using namespace zug;

TEST_CASE("transducer: comp composition")
{
    auto add_one = transducer<int>(map([](auto x) { return x + 1; }));

    auto xform  = comp(add_one, add_one);
    auto result = transduce(xform, last, 0, std::vector<int>{10});

    CHECK(result == 12);
}

TEST_CASE("transducer: operator| composition")
{
    auto add_one = transducer<int>(map([](auto x) { return x + 1; }));

    auto xform  = add_one | add_one;
    auto result = transduce(xform, last, 0, std::vector<int>{10});

    CHECK(result == 12);
}

TEST_CASE("transducer: operator| composition with non type-erased transducer")
{
    auto add_one = transducer<int>(map([](auto x) { return x + 1; }));
    auto add_two = map([](auto x) { return x + 2; });

    SECTION("type-erased | non type-erased")
    {
        auto xform  = add_one | add_two;
        auto result = transduce(xform, last, 0, std::vector<int>{10});

        CHECK(result == 13);
    }

    SECTION("non type-erased | type-erased")
    {
        auto xform  = add_two | add_one;
        auto result = transduce(xform, last, 0, std::vector<int>{10});

        CHECK(result == 13);
    }
}

TEST_CASE(
    "transducer: operator| composition with non-composable type transducer")
{
    auto add_five = [](auto&& step) {
        return [=](auto&& s, auto&&... is) mutable {
            return step(
                ZUG_FWD(s),
                compat::invoke([](auto x) { return x + 5; }, ZUG_FWD(is)...));
        };
    };
    auto add_one = transducer<int>(map([](auto x) { return x + 1; }));

    SECTION("transducer | non-composable")
    {
        auto xform  = add_one | add_five;
        auto result = transduce(xform, last, 0, std::vector<int>{660});

        CHECK(result == 666);
    }

    SECTION("non-composable | transducer")
    {
        auto xform  = add_five | add_one;
        auto result = transduce(xform, last, 0, std::vector<int>{660});

        CHECK(result == 666);
    }
}