diff --git a/test/bind_tests_simple.cpp b/test/bind_tests_simple.cpp index 8fc6f73..d562d44 100644 --- a/test/bind_tests_simple.cpp +++ b/test/bind_tests_simple.cpp @@ -7,6 +7,7 @@ #include + using namespace std; using namespace boost::lambda; @@ -50,77 +51,78 @@ void test_member_functions() int test_main(int, char *[]) { int i = 1; int j = 2; int k = 3; + int result; // bind all parameters - BOOST_TEST(bind(sum_of_args_0)()==0); - BOOST_TEST(bind(sum_of_args_1, 1)()==1); - BOOST_TEST(bind(sum_of_args_2, 1, 2)()==3); - BOOST_TEST(bind(sum_of_args_3, 1, 2, 3)()==6); - BOOST_TEST(bind(sum_of_args_4, 1, 2, 3, 4)()==10); - BOOST_TEST(bind(sum_of_args_5, 1, 2, 3, 4, 5)()==15); - BOOST_TEST(bind(sum_of_args_6, 1, 2, 3, 4, 5, 6)()==21); - BOOST_TEST(bind(sum_of_args_7, 1, 2, 3, 4, 5, 6, 7)()==28); - BOOST_TEST(bind(sum_of_args_8, 1, 2, 3, 4, 5, 6, 7, 8)()==36); - BOOST_TEST(bind(sum_of_args_9, 1, 2, 3, 4, 5, 6, 7, 8, 9)()==45); + BOOST_TEST(bind(&sum_of_args_0)()==0); + BOOST_TEST(bind(&sum_of_args_1, 1)()==1); + BOOST_TEST(bind(&sum_of_args_2, 1, 2)()==3); + BOOST_TEST(bind(&sum_of_args_3, 1, 2, 3)()==6); + BOOST_TEST(bind(&sum_of_args_4, 1, 2, 3, 4)()==10); + BOOST_TEST(bind(&sum_of_args_5, 1, 2, 3, 4, 5)()==15); + BOOST_TEST(bind(&sum_of_args_6, 1, 2, 3, 4, 5, 6)()==21); + BOOST_TEST(bind(&sum_of_args_7, 1, 2, 3, 4, 5, 6, 7)()==28); + BOOST_TEST(bind(&sum_of_args_8, 1, 2, 3, 4, 5, 6, 7, 8)()==36); + BOOST_TEST(bind(&sum_of_args_9, 1, 2, 3, 4, 5, 6, 7, 8, 9)()==45); // first parameter open - BOOST_TEST(bind(sum_of_args_0)()==0); - BOOST_TEST(bind(sum_of_args_1, _1)(i)==1); - BOOST_TEST(bind(sum_of_args_2, _1, 2)(i)==3); - BOOST_TEST(bind(sum_of_args_3, _1, 2, 3)(i)==6); - BOOST_TEST(bind(sum_of_args_4, _1, 2, 3, 4)(i)==10); - BOOST_TEST(bind(sum_of_args_5, _1, 2, 3, 4, 5)(i)==15); - BOOST_TEST(bind(sum_of_args_6, _1, 2, 3, 4, 5, 6)(i)==21); - BOOST_TEST(bind(sum_of_args_7, _1, 2, 3, 4, 5, 6, 7)(i)==28); - BOOST_TEST(bind(sum_of_args_8, _1, 2, 3, 4, 5, 6, 7, 8)(i)==36); - BOOST_TEST(bind(sum_of_args_9, _1, 2, 3, 4, 5, 6, 7, 8, 9)(i)==45); + BOOST_TEST(bind(&sum_of_args_0)()==0); + BOOST_TEST(bind(&sum_of_args_1, _1)(i)==1); + BOOST_TEST(bind(&sum_of_args_2, _1, 2)(i)==3); + BOOST_TEST(bind(&sum_of_args_3, _1, 2, 3)(i)==6); + BOOST_TEST(bind(&sum_of_args_4, _1, 2, 3, 4)(i)==10); + BOOST_TEST(bind(&sum_of_args_5, _1, 2, 3, 4, 5)(i)==15); + BOOST_TEST(bind(&sum_of_args_6, _1, 2, 3, 4, 5, 6)(i)==21); + BOOST_TEST(bind(&sum_of_args_7, _1, 2, 3, 4, 5, 6, 7)(i)==28); + BOOST_TEST(bind(&sum_of_args_8, _1, 2, 3, 4, 5, 6, 7, 8)(i)==36); + BOOST_TEST(bind(&sum_of_args_9, _1, 2, 3, 4, 5, 6, 7, 8, 9)(i)==45); // two open arguments - BOOST_TEST(bind(sum_of_args_0)()==0); - BOOST_TEST(bind(sum_of_args_1, _1)(i)==1); - BOOST_TEST(bind(sum_of_args_2, _1, _2)(i, j)==3); - BOOST_TEST(bind(sum_of_args_3, _1, _2, 3)(i, j)==6); - BOOST_TEST(bind(sum_of_args_4, _1, _2, 3, 4)(i, j)==10); - BOOST_TEST(bind(sum_of_args_5, _1, _2, 3, 4, 5)(i, j)==15); - BOOST_TEST(bind(sum_of_args_6, _1, _2, 3, 4, 5, 6)(i, j)==21); - BOOST_TEST(bind(sum_of_args_7, _1, _2, 3, 4, 5, 6, 7)(i, j)==28); - BOOST_TEST(bind(sum_of_args_8, _1, _2, 3, 4, 5, 6, 7, 8)(i, j)==36); - BOOST_TEST(bind(sum_of_args_9, _1, _2, 3, 4, 5, 6, 7, 8, 9)(i, j)==45); + BOOST_TEST(bind(&sum_of_args_0)()==0); + BOOST_TEST(bind(&sum_of_args_1, _1)(i)==1); + BOOST_TEST(bind(&sum_of_args_2, _1, _2)(i, j)==3); + BOOST_TEST(bind(&sum_of_args_3, _1, _2, 3)(i, j)==6); + BOOST_TEST(bind(&sum_of_args_4, _1, _2, 3, 4)(i, j)==10); + BOOST_TEST(bind(&sum_of_args_5, _1, _2, 3, 4, 5)(i, j)==15); + BOOST_TEST(bind(&sum_of_args_6, _1, _2, 3, 4, 5, 6)(i, j)==21); + BOOST_TEST(bind(&sum_of_args_7, _1, _2, 3, 4, 5, 6, 7)(i, j)==28); + BOOST_TEST(bind(&sum_of_args_8, _1, _2, 3, 4, 5, 6, 7, 8)(i, j)==36); + BOOST_TEST(bind(&sum_of_args_9, _1, _2, 3, 4, 5, 6, 7, 8, 9)(i, j)==45); // three open arguments - BOOST_TEST(bind(sum_of_args_0)()==0); - BOOST_TEST(bind(sum_of_args_1, _1)(i)==1); - BOOST_TEST(bind(sum_of_args_2, _1, _2)(i, j)==3); - BOOST_TEST(bind(sum_of_args_3, _1, _2, _3)(i, j, k)==6); - BOOST_TEST(bind(sum_of_args_4, _1, _2, _3, 4)(i, j, k)==10); - BOOST_TEST(bind(sum_of_args_5, _1, _2, _3, 4, 5)(i, j, k)==15); - BOOST_TEST(bind(sum_of_args_6, _1, _2, _3, 4, 5, 6)(i, j, k)==21); - BOOST_TEST(bind(sum_of_args_7, _1, _2, _3, 4, 5, 6, 7)(i, j, k)==28); - BOOST_TEST(bind(sum_of_args_8, _1, _2, _3, 4, 5, 6, 7, 8)(i, j, k)==36); - BOOST_TEST(bind(sum_of_args_9, _1, _2, _3, 4, 5, 6, 7, 8, 9)(i, j, k)==45); + BOOST_TEST(bind(&sum_of_args_0)()==0); + BOOST_TEST(bind(&sum_of_args_1, _1)(i)==1); + BOOST_TEST(bind(&sum_of_args_2, _1, _2)(i, j)==3); + BOOST_TEST(bind(&sum_of_args_3, _1, _2, _3)(i, j, k)==6); + BOOST_TEST(bind(&sum_of_args_4, _1, _2, _3, 4)(i, j, k)==10); + BOOST_TEST(bind(&sum_of_args_5, _1, _2, _3, 4, 5)(i, j, k)==15); + BOOST_TEST(bind(&sum_of_args_6, _1, _2, _3, 4, 5, 6)(i, j, k)==21); + BOOST_TEST(bind(&sum_of_args_7, _1, _2, _3, 4, 5, 6, 7)(i, j, k)==28); + BOOST_TEST(bind(&sum_of_args_8, _1, _2, _3, 4, 5, 6, 7, 8)(i, j, k)==36); + BOOST_TEST(bind(&sum_of_args_9, _1, _2, _3, 4, 5, 6, 7, 8, 9)(i, j, k)==45); // function compositions with bind - BOOST_TEST(bind(sum_of_args_3, bind(sum_of_args_2, _1, 2), 2, 3)(i)==8); + BOOST_TEST(bind(&sum_of_args_3, bind(&sum_of_args_2, _1, 2), 2, 3)(i)==8); BOOST_TEST( - bind(sum_of_args_9, - bind(sum_of_args_0), // 0 - bind(sum_of_args_1, _1), // 1 - bind(sum_of_args_2, _1, _2), // 3 - bind(sum_of_args_3, _1, _2, _3), // 6 - bind(sum_of_args_4, _1, _2, _3, 4), // 10 - bind(sum_of_args_5, _1, _2, _3, 4, 5), // 15 - bind(sum_of_args_6, _1, _2, _3, 4, 5, 6), // 21 - bind(sum_of_args_7, _1, _2, _3, 4, 5, 6, 7), // 28 - bind(sum_of_args_8, _1, _2, _3, 4, 5, 6, 7, 8) // 36 + bind(&sum_of_args_9, + bind(&sum_of_args_0), // 0 + bind(&sum_of_args_1, _1), // 1 + bind(&sum_of_args_2, _1, _2), // 3 + bind(&sum_of_args_3, _1, _2, _3), // 6 + bind(&sum_of_args_4, _1, _2, _3, 4), // 10 + bind(&sum_of_args_5, _1, _2, _3, 4, 5), // 15 + bind(&sum_of_args_6, _1, _2, _3, 4, 5, 6), // 21 + bind(&sum_of_args_7, _1, _2, _3, 4, 5, 6, 7), // 28 + bind(&sum_of_args_8, _1, _2, _3, 4, 5, 6, 7, 8) // 36 )(i, j, k) == 120); // deeper nesting - int result = - bind(sum_of_args_1, // 12 - bind(sum_of_args_4, // 12 - bind(sum_of_args_2, // 3 - bind(sum_of_args_1, // 1 - bind(sum_of_args_1, _1) // 1 + result = + bind(&sum_of_args_1, // 12 + bind(&sum_of_args_4, // 12 + bind(&sum_of_args_2, // 3 + bind(&sum_of_args_1, // 1 + bind(&sum_of_args_1, _1) // 1 ), _2), _2, @@ -131,5 +133,6 @@ int test_main(int, char *[]) { test_member_functions(); + return 0; } diff --git a/test/bind_tests_simple_function_references.cpp b/test/bind_tests_simple_function_references.cpp new file mode 100644 index 0000000..0ab0876 --- /dev/null +++ b/test/bind_tests_simple_function_references.cpp @@ -0,0 +1,138 @@ +// bind_tests_simple.cpp -------------------------------- + +#define BOOST_INCLUDE_MAIN // for testing, include rather than link +#include // see "Header Implementation Option" + +#include "boost/lambda/bind.hpp" + +#include + + + using namespace std; + using namespace boost::lambda; + + +int sum_of_args_0() { return 0; } +int sum_of_args_1(int a) { return a; } +int sum_of_args_2(int a, int b) { return a+b; } +int sum_of_args_3(int a, int b, int c) { return a+b+c; } +int sum_of_args_4(int a, int b, int c, int d) { return a+b+c+d; } +int sum_of_args_5(int a, int b, int c, int d, int e) { return a+b+c+d+e; } +int sum_of_args_6(int a, int b, int c, int d, int e, int f) { return a+b+c+d+e+f; } +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; } +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; } +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; } + + +// ---------------------------- + +class A { + int i; +public: + A(int n) : i(n) {}; + int add(const int& j) { return i + j; } +}; + +void test_member_functions() +{ + using boost::ref; + A a(10); + int i = 1; + + BOOST_TEST(bind(&A::add, ref(a), _1)(i) == 11); + BOOST_TEST(bind(&A::add, &a, _1)(i) == 11); + BOOST_TEST(bind(&A::add, _1, 1)(a) == 11); + BOOST_TEST(bind(&A::add, _1, 1)(make_const(&a)) == 11); + + // This should fail, as lambda functors store arguments as const + // bind(&A::add, a, _1); +} + +int test_main(int, char *[]) { + + int i = 1; int j = 2; int k = 3; + int result; + + // bind all parameters + BOOST_TEST(bind(sum_of_args_0)()==0); + BOOST_TEST(bind(sum_of_args_1, 1)()==1); + BOOST_TEST(bind(sum_of_args_2, 1, 2)()==3); + BOOST_TEST(bind(sum_of_args_3, 1, 2, 3)()==6); + BOOST_TEST(bind(sum_of_args_4, 1, 2, 3, 4)()==10); + BOOST_TEST(bind(sum_of_args_5, 1, 2, 3, 4, 5)()==15); + BOOST_TEST(bind(sum_of_args_6, 1, 2, 3, 4, 5, 6)()==21); + BOOST_TEST(bind(sum_of_args_7, 1, 2, 3, 4, 5, 6, 7)()==28); + BOOST_TEST(bind(sum_of_args_8, 1, 2, 3, 4, 5, 6, 7, 8)()==36); + BOOST_TEST(bind(sum_of_args_9, 1, 2, 3, 4, 5, 6, 7, 8, 9)()==45); + + // first parameter open + BOOST_TEST(bind(sum_of_args_0)()==0); + BOOST_TEST(bind(sum_of_args_1, _1)(i)==1); + BOOST_TEST(bind(sum_of_args_2, _1, 2)(i)==3); + BOOST_TEST(bind(sum_of_args_3, _1, 2, 3)(i)==6); + BOOST_TEST(bind(sum_of_args_4, _1, 2, 3, 4)(i)==10); + BOOST_TEST(bind(sum_of_args_5, _1, 2, 3, 4, 5)(i)==15); + BOOST_TEST(bind(sum_of_args_6, _1, 2, 3, 4, 5, 6)(i)==21); + BOOST_TEST(bind(sum_of_args_7, _1, 2, 3, 4, 5, 6, 7)(i)==28); + BOOST_TEST(bind(sum_of_args_8, _1, 2, 3, 4, 5, 6, 7, 8)(i)==36); + BOOST_TEST(bind(sum_of_args_9, _1, 2, 3, 4, 5, 6, 7, 8, 9)(i)==45); + + // two open arguments + BOOST_TEST(bind(sum_of_args_0)()==0); + BOOST_TEST(bind(sum_of_args_1, _1)(i)==1); + BOOST_TEST(bind(sum_of_args_2, _1, _2)(i, j)==3); + BOOST_TEST(bind(sum_of_args_3, _1, _2, 3)(i, j)==6); + BOOST_TEST(bind(sum_of_args_4, _1, _2, 3, 4)(i, j)==10); + BOOST_TEST(bind(sum_of_args_5, _1, _2, 3, 4, 5)(i, j)==15); + BOOST_TEST(bind(sum_of_args_6, _1, _2, 3, 4, 5, 6)(i, j)==21); + BOOST_TEST(bind(sum_of_args_7, _1, _2, 3, 4, 5, 6, 7)(i, j)==28); + BOOST_TEST(bind(sum_of_args_8, _1, _2, 3, 4, 5, 6, 7, 8)(i, j)==36); + BOOST_TEST(bind(sum_of_args_9, _1, _2, 3, 4, 5, 6, 7, 8, 9)(i, j)==45); + + // three open arguments + BOOST_TEST(bind(sum_of_args_0)()==0); + BOOST_TEST(bind(sum_of_args_1, _1)(i)==1); + BOOST_TEST(bind(sum_of_args_2, _1, _2)(i, j)==3); + BOOST_TEST(bind(sum_of_args_3, _1, _2, _3)(i, j, k)==6); + BOOST_TEST(bind(sum_of_args_4, _1, _2, _3, 4)(i, j, k)==10); + BOOST_TEST(bind(sum_of_args_5, _1, _2, _3, 4, 5)(i, j, k)==15); + BOOST_TEST(bind(sum_of_args_6, _1, _2, _3, 4, 5, 6)(i, j, k)==21); + BOOST_TEST(bind(sum_of_args_7, _1, _2, _3, 4, 5, 6, 7)(i, j, k)==28); + BOOST_TEST(bind(sum_of_args_8, _1, _2, _3, 4, 5, 6, 7, 8)(i, j, k)==36); + BOOST_TEST(bind(sum_of_args_9, _1, _2, _3, 4, 5, 6, 7, 8, 9)(i, j, k)==45); + + // function compositions with bind + BOOST_TEST(bind(sum_of_args_3, bind(sum_of_args_2, _1, 2), 2, 3)(i)==8); + BOOST_TEST( + bind(sum_of_args_9, + bind(sum_of_args_0), // 0 + bind(sum_of_args_1, _1), // 1 + bind(sum_of_args_2, _1, _2), // 3 + bind(sum_of_args_3, _1, _2, _3), // 6 + bind(sum_of_args_4, _1, _2, _3, 4), // 10 + bind(sum_of_args_5, _1, _2, _3, 4, 5), // 15 + bind(sum_of_args_6, _1, _2, _3, 4, 5, 6), // 21 + bind(sum_of_args_7, _1, _2, _3, 4, 5, 6, 7), // 28 + bind(sum_of_args_8, _1, _2, _3, 4, 5, 6, 7, 8) // 36 + )(i, j, k) == 120); + + // deeper nesting + result = + bind(sum_of_args_1, // 12 + bind(sum_of_args_4, // 12 + bind(sum_of_args_2, // 3 + bind(sum_of_args_1, // 1 + bind(sum_of_args_1, _1) // 1 + ), + _2), + _2, + _3, + 4) + )(i, j, k); + BOOST_TEST(result == 12); + + test_member_functions(); + + + return 0; +}