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