2 // Copyright (C) 2008-2018 Lorenzo Caminiti
3 // Distributed under the Boost Software License, Version 1.0 (see accompanying
4 // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
5 // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
7 // Test call_if equality check with C++14 generic lambdas.
9 #include "../detail/oteststream.hpp"
10 #include <boost/contract/call_if.hpp>
11 #include <boost/type_traits/has_equal_to.hpp>
12 #include <boost/detail/lightweight_test.hpp>
13 #include <functional> // std::bind for generic lambdas.
17 boost::contract::test::detail::oteststream out;
19 struct x {}; // Does not have operator==.
22 std::ostringstream ok;
24 out << std::boolalpha;
29 boost::contract::call_if<boost::has_equal_to<int> >(
30 std::bind([] (auto a, auto b) { return a == b; }, 123, 456)
31 ).else_([] { return true; })
33 ok.str(""); ok << false << std::endl;
34 BOOST_TEST(out.eq(ok.str()));
38 boost::contract::call_if<boost::has_equal_to<x> >(
39 std::bind([] (auto a, auto b) { return a == b; }, x1, x2)
40 ).else_([] { return true; })
42 ok.str(""); ok << true << std::endl;
43 BOOST_TEST(out.eq(ok.str()));
46 out << // Test explicit result, cannot deduce from lambda missing `-> bool`.
47 boost::contract::call_if<boost::has_equal_to<x> >(
48 std::bind([] (auto a, auto b) { return a == b; }, x1, x2)
49 ).else_([] { return true; })
51 ok.str(""); ok << true << std::endl;
52 BOOST_TEST(out.eq(ok.str()));
54 return boost::report_errors();