control_structures.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // -- 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 <algorithm>
  18. #include <vector>
  19. using namespace boost;
  20. using boost::lambda::constant;
  21. using boost::lambda::_1;
  22. using boost::lambda::_2;
  23. using boost::lambda::_3;
  24. using boost::lambda::make_const;
  25. using boost::lambda::for_loop;
  26. using boost::lambda::while_loop;
  27. using boost::lambda::do_while_loop;
  28. using boost::lambda::if_then;
  29. using boost::lambda::if_then_else;
  30. using boost::lambda::if_then_else_return;
  31. // 2 container for_each
  32. template <class InputIter1, class InputIter2, class Function>
  33. Function for_each(InputIter1 first, InputIter1 last,
  34. InputIter2 first2, Function f) {
  35. for ( ; first != last; ++first, ++first2)
  36. f(*first, *first2);
  37. return f;
  38. }
  39. void simple_loops() {
  40. // for loops ---------------------------------------------------------
  41. int i;
  42. int arithmetic_series = 0;
  43. for_loop(_1 = 0, _1 < 10, _1++, arithmetic_series += _1)(i);
  44. BOOST_CHECK(arithmetic_series == 45);
  45. // no body case
  46. for_loop(boost::lambda::var(i) = 0, boost::lambda::var(i) < 100, ++boost::lambda::var(i))();
  47. BOOST_CHECK(i == 100);
  48. // while loops -------------------------------------------------------
  49. int a = 0, b = 0, c = 0;
  50. while_loop((_1 + _2) >= (_1 * _2), (++_1, ++_2, ++_3))(a, b, c);
  51. BOOST_CHECK(c == 3);
  52. int count;
  53. count = 0; i = 0;
  54. while_loop(_1++ < 10, ++boost::lambda::var(count))(i);
  55. BOOST_CHECK(count == 10);
  56. // note that the first parameter of do_while_loop is the condition
  57. count = 0; i = 0;
  58. do_while_loop(_1++ < 10, ++boost::lambda::var(count))(i);
  59. BOOST_CHECK(count == 11);
  60. a = 0;
  61. do_while_loop(constant(false), _1++)(a);
  62. BOOST_CHECK(a == 1);
  63. // no body cases
  64. a = 40; b = 30;
  65. while_loop(--_1 > _2)(a, b);
  66. BOOST_CHECK(a == b);
  67. // (the no body case for do_while_loop is pretty redundant)
  68. a = 40; b = 30;
  69. do_while_loop(--_1 > _2)(a, b);
  70. BOOST_CHECK(a == b);
  71. }
  72. void simple_ifs () {
  73. int value = 42;
  74. if_then(_1 < 0, _1 = 0)(value);
  75. BOOST_CHECK(value == 42);
  76. value = -42;
  77. if_then(_1 < 0, _1 = -_1)(value);
  78. BOOST_CHECK(value == 42);
  79. int min;
  80. if_then_else(_1 < _2, boost::lambda::var(min) = _1, boost::lambda::var(min) = _2)
  81. (make_const(1), make_const(2));
  82. BOOST_CHECK(min == 1);
  83. if_then_else(_1 < _2, boost::lambda::var(min) = _1, boost::lambda::var(min) = _2)
  84. (make_const(5), make_const(3));
  85. BOOST_CHECK(min == 3);
  86. int x, y;
  87. x = -1; y = 1;
  88. BOOST_CHECK(if_then_else_return(_1 < _2, _2, _1)(x, y) == (std::max)(x ,y));
  89. BOOST_CHECK(if_then_else_return(_1 < _2, _2, _1)(y, x) == (std::max)(x ,y));
  90. }
  91. int test_main(int, char *[])
  92. {
  93. simple_loops();
  94. simple_ifs();
  95. return 0;
  96. }