Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / contract / test / call_if / equal_to_cxx14.cpp
1
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
6
7 // Test call_if equality check with C++14 generic lambdas.
8
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.
14 #include <sstream>
15 #include <ios>
16
17 boost::contract::test::detail::oteststream out;
18
19 struct x {}; // Does not have operator==.
20
21 int main() {
22     std::ostringstream ok;
23     ok << std::boolalpha;
24     out << std::boolalpha;
25     x x1, x2;
26     
27     out.str("");
28     out <<
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; })
32     << std::endl;
33     ok.str(""); ok << false << std::endl;
34     BOOST_TEST(out.eq(ok.str()));
35
36     out.str("");
37     out <<
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; })
41     << std::endl;
42     ok.str(""); ok << true << std::endl;
43     BOOST_TEST(out.eq(ok.str()));
44     
45     out.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; })
50     << std::endl;
51     ok.str(""); ok << true << std::endl;
52     BOOST_TEST(out.eq(ok.str()));
53
54     return boost::report_errors();
55 }
56