1 // constructor_tests.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
18 #include "boost/lambda/lambda.hpp"
19 #include "boost/lambda/bind.hpp"
20
21 #include "boost/lambda/construct.hpp"
22
23 #include <iostream>
24 #include <algorithm>
25 #include <vector>
26
27 #ifdef BOOST_MSVC
28 #pragma warning(disable:4512)
29 #endif
30
31 using namespace boost::lambda;
32 namespace bl = boost::lambda;
33
34 template<class T>
check_tuple(int n,const T & t)35 bool check_tuple(int n, const T& t)
36 {
37 return (t.get_head() == n) && check_tuple(n+1, t.get_tail());
38 }
39
40 template<class T>
check_tuple(int n,T * t)41 bool check_tuple(int n, T * t)
42 {
43 bool ok = check_tuple(n, *t);
44 delete t;
45 return ok;
46 }
47
48 template <>
check_tuple(int,const null_type &)49 bool check_tuple(int /*n*/, const null_type& ) { return true; }
50
51
constructor_all_lengths()52 void constructor_all_lengths()
53 {
54 bool ok;
55 ok = check_tuple(
56 1,
57 bind(constructor<tuple<int> >(),
58 1)()
59 );
60 BOOST_TEST(ok);
61
62 ok = check_tuple(
63 1,
64 bind(constructor<tuple<int, int> >(),
65 1, 2)()
66 );
67 BOOST_TEST(ok);
68
69 ok = check_tuple(
70 1,
71 bind(constructor<tuple<int, int, int> >(),
72 1, 2, 3)()
73 );
74 BOOST_TEST(ok);
75
76 ok = check_tuple(
77 1,
78 bind(constructor<tuple<int, int, int, int> >(),
79 1, 2, 3, 4)()
80 );
81 BOOST_TEST(ok);
82
83 ok = check_tuple(
84 1,
85 bind(constructor<tuple<int, int, int, int, int> >(),
86 1, 2, 3, 4, 5)()
87 );
88 BOOST_TEST(ok);
89
90 ok = check_tuple(
91 1,
92 bind(constructor<tuple<int, int, int, int, int, int> >(),
93 1, 2, 3, 4, 5, 6)()
94 );
95 BOOST_TEST(ok);
96
97 ok = check_tuple(
98 1,
99 bind(constructor<tuple<int, int, int, int, int, int, int> >(),
100 1, 2, 3, 4, 5, 6, 7)()
101 );
102 BOOST_TEST(ok);
103
104 ok = check_tuple(
105 1,
106 bind(constructor<tuple<int, int, int, int, int, int, int, int> >(),
107 1, 2, 3, 4, 5, 6, 7, 8)()
108 );
109 BOOST_TEST(ok);
110
111 ok = check_tuple(
112 1,
113 bind(constructor<tuple<int, int, int, int, int, int, int, int, int> >(),
114 1, 2, 3, 4, 5, 6, 7, 8, 9)()
115 );
116 BOOST_TEST(ok);
117
118 }
119
new_ptr_all_lengths()120 void new_ptr_all_lengths()
121 {
122 bool ok;
123 ok = check_tuple(
124 1,
125 (bind(new_ptr<tuple<int> >(),
126 1))()
127 );
128 BOOST_TEST(ok);
129
130 ok = check_tuple(
131 1,
132 (bind(new_ptr<tuple<int, int> >(),
133 1, 2))()
134 );
135 BOOST_TEST(ok);
136
137 ok = check_tuple(
138 1,
139 (bind(new_ptr<tuple<int, int, int> >(),
140 1, 2, 3))()
141 );
142 BOOST_TEST(ok);
143
144 ok = check_tuple(
145 1,
146 (bind(new_ptr<tuple<int, int, int, int> >(),
147 1, 2, 3, 4))()
148 );
149 BOOST_TEST(ok);
150
151 ok = check_tuple(
152 1,
153 (bind(new_ptr<tuple<int, int, int, int, int> >(),
154 1, 2, 3, 4, 5))()
155 );
156 BOOST_TEST(ok);
157
158 ok = check_tuple(
159 1,
160 (bind(new_ptr<tuple<int, int, int, int, int, int> >(),
161 1, 2, 3, 4, 5, 6))()
162 );
163 BOOST_TEST(ok);
164
165 ok = check_tuple(
166 1,
167 (bind(new_ptr<tuple<int, int, int, int, int, int, int> >(),
168 1, 2, 3, 4, 5, 6, 7))()
169 );
170 BOOST_TEST(ok);
171
172 ok = check_tuple(
173 1,
174 (bind(new_ptr<tuple<int, int, int, int, int, int, int, int> >(),
175 1, 2, 3, 4, 5, 6, 7, 8))()
176 );
177 BOOST_TEST(ok);
178
179 ok = check_tuple(
180 1,
181 (bind(new_ptr<tuple<int, int, int, int, int, int, int, int, int> >(),
182 1, 2, 3, 4, 5, 6, 7, 8, 9))()
183 );
184 BOOST_TEST(ok);
185
186 }
187
188 class is_destructor_called {
189 bool& b;
190 public:
is_destructor_called(bool & bb)191 is_destructor_called(bool& bb) : b(bb) { b = false; }
~is_destructor_called()192 ~is_destructor_called() { b = true; }
193 };
194
test_destructor()195 void test_destructor ()
196 {
197 char space[sizeof(is_destructor_called)];
198 bool flag = false;
199
200 is_destructor_called* idc = new(space) is_destructor_called(flag);
201 BOOST_TEST_EQ(flag, false);
202 bind(destructor(), _1)(idc);
203 BOOST_TEST_EQ(flag, true);
204
205 idc = new(space) is_destructor_called(flag);
206 BOOST_TEST_EQ(flag, false);
207 bind(destructor(), _1)(*idc);
208 BOOST_TEST_EQ(flag, true);
209 }
210
211
212 class count_deletes {
213 public:
214 static int count;
~count_deletes()215 ~count_deletes() { ++count; }
216 };
217
218 int count_deletes::count = 0;
219
test_news_and_deletes()220 void test_news_and_deletes ()
221 {
222 int* i[10];
223 std::for_each(i, i+10, _1 = bind(new_ptr<int>(), 2));
224 int count_errors = 0;
225
226 std::for_each(i, i+10, (*_1 == 2) || ++var(count_errors));
227 BOOST_TEST_EQ(count_errors, 0);
228 std::for_each(i, i+10, bind(delete_ptr(), _1));
229
230
231 count_deletes* ct[10];
232 std::for_each(ct, ct+10, _1 = bind(new_ptr<count_deletes>()));
233 count_deletes::count = 0;
234 std::for_each(ct, ct+10, bind(delete_ptr(), _1));
235 BOOST_TEST_EQ(count_deletes::count, 10);
236
237 }
238
test_array_new_and_delete()239 void test_array_new_and_delete()
240 {
241 count_deletes* c;
242 (_1 = bind(new_array<count_deletes>(), 5))(c);
243 count_deletes::count = 0;
244
245 bind(delete_array(), _1)(c);
246 BOOST_TEST_EQ(count_deletes::count, 5);
247 }
248
249
delayed_construction()250 void delayed_construction()
251 {
252 std::vector<int> x(3);
253 std::vector<int> y(3);
254
255 std::fill(x.begin(), x.end(), 0);
256 std::fill(y.begin(), y.end(), 1);
257
258 std::vector<std::pair<int, int> > v;
259
260 std::transform(x.begin(), x.end(), y.begin(), std::back_inserter(v),
261 bl::bind(constructor<std::pair<int, int> >(), _1, _2) );
262 }
263
main()264 int main()
265 {
266 constructor_all_lengths();
267 new_ptr_all_lengths();
268 delayed_construction();
269 test_destructor();
270 test_news_and_deletes();
271 test_array_new_and_delete();
272
273 return boost::report_errors();
274 }
275