• 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 check (class and macro).
8 
9 #include "../detail/oteststream.hpp"
10 #include <boost/contract/check.hpp>
11 #include <boost/contract/assert.hpp>
12 #include <boost/detail/lightweight_test.hpp>
13 #include <sstream>
14 
15 boost::contract::test::detail::oteststream out;
16 
17 struct err {}; // Global decl so visible in MSVC10 lambdas.
18 
f(bool check)19 void f(bool check) {
20     #ifdef BOOST_CONTRACT_TEST_CHECK_MACRO
21         BOOST_CONTRACT_CHECK([&] () -> bool {
22             out << "f::check" << std::endl;
23             return check;
24         }());
25     #else
26         boost::contract::check c = [&] {
27             out << "f::check" << std::endl;
28             BOOST_CONTRACT_ASSERT(check);
29         };
30     #endif
31     out << "f::body" << std::endl;
32 }
33 
main()34 int main() {
35     std::ostringstream ok;
36 
37     out.str("");
38     f(true);
39     ok.str(""); ok
40         #ifndef BOOST_CONTRACT_NO_CHECKS
41             << "f::check" << std::endl
42         #endif
43         << "f::body" << std::endl
44     ;
45     BOOST_TEST(out.eq(ok.str()));
46 
47     boost::contract::set_check_failure([] { throw err(); });
48 
49     out.str("");
50     try {
51         f(false);
52         #ifndef BOOST_CONTRACT_NO_CHECKS
53             BOOST_TEST(false);
54             } catch(err const&) {
55                 ok.str("");
56                 ok << "f::check" << std::endl;
57         #else
58             ok.str("");
59             ok << "f::body" << std::endl;
60         #endif
61         BOOST_TEST(out.eq(ok.str()));
62     } catch(...) { BOOST_TEST(false); }
63 
64     return boost::report_errors();
65 }
66 
67