• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_macro.hpp>
13 #include <boost/detail/lightweight_test.hpp>
14 #include <sstream>
15 
16 boost::contract::test::detail::oteststream out;
17 
18 struct b {
19     BOOST_CONTRACT_STATIC_INVARIANT({
20         boost::contract::test::detail::unprotected_commas<void, void, void>::
21                 call();
22         out << "b::static_inv" << std::endl;
23     })
24 
25     BOOST_CONTRACT_INVARIANT({
26         boost::contract::test::detail::unprotected_commas<void, void, void>::
27                 call();
28         out << "b::inv" << std::endl;
29     })
30 
~bb31     virtual ~b() {
32         BOOST_CONTRACT_OLD_PTR(
33             boost::contract::test::detail::unprotected_commas<int, void,
34                     void>::type1
35         )(
36             old_y,
37             (boost::contract::test::detail::unprotected_commas<void, void,
38                     void>::same(y))
39         );
40         BOOST_CONTRACT_DESTRUCTOR(boost::contract::test::detail::
41                 unprotected_commas<void, void, void>::same(this))
42             BOOST_CONTRACT_OLD([] {
43                 boost::contract::test::detail::unprotected_commas<
44                         void, void, void>::call();
45                 out << "b::dtor::old" << std::endl;
46             })
47             BOOST_CONTRACT_POSTCONDITION([] {
48                 boost::contract::test::detail::unprotected_commas<
49                         void, void, void>::call();
50                 out << "b::dtor::post" << std::endl;
51             })
52         ;
53         out << "b::dtor::body" << std::endl;
54     }
55 
56     static int y;
57 };
58 int b::y = 0;
59 
60 struct a : public b {
61     BOOST_CONTRACT_STATIC_INVARIANT({
62         boost::contract::test::detail::unprotected_commas<void, void, void>::
63                 call();
64         out << "a::static_inv" << std::endl;
65     })
66 
67     BOOST_CONTRACT_INVARIANT({
68         boost::contract::test::detail::unprotected_commas<void, void, void>::
69                 call();
70         out << "a::inv" << std::endl;
71     })
72 
~aa73     virtual ~a() {
74         BOOST_CONTRACT_OLD_PTR(
75             boost::contract::test::detail::unprotected_commas<int, void, void>::
76                     type1
77         )(
78             old_x,
79             (boost::contract::test::detail::unprotected_commas<void, void,
80                     void>::same(x))
81         );
82         BOOST_CONTRACT_DESTRUCTOR(boost::contract::test::detail::
83                 unprotected_commas<void, void, void>::same(this))
84             BOOST_CONTRACT_OLD([] {
85                 boost::contract::test::detail::unprotected_commas<
86                         void, void, void>::call();
87                 out << "a::dtor::old" << std::endl;
88             })
89             BOOST_CONTRACT_POSTCONDITION([] {
90                 boost::contract::test::detail::unprotected_commas<
91                         void, void, void>::call();
92                 out << "a::dtor::post" << std::endl;
93             })
94         ;
95         out << "a::dtor::body" << std::endl;
96     }
97 
98     static int x;
99 };
100 int a::x = 0;
101 
main()102 int main() {
103     std::ostringstream ok;
104     {
105         a aa;
106         out.str("");
107     }
108     ok.str(""); ok
109         #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
110             << "a::static_inv" << std::endl
111             << "a::inv" << std::endl
112         #endif
113         #ifndef BOOST_CONTRACT_NO_OLDS
114             << "a::dtor::old" << std::endl
115         #endif
116         << "a::dtor::body" << std::endl
117         #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
118             << "a::static_inv" << std::endl
119         #endif
120         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
121             << "a::dtor::post" << std::endl
122         #endif
123 
124         #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
125             << "b::static_inv" << std::endl
126             << "b::inv" << std::endl
127         #endif
128         #ifndef BOOST_CONTRACT_NO_OLDS
129             << "b::dtor::old" << std::endl
130         #endif
131         << "b::dtor::body" << std::endl
132         #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
133             << "b::static_inv" << std::endl
134         #endif
135         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
136             << "b::dtor::post" << std::endl
137         #endif
138     ;
139     BOOST_TEST(out.eq(ok.str()));
140     return boost::report_errors();
141 }
142 
143