comma.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright (C) 2016-2018 T. Zachary Laine
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/yap/expression.hpp>
  7. #include <boost/mpl/assert.hpp>
  8. #include <boost/test/minimal.hpp>
  9. #include <sstream>
  10. template<typename T>
  11. using term = boost::yap::terminal<boost::yap::expression, T>;
  12. template<typename T>
  13. using term_ref = boost::yap::expression_ref<boost::yap::expression, term<T> &>;
  14. template<typename T>
  15. using term_cref =
  16. boost::yap::expression_ref<boost::yap::expression, term<T> const &>;
  17. namespace yap = boost::yap;
  18. namespace bh = boost::hana;
  19. struct void_callable
  20. {
  21. void operator()() { *called_ = (*call_count_)++; }
  22. int * call_count_;
  23. int * called_;
  24. };
  25. struct int_callable
  26. {
  27. int operator()()
  28. {
  29. *called_ = (*call_count_)++;
  30. return 42;
  31. }
  32. int * call_count_;
  33. int * called_;
  34. };
  35. struct double_callable
  36. {
  37. double operator()()
  38. {
  39. *called_ = (*call_count_)++;
  40. return 13.0;
  41. }
  42. int * call_count_;
  43. int * called_;
  44. };
  45. int test_main(int, char * [])
  46. {
  47. {
  48. {
  49. int call_count = 0;
  50. int int_called = -1;
  51. int double_called = -1;
  52. auto int_double_expr =
  53. (term<int_callable>{{&call_count, &int_called}}(),
  54. term<double_callable>{{&call_count, &double_called}}());
  55. BOOST_CHECK(evaluate(int_double_expr) == 13.0);
  56. BOOST_CHECK(int_called == 0);
  57. BOOST_CHECK(double_called == 1);
  58. }
  59. {
  60. int call_count = 0;
  61. int int_called = -1;
  62. int double_called = -1;
  63. auto double_int_expr =
  64. (term<double_callable>{{&call_count, &double_called}}(),
  65. term<int_callable>{{&call_count, &int_called}}());
  66. BOOST_CHECK(evaluate(double_int_expr) == 42);
  67. BOOST_CHECK(int_called == 1);
  68. BOOST_CHECK(double_called == 0);
  69. }
  70. }
  71. {
  72. {
  73. int call_count = 0;
  74. int void_called = -1;
  75. int int_called = -1;
  76. auto void_int_expr =
  77. (term<void_callable>{{&call_count, &void_called}}(),
  78. term<int_callable>{{&call_count, &int_called}}());
  79. BOOST_CHECK(evaluate(void_int_expr) == 42);
  80. BOOST_CHECK(void_called == 0);
  81. BOOST_CHECK(int_called == 1);
  82. }
  83. {
  84. int call_count = 0;
  85. int void_called = -1;
  86. int int_called = -1;
  87. auto int_void_expr =
  88. (term<int_callable>{{&call_count, &int_called}}(),
  89. term<void_callable>{{&call_count, &void_called}}());
  90. using eval_type = decltype(evaluate(int_void_expr));
  91. BOOST_MPL_ASSERT(
  92. (std::is_same<void, eval_type>));
  93. evaluate(int_void_expr);
  94. BOOST_CHECK(void_called == 1);
  95. BOOST_CHECK(int_called == 0);
  96. }
  97. }
  98. return 0;
  99. }