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 with postconditions. 8 9 #undef BOOST_CONTRACT_TEST_NO_F_POST 10 #include "decl.hpp" 11 12 #include <boost/detail/lightweight_test.hpp> 13 #include <sstream> 14 #include <string> 15 ok_f()16std::string ok_f() { 17 std::ostringstream ok; ok 18 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS 19 << "f::pre" << std::endl 20 #endif 21 #ifndef BOOST_CONTRACT_NO_OLDS 22 << "f::old" << std::endl 23 #endif 24 << "f::body" << std::endl 25 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 26 << "f::post" << std::endl // This can fail. 27 #endif 28 ; 29 return ok.str(); 30 } 31 32 struct err {}; // Global decl so visible in MSVC10 lambdas. 33 main()34int main() { 35 std::ostringstream ok; 36 37 f_post = true; 38 out.str(""); 39 f(); 40 ok.str(""); ok // Test nothing failed. 41 << ok_f() 42 ; 43 BOOST_TEST(out.eq(ok.str())); 44 45 boost::contract::set_postcondition_failure( 46 [] (boost::contract::from) { throw err(); }); 47 48 f_post = false; 49 out.str(""); 50 try { 51 f(); 52 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 53 BOOST_TEST(false); 54 } catch(err const&) { 55 #endif 56 ok.str(""); ok 57 << ok_f() // Test f::post failed. 58 ; 59 BOOST_TEST(out.eq(ok.str())); 60 } catch(...) { BOOST_TEST(false); } 61 62 return boost::report_errors(); 63 } 64 65