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 invariant compilation on/off (using macro interface).
8
9 #include "../detail/oteststream.hpp"
10 #include "../detail/unprotected_commas.hpp"
11 #include <boost/contract_macro.hpp>
12 #include <boost/detail/lightweight_test.hpp>
13 #include <sstream>
14
15 boost::contract::test::detail::oteststream out;
16
17 class a {
18 public:
19 BOOST_CONTRACT_STATIC_INVARIANT({
20 boost::contract::test::detail::unprotected_commas<void, void, void>::
21 call();
22 out << "a::static_inv" << std::endl;
23 })
24
25 BOOST_CONTRACT_INVARIANT_VOLATILE({
26 boost::contract::test::detail::unprotected_commas<void, void, void>::
27 call();
28 out << "a::cv_inv" << std::endl;
29 })
30
31 BOOST_CONTRACT_INVARIANT({
32 boost::contract::test::detail::unprotected_commas<void, void, void>::
33 call();
34 out << "a::const_inv" << std::endl;
35 })
36
a()37 a() { // Test check both cv and const invariant (at exit if no throw).
38 BOOST_CONTRACT_CONSTRUCTOR(boost::contract::test::detail::
39 unprotected_commas<void, void, void>::same(this));
40 out << "a::ctor::body" << std::endl;
41 }
42
~a()43 ~a() { // Test check both cv and const invariant (at entry).
44 BOOST_CONTRACT_DESTRUCTOR(boost::contract::test::detail::
45 unprotected_commas<void, void, void>::same(this));
46 out << "a::dtor::body" << std::endl;
47 }
48
m()49 void m() { // Test check const invariant (at entry and exit).
50 BOOST_CONTRACT_PUBLIC_FUNCTION(boost::contract::test::detail::
51 unprotected_commas<void, void, void>::same(this));
52 out << "a::m::body" << std::endl;
53 }
54
c() const55 void c() const { // Test check const invariant (at entry and exit).
56 BOOST_CONTRACT_PUBLIC_FUNCTION(boost::contract::test::detail::
57 unprotected_commas<void, void, void>::same(this));
58 out << "a::c::body" << std::endl;
59 }
60
v()61 void v() volatile { // Test check cv invariant (at entry and exit).
62 BOOST_CONTRACT_PUBLIC_FUNCTION(boost::contract::test::detail::
63 unprotected_commas<void, void, void>::same(this));
64 out << "a::v::body" << std::endl;
65 }
66
cv() const67 void cv() const volatile { // Test check cv invariant (at entry and exit).
68 BOOST_CONTRACT_PUBLIC_FUNCTION(boost::contract::test::detail::
69 unprotected_commas<void, void, void>::same(this));
70 out << "a::cv::body" << std::endl;
71 }
72 };
73
main()74 int main() {
75 std::ostringstream ok;
76
77 {
78 out.str("");
79 a aa;
80 ok.str(""); ok
81 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
82 << "a::static_inv" << std::endl
83 #endif
84 << "a::ctor::body" << std::endl
85 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
86 << "a::static_inv" << std::endl
87 << "a::cv_inv" << std::endl
88 << "a::const_inv" << std::endl
89 #endif
90 ;
91 BOOST_TEST(out.eq(ok.str()));
92
93 out.str("");
94 aa.m();
95 ok.str(""); ok
96 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
97 << "a::static_inv" << std::endl
98 << "a::const_inv" << std::endl
99 #endif
100 << "a::m::body" << std::endl
101 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
102 << "a::static_inv" << std::endl
103 << "a::const_inv" << std::endl
104 #endif
105 ;
106 BOOST_TEST(out.eq(ok.str()));
107
108 out.str("");
109 aa.c();
110 ok.str(""); ok
111 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
112 << "a::static_inv" << std::endl
113 << "a::const_inv" << std::endl
114 #endif
115 << "a::c::body" << std::endl
116 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
117 << "a::static_inv" << std::endl
118 << "a::const_inv" << std::endl
119 #endif
120 ;
121 BOOST_TEST(out.eq(ok.str()));
122
123 out.str("");
124 aa.v();
125 ok.str(""); ok
126 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
127 << "a::static_inv" << std::endl
128 << "a::cv_inv" << std::endl
129 #endif
130 << "a::v::body" << std::endl
131 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
132 << "a::static_inv" << std::endl
133 << "a::cv_inv" << std::endl
134 #endif
135 ;
136 BOOST_TEST(out.eq(ok.str()));
137
138 out.str("");
139 aa.cv();
140 ok.str(""); ok
141 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
142 << "a::static_inv" << std::endl
143 << "a::cv_inv" << std::endl
144 #endif
145 << "a::cv::body" << std::endl
146 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
147 << "a::static_inv" << std::endl
148 << "a::cv_inv" << std::endl
149 #endif
150 ;
151 BOOST_TEST(out.eq(ok.str()));
152
153 out.str("");
154 } // Call dtor.
155 ok.str(""); ok
156 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
157 << "a::static_inv" << std::endl
158 << "a::cv_inv" << std::endl
159 << "a::const_inv" << std::endl
160 #endif
161 << "a::dtor::body" << std::endl
162 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
163 << "a::static_inv" << std::endl
164 #endif
165 ;
166 BOOST_TEST(out.eq(ok.str()));
167
168 return boost::report_errors();
169 }
170
171