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. 8 9 #include "../detail/oteststream.hpp" 10 #include <boost/contract/core/config.hpp> 11 #ifndef BOOST_CONTRACT_NO_CHECKS 12 #include <boost/contract/check.hpp> 13 #include <boost/contract/assert.hpp> 14 #else 15 #include <boost/contract/core/check_macro.hpp> 16 #include <boost/contract/core/exception.hpp> 17 #endif 18 #include <boost/detail/lightweight_test.hpp> 19 #include <sstream> 20 21 boost::contract::test::detail::oteststream out; 22 f(bool check1,bool check2)23void f(bool check1, bool check2) { 24 BOOST_CONTRACT_CHECK([&] () -> bool { // Macro already so #ifdef needed. 25 out << "f::check1" << std::endl; 26 return check1; 27 }()); 28 #ifndef BOOST_CONTRACT_NO_CHECKS 29 boost::contract::check c = [&] { 30 out << "f::check2" << std::endl; 31 BOOST_CONTRACT_ASSERT(check2); 32 }; 33 #endif 34 out << "f::body" << std::endl; 35 } 36 37 struct err {}; // Global decl so visible in MSVC10 lambdas. 38 main()39int main() { 40 std::ostringstream ok; 41 42 out.str(""); 43 f(true, true); 44 ok.str(""); ok 45 #ifndef BOOST_CONTRACT_NO_CHECKS 46 << "f::check1" << std::endl 47 << "f::check2" << std::endl 48 #endif 49 << "f::body" << std::endl 50 ; 51 BOOST_TEST(out.eq(ok.str())); 52 53 boost::contract::set_check_failure([] { throw err(); }); 54 55 out.str(""); 56 try { 57 f(false, true); 58 #ifndef BOOST_CONTRACT_NO_CHECKS 59 BOOST_TEST(false); 60 } catch(err const&) { 61 ok.str(""); 62 ok << "f::check1" << std::endl; 63 #else 64 ok.str(""); 65 ok << "f::body" << std::endl; 66 #endif 67 BOOST_TEST(out.eq(ok.str())); 68 } catch(...) { BOOST_TEST(false); } 69 70 out.str(""); 71 try { 72 f(true, false); 73 #ifndef BOOST_CONTRACT_NO_CHECKS 74 BOOST_TEST(false); 75 } catch(err const&) { 76 ok.str(""); 77 ok << "f::check1" << std::endl; 78 ok << "f::check2" << std::endl; 79 #else 80 ok.str(""); 81 ok << "f::body" << std::endl; 82 #endif 83 BOOST_TEST(out.eq(ok.str())); 84 } catch(...) { BOOST_TEST(false); } 85 86 // No need to test `f(false, false)` because same as `f(false, true)`. 87 88 return boost::report_errors(); 89 } 90 91