• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2017 Paul Fultz II
3     fold.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/fold.hpp>
8 #include "test.hpp"
9 
10 struct max_f
11 {
12     template<class T, class U>
operator ()max_f13     constexpr T operator()(T x, U y) const noexcept
14     {
15         return x > y ? x : y;
16     }
17 };
18 
19 struct sum_f
20 {
21     template<class T, class U>
operator ()sum_f22     constexpr T operator()(T x, U y) const BOOST_HOF_RETURNS_DEDUCE_NOEXCEPT(x+y)
23     {
24         return x + y;
25     }
26 };
27 
28 #if BOOST_HOF_HAS_NOEXCEPT_DEDUCTION
BOOST_HOF_TEST_CASE()29 BOOST_HOF_TEST_CASE()
30 {
31     static_assert(noexcept(boost::hof::fold(max_f(), 0)(2, 3, 4, 5)), "noexcept fold");
32     static_assert(noexcept(boost::hof::fold(sum_f(), 0)(2, 3, 4, 5)), "noexcept fold");
33     static_assert(!noexcept(boost::hof::fold(sum_f(), std::string())("hello", "-", "world")), "noexcept fold");
34 }
35 #endif
36 
BOOST_HOF_TEST_CASE()37 BOOST_HOF_TEST_CASE()
38 {
39     BOOST_HOF_TEST_CHECK(boost::hof::fold(max_f(), 0)(2, 3, 4, 5) == 5);
40     BOOST_HOF_TEST_CHECK(boost::hof::fold(max_f(), 0)(5, 4, 3, 2) == 5);
41     BOOST_HOF_TEST_CHECK(boost::hof::fold(max_f(), 0)(2, 3, 5, 4) == 5);
42 
43     BOOST_HOF_STATIC_TEST_CHECK(boost::hof::fold(max_f(), 0)(2, 3, 4, 5) == 5);
44     BOOST_HOF_STATIC_TEST_CHECK(boost::hof::fold(max_f(), 0)(5, 4, 3, 2) == 5);
45     BOOST_HOF_STATIC_TEST_CHECK(boost::hof::fold(max_f(), 0)(2, 3, 5, 4) == 5);
46 }
47 
BOOST_HOF_TEST_CASE()48 BOOST_HOF_TEST_CASE()
49 {
50     BOOST_HOF_TEST_CHECK(boost::hof::fold(max_f(), 0)() == 0);
51     BOOST_HOF_TEST_CHECK(boost::hof::fold(max_f(), 0)(5) == 5);
52 
53     BOOST_HOF_STATIC_TEST_CHECK(boost::hof::fold(max_f(), 0)() == 0);
54     BOOST_HOF_STATIC_TEST_CHECK(boost::hof::fold(max_f(), 0)(5) == 5);
55 }
56 
57 template<class... Ts>
58 constexpr auto find_positive_max(Ts... xs) BOOST_HOF_RETURNS
59 (
60     boost::hof::fold(max_f(), 0)(xs...)
61 );
62 
BOOST_HOF_TEST_CASE()63 BOOST_HOF_TEST_CASE()
64 {
65     BOOST_HOF_TEST_CHECK(find_positive_max() == 0);
66     BOOST_HOF_TEST_CHECK(find_positive_max(5) == 5);
67 
68     BOOST_HOF_STATIC_TEST_CHECK(find_positive_max() == 0);
69     BOOST_HOF_STATIC_TEST_CHECK(find_positive_max(5) == 5);
70 }
71 
BOOST_HOF_TEST_CASE()72 BOOST_HOF_TEST_CASE()
73 {
74     BOOST_HOF_TEST_CHECK(boost::hof::fold(max_f())(5) == 5);
75 
76     BOOST_HOF_STATIC_TEST_CHECK(boost::hof::fold(max_f())(5) == 5);
77 }
78 
BOOST_HOF_TEST_CASE()79 BOOST_HOF_TEST_CASE()
80 {
81     BOOST_HOF_TEST_CHECK(boost::hof::fold(max_f())(2, 3, 4, 5) == 5);
82     BOOST_HOF_TEST_CHECK(boost::hof::fold(max_f())(5, 4, 3, 2) == 5);
83     BOOST_HOF_TEST_CHECK(boost::hof::fold(max_f())(2, 3, 5, 4) == 5);
84 
85     BOOST_HOF_STATIC_TEST_CHECK(boost::hof::fold(max_f())(2, 3, 4, 5) == 5);
86     BOOST_HOF_STATIC_TEST_CHECK(boost::hof::fold(max_f())(5, 4, 3, 2) == 5);
87     BOOST_HOF_STATIC_TEST_CHECK(boost::hof::fold(max_f())(2, 3, 5, 4) == 5);
88 }
89 
BOOST_HOF_TEST_CASE()90 BOOST_HOF_TEST_CASE()
91 {
92     BOOST_HOF_TEST_CHECK(boost::hof::fold(sum_f(), std::string())("hello", "-", "world") == "hello-world");
93 }
94