File: state_traits.cpp

package info (click to toggle)
zug 0.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,052 kB
  • sloc: cpp: 6,168; makefile: 203; sh: 88; python: 62
file content (126 lines) | stat: -rw-r--r-- 3,413 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
//
// zug: transducers for C++
// Copyright (C) 2019 Juan Pedro Bolivar Puente
//
// This software is distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
//

#include <zug/any_state.hpp>
#include <zug/compose.hpp>
#include <zug/reducing/last.hpp>
#include <zug/state_wrapper.hpp>
#include <zug/transducer/enumerate.hpp>
#include <zug/transducer/filter.hpp>
#include <zug/transducer/take.hpp>
#include <zug/transducer/transducer.hpp>
#include <zug/util.hpp>

#include <catch2/catch.hpp>

using namespace zug;

TEST_CASE("state_traits, unwrap all wrap state")
{
    CHECK(state_unwrap_all(42) == 42);
    CHECK(state_unwrap_all(wrap_state(42, {})) == 42);
    CHECK(state_unwrap_all(wrap_state(wrap_state(42, {}), "")) == 42);
}

TEST_CASE("state_traits, unwrap all skip state")
{
    auto odd = [](int x) { return x % 2; };
    auto rf  = comp(filter(odd), take(10))(last);

    auto s = rf(int{}, 41);
    CHECK(state_unwrap_all(s) == 41);
    s = rf(s, 13);
    CHECK(state_unwrap_all(s) == 13);
}

TEST_CASE("state_traits, unwrap all type erased")
{
    auto rf = transducer<int>{identity}(last);

    auto s = rf(int{}, 41);
    CHECK(state_unwrap_all(s) == 41);
    s = rf(s, 13);
    CHECK(state_unwrap_all(s) == 13);
}

TEST_CASE("state_traits, unwrap all complex")
{
    auto odd = [](int x) { return x % 2; };
    auto rf  = transducer<int>{comp(filter(odd), take(10))}(last);

    auto s = rf(int{}, 41);
    CHECK(state_unwrap_all(s) == 41);
    s = rf(s, 13);
    CHECK(state_unwrap_all(s) == 13);
}

TEST_CASE("state_traits, unwrap all moar complex")
{
    auto odd = [](std::size_t x) { return x % 2; };
    auto rf  = transducer<meta::pack<>, std::size_t>{
        comp(enumerate, filter(odd), take(10))}(last);

    auto s = rf(std::size_t{});
    CHECK(state_unwrap_all(s) == 0);
    s = rf(s);
    CHECK(state_unwrap_all(s) == 1);
    s = rf(s);
    CHECK(state_unwrap_all(s) == 1);
    s = rf(s);
    CHECK(state_unwrap_all(s) == 3);
}

TEST_CASE("state_traits, rewrap wrap state")
{
    struct t1 : meta::pack<t1>
    {};
    struct t2 : meta::pack<t2>
    {};
    CHECK(state_rewrap(42, 13) == 13);
    CHECK(state_rewrap(wrap_state<t1>(42, {}), 13) == wrap_state<t1>(13, {}));
    CHECK(state_rewrap(wrap_state<t2>(wrap_state<t1>(42, {}), ""), 13) ==
          wrap_state<t2>(wrap_state<t1>(13, {}), ""));
}

TEST_CASE("state_traits, rewrap skip state")
{
    auto odd = [](int x) { return x % 2; };
    auto rf  = comp(filter(odd), take(3))(last);

    auto s = rf(int{}, 41);
    s      = state_rewrap(s, 13);
    CHECK(state_unwrap_all(s) == 13);
    s = rf(rf(rf(s, 1), 2), 3);
    CHECK(state_unwrap_all(s) == 3);
    CHECK(state_is_reduced(s));
}

TEST_CASE("state_traits, rewrap type erased")
{
    auto rf = transducer<int>{identity}(last);

    auto s = rf(int{}, 41);
    s      = state_rewrap(s, 13);
    CHECK(state_unwrap_all(s) == 13);
}

TEST_CASE("state_traits, rewrap moar complex")
{
    auto odd = [](std::size_t x) { return x % 2; };
    auto rf  = transducer<meta::pack<>, std::size_t>{
        comp(enumerate, filter(odd), take(10))}(last);

    auto s = rf(std::size_t{});
    s      = rf(s);
    s      = state_rewrap(s, std::size_t{13});
    CHECK(state_unwrap_all(s) == 13);
    s = rf(s);
    CHECK(state_unwrap_all(s) == 13);
    s = rf(s);
    CHECK(state_unwrap_all(s) == 3);
}