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 #if !defined(BOOST_CONTRACT_NO_CONSTRUCTORS) || \ 12 !defined(BOOST_CONTRACT_NO_PRECONDITIONS) 13 #include <boost/contract/constructor.hpp> 14 #endif 15 #ifndef BOOST_CONTRACT_NO_CONSTRUCTORS 16 #include <boost/contract/check.hpp> 17 #include <boost/contract/old.hpp> 18 #endif 19 #include <boost/detail/lightweight_test.hpp> 20 #include <sstream> 21 22 boost::contract::test::detail::oteststream out; 23 24 struct b 25 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS 26 : private boost::contract::constructor_precondition<b> 27 #endif 28 { 29 #ifndef BOOST_CONTRACT_NO_INVARIANTS static_invariantb30 static void static_invariant() { out << "b::static_inv" << std::endl; } invariantb31 void invariant() const { out << "b::inv" << std::endl; } 32 #endif 33 bb34 explicit b(int x) 35 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS 36 : boost::contract::constructor_precondition<b>([] { 37 out << "b::ctor::pre" << std::endl; 38 }) 39 #endif 40 { 41 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 42 boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(x); 43 #endif 44 #ifndef BOOST_CONTRACT_NO_CONSTRUCTORS 45 boost::contract::check c = boost::contract::constructor(this) 46 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS __anon751b0b930202null47 .old([] { out << "b::f::old" << std::endl; }) __anon751b0b930302null48 .postcondition([] { out << "b::ctor::post" << std::endl; }) 49 #endif 50 ; 51 #endif 52 out << "b::ctor::body" << std::endl; 53 } 54 }; 55 56 struct a : 57 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS 58 private boost::contract::constructor_precondition<a>, 59 #endif 60 public b 61 { 62 #ifndef BOOST_CONTRACT_NO_INVARIANTS static_invarianta63 static void static_invariant() { out << "a::static_inv" << std::endl; } invarianta64 void invariant() const { out << "a::inv" << std::endl; } 65 #endif 66 aa67 explicit a(int x) : 68 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS 69 boost::contract::constructor_precondition<a>([] { 70 out << "a::ctor::pre" << std::endl; 71 }), 72 #endif 73 b(x) 74 { 75 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 76 boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(x); 77 #endif 78 #ifndef BOOST_CONTRACT_NO_CONSTRUCTORS 79 boost::contract::check c = boost::contract::constructor(this) 80 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS __anon751b0b930502null81 .old([] { out << "a::f::old" << std::endl; }) __anon751b0b930602null82 .postcondition([] { out << "a::ctor::post" << std::endl; }) 83 #endif 84 ; 85 #endif 86 out << "a::ctor::body" << std::endl; 87 } 88 }; 89 main()90int main() { 91 std::ostringstream ok; 92 out.str(""); 93 a aa(123); 94 ok.str(""); ok 95 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS 96 << "a::ctor::pre" << std::endl 97 << "b::ctor::pre" << std::endl 98 #endif 99 100 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS 101 << "b::static_inv" << std::endl 102 #endif 103 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 104 << "b::f::old" << std::endl 105 #endif 106 << "b::ctor::body" << std::endl 107 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS 108 << "b::static_inv" << std::endl 109 << "b::inv" << std::endl 110 #endif 111 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 112 << "b::ctor::post" << std::endl 113 #endif 114 115 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS 116 << "a::static_inv" << std::endl 117 #endif 118 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 119 << "a::f::old" << std::endl 120 #endif 121 << "a::ctor::body" << std::endl 122 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS 123 << "a::static_inv" << std::endl 124 << "a::inv" << std::endl 125 #endif 126 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 127 << "a::ctor::post" << std::endl 128 #endif 129 ; 130 BOOST_TEST(out.eq(ok.str())); 131 return boost::report_errors(); 132 } 133 134