9 #include <boost/test/data/test_case.hpp>
10 #include <boost/test/unit_test.hpp>
18 #include <type_traits>
26 BOOST_AUTO_TEST_SUITE(DelegateTests)
35 BOOST_CHECK(!sum.connected());
39 BOOST_CHECK_EQUAL(
sum(2, 5), 7);
40 BOOST_CHECK_NE(
sum(2, 3), 7);
42 sum.connect([](
const void*,
int a,
int b) ->
int {
return a +
b; });
45 BOOST_CHECK(sum.connected());
47 BOOST_CHECK_EQUAL(
sum(2, 5), 7);
48 BOOST_CHECK_NE(
sum(2, 3), 7);
59 BOOST_CHECK(!mult.connected());
64 BOOST_CHECK(mult.connected());
67 BOOST_CHECK_NE(mult(2, 3.2), 58.9);
79 BOOST_CHECK(!sub.connected());
85 BOOST_CHECK(sub.connected());
87 BOOST_CHECK_EQUAL(sub(7), 7 - 18);
98 BOOST_CHECK(!add.connected());
102 BOOST_CHECK(add.connected());
104 BOOST_CHECK_EQUAL(add(4, 4), 8);
111 BOOST_CHECK(add.connected());
113 BOOST_CHECK_EQUAL(add(4, 4), 8);
119 BOOST_CHECK(!add.connected());
123 BOOST_CHECK(add.connected());
125 BOOST_CHECK_EQUAL(add(4, 4), 8);
144 BOOST_CHECK_EQUAL(v, 42);
166 BOOST_CHECK_EQUAL(v, 42);
175 auto lambda = [&](
int n) ->
int {
183 BOOST_CHECK(d.connected());
184 BOOST_CHECK(d(2) == 1);
190 BOOST_CHECK(d.connected());
191 BOOST_CHECK(d(2) == 2);
197 BOOST_CHECK(d.connected());
198 BOOST_CHECK(d(2) == 3);
207 bool* destructorCalled;
224 auto s = std::make_unique<const SignatureTest>();
230 BOOST_CHECK_EQUAL(v, 42);
234 bool destructorCalled =
false;
235 auto s = std::make_unique<const CheckDestructor>(&destructorCalled);
237 BOOST_CHECK_EQUAL(destructorCalled,
false);
239 BOOST_CHECK_EQUAL(destructorCalled,
false);
241 BOOST_CHECK_EQUAL(destructorCalled,
false);
243 BOOST_CHECK_EQUAL(d(), 4);
244 BOOST_CHECK_EQUAL(dCopy(), 4);
245 BOOST_CHECK_EQUAL(destructorCalled,
false);
248 BOOST_CHECK_EQUAL(destructorCalled,
false);
251 BOOST_CHECK_EQUAL(destructorCalled,
false);
255 BOOST_CHECK_EQUAL(destructorCalled,
false);
260 BOOST_CHECK_EQUAL(destructorCalled,
false);
261 BOOST_CHECK_EQUAL(d(), 4);
262 BOOST_CHECK_EQUAL(destructorCalled,
false);
265 BOOST_CHECK_EQUAL(destructorCalled,
true);
267 destructorCalled =
false;
268 s = std::make_unique<const CheckDestructor>(&destructorCalled);
270 BOOST_CHECK_EQUAL(destructorCalled,
false);
274 BOOST_CHECK_EQUAL(destructorCalled,
false);
276 BOOST_CHECK_EQUAL(destructorCalled,
false);
277 BOOST_CHECK_EQUAL(d(), 4);
278 BOOST_CHECK_EQUAL(destructorCalled,
false);
281 BOOST_CHECK_EQUAL(destructorCalled,
true);
285 bool destructorCalled =
false;
286 auto s = std::make_unique<const CheckDestructor>(&destructorCalled);
288 BOOST_CHECK_EQUAL(destructorCalled,
false);
290 BOOST_CHECK_EQUAL(destructorCalled,
false);
293 BOOST_CHECK_EQUAL(destructorCalled,
false);
294 BOOST_CHECK_EQUAL(d(), 4);
295 BOOST_CHECK_EQUAL(dCopy(), 4);
296 BOOST_CHECK_EQUAL(destructorCalled,
false);
298 BOOST_CHECK_EQUAL(destructorCalled,
false);
302 BOOST_CHECK_EQUAL(destructorCalled,
false);
306 BOOST_CHECK_EQUAL(destructorCalled,
false);
311 BOOST_CHECK_EQUAL(destructorCalled,
false);
312 BOOST_CHECK_EQUAL(d(), 4);
313 BOOST_CHECK_EQUAL(destructorCalled,
false);
315 BOOST_CHECK_EQUAL(destructorCalled,
true);
318 BOOST_CHECK_EQUAL(destructorCalled,
true);
324 BOOST_CHECK_EQUAL(d(), 8);
328 BOOST_CHECK_EQUAL(d(), 8);
332 BOOST_CHECK_EQUAL(d(), 8);
358 BOOST_CHECK_EQUAL(d(),
"derived");
376 BOOST_CHECK_EQUAL(d(),
"derived");
378 const auto* instance = d.instance();
381 std::remove_const_t<std::remove_pointer_t<decltype(instance)>>,
383 "Did not get correct instance pointer");
384 BOOST_CHECK_NE(dynamic_cast<const DelegateInterface*>(instance),
nullptr);
385 BOOST_CHECK_NE(dynamic_cast<const ConcreteDelegate*>(instance),
nullptr);
392 BOOST_CHECK_EQUAL(d(),
"derived");
394 const auto* instance = d.instance();
397 std::remove_const_t<std::remove_pointer_t<decltype(instance)>>,
399 "Did not get correct instance pointer");
400 BOOST_CHECK_NE(dynamic_cast<const DelegateInterface*>(instance),
nullptr);
401 BOOST_CHECK_NE(dynamic_cast<const ConcreteDelegate*>(instance),
nullptr);
407 std::make_unique<const ConcreteDelegate>());
408 BOOST_CHECK_EQUAL(d(),
"derived");
410 const auto* instance = d.instance();
413 std::remove_const_t<std::remove_pointer_t<decltype(instance)>>,
415 "Did not get correct instance pointer");
416 BOOST_CHECK_NE(dynamic_cast<const DelegateInterface*>(instance),
nullptr);
417 BOOST_CHECK_NE(dynamic_cast<const ConcreteDelegate*>(instance),
nullptr);
424 std::make_unique<const ConcreteDelegate>());
425 BOOST_CHECK_EQUAL(d(),
"derived");
427 const auto* instance = d.instance();
430 std::remove_const_t<std::remove_pointer_t<decltype(instance)>>,
432 "Did not get correct instance pointer");
433 BOOST_CHECK_NE(dynamic_cast<const DelegateInterface*>(instance),
nullptr);
434 BOOST_CHECK_NE(dynamic_cast<const ConcreteDelegate*>(instance),
nullptr);
449 BOOST_AUTO_TEST_SUITE_END()