Blame view

3rdparty/boost_1_81_0/libs/yap/test/comma.cpp 2.81 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
  // Copyright (C) 2016-2018 T. Zachary Laine
  //
  // 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/yap/expression.hpp>
  
  #include <boost/mpl/assert.hpp>
  
  #include <boost/core/lightweight_test.hpp>
  
  #include <sstream>
  
  
  template<typename T>
  using term = boost::yap::terminal<boost::yap::expression, T>;
  
  template<typename T>
  using term_ref = boost::yap::expression_ref<boost::yap::expression, term<T> &>;
  
  template<typename T>
  using term_cref =
      boost::yap::expression_ref<boost::yap::expression, term<T> const &>;
  
  namespace yap = boost::yap;
  namespace bh = boost::hana;
  
  
  struct void_callable
  {
      void operator()() { *called_ = (*call_count_)++; }
      int * call_count_;
      int * called_;
  };
  
  struct int_callable
  {
      int operator()()
      {
          *called_ = (*call_count_)++;
          return 42;
      }
      int * call_count_;
      int * called_;
  };
  
  struct double_callable
  {
      double operator()()
      {
          *called_ = (*call_count_)++;
          return 13.0;
      }
      int * call_count_;
      int * called_;
  };
  
  
  int main()
  {
  {
      {
          int call_count = 0;
          int int_called = -1;
          int double_called = -1;
  
          auto int_double_expr =
              (term<int_callable>{{&call_count, &int_called}}(),
               term<double_callable>{{&call_count, &double_called}}());
  
          BOOST_TEST(evaluate(int_double_expr) == 13.0);
          BOOST_TEST(int_called == 0);
          BOOST_TEST(double_called == 1);
      }
  
      {
          int call_count = 0;
          int int_called = -1;
          int double_called = -1;
  
          auto double_int_expr =
              (term<double_callable>{{&call_count, &double_called}}(),
               term<int_callable>{{&call_count, &int_called}}());
  
          BOOST_TEST(evaluate(double_int_expr) == 42);
          BOOST_TEST(int_called == 1);
          BOOST_TEST(double_called == 0);
      }
  }
  
  {
      {
          int call_count = 0;
          int void_called = -1;
          int int_called = -1;
  
          auto void_int_expr =
              (term<void_callable>{{&call_count, &void_called}}(),
               term<int_callable>{{&call_count, &int_called}}());
  
          BOOST_TEST(evaluate(void_int_expr) == 42);
          BOOST_TEST(void_called == 0);
          BOOST_TEST(int_called == 1);
      }
  
      {
          int call_count = 0;
          int void_called = -1;
          int int_called = -1;
  
          auto int_void_expr =
              (term<int_callable>{{&call_count, &int_called}}(),
               term<void_callable>{{&call_count, &void_called}}());
  
          using eval_type = decltype(evaluate(int_void_expr));
          BOOST_MPL_ASSERT(
              (std::is_same<void, eval_type>));
  
          evaluate(int_void_expr);
          BOOST_TEST(void_called == 1);
          BOOST_TEST(int_called == 0);
      }
  }
  
  return boost::report_errors();
  }