• 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/constructor.hpp> // Outside #if below for ctor pre.
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     private boost::contract::constructor_precondition<b> // OK, always in code.
21 {
22     BOOST_CONTRACT_STATIC_INVARIANT({
23         boost::contract::test::detail::unprotected_commas<void, void, void>::
24                 call();
25         out << "b::static_inv" << std::endl;
26     })
27 
28     BOOST_CONTRACT_INVARIANT({
29         boost::contract::test::detail::unprotected_commas<void, void, void>::
30                 call();
31         out << "b::inv" << std::endl;
32     })
33 
bb34     explicit b(int x) :
35         BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION(b)([] {
36             boost::contract::test::detail::unprotected_commas<void, void, void>
37                     ::call();
38             out << "b::ctor::pre" << std::endl;
39         })
40     {
41         BOOST_CONTRACT_OLD_PTR(
42             boost::contract::test::detail::unprotected_commas<int, void,
43                     void>::type1
44         )(
45             old_x,
46             (boost::contract::test::detail::unprotected_commas<void, void,
47                     void>::same(x))
48         );
49         BOOST_CONTRACT_CONSTRUCTOR(this)
__anona5565c230202null50             BOOST_CONTRACT_OLD([] {
51                 boost::contract::test::detail::unprotected_commas<
52                         void, void, void>::call();
53                 out << "b::f::old" << std::endl;
54             })
__anona5565c230302null55             BOOST_CONTRACT_POSTCONDITION([] {
56                 boost::contract::test::detail::unprotected_commas<
57                         void, void, void>::call();
58                 out << "b::ctor::post" << std::endl;
59             })
60         ;
61         out << "b::ctor::body" << std::endl;
62     }
63 };
64 
65 struct a:
66     private boost::contract::constructor_precondition<a>, // OK, always in code.
67     public b
68 {
69     BOOST_CONTRACT_STATIC_INVARIANT({
70         boost::contract::test::detail::unprotected_commas<void, void, void>::
71                 call();
72         out << "a::static_inv" << std::endl;
73     })
74 
75     BOOST_CONTRACT_INVARIANT({
76         boost::contract::test::detail::unprotected_commas<void, void, void>::
77                 call();
78         out << "a::inv" << std::endl;
79     })
80 
aa81     explicit a(int x) :
82         BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION(a)([] {
83             boost::contract::test::detail::unprotected_commas<void, void, void>
84                     ::call();
85             out << "a::ctor::pre" << std::endl; }
86         ),
87         b(x)
88     {
89         BOOST_CONTRACT_OLD_PTR(
90             boost::contract::test::detail::unprotected_commas<int, void,
91                     void>::type1
92         )(
93             old_x,
94             (boost::contract::test::detail::unprotected_commas<void, void,
95                     void>::same(x))
96         );
97         BOOST_CONTRACT_CONSTRUCTOR(boost::contract::test::detail::
98                 unprotected_commas<void, void, void>::same(this))
__anona5565c230502null99             BOOST_CONTRACT_OLD([] {
100                 boost::contract::test::detail::unprotected_commas<
101                         void, void, void>::call();
102                 out << "a::f::old" << std::endl;
103             })
__anona5565c230602null104             BOOST_CONTRACT_POSTCONDITION([] {
105                 boost::contract::test::detail::unprotected_commas<
106                         void, void, void>::call();
107                 out << "a::ctor::post" << std::endl;
108             })
109         ;
110         out << "a::ctor::body" << std::endl;
111     }
112 };
113 
main()114 int main() {
115     std::ostringstream ok;
116     out.str("");
117     a aa(123);
118     ok.str(""); ok
119         #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
120             << "a::ctor::pre" << std::endl
121             << "b::ctor::pre" << std::endl
122         #endif
123 
124         #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
125             << "b::static_inv" << std::endl
126         #endif
127         #ifndef BOOST_CONTRACT_NO_OLDS
128             << "b::f::old" << std::endl
129         #endif
130         << "b::ctor::body" << std::endl
131         #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
132             << "b::static_inv" << std::endl
133             << "b::inv" << std::endl
134         #endif
135         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
136             << "b::ctor::post" << std::endl
137         #endif
138 
139         #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
140             << "a::static_inv" << std::endl
141         #endif
142         #ifndef BOOST_CONTRACT_NO_OLDS
143             << "a::f::old" << std::endl
144         #endif
145         << "a::ctor::body" << std::endl
146         #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
147             << "a::static_inv" << std::endl
148             << "a::inv" << std::endl
149         #endif
150         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
151             << "a::ctor::post" << std::endl
152         #endif
153     ;
154     BOOST_TEST(out.eq(ok.str()));
155     return boost::report_errors();
156 }
157 
158