]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
f67539c2 | 15 | #include <boost/core/lightweight_test.hpp> |
7c673cae FG |
16 | |
17 | #include "boost/lambda/bind.hpp" | |
18 | ||
19 | #include <iostream> | |
20 | ||
21 | using namespace boost::lambda; | |
22 | ||
23 | ||
24 | int sum_of_args_0() { return 0; } | |
25 | int sum_of_args_1(int a) { return a; } | |
26 | int sum_of_args_2(int a, int b) { return a+b; } | |
27 | int sum_of_args_3(int a, int b, int c) { return a+b+c; } | |
28 | int sum_of_args_4(int a, int b, int c, int d) { return a+b+c+d; } | |
29 | int sum_of_args_5(int a, int b, int c, int d, int e) { return a+b+c+d+e; } | |
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; } | |
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; } | |
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; } | |
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: | |
41 | A(int n) : i(n) {}; | |
42 | int add(const int& j) { return i + j; } | |
43 | }; | |
44 | ||
45 | void test_member_functions() | |
46 | { | |
47 | using boost::ref; | |
48 | A a(10); | |
49 | int i = 1; | |
50 | ||
f67539c2 TL |
51 | BOOST_TEST_EQ(bind(&A::add, ref(a), _1)(i), 11); |
52 | BOOST_TEST_EQ(bind(&A::add, &a, _1)(i), 11); | |
53 | BOOST_TEST_EQ(bind(&A::add, _1, 1)(a), 11); | |
54 | BOOST_TEST_EQ(bind(&A::add, _1, 1)(make_const(&a)), 11); | |
7c673cae FG |
55 | |
56 | // This should fail, as lambda functors store arguments as const | |
57 | // bind(&A::add, a, _1); | |
58 | } | |
59 | ||
f67539c2 TL |
60 | int main() |
61 | { | |
7c673cae FG |
62 | int i = 1; int j = 2; int k = 3; |
63 | int result; | |
64 | ||
65 | ||
66 | // bind all parameters | |
f67539c2 TL |
67 | BOOST_TEST_EQ(bind(sum_of_args_0)(), 0); |
68 | BOOST_TEST_EQ(bind(sum_of_args_1, 1)(), 1); | |
69 | BOOST_TEST_EQ(bind(sum_of_args_2, 1, 2)(), 3); | |
70 | BOOST_TEST_EQ(bind(sum_of_args_3, 1, 2, 3)(), 6); | |
71 | BOOST_TEST_EQ(bind(sum_of_args_4, 1, 2, 3, 4)(), 10); | |
72 | BOOST_TEST_EQ(bind(sum_of_args_5, 1, 2, 3, 4, 5)(), 15); | |
73 | BOOST_TEST_EQ(bind(sum_of_args_6, 1, 2, 3, 4, 5, 6)(), 21); | |
74 | BOOST_TEST_EQ(bind(sum_of_args_7, 1, 2, 3, 4, 5, 6, 7)(), 28); | |
75 | BOOST_TEST_EQ(bind(sum_of_args_8, 1, 2, 3, 4, 5, 6, 7, 8)(), 36); | |
76 | BOOST_TEST_EQ(bind(sum_of_args_9, 1, 2, 3, 4, 5, 6, 7, 8, 9)(), 45); | |
7c673cae FG |
77 | |
78 | // first parameter open | |
f67539c2 TL |
79 | BOOST_TEST_EQ(bind(sum_of_args_0)(), 0); |
80 | BOOST_TEST_EQ(bind(sum_of_args_1, _1)(i), 1); | |
81 | BOOST_TEST_EQ(bind(sum_of_args_2, _1, 2)(i), 3); | |
82 | BOOST_TEST_EQ(bind(sum_of_args_3, _1, 2, 3)(i), 6); | |
83 | BOOST_TEST_EQ(bind(sum_of_args_4, _1, 2, 3, 4)(i), 10); | |
84 | BOOST_TEST_EQ(bind(sum_of_args_5, _1, 2, 3, 4, 5)(i), 15); | |
85 | BOOST_TEST_EQ(bind(sum_of_args_6, _1, 2, 3, 4, 5, 6)(i), 21); | |
86 | BOOST_TEST_EQ(bind(sum_of_args_7, _1, 2, 3, 4, 5, 6, 7)(i), 28); | |
87 | BOOST_TEST_EQ(bind(sum_of_args_8, _1, 2, 3, 4, 5, 6, 7, 8)(i), 36); | |
88 | BOOST_TEST_EQ(bind(sum_of_args_9, _1, 2, 3, 4, 5, 6, 7, 8, 9)(i), 45); | |
7c673cae FG |
89 | |
90 | // two open arguments | |
f67539c2 TL |
91 | BOOST_TEST_EQ(bind(sum_of_args_0)(), 0); |
92 | BOOST_TEST_EQ(bind(sum_of_args_1, _1)(i), 1); | |
93 | BOOST_TEST_EQ(bind(sum_of_args_2, _1, _2)(i, j), 3); | |
94 | BOOST_TEST_EQ(bind(sum_of_args_3, _1, _2, 3)(i, j), 6); | |
95 | BOOST_TEST_EQ(bind(sum_of_args_4, _1, _2, 3, 4)(i, j), 10); | |
96 | BOOST_TEST_EQ(bind(sum_of_args_5, _1, _2, 3, 4, 5)(i, j), 15); | |
97 | BOOST_TEST_EQ(bind(sum_of_args_6, _1, _2, 3, 4, 5, 6)(i, j), 21); | |
98 | BOOST_TEST_EQ(bind(sum_of_args_7, _1, _2, 3, 4, 5, 6, 7)(i, j), 28); | |
99 | BOOST_TEST_EQ(bind(sum_of_args_8, _1, _2, 3, 4, 5, 6, 7, 8)(i, j), 36); | |
100 | BOOST_TEST_EQ(bind(sum_of_args_9, _1, _2, 3, 4, 5, 6, 7, 8, 9)(i, j), 45); | |
7c673cae FG |
101 | |
102 | // three open arguments | |
f67539c2 TL |
103 | BOOST_TEST_EQ(bind(sum_of_args_0)(), 0); |
104 | BOOST_TEST_EQ(bind(sum_of_args_1, _1)(i), 1); | |
105 | BOOST_TEST_EQ(bind(sum_of_args_2, _1, _2)(i, j), 3); | |
106 | BOOST_TEST_EQ(bind(sum_of_args_3, _1, _2, _3)(i, j, k), 6); | |
107 | BOOST_TEST_EQ(bind(sum_of_args_4, _1, _2, _3, 4)(i, j, k), 10); | |
108 | BOOST_TEST_EQ(bind(sum_of_args_5, _1, _2, _3, 4, 5)(i, j, k), 15); | |
109 | BOOST_TEST_EQ(bind(sum_of_args_6, _1, _2, _3, 4, 5, 6)(i, j, k), 21); | |
110 | BOOST_TEST_EQ(bind(sum_of_args_7, _1, _2, _3, 4, 5, 6, 7)(i, j, k), 28); | |
111 | BOOST_TEST_EQ(bind(sum_of_args_8, _1, _2, _3, 4, 5, 6, 7, 8)(i, j, k), 36); | |
112 | BOOST_TEST_EQ(bind(sum_of_args_9, _1, _2, _3, 4, 5, 6, 7, 8, 9)(i, j, k), 45); | |
7c673cae FG |
113 | |
114 | // function compositions with bind | |
f67539c2 TL |
115 | BOOST_TEST_EQ(bind(sum_of_args_3, bind(sum_of_args_2, _1, 2), 2, 3)(i), 8); |
116 | BOOST_TEST_EQ( | |
7c673cae FG |
117 | bind(sum_of_args_9, |
118 | bind(sum_of_args_0), // 0 | |
119 | bind(sum_of_args_1, _1), // 1 | |
120 | bind(sum_of_args_2, _1, _2), // 3 | |
121 | bind(sum_of_args_3, _1, _2, _3), // 6 | |
122 | bind(sum_of_args_4, _1, _2, _3, 4), // 10 | |
123 | bind(sum_of_args_5, _1, _2, _3, 4, 5), // 15 | |
124 | bind(sum_of_args_6, _1, _2, _3, 4, 5, 6), // 21 | |
125 | bind(sum_of_args_7, _1, _2, _3, 4, 5, 6, 7), // 28 | |
126 | bind(sum_of_args_8, _1, _2, _3, 4, 5, 6, 7, 8) // 36 | |
f67539c2 | 127 | )(i, j, k), 120); |
7c673cae FG |
128 | |
129 | // deeper nesting | |
130 | result = | |
131 | bind(sum_of_args_1, // 12 | |
132 | bind(sum_of_args_4, // 12 | |
133 | bind(sum_of_args_2, // 3 | |
134 | bind(sum_of_args_1, // 1 | |
135 | bind(sum_of_args_1, _1) // 1 | |
136 | ), | |
137 | _2), | |
138 | _2, | |
139 | _3, | |
140 | 4) | |
141 | )(i, j, k); | |
f67539c2 | 142 | BOOST_TEST_EQ(result, 12); |
7c673cae FG |
143 | |
144 | test_member_functions(); | |
145 | ||
f67539c2 | 146 | return boost::report_errors(); |
7c673cae | 147 | } |