• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 with true condition and void result type.
8 
9 #include "../detail/oteststream.hpp"
10 #include <boost/contract/call_if.hpp>
11 #include <boost/bind.hpp>
12 #include <boost/type_traits/has_equal_to.hpp>
13 #include <boost/detail/lightweight_test.hpp>
14 #include <sstream>
15 #include <ios>
16 
17 boost::contract::test::detail::oteststream out;
18 
19 struct eq {
20     typedef void result_type; // Test void result type.
21 
22     template<typename L, typename R>
operator ()eq23     result_type operator()(L left, R right) const {
24         out << (left == right) << std::endl; // Requires operator==.
25     }
26 };
27 
28 struct x {}; // Doest not have operator==.
29 
main()30 int main() {
31     std::ostringstream ok;
32     ok << std::boolalpha;
33     out << std::boolalpha;
34     x x1, x2;;
35 
36     out.str("");
37     boost::contract::call_if<boost::has_equal_to<int> >(
38         boost::bind(eq(), 123, 456) // False.
39     ); // Test no else (not called).
40     ok.str(""); ok << false << std::endl;
41     BOOST_TEST(out.eq(ok.str()));
42 
43     out.str("");
44     boost::contract::call_if<boost::has_equal_to<int> >(
45         boost::bind(eq(), 123, 123) // True.
46     ).else_(
47         [] { return false; }
48     ); // Test else (not called).
49     ok.str(""); ok << true << std::endl;
50     BOOST_TEST(out.eq(ok.str()));
51 
52     out.str("");
53     // Test "..._c".
54     boost::contract::call_if_c<boost::has_equal_to<int>::value>(
55         boost::bind(eq(), 123, 123) // True.
56     ).else_( // Test else (not called).
57         boost::bind(eq(), x1, x2) // Compiler-error... but not called.
58     );
59     ok.str(""); ok << true << std::endl;
60     BOOST_TEST(out.eq(ok.str()));
61 
62     return boost::report_errors();
63 }
64 
65