/Src/Dependencies/Boost/libs/phoenix/test/scope/lambda_tests.cpp

http://hadesmem.googlecode.com/ · C++ · 208 lines · 166 code · 34 blank · 8 comment · 18 complexity · ce0ac39affcda321a1fcb425bc4983b7 MD5 · raw file

  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 <typeinfo>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <boost/phoenix/core.hpp>
  13. #include <boost/phoenix/operator.hpp>
  14. #include <boost/phoenix/function.hpp>
  15. #include <boost/phoenix/bind.hpp>
  16. #include <boost/phoenix/scope.hpp>
  17. namespace boost { namespace phoenix
  18. {
  19. struct for_each_impl
  20. {
  21. template <typename Sig>
  22. struct result;
  23. template <typename This, typename C, typename F>
  24. struct result<This(C,F)>
  25. {
  26. typedef void type;
  27. };
  28. template <typename C, typename F>
  29. void operator()(C& c, F f) const
  30. {
  31. std::for_each(c.begin(), c.end(), f);
  32. }
  33. };
  34. function<for_each_impl> const for_each = for_each_impl();
  35. struct push_back_impl
  36. {
  37. template <typename Sig>
  38. struct result;
  39. template <typename This, typename C, typename T>
  40. struct result<This(C,T)>
  41. {
  42. typedef void type;
  43. };
  44. template <typename C, typename T>
  45. void operator()(C& c, T& x) const
  46. {
  47. c.push_back(x);
  48. }
  49. };
  50. function<push_back_impl> const push_back = push_back_impl();
  51. }}
  52. struct zzz {};
  53. int
  54. main()
  55. {
  56. using boost::phoenix::lambda;
  57. using boost::phoenix::let;
  58. using boost::phoenix::ref;
  59. using boost::phoenix::val;
  60. using boost::phoenix::arg_names::_1;
  61. using boost::phoenix::arg_names::_2;
  62. using boost::phoenix::local_names::_a;
  63. using boost::phoenix::local_names::_b;
  64. using boost::phoenix::placeholders::arg1;
  65. {
  66. int x = 1;
  67. int y = lambda[_1]()(x);
  68. BOOST_TEST(x == y);
  69. }
  70. {
  71. int x = 1, y = 10;
  72. BOOST_TEST(
  73. (_1 + lambda[_1 + 2])(x)(y) == 1+10+2
  74. );
  75. BOOST_TEST(
  76. (_1 + lambda[-_1])(x)(y) == 1+-10
  77. );
  78. }
  79. {
  80. int x = 1, y = 10, z = 13;
  81. BOOST_TEST(
  82. lambda(_a = _1, _b = _2)
  83. [
  84. _1 + _a + _b
  85. ]
  86. (x, z)(y) == x + y + z
  87. );
  88. }
  89. {
  90. int x = 4;
  91. int y = 5;
  92. lambda(_a = _1)[_a = 555](x)();
  93. BOOST_TEST(x == 555);
  94. (void)y;
  95. }
  96. {
  97. int x = 1;
  98. long x2 = 2;
  99. short x3 = 3;
  100. char const* y = "hello";
  101. zzz z;
  102. BOOST_TEST(lambda[_1](x)(y) == y);
  103. BOOST_TEST(lambda(_a = _1)[_a](x)(y) == x);
  104. BOOST_TEST(lambda(_a = _1)[lambda[_a]](x)(y)(z) == x);
  105. BOOST_TEST(lambda(_a = _1)[lambda[_a + _1]](x)(y)(x) == 2);
  106. BOOST_TEST(lambda(_a = _1)[lambda(_b = _1)[_a + _b + _1]](x)(x2)(x3) == 6);
  107. }
  108. {
  109. int x = 1, y = 10;
  110. BOOST_TEST(
  111. (_1 + lambda(_a = _1)[_a + _1 + 2])(x)(y) == 1+1+10+2
  112. );
  113. }
  114. {
  115. int x = 1, y = 10;
  116. BOOST_TEST(
  117. (
  118. _1 +
  119. lambda(_a = _1)
  120. [
  121. _a + lambda[_a + 2]
  122. ]
  123. )
  124. (x)(y)(y) == 1+1+1+2
  125. );
  126. }
  127. {
  128. using boost::phoenix::for_each;
  129. int init[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  130. std::vector<int> v(init, init+10);
  131. int x = 0;
  132. for_each(_1, lambda(_a = _2)[_a += _1])(v, x);
  133. BOOST_TEST(x == 55);
  134. }
  135. {
  136. using boost::phoenix::for_each;
  137. using boost::phoenix::push_back;
  138. int x = 10;
  139. std::vector<std::vector<int> > v(10);
  140. for_each(_1, lambda(_a = _2)[push_back(_1, _a)])(v, x);
  141. int y = 0;
  142. for_each(arg1, lambda[ref(y) += _1[0]])(v);
  143. BOOST_TEST(y == 100);
  144. }
  145. {
  146. int x = 1, y = 10, z = 13;
  147. BOOST_TEST(
  148. lambda(_a = _1, _b = _2)
  149. [
  150. _1 + _a + _b
  151. ]
  152. (x, z)(y) == x + y + z
  153. );
  154. }
  155. {
  156. {
  157. // $$$ Fixme. This should not be failing $$$
  158. int x = (let(_a = lambda[val(1)])[_a])()();
  159. //BOOST_TEST(x == 1);
  160. }
  161. {
  162. int x = (let(_a = lambda[val(1)])[bind(_a)])();
  163. BOOST_TEST(x == 1);
  164. }
  165. }
  166. {
  167. int i = 0;
  168. int j = 2;
  169. BOOST_TEST(lambda[let(_a = _1)[_a = _2]]()(i, j) == j);
  170. BOOST_TEST(i == j);
  171. }
  172. return boost::report_errors();
  173. }