1 /*=============================================================================
2 Copyright (c) 2017 Paul Fultz II
3 apply_eval.cpp
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 #include <boost/hof/apply_eval.hpp>
8 #include <boost/hof/always.hpp>
9 #include <boost/hof/placeholders.hpp>
10 #include "test.hpp"
11
12 #include <memory>
13
BOOST_HOF_TEST_CASE()14 BOOST_HOF_TEST_CASE()
15 {
16 BOOST_HOF_STATIC_TEST_CHECK(boost::hof::apply_eval(binary_class(), boost::hof::always(1), boost::hof::always(2)) == 3);
17 BOOST_HOF_TEST_CHECK(boost::hof::apply_eval(binary_class(), []{ return 1; }, []{ return 2;}) == 3);
18 }
19
BOOST_HOF_TEST_CASE()20 BOOST_HOF_TEST_CASE()
21 {
22 boost::hof::apply_eval(boost::hof::always(), boost::hof::always(1), boost::hof::always(2));
23 }
24 #if BOOST_HOF_HAS_NOEXCEPT_DEDUCTION
BOOST_HOF_TEST_CASE()25 BOOST_HOF_TEST_CASE()
26 {
27 static_assert(noexcept(boost::hof::apply_eval(boost::hof::always(), boost::hof::always(1), boost::hof::always(2))), "noexcept apply_eval");
28 }
29 #endif
BOOST_HOF_TEST_CASE()30 BOOST_HOF_TEST_CASE()
31 {
32 int i = 3;
33 BOOST_HOF_TEST_CHECK(boost::hof::apply_eval(boost::hof::_ - boost::hof::_, [&]{ return i++; }, [&]{ return i++;}) == -1);
34 BOOST_HOF_TEST_CHECK(boost::hof::apply_eval(boost::hof::_ - boost::hof::_, [&]{ return ++i; }, [&]{ return ++i;}) == -1);
35 }
36
37 struct indirect_sum_f
38 {
39 template<class T, class U>
40 auto operator()(T x, U y) const
41 BOOST_HOF_RETURNS(*x + *y);
42 };
43
BOOST_HOF_TEST_CASE()44 BOOST_HOF_TEST_CASE()
45 {
46 BOOST_HOF_TEST_CHECK(
47 boost::hof::apply_eval(
48 indirect_sum_f(),
49 []{ return std::unique_ptr<int>(new int(1)); },
50 []{ return std::unique_ptr<int>(new int(2)); })
51 == 3);
52 }
53
moveable(int i)54 std::unique_ptr<int> moveable(int i)
55 {
56 return std::unique_ptr<int>{new int(i)};
57 }
58
BOOST_HOF_TEST_CASE()59 BOOST_HOF_TEST_CASE()
60 {
61 BOOST_HOF_TEST_CHECK(*boost::hof::apply_eval(&moveable, boost::hof::always(1)) == 1);
62 BOOST_HOF_TEST_CHECK(*boost::hof::apply_eval(&moveable, boost::hof::always(3)) == 3);
63 }
64