• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2017 Paul Fultz II
3     function.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/function.hpp>
8 #include <boost/hof/partial.hpp>
9 #include <boost/hof/infix.hpp>
10 #include <memory>
11 #include "test.hpp"
12 
13 namespace test_constexpr {
14 
15 struct sum_f
16 {
17     template<class T, class U>
operator ()test_constexpr::sum_f18     constexpr T operator()(T x, U y) const
19     {
20         return x+y;
21     }
22 };
23 
24 BOOST_HOF_STATIC_FUNCTION(sum_init) = sum_f();
25 
BOOST_HOF_TEST_CASE()26 BOOST_HOF_TEST_CASE()
27 {
28 // TODO: Should be empty on MSVC as well
29 #ifndef _MSC_VER
30     STATIC_ASSERT_EMPTY(sum_init);
31 #endif
32     BOOST_HOF_TEST_CHECK(3 == sum_init(1, 2));
33 
34     BOOST_HOF_STATIC_TEST_CHECK(3 == sum_init(1, 2));
35 }
36 
37 BOOST_HOF_STATIC_FUNCTION(sum_partial) = boost::hof::partial(sum_f());
BOOST_HOF_TEST_CASE()38 BOOST_HOF_TEST_CASE()
39 {
40 #ifndef _MSC_VER
41     STATIC_ASSERT_EMPTY(sum_partial);
42 #endif
43     BOOST_HOF_TEST_CHECK(3 == sum_partial(1, 2));
44     BOOST_HOF_TEST_CHECK(3 == sum_partial(1)(2));
45 
46     BOOST_HOF_STATIC_TEST_CHECK(3 == sum_partial(1, 2));
47     BOOST_HOF_STATIC_TEST_CHECK(3 == sum_partial(1)(2));
48 }
49 
50 }
51 
52 namespace test_static {
53 
54 struct sum_f
55 {
56     template<class T, class U>
operator ()test_static::sum_f57     constexpr T operator()(T x, U y) const
58     {
59         return x+y;
60     }
61 };
62 
63 struct add_one_f
64 {
65     template<class T>
operator ()test_static::add_one_f66     constexpr T operator()(T x) const
67     {
68         return x+1;
69     }
70 };
71 
72 BOOST_HOF_STATIC_FUNCTION(sum_partial) = boost::hof::partial(sum_f());
73 
BOOST_HOF_TEST_CASE()74 BOOST_HOF_TEST_CASE()
75 {
76 #ifndef _MSC_VER
77     STATIC_ASSERT_EMPTY(sum_partial);
78 #endif
79     BOOST_HOF_TEST_CHECK(3 == sum_partial(1, 2));
80     BOOST_HOF_TEST_CHECK(3 == sum_partial(1)(2));
81 }
82 
83 BOOST_HOF_STATIC_FUNCTION(add_one_pipable) = boost::hof::pipable(add_one_f());
84 
BOOST_HOF_TEST_CASE()85 BOOST_HOF_TEST_CASE()
86 {
87 // TODO: Make this work on msvc
88 #ifndef _MSC_VER
89     STATIC_ASSERT_EMPTY(add_one_pipable);
90 #endif
91     BOOST_HOF_TEST_CHECK(3 == add_one_pipable(2));
92     BOOST_HOF_TEST_CHECK(3 == (2 | add_one_pipable));
93 }
94 
95 BOOST_HOF_STATIC_FUNCTION(sum_infix) = boost::hof::infix(sum_f());
96 
BOOST_HOF_TEST_CASE()97 BOOST_HOF_TEST_CASE()
98 {
99 // TODO: Make this work on msvc
100 #ifndef _MSC_VER
101     STATIC_ASSERT_EMPTY(sum_infix);
102 #endif
103     BOOST_HOF_TEST_CHECK(3 == (1 <sum_infix> 2));
104 }
105 
106 }
107