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 all derived and base classes with postconditions. 8 9 #undef BOOST_CONTRACT_TEST_NO_A_POST 10 #undef BOOST_CONTRACT_TEST_NO_B_POST 11 #undef BOOST_CONTRACT_TEST_NO_C_POST 12 #include "decl.hpp" 13 14 #include <boost/detail/lightweight_test.hpp> 15 #include <sstream> 16 #include <string> 17 ok_c()18std::string ok_c() { 19 std::ostringstream ok; ok 20 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS 21 << "a::ctor::pre" << std::endl 22 << "b::ctor::pre" << std::endl 23 << "c::ctor::pre" << std::endl 24 #endif 25 26 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS 27 << "c::static_inv" << std::endl 28 #endif 29 #ifndef BOOST_CONTRACT_NO_OLDS 30 << "c::ctor::old" << std::endl 31 #endif 32 << "c::ctor::body" << std::endl 33 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS 34 << "c::static_inv" << std::endl 35 << "c::inv" << std::endl 36 #endif 37 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 38 << "c::ctor::post" << std::endl 39 #endif 40 ; 41 return ok.str(); 42 } 43 ok_b()44std::string ok_b() { 45 std::ostringstream ok; ok 46 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS 47 << "b::static_inv" << std::endl 48 #endif 49 #ifndef BOOST_CONTRACT_NO_OLDS 50 << "b::ctor::old" << std::endl 51 #endif 52 << "b::ctor::body" << std::endl 53 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS 54 << "b::static_inv" << std::endl 55 << "b::inv" << std::endl 56 #endif 57 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 58 << "b::ctor::post" << std::endl 59 #endif 60 ; 61 return ok.str(); 62 } 63 ok_a()64std::string ok_a() { 65 std::ostringstream ok; ok 66 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS 67 << "a::static_inv" << std::endl 68 #endif 69 #ifndef BOOST_CONTRACT_NO_OLDS 70 << "a::ctor::old" << std::endl 71 #endif 72 << "a::ctor::body" << std::endl 73 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS 74 << "a::static_inv" << std::endl 75 << "a::inv" << std::endl 76 #endif 77 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 78 << "a::ctor::post" << std::endl 79 #endif 80 ; 81 return ok.str(); 82 } 83 84 struct err {}; // Global decl so visible in MSVC10 lambdas. 85 main()86int main() { 87 std::ostringstream ok; 88 89 a_post = true; 90 b_post = true; 91 c_post = true; 92 out.str(""); 93 { 94 a aa; 95 ok.str(""); ok // Test nothing failed. 96 << ok_c() 97 << ok_b() 98 << ok_a() 99 ; 100 BOOST_TEST(out.eq(ok.str())); 101 } 102 103 boost::contract::set_postcondition_failure( 104 [] (boost::contract::from) { throw err(); }); 105 106 a_post = false; 107 b_post = true; 108 c_post = true; 109 out.str(""); 110 try { 111 a aa; 112 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 113 BOOST_TEST(false); 114 } catch(err const&) { 115 #endif 116 ok.str(""); ok 117 << ok_c() 118 << ok_b() 119 << ok_a() 120 ; 121 BOOST_TEST(out.eq(ok.str())); 122 } catch(...) { BOOST_TEST(false); } 123 124 a_post = true; 125 b_post = false; 126 c_post = true; 127 out.str(""); 128 try { 129 a aa; 130 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 131 BOOST_TEST(false); 132 } catch(err const&) { 133 #endif 134 ok.str(""); ok 135 << ok_c() 136 << ok_b() // Test b::ctor::post failed. 137 #ifdef BOOST_CONTRACT_NO_POSTCONDITIONS 138 << ok_a() 139 #endif 140 ; 141 BOOST_TEST(out.eq(ok.str())); 142 } catch(...) { BOOST_TEST(false); } 143 144 a_post = true; 145 b_post = true; 146 c_post = false; 147 out.str(""); 148 try { 149 a aa; 150 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 151 BOOST_TEST(false); 152 } catch(err const&) { 153 #endif 154 ok.str(""); ok 155 << ok_c() // Test c::ctor::post failed. 156 #ifdef BOOST_CONTRACT_NO_POSTCONDITIONS 157 << ok_b() 158 << ok_a() 159 #endif 160 ; 161 BOOST_TEST(out.eq(ok.str())); 162 } catch(...) { BOOST_TEST(false); } 163 164 a_post = false; 165 b_post = false; 166 c_post = false; 167 out.str(""); 168 try { 169 a aa; 170 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 171 BOOST_TEST(false); 172 } catch(err const&) { 173 #endif 174 ok.str(""); ok 175 << ok_c() // Test c::ctor::post failed (as all did) 176 #ifdef BOOST_CONTRACT_NO_POSTCONDITIONS 177 << ok_b() 178 << ok_a() 179 #endif 180 ; 181 BOOST_TEST(out.eq(ok.str())); 182 } catch(...) { BOOST_TEST(false); } 183 184 return boost::report_errors(); 185 } 186 187