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 STL equal_to with call_if.
9 // C++17 warning from Boost.Bind.
10 #define _SILENCE_CXX17_ADAPTOR_TYPEDEFS_DEPRECATION_WARNING
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>
21 boost::contract::test::detail::oteststream out;
24 struct void_equal_to {
25 typedef void result_type; // Test void result type.
27 void operator()(T const& left, T const& right) const {
28 out << (left == right) << std::endl;
32 struct x {}; // Doest not have operator==.
35 std::ostringstream ok;
37 out << std::boolalpha;
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.
45 // Compiler-error... but not called.
46 boost::bind(std::equal_to<x>(), x1, x2)
49 ok.str(""); ok << true << std::endl;
50 BOOST_TEST(out.eq(ok.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; })
59 ok.str(""); ok << true << std::endl;
60 BOOST_TEST(out.eq(ok.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.
67 // Compiler-error... but not called.
68 boost::bind(void_equal_to<x>(), x1, x1)
70 ok.str(""); ok << false << std::endl;
71 BOOST_TEST(out.eq(ok.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)
79 boost::bind(void_equal_to<int>(), 123, 456) // False.
81 ok.str(""); ok << false << std::endl;
82 BOOST_TEST(out.eq(ok.str()));
84 return boost::report_errors();