let_tests.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*=============================================================================
  2. Copyright (c) 2001-2007 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #include <iostream>
  7. #include <cmath>
  8. #include <algorithm>
  9. #include <vector>
  10. #include <boost/phoenix/core/limits.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <boost/fusion/tuple.hpp>
  13. #include <boost/phoenix/core.hpp>
  14. #include <boost/phoenix/operator.hpp>
  15. #include <boost/phoenix/function.hpp>
  16. #include <boost/phoenix/fusion.hpp>
  17. #include <boost/phoenix/scope.hpp>
  18. #include <boost/phoenix/object/construct.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. int x = 1;
  41. BOOST_TEST(
  42. let(_a = _1)
  43. [
  44. _a
  45. ]
  46. (x) == x
  47. )
  48. ;
  49. }
  50. {
  51. int x = 1, y = 10;
  52. BOOST_TEST(
  53. let(_a = _1, _b = _2)
  54. [
  55. _a + _b
  56. ]
  57. (x, y) == x + y
  58. );
  59. }
  60. {
  61. int x = 1, y = 10, z = 13;
  62. BOOST_TEST(
  63. let(_x = _1, _y = _2)
  64. [
  65. let(_z = _3)
  66. [
  67. _x + _y + _z
  68. ]
  69. ]
  70. (x, y, z) == x + y + z
  71. );
  72. }
  73. {
  74. int x = 1, y = 10;
  75. BOOST_TEST(
  76. let(_x = _1)
  77. [
  78. _x +
  79. let(_x = _2)
  80. [
  81. -_x
  82. ]
  83. ]
  84. (x, y) == x + -y
  85. );
  86. }
  87. {
  88. int x = 999;
  89. BOOST_TEST(
  90. let(_x = _1) // _x is a reference to x
  91. [
  92. _x += 888
  93. ]
  94. (x) == 999 + 888
  95. );
  96. BOOST_TEST(x == 888 + 999);
  97. }
  98. {
  99. int x = 999;
  100. /*
  101. BOOST_TEST(
  102. let(_x = val(_1)) // _x holds x by value
  103. [
  104. _x += 888
  105. ]
  106. (x) == x + 888
  107. );
  108. BOOST_TEST(x == 999);
  109. */
  110. BOOST_TEST(
  111. let(_x = val(_1)) // _x holds x by value
  112. [
  113. val(_x += 888)
  114. ]
  115. (x) == x + 888
  116. );
  117. BOOST_TEST(x == 999);
  118. }
  119. {
  120. BOOST_TEST(
  121. let(_a = 1, _b = 2, _c = 3, _d = 4, _e = 5)
  122. [
  123. _a + _b + _c + _d + _e
  124. ]
  125. () == 1 + 2 + 3 + 4 + 5
  126. );
  127. }
  128. #ifdef PHOENIX_SHOULD_NOT_COMPILE_TEST
  129. {
  130. // disallow this:
  131. int i;
  132. (_a + _b)(i);
  133. }
  134. #endif
  135. {
  136. // show that we can return a local from an outer scope
  137. int y = 0;
  138. #if defined(__OPTIMIZE__) && __OPTIMIZE__
  139. int x = (let(_a = _2)[let(_b = _1)[ _a ]])(y,1);
  140. #else
  141. int x = (let(_a = 1)[let(_b = _1)[ _a ]])(y);
  142. #endif
  143. BOOST_TEST(x == 1);
  144. }
  145. {
  146. // show that this code returns an lvalue
  147. int i = 1;
  148. let(_a = arg1)[ _a ](i)++;
  149. BOOST_TEST(i == 2);
  150. }
  151. {
  152. // show that what you put in is what you get out
  153. int i = 1;
  154. int& j = let(_a = arg1)[_a](i);
  155. BOOST_TEST(&i == &j);
  156. }
  157. {
  158. // show that a let with a void result can compile
  159. using boost::phoenix::construct;
  160. let(_a = 1)[ // need at least one expression here
  161. construct<void>() // produce a void result
  162. ]();
  163. }
  164. {
  165. using boost::phoenix::at_c;
  166. boost::fusion::tuple<int, int> t = boost::fusion::make_tuple(0, 1);
  167. int i = let(_a = at_c<0>(_1))[_a](t);
  168. BOOST_TEST( i == 0 );
  169. }
  170. {
  171. int i = 0;
  172. let(_a = _1)[_a = _2](i, 2);
  173. BOOST_TEST(i == 2);
  174. }
  175. return boost::report_errors();
  176. }