summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hof/test/mutable.cpp
blob: d071bc639e7b2372dd7240c4134ad4a23ebc0c0a (plain)
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
/*=============================================================================
    Copyright (c) 2017 Paul Fultz II
    mutable.cpp
    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#include <boost/hof/mutable.hpp>
#include <boost/hof/lazy.hpp>
#include <boost/hof/detail/move.hpp>
#include <memory>
#include "test.hpp"

struct mutable_fun
{
    int x;
    mutable_fun() noexcept
    : x(1)
    {}

    int operator()(int i) noexcept
    {
        x+=i;
        return x;
    }
};

#if BOOST_HOF_HAS_NOEXCEPT_DEDUCTION
BOOST_HOF_TEST_CASE()
{
    static_assert(noexcept(boost::hof::mutable_(mutable_fun())(3)), "noexcept mutable_");
}
#endif

BOOST_HOF_TEST_CASE()
{
    BOOST_HOF_TEST_CHECK(boost::hof::mutable_(mutable_fun())(3) == 4);
}

BOOST_HOF_TEST_CASE()
{
    auto mut_fun = mutable_fun();
    auto by_5 = boost::hof::lazy(boost::hof::mutable_(std::ref(mut_fun)))(5);
    BOOST_HOF_TEST_CHECK(by_5() == 6);
    BOOST_HOF_TEST_CHECK(by_5() == 11);
}

struct mutable_move_fun
{
    std::unique_ptr<int> x;
    mutable_move_fun() : x(new int(1))
    {}

    int operator()(int i)
    {
        *x+=i;
        return *x;
    }
};

BOOST_HOF_TEST_CASE()
{
    BOOST_HOF_TEST_CHECK(boost::hof::mutable_(mutable_move_fun())(3) == 4);
}

BOOST_HOF_TEST_CASE()
{
    auto mut_fun = mutable_move_fun();
    auto by_5 = boost::hof::lazy(boost::hof::mutable_(boost::hof::move(mut_fun)))(5);
    BOOST_HOF_TEST_CHECK(by_5() == 6);
    BOOST_HOF_TEST_CHECK(by_5() == 11);
}