Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / contract / test / public_function / ifdef_macro.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 contract compilation on/off (using macro interface).
8
9 #include "../detail/oteststream.hpp"
10 #include "../detail/unprotected_commas.hpp"
11 #include <boost/contract/core/config.hpp>
12 #include <boost/contract/core/virtual.hpp>
13 #include <boost/contract_macro.hpp>
14 #include <boost/detail/lightweight_test.hpp>
15 #include <sstream>
16
17 boost::contract::test::detail::oteststream out;
18
19 struct b {
20     BOOST_CONTRACT_STATIC_INVARIANT({
21         boost::contract::test::detail::unprotected_commas<void, void, void>::
22                 call();
23         out << "b::static_inv" << std::endl;
24     })
25     
26     BOOST_CONTRACT_INVARIANT({
27         boost::contract::test::detail::unprotected_commas<void, void, void>::
28                 call();
29         out << "b::inv" << std::endl;
30     })
31
32     virtual void f(int x, boost::contract::virtual_* v = 0) = 0;
33 };
34
35 void b::f(int x, boost::contract::virtual_* v) {
36     BOOST_CONTRACT_OLD_PTR(
37         boost::contract::test::detail::unprotected_commas<int, void, void>::
38                 type1
39     )(
40         (boost::contract::test::detail::unprotected_commas<void, void, void>::
41                 same(v)),
42         old_x,
43         (boost::contract::test::detail::unprotected_commas<void, void, void>::
44                 same(x))
45     );
46     BOOST_CONTRACT_PUBLIC_FUNCTION(
47         boost::contract::test::detail::unprotected_commas<void, void, void>::
48                 same(v),
49         boost::contract::test::detail::unprotected_commas<void, void, void>::
50                 same(this)
51     )
52         BOOST_CONTRACT_PRECONDITION([] {
53             boost::contract::test::detail::unprotected_commas<
54                     void, void, void>::call();
55             out << "b::f::pre" << std::endl;
56         })
57         BOOST_CONTRACT_OLD([] {
58             boost::contract::test::detail::unprotected_commas<
59                     void, void, void>::call();
60             out << "b::f::old" << std::endl;
61         })
62         BOOST_CONTRACT_POSTCONDITION([] {
63             boost::contract::test::detail::unprotected_commas<
64                     void, void, void>::call();
65             out << "b::f::post" << std::endl;
66         })
67     ;
68     out << "b::f::body" << std::endl;
69 }
70
71 struct a
72     #define BASES public boost::contract::test::detail::unprotected_commas< \
73             b, void, void>::type1
74     : BASES
75 {
76     typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
77     BOOST_CONTRACT_OVERRIDE(f)
78
79     BOOST_CONTRACT_STATIC_INVARIANT({
80         boost::contract::test::detail::unprotected_commas<void, void, void>::
81                 call();
82         out << "a::static_inv" << std::endl;
83     })
84     
85     BOOST_CONTRACT_INVARIANT({
86         boost::contract::test::detail::unprotected_commas<void, void, void>::
87                 call();
88         out << "a::inv" << std::endl;
89     })
90
91     virtual void f(int x, boost::contract::virtual_* v = 0) {
92         BOOST_CONTRACT_OLD_PTR(
93             boost::contract::test::detail::unprotected_commas<int, void, void>::
94                     type1
95         )(
96             (boost::contract::test::detail::unprotected_commas<void, void,
97                     void>::same(v)),
98             old_x,
99             (boost::contract::test::detail::unprotected_commas<void, void,
100                     void>::same(x))
101         );
102         BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE(
103             boost::contract::test::detail::unprotected_commas<override_f, void,
104                     void>::type1
105         )(
106             boost::contract::test::detail::unprotected_commas<void, void, void>
107                     ::same(v),
108             &a::f,
109             boost::contract::test::detail::unprotected_commas<void, void, void>
110                     ::same(this),
111             boost::contract::test::detail::unprotected_commas<void, void, void>
112                     ::same(x)
113         )
114             BOOST_CONTRACT_PRECONDITION([] {
115                 boost::contract::test::detail::unprotected_commas<
116                         void, void, void>::call();
117                 out << "a::f::pre" << std::endl;
118             })
119             BOOST_CONTRACT_OLD([] {
120                 boost::contract::test::detail::unprotected_commas<
121                         void, void, void>::call();
122                 out << "a::f::old" << std::endl;
123             })
124             BOOST_CONTRACT_POSTCONDITION([] {
125                 boost::contract::test::detail::unprotected_commas<
126                         void, void, void>::call();
127                 out << "a::f::post" << std::endl;
128             })
129         ;
130         out << "a::f::body" << std::endl;
131     }
132 };
133
134 int main() {
135     std::ostringstream ok;
136     
137     a aa;
138     out.str("");
139     aa.f(123);
140     ok.str(""); ok
141         #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
142             << "b::static_inv" << std::endl
143             << "b::inv" << std::endl
144             << "a::static_inv" << std::endl
145             << "a::inv" << std::endl
146         #endif
147         #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
148             << "b::f::pre" << std::endl
149         #endif
150         #ifndef BOOST_CONTRACT_NO_OLDS
151             << "b::f::old" << std::endl
152             << "a::f::old" << std::endl
153         #endif
154         << "a::f::body" << std::endl
155         #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
156             << "b::static_inv" << std::endl
157             << "b::inv" << std::endl
158             << "a::static_inv" << std::endl
159             << "a::inv" << std::endl
160         #endif
161         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
162             << "b::f::old" << std::endl // Called by post (so under NO_POST).
163             << "b::f::post" << std::endl
164             << "a::f::post" << std::endl
165         #endif
166     ;
167     BOOST_TEST(out.eq(ok.str()));
168
169     return boost::report_errors();
170 }
171