let_tests_157a.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <typeinfo>
  19. namespace fusion = boost::fusion;
  20. namespace mpl = boost::mpl;
  21. int
  22. main()
  23. {
  24. using boost::phoenix::let;
  25. using boost::phoenix::val;
  26. using boost::phoenix::arg_names::_1;
  27. using boost::phoenix::arg_names::_2;
  28. //using boost::phoenix::arg_names::_3;
  29. using boost::phoenix::local_names::_a;
  30. using boost::phoenix::local_names::_b;
  31. //using boost::phoenix::local_names::_c;
  32. //using boost::phoenix::local_names::_d;
  33. //using boost::phoenix::local_names::_e;
  34. //using boost::phoenix::local_names::_x;
  35. //using boost::phoenix::local_names::_y;
  36. //using boost::phoenix::local_names::_z;
  37. //using boost::phoenix::placeholders::arg1;
  38. /*
  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. BOOST_TEST(
  101. let(_x = val(_1)) // _x holds x by value
  102. [
  103. _x += 888
  104. ]
  105. (x) == x + 888
  106. );
  107. BOOST_TEST(x == 999);
  108. BOOST_TEST(
  109. let(_x = val(_1)) // _x holds x by value
  110. [
  111. val(_x += 888)
  112. ]
  113. (x) == x + 888
  114. );
  115. BOOST_TEST(x == 999);
  116. }
  117. {
  118. BOOST_TEST(
  119. let(_a = 1, _b = 2, _c = 3, _d = 4, _e = 5)
  120. [
  121. _a + _b + _c + _d + _e
  122. ]
  123. () == 1 + 2 + 3 + 4 + 5
  124. );
  125. }
  126. #ifdef PHOENIX_SHOULD_NOT_COMPILE_TEST
  127. {
  128. // disallow this:
  129. int i;
  130. (_a + _b)(i);
  131. }
  132. #endif
  133. */
  134. {
  135. // show that we can return a local from an outer scope
  136. int y = 0;
  137. int x = (let(_a = _2)[let(_b = _1)[ _a ]])(y,1);
  138. BOOST_TEST(x == 1);
  139. }
  140. /*
  141. {
  142. // show that this code returns an lvalue
  143. int i = 1;
  144. let(_a = arg1)[ _a ](i)++;
  145. BOOST_TEST(i == 2);
  146. }
  147. {
  148. // show that what you put in is what you get out
  149. int i = 1;
  150. int& j = let(_a = arg1)[_a](i);
  151. BOOST_TEST(&i == &j);
  152. }
  153. {
  154. using boost::phoenix::at_c;
  155. boost::fusion::tuple<int, int> t = boost::fusion::make_tuple(0, 1);
  156. int i = let(_a = at_c<0>(_1))[_a](t);
  157. BOOST_TEST( i == 0 );
  158. }
  159. {
  160. int i = 0;
  161. let(_a = _1)[_a = _2](i, 2);
  162. BOOST_TEST(i == 2);
  163. }
  164. */
  165. return boost::report_errors();
  166. }