• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 #ifndef BOOST_CONTRACT_TEST_PUBLIC_FUNCTION_DECL_HPP_
3 #define BOOST_CONTRACT_TEST_PUBLIC_FUNCTION_DECL_HPP_
4 
5 // Copyright (C) 2008-2018 Lorenzo Caminiti
6 // Distributed under the Boost Software License, Version 1.0 (see accompanying
7 // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
8 // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
9 
10 // Test with and without pre, post, and inv declarations.
11 
12 #include "../detail/oteststream.hpp"
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/assert.hpp>
18 
19 boost::contract::test::detail::oteststream out;
20 
21 bool c_pre = true, c_post = true;
22 bool c_entering_static_inv = true, c_entry_static_inv = true,
23         c_exit_static_inv = true;
24 bool c_entering_inv = true, c_entry_inv = true, c_exit_inv = true;
25 struct c {
26     #ifndef BOOST_CONTRACT_TEST_NO_C_STATIC_INV
static_invariantc27         static void static_invariant() {
28             out << "c::static_inv" << std::endl;
29             if(c_entering_static_inv) BOOST_CONTRACT_ASSERT(c_entry_static_inv);
30             else BOOST_CONTRACT_ASSERT(c_exit_static_inv);
31             c_entering_static_inv = false;
32         }
33     #endif
34     #ifndef BOOST_CONTRACT_TEST_NO_C_INV
invariantc35         void invariant() const {
36             out << "c::inv" << std::endl;
37             if(c_entering_inv) BOOST_CONTRACT_ASSERT(c_entry_inv);
38             else BOOST_CONTRACT_ASSERT(c_exit_inv);
39             c_entering_inv = false;
40         }
41     #endif
42 
fc43     virtual void f(boost::contract::virtual_* v = 0) {
44         boost::contract::check c = boost::contract::public_function(v, this)
45             #ifndef BOOST_CONTRACT_TEST_NO_C_PRE
46                 .precondition([] {
47                     out << "c::f::pre" << std::endl;
48                     BOOST_CONTRACT_ASSERT(c_pre);
49                 })
50             #endif
51             .old([] { out << "c::f::old" << std::endl; })
52             #ifndef BOOST_CONTRACT_TEST_NO_C_POST
53                 .postcondition([] {
54                     out << "c::f::post" << std::endl;
55                     BOOST_CONTRACT_ASSERT(c_post);
56                 })
57             #endif
58         ;
59         out << "c::f::body" << std::endl;
60     }
61 };
62 
63 bool b_pre = true, b_post = true;
64 bool b_entering_static_inv = true, b_entry_static_inv = true,
65         b_exit_static_inv = true;
66 bool b_entering_inv = true, b_entry_inv = true, b_exit_inv = true;
67 struct b
68     #define BASES public c
69     : BASES
70 {
71     typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
72     #undef BASES
73 
74     #ifndef BOOST_CONTRACT_TEST_NO_B_STATIC_INV
static_invariantb75         static void static_invariant() {
76             out << "b::static_inv" << std::endl;
77             if(b_entering_static_inv) BOOST_CONTRACT_ASSERT(b_entry_static_inv);
78             else BOOST_CONTRACT_ASSERT(b_exit_static_inv);
79             b_entering_static_inv = false;
80         }
81     #endif
82     #ifndef BOOST_CONTRACT_TEST_NO_B_INV
invariantb83         void invariant() const {
84             out << "b::inv" << std::endl;
85             if(b_entering_inv) BOOST_CONTRACT_ASSERT(b_entry_inv);
86             else BOOST_CONTRACT_ASSERT(b_exit_inv);
87             b_entering_inv = false;
88         }
89     #endif
90 
fb91     virtual void f(boost::contract::virtual_* v = 0) {
92         boost::contract::check c = boost::contract::public_function(v, this)
93             #ifndef BOOST_CONTRACT_TEST_NO_B_PRE
94                 .precondition([] {
95                     out << "b::f::pre" << std::endl;
96                     BOOST_CONTRACT_ASSERT(b_pre);
97                 })
98             #endif
99             .old([] { out << "b::f::old" << std::endl; })
100             #ifndef BOOST_CONTRACT_TEST_NO_B_POST
101                 .postcondition([] {
102                     out << "b::f::post" << std::endl;
103                     BOOST_CONTRACT_ASSERT(b_post);
104                 })
105             #endif
106         ;
107         out << "a::f::body" << std::endl;
108     }
109 };
110 
111 bool a_pre = true, a_post = true;
112 bool a_entering_static_inv = true, a_entry_static_inv = true,
113         a_exit_static_inv = true;
114 bool a_entering_inv = true, a_entry_inv = true, a_exit_inv = true;
115 struct a
116     #define BASES public b
117     : BASES
118 {
119     typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
120     #undef BASES
121 
122     #ifndef BOOST_CONTRACT_TEST_NO_A_STATIC_INV
static_invarianta123         static void static_invariant() {
124             out << "a::static_inv" << std::endl;
125             if(a_entering_static_inv) BOOST_CONTRACT_ASSERT(a_entry_static_inv);
126             else BOOST_CONTRACT_ASSERT(a_exit_static_inv);
127             a_entering_static_inv = false;
128         }
129     #endif
130     #ifndef BOOST_CONTRACT_TEST_NO_A_INV
invarianta131         void invariant() const {
132             out << "a::inv" << std::endl;
133             if(a_entering_inv) BOOST_CONTRACT_ASSERT(a_entry_inv);
134             else BOOST_CONTRACT_ASSERT(a_exit_inv);
135             a_entering_inv = false;
136         }
137     #endif
138 
fa139     virtual void f(boost::contract::virtual_* v = 0) /* override */ {
140         boost::contract::check c = boost::contract::public_function<override_f>(
141                 v, &a::f, this)
142             #ifndef BOOST_CONTRACT_TEST_NO_A_PRE
143                 .precondition([] {
144                     out << "a::f::pre" << std::endl;
145                     BOOST_CONTRACT_ASSERT(a_pre);
146                 })
147             #endif
148             .old([] { out << "a::f::old" << std::endl; })
149             #ifndef BOOST_CONTRACT_TEST_NO_A_POST
150                 .postcondition([] {
151                     out << "a::f::post" << std::endl;
152                     BOOST_CONTRACT_ASSERT(a_post);
153                 })
154             #endif
155         ;
156         out << "a::f::body" << std::endl;
157     }
158     BOOST_CONTRACT_OVERRIDE(f)
159 };
160 
161 #endif // #include guard
162 
163