1 /*=============================================================================
2 Copyright (c) 2017 Paul Fultz II
3 infix.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/infix.hpp>
8 #include <boost/hof/function.hpp>
9 #include <boost/hof/lambda.hpp>
10 #include <boost/hof/pipable.hpp>
11 #include <boost/hof/placeholders.hpp>
12 #include "test.hpp"
13
14 struct sum_f
15 {
16 template<class T, class U>
operator ()sum_f17 constexpr T operator()(T x, U y) const BOOST_HOF_RETURNS_DEDUCE_NOEXCEPT(x+y)
18 {
19 return x+y;
20 }
21 };
22
23 static constexpr boost::hof::infix_adaptor<sum_f> sum = {};
24
25 #if BOOST_HOF_HAS_NOEXCEPT_DEDUCTION
BOOST_HOF_TEST_CASE()26 BOOST_HOF_TEST_CASE()
27 {
28 static_assert(noexcept(1 <sum> 2), "noexcept infix");
29 static_assert(!noexcept(std::string() <sum> std::string()), "noexcept infix");
30 }
31 #endif
32
BOOST_HOF_TEST_CASE()33 BOOST_HOF_TEST_CASE()
34 {
35 BOOST_HOF_TEST_CHECK(3 == (1 <sum> 2));
36 BOOST_HOF_STATIC_TEST_CHECK(3 == (1 <sum> 2));
37
38 BOOST_HOF_TEST_CHECK(3 == (sum(1, 2)));
39 BOOST_HOF_STATIC_TEST_CHECK(3 == (sum(1, 2)));
40 }
41
42 BOOST_HOF_STATIC_FUNCTION(sum1) = boost::hof::infix(sum_f());
43
BOOST_HOF_TEST_CASE()44 BOOST_HOF_TEST_CASE()
45 {
46 BOOST_HOF_TEST_CHECK(3 == (1 <sum1> 2));
47 BOOST_HOF_STATIC_TEST_CHECK(3 == (1 <sum1> 2));
48
49 BOOST_HOF_TEST_CHECK(3 == (sum1(1, 2)));
50 BOOST_HOF_STATIC_TEST_CHECK(3 == (sum1(1, 2)));
51 }
52
__anonaa77e7dd0102(int x, int y) 53 BOOST_HOF_STATIC_LAMBDA_FUNCTION(sum2) = boost::hof::infix([](int x, int y) { return x + y; });
54
BOOST_HOF_TEST_CASE()55 BOOST_HOF_TEST_CASE()
56 {
57 BOOST_HOF_TEST_CHECK(3 == (1 <sum2> 2));
58
59 BOOST_HOF_TEST_CHECK(3 == (sum2(1, 2)));
60 }
61
62 BOOST_HOF_STATIC_FUNCTION(sum3) = boost::hof::infix(boost::hof::_ + boost::hof::_);
63
BOOST_HOF_TEST_CASE()64 BOOST_HOF_TEST_CASE()
65 {
66 BOOST_HOF_TEST_CHECK(3 == (1 <sum3> 2));
67 BOOST_HOF_STATIC_TEST_CHECK(3 == (1 <sum3> 2));
68
69 BOOST_HOF_TEST_CHECK(3 == (sum3(1, 2)));
70 BOOST_HOF_STATIC_TEST_CHECK(3 == (sum3(1, 2)));
71 }
72
73
__anonaa77e7dd0202(int x, int y) 74 BOOST_HOF_STATIC_LAMBDA_FUNCTION(sum4) = boost::hof::infix(boost::hof::infix([](int x, int y) { return x + y; }));
75
BOOST_HOF_TEST_CASE()76 BOOST_HOF_TEST_CASE()
77 {
78 BOOST_HOF_TEST_CHECK(3 == (1 <sum4> 2));
79
80 BOOST_HOF_TEST_CHECK(3 == (sum4(1, 2)));
81 }
82
83 BOOST_HOF_STATIC_FUNCTION(sum5) = boost::hof::infix(boost::hof::infix(boost::hof::_ + boost::hof::_));
84
BOOST_HOF_TEST_CASE()85 BOOST_HOF_TEST_CASE()
86 {
87 BOOST_HOF_TEST_CHECK(3 == (1 <sum5> 2));
88 BOOST_HOF_STATIC_TEST_CHECK(3 == (1 <sum5> 2));
89
90 BOOST_HOF_TEST_CHECK(3 == (sum5(1, 2)));
91 BOOST_HOF_STATIC_TEST_CHECK(3 == (sum5(1, 2)));
92 }
93
BOOST_HOF_TEST_CASE()94 BOOST_HOF_TEST_CASE()
95 {
96 #if (defined(__GNUC__) && !defined (__clang__))
97 #pragma GCC diagnostic push
98 #pragma GCC diagnostic ignored "-Wparentheses"
99 #endif
100 BOOST_HOF_TEST_CHECK(6 == (1 + 2 <sum> 3));
101 BOOST_HOF_TEST_CHECK(3 == 1 <sum> 2);
102 #if (defined(__GNUC__) && !defined (__clang__))
103 #pragma GCC diagnostic pop
104 #endif
105 }
106
107 struct foo {};
108
BOOST_HOF_TEST_CASE()109 BOOST_HOF_TEST_CASE()
110 {
111 auto f = boost::hof::infix([](int, int) { return foo{}; });
112 auto g = boost::hof::infix([](foo, foo) { return std::string("hello"); });
113 BOOST_HOF_TEST_CHECK((1 <f> 2 <g> foo{}) == "hello");
114
115 }
116