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
7 // Test only middle base class with exit static invariants.
9 #define BOOST_CONTRACT_TEST_NO_A_STATIC_INV
10 #undef BOOST_CONTRACT_TEST_NO_B_STATIC_INV
11 #define BOOST_CONTRACT_TEST_NO_C_STATIC_INV
14 #include <boost/preprocessor/control/iif.hpp>
15 #include <boost/detail/lightweight_test.hpp>
20 std::ostringstream ok
; ok
21 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
22 << "a::inv" << std::endl
24 #ifndef BOOST_CONTRACT_NO_OLDS
25 << "a::dtor::old" << std::endl
27 << "a::dtor::body" << std::endl
28 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
29 << "a::dtor::post" << std::endl
35 std::string
ok_b(bool failed
= false) {
36 std::ostringstream ok
; ok
37 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
38 << "b::static_inv" << std::endl
39 << "b::inv" << std::endl
41 #ifndef BOOST_CONTRACT_NO_OLDS
42 << "b::dtor::old" << std::endl
44 << "b::dtor::body" << std::endl
45 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
46 << "b::static_inv" << std::endl
// This can fail.
48 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
49 << (!failed
? "b::dtor::post\n" : "")
55 std::string
ok_c(bool threw
= false) {
56 std::ostringstream ok
; ok
57 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
58 << "c::inv" << std::endl
60 #ifndef BOOST_CONTRACT_NO_OLDS
61 << "c::dtor::old" << std::endl
63 << "c::dtor::body" << std::endl
64 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
65 << (threw
? "c::inv\n" : "")
67 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
68 << (!threw
? "c::dtor::post\n" : "")
74 struct err
{}; // Global decl so visible in MSVC10 lambdas.
77 std::ostringstream ok
;
79 #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
80 #define BOOST_CONTRACT_TEST_entry_inv 0
82 #define BOOST_CONTRACT_TEST_entry_inv 1
84 #ifdef BOOST_CONTRACT_NO_EXIT_INVARIANTS
85 #define BOOST_CONTRACT_TEST_exit_inv 0
87 #define BOOST_CONTRACT_TEST_exit_inv 1
90 a_exit_static_inv
= true;
91 b_exit_static_inv
= true;
92 c_exit_static_inv
= true;
93 a_entering_static_inv
= b_entering_static_inv
= c_entering_static_inv
=
94 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv
, true, false);
99 ok
.str(""); ok
// Test nothing failed.
104 BOOST_TEST(out
.eq(ok
.str()));
106 boost::contract::set_exit_invariant_failure([&ok
] (boost::contract::from
) {
107 BOOST_TEST(out
.eq(ok
.str())); // Must check before dtor throws.
108 throw err(); // For testing only (as dtors should not throw otherwise).
111 a_exit_static_inv
= false;
112 b_exit_static_inv
= true;
113 c_exit_static_inv
= true;
114 a_entering_static_inv
= b_entering_static_inv
= c_entering_static_inv
=
115 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv
, true, false);
123 << ok_a() // Test no exit a::static_inv so no failure here.
127 BOOST_TEST(out
.eq(ok
.str()));
128 } catch(...) { BOOST_TEST(false); }
130 a_exit_static_inv
= true;
131 b_exit_static_inv
= false;
132 c_exit_static_inv
= true;
133 a_entering_static_inv
= b_entering_static_inv
= c_entering_static_inv
=
134 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv
, true, false);
140 // Test exit b::static_inv failed...
141 << ok_b(BOOST_CONTRACT_TEST_exit_inv
)
145 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
147 } catch(err
const&) {
149 ok
// ...then exec other dtors and check inv on throw (as dtor threw).
150 << ok_c(BOOST_CONTRACT_TEST_exit_inv
)
152 BOOST_TEST(out
.eq(ok
.str()));
153 } catch(...) { BOOST_TEST(false); }
155 a_exit_static_inv
= true;
156 b_exit_static_inv
= true;
157 c_exit_static_inv
= false;
158 a_entering_static_inv
= b_entering_static_inv
= c_entering_static_inv
=
159 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv
, true, false);
169 // Test no exit c::static_inv so no failure here.
172 BOOST_TEST(out
.eq(ok
.str()));
173 } catch(...) { BOOST_TEST(false); }
175 boost::contract::set_exit_invariant_failure([] (boost::contract::from
) {
176 // Testing multiple failures so dtors must not throw multiple except,
177 // just ignore failure and continue test program (for testing only).
180 a_exit_static_inv
= false;
181 b_exit_static_inv
= false;
182 c_exit_static_inv
= false;
183 a_entering_static_inv
= b_entering_static_inv
= c_entering_static_inv
=
184 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv
, true, false);
190 // Test no exit a::static_inv so no faliure here.
192 // Test exit b::static_inv failed (as all did).
193 << ok_b(BOOST_CONTRACT_TEST_exit_inv
)
194 // Test no exit c::static_inv so no failure here.
197 BOOST_TEST(out
.eq(ok
.str()));
199 #undef BOOST_CONTRACT_TEST_entry_inv
200 #undef BOOST_CONTRACT_TEST_exit_inv
201 return boost::report_errors();