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
7 // Test contract compilation on/off.
9 #include "../detail/oteststream.hpp"
10 #include <boost/contract/core/config.hpp>
11 #include <boost/contract/core/virtual.hpp>
12 #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
13 #include <boost/contract/public_function.hpp>
14 #include <boost/contract/base_types.hpp>
15 #include <boost/contract/override.hpp>
16 #include <boost/contract/check.hpp>
17 #include <boost/contract/old.hpp>
19 #include <boost/detail/lightweight_test.hpp>
22 boost::contract::test::detail::oteststream out;
25 #ifndef BOOST_CONTRACT_NO_INVARIANTS
26 static void static_invariant() { out << "b::static_inv" << std::endl; }
27 void invariant() const { out << "b::inv" << std::endl; }
30 virtual void f(int x, boost::contract::virtual_* v = 0) = 0;
33 void b::f(int x, boost::contract::virtual_* v) {
34 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
35 boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(v, x);
37 #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
38 boost::contract::check c = boost::contract::public_function(v, this)
39 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
40 .precondition([] { out << "b::f::pre" << std::endl; })
42 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
43 .old([] { out << "b::f::old" << std::endl; })
44 .postcondition([] { out << "b::f::post" << std::endl; })
48 out << "b::f::body" << std::endl;
52 #define BASES public b
55 #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
56 typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
57 BOOST_CONTRACT_OVERRIDE(f)
60 #ifndef BOOST_CONTRACT_NO_INVARIANTS
61 static void static_invariant() { out << "a::static_inv" << std::endl; }
62 void invariant() const { out << "a::inv" << std::endl; }
65 virtual void f(int x, boost::contract::virtual_* v = 0) {
66 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
67 boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(v, x);
69 #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
70 boost::contract::check c = boost::contract::public_function<
71 override_f>(v, &a::f, this, x)
72 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
73 .precondition([] { out << "a::f::pre" << std::endl; })
75 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
76 .old([] { out << "a::f::old" << std::endl; })
77 .postcondition([] { out << "a::f::post" << std::endl; })
81 out << "a::f::body" << std::endl;
86 std::ostringstream ok;
92 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
93 << "b::static_inv" << std::endl
94 << "b::inv" << std::endl
95 << "a::static_inv" << std::endl
96 << "a::inv" << std::endl
98 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
99 << "b::f::pre" << std::endl
101 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
102 << "b::f::old" << std::endl
103 << "a::f::old" << std::endl
105 << "a::f::body" << std::endl
106 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
107 << "b::static_inv" << std::endl
108 << "b::inv" << std::endl
109 << "a::static_inv" << std::endl
110 << "a::inv" << std::endl
112 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
113 << "b::f::old" << std::endl
114 << "b::f::post" << std::endl
115 << "a::f::post" << std::endl
118 BOOST_TEST(out.eq(ok.str()));
120 return boost::report_errors();