phoenix_control_structures.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // phoenix_style_control_structures.cpp -- The Boost Lambda Library ------
  2. //
  3. // Copyright (C) 2000-2003 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
  4. // Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // For more information, see www.boost.org
  11. // -----------------------------------------------------------------------
  12. #include <boost/test/minimal.hpp> // see "Header Implementation Option"
  13. #include "boost/lambda/lambda.hpp"
  14. #include "boost/lambda/if.hpp"
  15. #include "boost/lambda/loops.hpp"
  16. #include <iostream>
  17. #include <vector>
  18. #include <list>
  19. #include <algorithm>
  20. #include <cmath>
  21. #include <cassert>
  22. #include <functional>
  23. using namespace boost::lambda;
  24. using namespace std;
  25. // If-else, while, do-while, for statements
  26. int test_main(int, char *[]) {
  27. vector<int> v;
  28. v.clear();
  29. v.push_back(1);
  30. v.push_back(2);
  31. v.push_back(3);
  32. v.push_back(4);
  33. v.push_back(5);
  34. v.push_back(6);
  35. v.push_back(7);
  36. v.push_back(8);
  37. v.push_back(9);
  38. v.push_back(10);
  39. int sum = 0;
  40. //////////////////////////////////
  41. for_each(v.begin(), v.end(),
  42. if_(_1 > 3 && _1 <= 8)
  43. [
  44. sum += _1
  45. ]
  46. );
  47. BOOST_CHECK(sum == 4+5+6+7+8);
  48. int gt = 0, eq = 0, lt = 0;
  49. //////////////////////////////////
  50. for_each(v.begin(), v.end(),
  51. if_(_1 > 5)
  52. [
  53. ++var(gt)
  54. ]
  55. .else_
  56. [
  57. if_(_1 == 5)
  58. [
  59. ++var(eq)
  60. ]
  61. .else_
  62. [
  63. ++var(lt)
  64. ]
  65. ]
  66. );
  67. BOOST_CHECK(lt==4);
  68. BOOST_CHECK(eq==1);
  69. BOOST_CHECK(gt==5);
  70. vector<int> t = v;
  71. int counta = 0;
  72. int countb = 0;
  73. //////////////////////////////////
  74. for_each(v.begin(), v.end(),
  75. (
  76. while_(_1--)
  77. [
  78. ++var(counta)
  79. ],
  80. ++var(countb)
  81. )
  82. );
  83. BOOST_CHECK(counta == 55);
  84. BOOST_CHECK(countb == 10);
  85. v = t;
  86. counta = 0; countb = 0;
  87. //////////////////////////////////
  88. for_each(v.begin(), v.end(),
  89. (
  90. do_
  91. [
  92. ++var(counta)
  93. ]
  94. .while_(_1--),
  95. ++var(countb)
  96. )
  97. );
  98. BOOST_CHECK(counta == (2+11)*10/2);
  99. BOOST_CHECK(countb == 10);
  100. v = t;
  101. counta = 0; countb = 0;
  102. //////////////////////////////////
  103. int iii;
  104. for_each(v.begin(), v.end(),
  105. (
  106. for_(var(iii) = 0, var(iii) < _1, ++var(iii))
  107. [
  108. ++var(counta)
  109. ],
  110. ++var(countb)
  111. )
  112. );
  113. BOOST_CHECK(counta == (1+10)*10/2);
  114. BOOST_CHECK(countb == 10);
  115. v = t;
  116. return 0;
  117. }