let_tests_113a.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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::arg_names::_1;
  28. //using boost::phoenix::arg_names::_2;
  29. //using boost::phoenix::arg_names::_3;
  30. //using boost::phoenix::local_names::_a;
  31. //using boost::phoenix::local_names::_b;
  32. //using boost::phoenix::local_names::_c;
  33. //using boost::phoenix::local_names::_d;
  34. //using boost::phoenix::local_names::_e;
  35. using boost::phoenix::local_names::_x;
  36. //using boost::phoenix::local_names::_y;
  37. //using boost::phoenix::local_names::_z;
  38. //using boost::phoenix::placeholders::arg1;
  39. /*
  40. {
  41. int x = 1;
  42. BOOST_TEST(
  43. let(_a = _1)
  44. [
  45. _a
  46. ]
  47. (x) == x
  48. )
  49. ;
  50. }
  51. {
  52. int x = 1, y = 10;
  53. BOOST_TEST(
  54. let(_a = _1, _b = _2)
  55. [
  56. _a + _b
  57. ]
  58. (x, y) == x + y
  59. );
  60. }
  61. {
  62. int x = 1, y = 10, z = 13;
  63. BOOST_TEST(
  64. let(_x = _1, _y = _2)
  65. [
  66. let(_z = _3)
  67. [
  68. _x + _y + _z
  69. ]
  70. ]
  71. (x, y, z) == x + y + z
  72. );
  73. }
  74. {
  75. int x = 1, y = 10;
  76. BOOST_TEST(
  77. let(_x = _1)
  78. [
  79. _x +
  80. let(_x = _2)
  81. [
  82. -_x
  83. ]
  84. ]
  85. (x, y) == x + -y
  86. );
  87. }
  88. {
  89. int x = 999;
  90. BOOST_TEST(
  91. let(_x = _1) // _x is a reference to x
  92. [
  93. _x += 888
  94. ]
  95. (x) == 999 + 888
  96. );
  97. BOOST_TEST(x == 888 + 999);
  98. }
  99. */
  100. {
  101. int x = 999;
  102. BOOST_TEST(
  103. let(_x = val(_1)) // _x holds x by value
  104. [
  105. val(_x += 888)
  106. ]
  107. (x) == x + 888
  108. );
  109. BOOST_TEST(x == 999);
  110. /*
  111. BOOST_TEST(
  112. let(_x = val(_1)) // _x holds x by value
  113. [
  114. val(_x += 888)
  115. ]
  116. (x) == x + 888
  117. );
  118. BOOST_TEST(x == 999); */
  119. }
  120. /*
  121. {
  122. BOOST_TEST(
  123. let(_a = 1, _b = 2, _c = 3, _d = 4, _e = 5)
  124. [
  125. _a + _b + _c + _d + _e
  126. ]
  127. () == 1 + 2 + 3 + 4 + 5
  128. );
  129. }
  130. #ifdef PHOENIX_SHOULD_NOT_COMPILE_TEST
  131. {
  132. // disallow this:
  133. int i;
  134. (_a + _b)(i);
  135. }
  136. #endif
  137. {
  138. // show that we can return a local from an outer scope
  139. int y = 0;
  140. int x = (let(_a = 1)[let(_b = _1)[ _a ]])(y);
  141. BOOST_TEST(x == 1);
  142. }
  143. {
  144. // show that this code returns an lvalue
  145. int i = 1;
  146. let(_a = arg1)[ _a ](i)++;
  147. BOOST_TEST(i == 2);
  148. }
  149. {
  150. // show that what you put in is what you get out
  151. int i = 1;
  152. int& j = let(_a = arg1)[_a](i);
  153. BOOST_TEST(&i == &j);
  154. }
  155. {
  156. using boost::phoenix::at_c;
  157. boost::fusion::tuple<int, int> t = boost::fusion::make_tuple(0, 1);
  158. int i = let(_a = at_c<0>(_1))[_a](t);
  159. BOOST_TEST( i == 0 );
  160. }
  161. {
  162. int i = 0;
  163. let(_a = _1)[_a = _2](i, 2);
  164. BOOST_TEST(i == 2);
  165. }
  166. */
  167. return boost::report_errors();
  168. }