| 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_DESTRUCTORS |
| 12 | #include <boost/contract/destructor.hpp> |
| 13 | #include <boost/contract/check.hpp> |
| 14 | #include <boost/contract/old.hpp> |
| 15 | #endif |
| 16 | #include <boost/detail/lightweight_test.hpp> |
| 17 | #include <sstream> |
| 18 | |
| 19 | boost::contract::test::detail::oteststream out; |
| 20 | |
| 21 | struct b { |
| 22 | #ifndef BOOST_CONTRACT_NO_INVARIANTS |
| 23 | static void static_invariant() { out << "b::static_inv"<< std::endl; } |
| 24 | void invariant() const { out << "b::inv"<< std::endl; } |
| 25 | #endif |
| 26 | |
| 27 | virtual ~b() { |
| 28 | #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS |
| 29 | boost::contract::old_ptr<int> old_y = BOOST_CONTRACT_OLDOF(y); |
| 30 | #endif |
| 31 | #ifndef BOOST_CONTRACT_NO_DESTRUCTORS |
| 32 | boost::contract::check c = boost::contract::destructor(obj: this) |
| 33 | #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS |
| 34 | .old(f: [] { out << "b::dtor::old"<< std::endl; }) |
| 35 | .postcondition(f: [] { out << "b::dtor::post"<< std::endl; }) |
| 36 | #endif |
| 37 | ; |
| 38 | #endif |
| 39 | out << "b::dtor::body"<< std::endl; |
| 40 | } |
| 41 | |
| 42 | static int y; |
| 43 | }; |
| 44 | int b::y = 0; |
| 45 | |
| 46 | struct a : public b { |
| 47 | #ifndef BOOST_CONTRACT_NO_INVARIANTS |
| 48 | static void static_invariant() { out << "a::static_inv"<< std::endl; } |
| 49 | void invariant() const { out << "a::inv"<< std::endl; } |
| 50 | #endif |
| 51 | |
| 52 | virtual ~a() { |
| 53 | #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS |
| 54 | boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(x); |
| 55 | #endif |
| 56 | #ifndef BOOST_CONTRACT_NO_DESTRUCTORS |
| 57 | boost::contract::check c = boost::contract::destructor(obj: this) |
| 58 | #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS |
| 59 | .old(f: [] { out << "a::dtor::old"<< std::endl; }) |
| 60 | .postcondition(f: [] { out << "a::dtor::post"<< std::endl; }) |
| 61 | #endif |
| 62 | ; |
| 63 | #endif |
| 64 | out << "a::dtor::body"<< std::endl; |
| 65 | } |
| 66 | |
| 67 | static int x; |
| 68 | }; |
| 69 | int a::x = 0; |
| 70 | |
| 71 | int main() { |
| 72 | std::ostringstream ok; |
| 73 | { |
| 74 | a aa; |
| 75 | out.str(s: ""); |
| 76 | } |
| 77 | ok.str(s: ""); ok |
| 78 | #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS |
| 79 | << "a::static_inv"<< std::endl |
| 80 | << "a::inv"<< std::endl |
| 81 | #endif |
| 82 | #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS |
| 83 | << "a::dtor::old"<< std::endl |
| 84 | #endif |
| 85 | << "a::dtor::body"<< std::endl |
| 86 | #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS |
| 87 | << "a::static_inv"<< std::endl |
| 88 | #endif |
| 89 | #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS |
| 90 | << "a::dtor::post"<< std::endl |
| 91 | #endif |
| 92 | |
| 93 | #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS |
| 94 | << "b::static_inv"<< std::endl |
| 95 | << "b::inv"<< std::endl |
| 96 | #endif |
| 97 | #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS |
| 98 | << "b::dtor::old"<< std::endl |
| 99 | #endif |
| 100 | << "b::dtor::body"<< std::endl |
| 101 | #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS |
| 102 | << "b::static_inv"<< std::endl |
| 103 | #endif |
| 104 | #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS |
| 105 | << "b::dtor::post"<< std::endl |
| 106 | #endif |
| 107 | ; |
| 108 | BOOST_TEST(out.eq(ok.str())); |
| 109 | return boost::report_errors(); |
| 110 | } |
| 111 | |
| 112 |
