algorithm_test.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // bll_and_function.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. // test using BLL and boost::function
  12. #include <boost/test/minimal.hpp> // see "Header Implementation Option"
  13. /*
  14. #include "boost/lambda/lambda.hpp"
  15. #include "boost/lambda/bind.hpp"
  16. #include "boost/lambda/algorithm.hpp"
  17. */
  18. #include <boost/phoenix/core.hpp>
  19. #include <boost/phoenix/operator.hpp>
  20. #include <boost/phoenix/bind.hpp>
  21. #include <boost/phoenix/scope.hpp>
  22. #include <boost/phoenix/stl/algorithm/iteration.hpp>
  23. #include <vector>
  24. #include <map>
  25. #include <set>
  26. #include <string>
  27. #include <iostream>
  28. namespace phoenix = boost::phoenix;
  29. void test_foreach() {
  30. using phoenix::placeholders::_1;
  31. using phoenix::ref;
  32. using phoenix::lambda;
  33. int a[10][20];
  34. int sum = 0;
  35. //for_each(arg1, for_each_tester())(array).value_;
  36. // Was:
  37. // std::for_each(a, a + 10,
  38. // bind(ll::for_each(), _1, _1 + 20,
  39. // protect((_1 = var(sum), ++var(sum)))));
  40. // var replaced with ref, protect(..) replaced with lambda[..], no need for bind
  41. // phoenix algorithms are range based
  42. std::for_each(a, a + 10,
  43. phoenix::for_each(_1, lambda[_1 = ref(sum), ++ref(sum)]));
  44. /*phoenix::bind(phoenix::for_each, _1,
  45. lambda[_1 = ref(sum), ++ref(sum)]));*/
  46. sum = 0;
  47. // Was:
  48. // std::for_each(a, a + 10,
  49. // bind(ll::for_each(), _1, _1 + 20,
  50. // protect((sum += _1))));
  51. //
  52. std::for_each(a, a + 10,
  53. phoenix::for_each( _1,
  54. lambda[(ref(sum) += _1)]));
  55. BOOST_CHECK(sum == (199 + 1)/ 2 * 199);
  56. }
  57. // More tests needed (for all algorithms)
  58. int test_main(int, char *[]) {
  59. test_foreach();
  60. return 0;
  61. }