Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / contract / test / call_if / equal_to.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 STL equal_to with call_if.
8
9 // C++17 warning from Boost.Bind.
10 #define _SILENCE_CXX17_ADAPTOR_TYPEDEFS_DEPRECATION_WARNING
11
12 #include "../detail/oteststream.hpp"
13 #include <boost/contract/call_if.hpp>
14 #include <boost/bind.hpp>
15 #include <boost/type_traits/has_equal_to.hpp>
16 #include <boost/detail/lightweight_test.hpp>
17 #include <functional>
18 #include <sstream>
19 #include <ios>
20
21 boost::contract::test::detail::oteststream out;
22
23 template<typename T>
24 struct void_equal_to {
25     typedef void result_type; // Test void result type.
26
27     void operator()(T const& left, T const& right) const {
28         out << (left == right) << std::endl;
29     }
30 };
31
32 struct x {}; // Doest not have operator==.
33
34 int main() {
35     std::ostringstream ok;
36     ok << std::boolalpha;
37     out << std::boolalpha;
38     x x1, x2;;
39
40     out.str("");
41     out << // Test on true condition with non-void result type.
42         boost::contract::call_if<boost::has_equal_to<int> >(
43             boost::bind(std::equal_to<int>(), 123, 123) // True.
44         ).else_(
45             // Compiler-error... but not called.
46             boost::bind(std::equal_to<x>(), x1, x2)
47         )
48     << std::endl;
49     ok.str(""); ok << true << std::endl;
50     BOOST_TEST(out.eq(ok.str()));
51     
52     out.str("");
53     out << // Test on false condition with non-void result type.
54         boost::contract::call_if<boost::has_equal_to<x> >(
55             // Compiler-error... but not called.
56             boost::bind(std::equal_to<x>(), x1, x2)
57         ).else_([] { return true; })
58     << std::endl;
59     ok.str(""); ok << true << std::endl;
60     BOOST_TEST(out.eq(ok.str()));
61     
62     out.str("");
63     // Test on true condition void result type.
64     boost::contract::call_if<boost::has_equal_to<int> >(
65         boost::bind(void_equal_to<int>(), 123, 456) // False.
66     ).else_(
67         // Compiler-error... but not called.
68         boost::bind(void_equal_to<x>(), x1, x1)
69     );
70     ok.str(""); ok << false << std::endl;
71     BOOST_TEST(out.eq(ok.str()));
72     
73     out.str("");
74     // Test on false condition with void result type.
75     boost::contract::call_if<boost::has_equal_to<x> >(
76         // Compiler-error... but not called.
77         boost::bind(void_equal_to<x>(), x1, x1)
78     ).else_(
79         boost::bind(void_equal_to<int>(), 123, 456) // False.
80     );
81     ok.str(""); ok << false << std::endl;
82     BOOST_TEST(out.eq(ok.str()));
83
84     return boost::report_errors();
85 }
86