loops_tests.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 <vector>
  8. #include <algorithm>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <boost/phoenix/core.hpp>
  11. #include <boost/phoenix/statement.hpp>
  12. #include <boost/phoenix/operator.hpp>
  13. int
  14. main()
  15. {
  16. using boost::phoenix::arg_names::arg1;
  17. using boost::phoenix::do_;
  18. using boost::phoenix::ref;
  19. using boost::phoenix::val;
  20. using std::cout;
  21. using std::endl;
  22. using std::for_each;
  23. using std::vector;
  24. int init[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  25. vector<int> v(init, init+10);
  26. vector<int> t = v;
  27. cout << endl;
  28. int x = 0;
  29. for_each(v.begin(), v.end(),
  30. (
  31. while_(arg1--)
  32. [
  33. cout << arg1 << ", ",
  34. ++ref(x)
  35. ],
  36. cout << val("\n")
  37. )
  38. );
  39. BOOST_TEST(x == 1+2+3+4+5+6+7+8+9+10);
  40. cout << endl;
  41. v = t;
  42. x = 0;
  43. for_each(v.begin(), v.end(),
  44. (
  45. do_
  46. [
  47. cout << arg1 << ", ",
  48. ++ref(x)
  49. ]
  50. .while_(arg1--),
  51. cout << val("\n")
  52. )
  53. );
  54. BOOST_TEST(x == 2+3+4+5+6+7+8+9+10+11);
  55. cout << endl;
  56. v = t;
  57. x = 0;
  58. int iii;
  59. for_each(v.begin(), v.end(),
  60. (
  61. for_(ref(iii) = 0, ref(iii) < arg1, ++ref(iii))
  62. [
  63. cout << arg1 << ", ",
  64. ++ref(x)
  65. ],
  66. cout << val("\n")
  67. )
  68. );
  69. BOOST_TEST(x == 1+2+3+4+5+6+7+8+9+10);
  70. cout << endl;
  71. v = t;
  72. x = 0;
  73. return boost::report_errors();
  74. }