iteration.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*=============================================================================
  2. Copyright (c) 2005-2007 Dan Marsden
  3. Copyright (c) 2005-2007 Joel de Guzman
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #include <boost/phoenix/core.hpp>
  8. #include <boost/phoenix/stl/algorithm/iteration.hpp>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <functional>
  11. namespace
  12. {
  13. struct for_each_tester
  14. {
  15. int value_;
  16. for_each_tester() : value_(0) { }
  17. void operator()(
  18. int& i)
  19. {
  20. value_ += i++;
  21. return;
  22. }
  23. };
  24. void for_each_test()
  25. {
  26. using boost::phoenix::for_each;
  27. using boost::phoenix::arg_names::arg1;
  28. int array[] = {1,2,3};
  29. BOOST_TEST(for_each(arg1, for_each_tester())(array).value_ == 6);
  30. BOOST_TEST(array[0] == 2);
  31. BOOST_TEST(array[1] == 3);
  32. BOOST_TEST(array[2] == 4);
  33. return;
  34. }
  35. void accumulate_test()
  36. {
  37. using boost::phoenix::accumulate;
  38. using boost::phoenix::arg_names::arg1;
  39. int array[] = {1,2,3};
  40. BOOST_TEST(accumulate(arg1, 0)(array) == 6);
  41. BOOST_TEST(boost::phoenix::accumulate(arg1, 0, std::minus<int>())(array) == -6);
  42. return;
  43. }
  44. }
  45. int main()
  46. {
  47. for_each_test();
  48. accumulate_test();
  49. boost::report_errors();
  50. }