Blame view

3rdparty/boost_1_81_0/libs/hof/test/proj.cpp 4.61 KB
73ef4ff3   Hu Chunming   提交三方库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  /*=============================================================================
      Copyright (c) 2017 Paul Fultz II
      proj.cpp
      Distributed under the Boost Software License, Version 1.0. (See accompanying
      file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  ==============================================================================*/
  #include <boost/hof/proj.hpp>
  #include <boost/hof/placeholders.hpp>
  #include <boost/hof/mutable.hpp>
  #include "test.hpp"
  
  #include <memory>
  
  struct foo
  {
      constexpr foo(int xp) : x(xp)
      {}
      int x;
  };
  
  struct select_x
  {
      template<class T>
      constexpr auto operator()(T&& x) const BOOST_HOF_RETURNS(x.x);
  };
  
  BOOST_HOF_TEST_CASE()
  {
  #ifndef _MSC_VER
      constexpr 
  #endif
      auto add = boost::hof::_ + boost::hof::_;
      BOOST_HOF_STATIC_TEST_CHECK(boost::hof::proj(select_x(), add)(foo(1), foo(2)) == 3);
      // Using mutable_ as a workaround on libc++, since mem_fn does not meet the
      // requirements of a FunctionObject
      BOOST_HOF_TEST_CHECK(boost::hof::proj(boost::hof::mutable_(std::mem_fn(&foo::x)), add)(foo(1), foo(2)) == 3);
      static_assert(boost::hof::detail::is_default_constructible<decltype(boost::hof::proj(select_x(), add))>::value, "Not default constructible");
  }
  
  BOOST_HOF_TEST_CASE()
  {
  #ifndef _MSC_VER
      constexpr 
  #endif
      auto add = boost::hof::_ + boost::hof::_;
      BOOST_HOF_STATIC_TEST_CHECK(boost::hof::proj(select_x(), add)(foo(1), foo(2)) == 3);
      BOOST_HOF_TEST_CHECK(boost::hof::proj(&foo::x, add)(foo(1), foo(2)) == 3);
      static_assert(boost::hof::detail::is_default_constructible<decltype(boost::hof::proj(select_x(), add))>::value, "Not default constructible");
  }
  
  BOOST_HOF_TEST_CASE()
  {
      auto indirect_add = boost::hof::proj(*boost::hof::_, boost::hof::_ + boost::hof::_);
      BOOST_HOF_TEST_CHECK(indirect_add(std::unique_ptr<int>(new int(1)), std::unique_ptr<int>(new int(2))) == 3);
      static_assert(boost::hof::detail::is_default_constructible<decltype(indirect_add)>::value, "Not default constructible");
  }
  
  struct select_x_1
  {
      std::unique_ptr<int> i;
      select_x_1() : i(new int(2))
      {}
      template<class T>
      auto operator()(T&& x) const BOOST_HOF_RETURNS(x.x * (*i));
  };
  
  struct sum_1
  {
      std::unique_ptr<int> i;
      sum_1() : i(new int(1))
      {}
      template<class T, class U>
      auto operator()(T&& x, U&& y) const BOOST_HOF_RETURNS(x + y + *i);
  };
  
  BOOST_HOF_TEST_CASE()
  {
      BOOST_HOF_TEST_CHECK(boost::hof::proj(select_x_1(), sum_1())(foo(1), foo(2)) == 7);
  }
  
  BOOST_HOF_TEST_CASE()
  {
      std::string s;
      auto f = [&](const std::string& x)
      {
          s += x;
      };
      boost::hof::proj(f)("hello", "-", "world");
      BOOST_HOF_TEST_CHECK(s == "hello-world");
      boost::hof::proj(f)();
      BOOST_HOF_TEST_CHECK(s == "hello-world");
  }
  
  BOOST_HOF_TEST_CASE()
  {
      std::string s;
      auto f = [&](const std::string& x)
      {
          s += x;
          return s;
      };
      auto last = [](const std::string& x, const std::string& y, const std::string& z)
      {
          BOOST_HOF_TEST_CHECK(x == "hello");
          BOOST_HOF_TEST_CHECK(y == "hello-");
          BOOST_HOF_TEST_CHECK(z == "hello-world");
          return z;
      };
      BOOST_HOF_TEST_CHECK(boost::hof::proj(f, last)("hello", "-", "world") == "hello-world");
  }
  
  template<bool B>
  struct bool_
  {
      static const bool value = B;
  };
  
  struct constexpr_check
  {
      template<class T>
      constexpr int operator()(T) const
      {
          static_assert(T::value, "Failed");
          return 0;
      }
  };
  
  struct constexpr_check_each
  {
      template<class T>
      constexpr bool operator()(T x) const
      {
          return boost::hof::proj(constexpr_check())(x, x), true;
      }
  };
  
  BOOST_HOF_TEST_CASE()
  {
      BOOST_HOF_STATIC_TEST_CHECK(constexpr_check_each()(bool_<true>()));
  }
  
  BOOST_HOF_TEST_CASE()
  {
      boost::hof::proj(boost::hof::identity, boost::hof::identity)(0);
  }
  
  struct bar {};
  
  BOOST_HOF_TEST_CASE()
  {
      auto f = boost::hof::proj(bar{});
      static_assert(!boost::hof::is_invocable<decltype(f), int>::value, "Not sfinae friendly");
      static_assert(!boost::hof::is_invocable<decltype(f), int, int>::value, "Not sfinae friendly");
      static_assert(!boost::hof::is_invocable<decltype(f), int, int, int>::value, "Not sfinae friendly");
  }
  
  BOOST_HOF_TEST_CASE()
  {
      auto f = boost::hof::proj(bar{}, bar{});
      static_assert(!boost::hof::is_invocable<decltype(f), int>::value, "Not sfinae friendly");
      static_assert(!boost::hof::is_invocable<decltype(f), int, int>::value, "Not sfinae friendly");
      static_assert(!boost::hof::is_invocable<decltype(f), int, int, int>::value, "Not sfinae friendly");
      static_assert(!boost::hof::is_invocable<decltype(f)>::value, "Not sfinae friendly");
  }