bind_tests_simple_f_refs.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // bind_tests_simple.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/bind.hpp"
  14. #include <iostream>
  15. using namespace boost::lambda;
  16. int sum_of_args_0() { return 0; }
  17. int sum_of_args_1(int a) { return a; }
  18. int sum_of_args_2(int a, int b) { return a+b; }
  19. int sum_of_args_3(int a, int b, int c) { return a+b+c; }
  20. int sum_of_args_4(int a, int b, int c, int d) { return a+b+c+d; }
  21. int sum_of_args_5(int a, int b, int c, int d, int e) { return a+b+c+d+e; }
  22. int sum_of_args_6(int a, int b, int c, int d, int e, int f) { return a+b+c+d+e+f; }
  23. int sum_of_args_7(int a, int b, int c, int d, int e, int f, int g) { return a+b+c+d+e+f+g; }
  24. int sum_of_args_8(int a, int b, int c, int d, int e, int f, int g, int h) { return a+b+c+d+e+f+g+h; }
  25. int sum_of_args_9(int a, int b, int c, int d, int e, int f, int g, int h, int i) { return a+b+c+d+e+f+g+h+i; }
  26. // ----------------------------
  27. class A {
  28. int i;
  29. public:
  30. A(int n) : i(n) {};
  31. int add(const int& j) { return i + j; }
  32. };
  33. void test_member_functions()
  34. {
  35. using boost::ref;
  36. A a(10);
  37. int i = 1;
  38. BOOST_CHECK(bind(&A::add, ref(a), _1)(i) == 11);
  39. BOOST_CHECK(bind(&A::add, &a, _1)(i) == 11);
  40. BOOST_CHECK(bind(&A::add, _1, 1)(a) == 11);
  41. BOOST_CHECK(bind(&A::add, _1, 1)(make_const(&a)) == 11);
  42. // This should fail, as lambda functors store arguments as const
  43. // bind(&A::add, a, _1);
  44. }
  45. int test_main(int, char *[]) {
  46. int i = 1; int j = 2; int k = 3;
  47. int result;
  48. // bind all parameters
  49. BOOST_CHECK(bind(sum_of_args_0)()==0);
  50. BOOST_CHECK(bind(sum_of_args_1, 1)()==1);
  51. BOOST_CHECK(bind(sum_of_args_2, 1, 2)()==3);
  52. BOOST_CHECK(bind(sum_of_args_3, 1, 2, 3)()==6);
  53. BOOST_CHECK(bind(sum_of_args_4, 1, 2, 3, 4)()==10);
  54. BOOST_CHECK(bind(sum_of_args_5, 1, 2, 3, 4, 5)()==15);
  55. BOOST_CHECK(bind(sum_of_args_6, 1, 2, 3, 4, 5, 6)()==21);
  56. BOOST_CHECK(bind(sum_of_args_7, 1, 2, 3, 4, 5, 6, 7)()==28);
  57. BOOST_CHECK(bind(sum_of_args_8, 1, 2, 3, 4, 5, 6, 7, 8)()==36);
  58. BOOST_CHECK(bind(sum_of_args_9, 1, 2, 3, 4, 5, 6, 7, 8, 9)()==45);
  59. // first parameter open
  60. BOOST_CHECK(bind(sum_of_args_0)()==0);
  61. BOOST_CHECK(bind(sum_of_args_1, _1)(i)==1);
  62. BOOST_CHECK(bind(sum_of_args_2, _1, 2)(i)==3);
  63. BOOST_CHECK(bind(sum_of_args_3, _1, 2, 3)(i)==6);
  64. BOOST_CHECK(bind(sum_of_args_4, _1, 2, 3, 4)(i)==10);
  65. BOOST_CHECK(bind(sum_of_args_5, _1, 2, 3, 4, 5)(i)==15);
  66. BOOST_CHECK(bind(sum_of_args_6, _1, 2, 3, 4, 5, 6)(i)==21);
  67. BOOST_CHECK(bind(sum_of_args_7, _1, 2, 3, 4, 5, 6, 7)(i)==28);
  68. BOOST_CHECK(bind(sum_of_args_8, _1, 2, 3, 4, 5, 6, 7, 8)(i)==36);
  69. BOOST_CHECK(bind(sum_of_args_9, _1, 2, 3, 4, 5, 6, 7, 8, 9)(i)==45);
  70. // two open arguments
  71. BOOST_CHECK(bind(sum_of_args_0)()==0);
  72. BOOST_CHECK(bind(sum_of_args_1, _1)(i)==1);
  73. BOOST_CHECK(bind(sum_of_args_2, _1, _2)(i, j)==3);
  74. BOOST_CHECK(bind(sum_of_args_3, _1, _2, 3)(i, j)==6);
  75. BOOST_CHECK(bind(sum_of_args_4, _1, _2, 3, 4)(i, j)==10);
  76. BOOST_CHECK(bind(sum_of_args_5, _1, _2, 3, 4, 5)(i, j)==15);
  77. BOOST_CHECK(bind(sum_of_args_6, _1, _2, 3, 4, 5, 6)(i, j)==21);
  78. BOOST_CHECK(bind(sum_of_args_7, _1, _2, 3, 4, 5, 6, 7)(i, j)==28);
  79. BOOST_CHECK(bind(sum_of_args_8, _1, _2, 3, 4, 5, 6, 7, 8)(i, j)==36);
  80. BOOST_CHECK(bind(sum_of_args_9, _1, _2, 3, 4, 5, 6, 7, 8, 9)(i, j)==45);
  81. // three open arguments
  82. BOOST_CHECK(bind(sum_of_args_0)()==0);
  83. BOOST_CHECK(bind(sum_of_args_1, _1)(i)==1);
  84. BOOST_CHECK(bind(sum_of_args_2, _1, _2)(i, j)==3);
  85. BOOST_CHECK(bind(sum_of_args_3, _1, _2, _3)(i, j, k)==6);
  86. BOOST_CHECK(bind(sum_of_args_4, _1, _2, _3, 4)(i, j, k)==10);
  87. BOOST_CHECK(bind(sum_of_args_5, _1, _2, _3, 4, 5)(i, j, k)==15);
  88. BOOST_CHECK(bind(sum_of_args_6, _1, _2, _3, 4, 5, 6)(i, j, k)==21);
  89. BOOST_CHECK(bind(sum_of_args_7, _1, _2, _3, 4, 5, 6, 7)(i, j, k)==28);
  90. BOOST_CHECK(bind(sum_of_args_8, _1, _2, _3, 4, 5, 6, 7, 8)(i, j, k)==36);
  91. BOOST_CHECK(bind(sum_of_args_9, _1, _2, _3, 4, 5, 6, 7, 8, 9)(i, j, k)==45);
  92. // function compositions with bind
  93. BOOST_CHECK(bind(sum_of_args_3, bind(sum_of_args_2, _1, 2), 2, 3)(i)==8);
  94. BOOST_CHECK(
  95. bind(sum_of_args_9,
  96. bind(sum_of_args_0), // 0
  97. bind(sum_of_args_1, _1), // 1
  98. bind(sum_of_args_2, _1, _2), // 3
  99. bind(sum_of_args_3, _1, _2, _3), // 6
  100. bind(sum_of_args_4, _1, _2, _3, 4), // 10
  101. bind(sum_of_args_5, _1, _2, _3, 4, 5), // 15
  102. bind(sum_of_args_6, _1, _2, _3, 4, 5, 6), // 21
  103. bind(sum_of_args_7, _1, _2, _3, 4, 5, 6, 7), // 28
  104. bind(sum_of_args_8, _1, _2, _3, 4, 5, 6, 7, 8) // 36
  105. )(i, j, k) == 120);
  106. // deeper nesting
  107. result =
  108. bind(sum_of_args_1, // 12
  109. bind(sum_of_args_4, // 12
  110. bind(sum_of_args_2, // 3
  111. bind(sum_of_args_1, // 1
  112. bind(sum_of_args_1, _1) // 1
  113. ),
  114. _2),
  115. _2,
  116. _3,
  117. 4)
  118. )(i, j, k);
  119. BOOST_CHECK(result == 12);
  120. test_member_functions();
  121. return 0;
  122. }