let_tests_113.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*=============================================================================
  2. Copyright (c) 2001-2007 Joel de Guzman
  3. Copyright (c) 2015 John Fletcher
  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 <iostream>
  8. #include <cmath>
  9. #include <algorithm>
  10. #include <vector>
  11. #include <boost/phoenix/core/limits.hpp>
  12. #include <boost/detail/lightweight_test.hpp>
  13. #include <boost/fusion/tuple.hpp>
  14. #include <boost/phoenix/core.hpp>
  15. #include <boost/phoenix/operator.hpp>
  16. #include <boost/phoenix/function.hpp>
  17. #include <boost/phoenix/fusion.hpp>
  18. #include <boost/phoenix/scope.hpp>
  19. #include <typeinfo>
  20. namespace fusion = boost::fusion;
  21. namespace mpl = boost::mpl;
  22. int
  23. main()
  24. {
  25. using boost::phoenix::let;
  26. using boost::phoenix::val;
  27. using boost::phoenix::ref;
  28. using boost::phoenix::arg_names::_1;
  29. //using boost::phoenix::arg_names::_2;
  30. //using boost::phoenix::arg_names::_3;
  31. //using boost::phoenix::local_names::_a;
  32. //using boost::phoenix::local_names::_b;
  33. //using boost::phoenix::local_names::_c;
  34. //using boost::phoenix::local_names::_d;
  35. //using boost::phoenix::local_names::_e;
  36. using boost::phoenix::local_names::_x;
  37. //using boost::phoenix::local_names::_y;
  38. //using boost::phoenix::local_names::_z;
  39. //using boost::phoenix::placeholders::arg1;
  40. /*
  41. {
  42. int x = 1;
  43. BOOST_TEST(
  44. let(_a = _1)
  45. [
  46. _a
  47. ]
  48. (x) == x
  49. )
  50. ;
  51. }
  52. {
  53. int x = 1, y = 10;
  54. BOOST_TEST(
  55. let(_a = _1, _b = _2)
  56. [
  57. _a + _b
  58. ]
  59. (x, y) == x + y
  60. );
  61. }
  62. {
  63. int x = 1, y = 10, z = 13;
  64. BOOST_TEST(
  65. let(_x = _1, _y = _2)
  66. [
  67. let(_z = _3)
  68. [
  69. _x + _y + _z
  70. ]
  71. ]
  72. (x, y, z) == x + y + z
  73. );
  74. }
  75. {
  76. int x = 1, y = 10;
  77. BOOST_TEST(
  78. let(_x = _1)
  79. [
  80. _x +
  81. let(_x = _2)
  82. [
  83. -_x
  84. ]
  85. ]
  86. (x, y) == x + -y
  87. );
  88. }
  89. {
  90. int x = 999;
  91. BOOST_TEST(
  92. let(_x = _1) // _x is a reference to x
  93. [
  94. _x += 888
  95. ]
  96. (x) == 999 + 888
  97. );
  98. BOOST_TEST(x == 888 + 999);
  99. }
  100. */
  101. {
  102. int x = 999;
  103. BOOST_TEST(
  104. let(_x = val(_1)) // _x holds x by value
  105. [
  106. val(_x += 888) // so use value here too.
  107. ]
  108. (x) == x + 888
  109. );
  110. BOOST_TEST(x == 999);
  111. /*
  112. BOOST_TEST(
  113. let(_x = ref(_1)) // _x holds x by reference
  114. [
  115. _x += 888
  116. ]
  117. (x) == x + 888
  118. );
  119. BOOST_TEST(x == 999);
  120. */
  121. BOOST_TEST(
  122. let( _x = _1 ) // _x holds x by reference
  123. [
  124. _x += 888
  125. ]
  126. (x) == 999 + 888
  127. );
  128. BOOST_TEST(x == 888 + 999);
  129. }
  130. /*
  131. {
  132. BOOST_TEST(
  133. let(_a = 1, _b = 2, _c = 3, _d = 4, _e = 5)
  134. [
  135. _a + _b + _c + _d + _e
  136. ]
  137. () == 1 + 2 + 3 + 4 + 5
  138. );
  139. }
  140. #ifdef PHOENIX_SHOULD_NOT_COMPILE_TEST
  141. {
  142. // disallow this:
  143. int i;
  144. (_a + _b)(i);
  145. }
  146. #endif
  147. {
  148. // show that we can return a local from an outer scope
  149. int y = 0;
  150. int x = (let(_a = 1)[let(_b = _1)[ _a ]])(y);
  151. BOOST_TEST(x == 1);
  152. }
  153. {
  154. // show that this code returns an lvalue
  155. int i = 1;
  156. let(_a = arg1)[ _a ](i)++;
  157. BOOST_TEST(i == 2);
  158. }
  159. {
  160. // show that what you put in is what you get out
  161. int i = 1;
  162. int& j = let(_a = arg1)[_a](i);
  163. BOOST_TEST(&i == &j);
  164. }
  165. {
  166. using boost::phoenix::at_c;
  167. boost::fusion::tuple<int, int> t = boost::fusion::make_tuple(0, 1);
  168. int i = let(_a = at_c<0>(_1))[_a](t);
  169. BOOST_TEST( i == 0 );
  170. }
  171. {
  172. int i = 0;
  173. let(_a = _1)[_a = _2](i, 2);
  174. BOOST_TEST(i == 2);
  175. }
  176. */
  177. return boost::report_errors();
  178. }