1 /*=============================================================================
2 Copyright (c) 2017 Paul Fultz II
3 capture.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/capture.hpp>
8 #include <boost/hof/identity.hpp>
9 #include "test.hpp"
10
BOOST_HOF_TEST_CASE()11 BOOST_HOF_TEST_CASE()
12 {
13 BOOST_HOF_STATIC_TEST_CHECK(boost::hof::capture_basic(1, 2)(binary_class())() == 3);
14 BOOST_HOF_TEST_CHECK(boost::hof::capture_basic(1, 2)(binary_class())() == 3);
15
16 BOOST_HOF_STATIC_TEST_CHECK(boost::hof::capture_basic(1)(binary_class())(2) == 3);
17 BOOST_HOF_TEST_CHECK(boost::hof::capture_basic(1)(binary_class())(2) == 3);
18
19 BOOST_HOF_STATIC_TEST_CHECK(boost::hof::capture_basic()(binary_class())(1, 2) == 3);
20 BOOST_HOF_TEST_CHECK(boost::hof::capture_basic()(binary_class())(1, 2) == 3);
21
22 static const int one = 1;
23 static const int two = 2;
24 BOOST_HOF_STATIC_TEST_CHECK(boost::hof::capture_forward(one, two)(binary_class())() == 3);
25 BOOST_HOF_TEST_CHECK(boost::hof::capture_forward(one, two)(binary_class())() == 3);
26 BOOST_HOF_TEST_CHECK(boost::hof::capture_forward(1, 2)(binary_class())() == 3);
27
28 BOOST_HOF_STATIC_TEST_CHECK(boost::hof::capture_forward(one)(binary_class())(two) == 3);
29 BOOST_HOF_TEST_CHECK(boost::hof::capture_forward(1)(binary_class())(2) == 3);
30
31 BOOST_HOF_STATIC_TEST_CHECK(boost::hof::capture_forward()(binary_class())(one, two) == 3);
32 BOOST_HOF_TEST_CHECK(boost::hof::capture_forward()(binary_class())(one, two) == 3);
33
34 BOOST_HOF_STATIC_TEST_CHECK(boost::hof::capture(1, 2)(binary_class())() == 3);
35 BOOST_HOF_TEST_CHECK(boost::hof::capture(1, 2)(binary_class())() == 3);
36
37 BOOST_HOF_STATIC_TEST_CHECK(boost::hof::capture(1)(binary_class())(2) == 3);
38 BOOST_HOF_TEST_CHECK(boost::hof::capture(1)(binary_class())(2) == 3);
39
40 BOOST_HOF_STATIC_TEST_CHECK(boost::hof::capture()(binary_class())(1, 2) == 3);
41 BOOST_HOF_TEST_CHECK(boost::hof::capture()(binary_class())(1, 2) == 3);
42 }
43
44 struct add_member
45 {
46 int i;
47
add_memberadd_member48 add_member(int ip) : i(ip)
49 {}
50
addadd_member51 int add(int j) const
52 {
53 return i + j;
54 }
55 };
56
BOOST_HOF_TEST_CASE()57 BOOST_HOF_TEST_CASE()
58 {
59 BOOST_HOF_TEST_CHECK(boost::hof::capture_basic(add_member(1), 2)(&add_member::add)() == 3);
60 BOOST_HOF_TEST_CHECK(boost::hof::capture_basic(add_member(1))(&add_member::add)(2) == 3);
61 }
62
BOOST_HOF_TEST_CASE()63 BOOST_HOF_TEST_CASE()
64 {
65 auto id = boost::hof::identity;
66 auto f = boost::hof::capture(boost::hof::identity)(boost::hof::identity);
67 static_assert(BOOST_HOF_IS_DEFAULT_CONSTRUCTIBLE(decltype(id)), "Id not default constructible");
68 static_assert(BOOST_HOF_IS_DEFAULT_CONSTRUCTIBLE(decltype(f)), "Not default constructible");
69 f();
70 }
71 #if BOOST_HOF_HAS_NOEXCEPT_DEDUCTION
BOOST_HOF_TEST_CASE()72 BOOST_HOF_TEST_CASE()
73 {
74 static_assert(noexcept(boost::hof::capture(boost::hof::identity)(boost::hof::identity)()), "noexcept capture");
75 static_assert(noexcept(boost::hof::capture_basic(boost::hof::identity)(boost::hof::identity)()), "noexcept capture");
76 static_assert(noexcept(boost::hof::capture_forward(boost::hof::identity)(boost::hof::identity)()), "noexcept capture");
77 }
78 #endif
BOOST_HOF_TEST_CASE()79 BOOST_HOF_TEST_CASE()
80 {
81 auto f = boost::hof::capture_basic(boost::hof::identity)(boost::hof::identity);
82 f();
83 }
84
BOOST_HOF_TEST_CASE()85 BOOST_HOF_TEST_CASE()
86 {
87 auto f = boost::hof::capture_forward(boost::hof::identity)(boost::hof::identity);
88 f();
89 }
90
BOOST_HOF_TEST_CASE()91 BOOST_HOF_TEST_CASE()
92 {
93 auto f = boost::hof::capture(boost::hof::identity)(add_member{1});
94 static_assert(!boost::hof::is_invocable<decltype(f), int>::value, "Not sfinae friendly");
95 }
96
97