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