File: eager.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 (78 lines) | stat: -rw-r--r-- 1,962 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
//
// 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 <catch2/catch.hpp>

#include <zug/compose.hpp>
#include <zug/into_vector.hpp>
#include <zug/transducer/eager.hpp>
#include <zug/transducer/filter.hpp>
#include <zug/transducer/map.hpp>

#include "../spies.hpp"

using namespace zug;

TEST_CASE("eager, into_vector")
{
    auto v = std::vector<int>{1, 2, 3, 4};
    auto r = into_vector(eager(identity), v);
    CHECK(r == (decltype(r){1, 2, 3, 4}));
}

TEST_CASE("eager, sorted")
{
    auto v      = std::vector<int>{6, 2, 1, 12, 3};
    auto times2 = [](int x) { return x * 2; };
    auto div3   = [](int x) { return x % 3 == 0; };

    auto r =
        into(std::vector<int>{}, comp(map(times2), sorted, filter(div3)), v);
    CHECK(r == (decltype(r){6, 12, 24}));
}

TEST_CASE("eager, sorted example")
{
    // example1 {
    auto v = std::vector<int>{6, 2, 1, 12, 3};
    auto r = into_vector(sorted, v);
    CHECK(r == (decltype(r){1, 2, 3, 6, 12}));
    // }
}

TEST_CASE("eager, reversed")
{
    auto v      = std::vector<int>{1, 2, 3, 6, 12};
    auto times2 = [](int x) { return x * 2; };
    auto div3   = [](int x) { return x % 3 == 0; };

    auto r =
        into(std::vector<int>{}, comp(map(times2), reversed, filter(div3)), v);
    CHECK(r == (decltype(r){24, 12, 6}));
}

TEST_CASE("eager, reversed example")
{
    // example2 {
    auto v = std::vector<int>{1, 2, 3, 4, 5};
    auto r = into_vector(reversed, v);
    CHECK(r == (decltype(r){5, 4, 3, 2, 1}));
    // }
}

TEST_CASE("eager, moves_data_around")
{
    using elem = testing::copy_spy<>;

    auto x      = elem{};
    auto v      = std::vector<elem>{x, x, x, x};
    auto copies = x.copied.count();

    into_vector(reversed, std::move(v));
    CHECK(x.copied.count() == copies);
}