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

source code of boost/libs/lambda/test/constructor_tests.cpp