Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / contract / test / public_function / protected.cpp
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 overriding function never overrides protected function contract.
8
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>
15 #include <sstream>
16
17 boost::contract::test::detail::oteststream out;
18
19 struct b {
20     static void static_invariant() { out << "b::static_inv" << std::endl; }
21     void invariant() const { out << "b::inv" << std::endl; }
22
23 protected:
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; })
33         ;
34         out << "b::f::body" << std::endl;
35     }
36 };
37 struct a
38     #define BASES public b
39     : BASES
40 {
41     typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
42     #undef BASES
43
44     static void static_invariant() { out << "a::static_inv" << std::endl; }
45     void invariant() const { out << "a::inv" << std::endl; }
46
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; })
53         ;
54         out << "a::f::body" << std::endl;
55     }
56 };
57
58 int main() {
59     std::ostringstream ok;
60
61     a aa;
62     out.str("");
63     aa.f();
64     ok.str(""); ok
65         #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
66             << "a::static_inv" << std::endl
67             << "a::inv" << std::endl
68         #endif
69         #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
70             << "a::f::pre" << std::endl
71         #endif
72         #ifndef BOOST_CONTRACT_NO_OLDS
73             << "a::f::old" << std::endl
74         #endif
75         << "a::f::body" << std::endl
76         #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
77             << "a::static_inv" << std::endl
78             << "a::inv" << std::endl
79         #endif
80         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
81             << "a::f::post" << std::endl
82         #endif
83     ;
84     BOOST_TEST(out.eq(ok.str()));
85     
86     return boost::report_errors();
87 }
88