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 overriding function never overrides protected function contract.
9 #include "../detail/oteststream.hpp"
10 #include <boost/contract/function.hpp>
11 #include <boost/contract/public_function.hpp>
12 #include <boost/contract/base_types.hpp>
13 #include <boost/contract/check.hpp>
14 #include <boost/detail/lightweight_test.hpp>
17 boost::contract::test::detail::oteststream out;
20 static void static_invariant() { out << "b::static_inv" << std::endl; }
21 void invariant() const { out << "b::inv" << std::endl; }
24 // NOTE: This is the correct way of programming contracts for overridden
25 // protected and overriding public functions: Both must use virtual_
26 // (otherwise C++ won't override because mismatching parameters), but
27 // overridden protected does not use public_function.
28 virtual void f(boost::contract::virtual_* /* v */ = 0) {
29 boost::contract::check c = boost::contract::function()
30 .precondition([] { out << "b::f::pre" << std::endl; })
31 .old([] { out << "b::f::old" << std::endl; })
32 .postcondition([] { out << "b::f::post" << std::endl; })
34 out << "b::f::body" << std::endl;
38 #define BASES public b
41 typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
44 static void static_invariant() { out << "a::static_inv" << std::endl; }
45 void invariant() const { out << "a::inv" << std::endl; }
47 void f(boost::contract::virtual_* v = 0) /* not override */ {
48 // C++ func a::f overrides b::f, but contracts don't (so no override_f).
49 boost::contract::check c = boost::contract::public_function(v, this)
50 .precondition([] { out << "a::f::pre" << std::endl; })
51 .old([] { out << "a::f::old" << std::endl; })
52 .postcondition([] { out << "a::f::post" << std::endl; })
54 out << "a::f::body" << std::endl;
59 std::ostringstream ok;
65 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
66 << "a::static_inv" << std::endl
67 << "a::inv" << std::endl
69 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
70 << "a::f::pre" << std::endl
72 #ifndef BOOST_CONTRACT_NO_OLDS
73 << "a::f::old" << std::endl
75 << "a::f::body" << std::endl
76 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
77 << "a::static_inv" << std::endl
78 << "a::inv" << std::endl
80 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
81 << "a::f::post" << std::endl
84 BOOST_TEST(out.eq(ok.str()));
86 return boost::report_errors();