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