bind_tests_simple.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. int add2(int a1, int a2) { return i + a1 + a2; }
  33. int add3(int a1, int a2, int a3) { return i + a1 + a2 + a3; }
  34. int add4(int a1, int a2, int a3, int a4) { return i + a1 + a2 + a3 + a4; }
  35. int add5(int a1, int a2, int a3, int a4, int a5)
  36. { return i + a1 + a2 + a3 + a4 + a5; }
  37. int add6(int a1, int a2, int a3, int a4, int a5, int a6)
  38. { return i + a1 + a2 + a3 + a4 + a5 + a6; }
  39. int add7(int a1, int a2, int a3, int a4, int a5, int a6, int a7)
  40. { return i + a1 + a2 + a3 + a4 + a5 + a6 + a7; }
  41. int add8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
  42. { return i + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8; }
  43. };
  44. void test_member_functions()
  45. {
  46. using boost::ref;
  47. A a(10);
  48. int i = 1;
  49. BOOST_CHECK(bind(&A::add, ref(a), _1)(i) == 11);
  50. BOOST_CHECK(bind(&A::add, &a, _1)(i) == 11);
  51. BOOST_CHECK(bind(&A::add, _1, 1)(a) == 11);
  52. BOOST_CHECK(bind(&A::add, _1, 1)(make_const(&a)) == 11);
  53. BOOST_CHECK(bind(&A::add2, _1, 1, 1)(a) == 12);
  54. BOOST_CHECK(bind(&A::add3, _1, 1, 1, 1)(a) == 13);
  55. BOOST_CHECK(bind(&A::add4, _1, 1, 1, 1, 1)(a) == 14);
  56. BOOST_CHECK(bind(&A::add5, _1, 1, 1, 1, 1, 1)(a) == 15);
  57. BOOST_CHECK(bind(&A::add6, _1, 1, 1, 1, 1, 1, 1)(a) == 16);
  58. BOOST_CHECK(bind(&A::add7, _1, 1, 1, 1, 1, 1, 1, 1)(a) == 17);
  59. BOOST_CHECK(bind(&A::add8, _1, 1, 1, 1, 1, 1, 1, 1, 1)(a) == 18);
  60. // This should fail, as lambda functors store arguments as const
  61. // bind(&A::add, a, _1);
  62. }
  63. struct B {
  64. B(int n) : i(n) {};
  65. int i;
  66. };
  67. void test_data_members()
  68. {
  69. using boost::ref;
  70. B b(10);
  71. BOOST_CHECK(bind(&B::i, ref(b))() == 10);
  72. BOOST_CHECK(bind(&B::i, b)() == 10);
  73. BOOST_CHECK(bind(&B::i, _1)(b) == 10);
  74. BOOST_CHECK(bind(&B::i, _1)(B(11)) == 11);
  75. bind(&B::i, ref(b))() = 1;
  76. BOOST_CHECK(b.i == 1);
  77. }
  78. int test_main(int, char *[]) {
  79. int i = 1; int j = 2; int k = 3;
  80. int result;
  81. // bind all parameters
  82. BOOST_CHECK(bind(&sum_of_args_0)()==0);
  83. BOOST_CHECK(bind(&sum_of_args_1, 1)()==1);
  84. BOOST_CHECK(bind(&sum_of_args_2, 1, 2)()==3);
  85. BOOST_CHECK(bind(&sum_of_args_3, 1, 2, 3)()==6);
  86. BOOST_CHECK(bind(&sum_of_args_4, 1, 2, 3, 4)()==10);
  87. BOOST_CHECK(bind(&sum_of_args_5, 1, 2, 3, 4, 5)()==15);
  88. BOOST_CHECK(bind(&sum_of_args_6, 1, 2, 3, 4, 5, 6)()==21);
  89. BOOST_CHECK(bind(&sum_of_args_7, 1, 2, 3, 4, 5, 6, 7)()==28);
  90. BOOST_CHECK(bind(&sum_of_args_8, 1, 2, 3, 4, 5, 6, 7, 8)()==36);
  91. BOOST_CHECK(bind(&sum_of_args_9, 1, 2, 3, 4, 5, 6, 7, 8, 9)()==45);
  92. // first parameter open
  93. BOOST_CHECK(bind(&sum_of_args_0)()==0);
  94. BOOST_CHECK(bind(&sum_of_args_1, _1)(i)==1);
  95. BOOST_CHECK(bind(&sum_of_args_2, _1, 2)(i)==3);
  96. BOOST_CHECK(bind(&sum_of_args_3, _1, 2, 3)(i)==6);
  97. BOOST_CHECK(bind(&sum_of_args_4, _1, 2, 3, 4)(i)==10);
  98. BOOST_CHECK(bind(&sum_of_args_5, _1, 2, 3, 4, 5)(i)==15);
  99. BOOST_CHECK(bind(&sum_of_args_6, _1, 2, 3, 4, 5, 6)(i)==21);
  100. BOOST_CHECK(bind(&sum_of_args_7, _1, 2, 3, 4, 5, 6, 7)(i)==28);
  101. BOOST_CHECK(bind(&sum_of_args_8, _1, 2, 3, 4, 5, 6, 7, 8)(i)==36);
  102. BOOST_CHECK(bind(&sum_of_args_9, _1, 2, 3, 4, 5, 6, 7, 8, 9)(i)==45);
  103. // two open arguments
  104. BOOST_CHECK(bind(&sum_of_args_0)()==0);
  105. BOOST_CHECK(bind(&sum_of_args_1, _1)(i)==1);
  106. BOOST_CHECK(bind(&sum_of_args_2, _1, _2)(i, j)==3);
  107. BOOST_CHECK(bind(&sum_of_args_3, _1, _2, 3)(i, j)==6);
  108. BOOST_CHECK(bind(&sum_of_args_4, _1, _2, 3, 4)(i, j)==10);
  109. BOOST_CHECK(bind(&sum_of_args_5, _1, _2, 3, 4, 5)(i, j)==15);
  110. BOOST_CHECK(bind(&sum_of_args_6, _1, _2, 3, 4, 5, 6)(i, j)==21);
  111. BOOST_CHECK(bind(&sum_of_args_7, _1, _2, 3, 4, 5, 6, 7)(i, j)==28);
  112. BOOST_CHECK(bind(&sum_of_args_8, _1, _2, 3, 4, 5, 6, 7, 8)(i, j)==36);
  113. BOOST_CHECK(bind(&sum_of_args_9, _1, _2, 3, 4, 5, 6, 7, 8, 9)(i, j)==45);
  114. // three open arguments
  115. BOOST_CHECK(bind(&sum_of_args_0)()==0);
  116. BOOST_CHECK(bind(&sum_of_args_1, _1)(i)==1);
  117. BOOST_CHECK(bind(&sum_of_args_2, _1, _2)(i, j)==3);
  118. BOOST_CHECK(bind(&sum_of_args_3, _1, _2, _3)(i, j, k)==6);
  119. BOOST_CHECK(bind(&sum_of_args_4, _1, _2, _3, 4)(i, j, k)==10);
  120. BOOST_CHECK(bind(&sum_of_args_5, _1, _2, _3, 4, 5)(i, j, k)==15);
  121. BOOST_CHECK(bind(&sum_of_args_6, _1, _2, _3, 4, 5, 6)(i, j, k)==21);
  122. BOOST_CHECK(bind(&sum_of_args_7, _1, _2, _3, 4, 5, 6, 7)(i, j, k)==28);
  123. BOOST_CHECK(bind(&sum_of_args_8, _1, _2, _3, 4, 5, 6, 7, 8)(i, j, k)==36);
  124. BOOST_CHECK(bind(&sum_of_args_9, _1, _2, _3, 4, 5, 6, 7, 8, 9)(i, j, k)==45);
  125. // function compositions with bind
  126. BOOST_CHECK(bind(&sum_of_args_3, bind(&sum_of_args_2, _1, 2), 2, 3)(i)==8);
  127. BOOST_CHECK(
  128. bind(&sum_of_args_9,
  129. bind(&sum_of_args_0), // 0
  130. bind(&sum_of_args_1, _1), // 1
  131. bind(&sum_of_args_2, _1, _2), // 3
  132. bind(&sum_of_args_3, _1, _2, _3), // 6
  133. bind(&sum_of_args_4, _1, _2, _3, 4), // 10
  134. bind(&sum_of_args_5, _1, _2, _3, 4, 5), // 15
  135. bind(&sum_of_args_6, _1, _2, _3, 4, 5, 6), // 21
  136. bind(&sum_of_args_7, _1, _2, _3, 4, 5, 6, 7), // 28
  137. bind(&sum_of_args_8, _1, _2, _3, 4, 5, 6, 7, 8) // 36
  138. )(i, j, k) == 120);
  139. // deeper nesting
  140. result =
  141. bind(&sum_of_args_1, // 12
  142. bind(&sum_of_args_4, // 12
  143. bind(&sum_of_args_2, // 3
  144. bind(&sum_of_args_1, // 1
  145. bind(&sum_of_args_1, _1) // 1
  146. ),
  147. _2),
  148. _2,
  149. _3,
  150. 4)
  151. )(i, j, k);
  152. BOOST_CHECK(result == 12);
  153. test_member_functions();
  154. return 0;
  155. }