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 invariants.
9 #define BOOST_CONTRACT_TEST_NO_A_INV
10 #undef BOOST_CONTRACT_TEST_NO_B_INV
11 #define BOOST_CONTRACT_TEST_NO_C_INV
14 #include <boost/preprocessor/control/iif.hpp>
15 #include <boost/detail/lightweight_test.hpp>
19 std::string
ok_begin() {
20 std::ostringstream ok
; ok
21 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
22 << "c::static_inv" << std::endl
23 << "b::static_inv" << std::endl
24 << "b::inv" << std::endl
25 << "a::static_inv" << std::endl
27 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
28 << "c::f::pre" << std::endl
30 #ifndef BOOST_CONTRACT_NO_OLDS
31 << "c::f::old" << std::endl
32 << "b::f::old" << std::endl
33 << "a::f::old" << std::endl
35 << "a::f::body" << std::endl
40 std::string
ok_end() {
41 std::ostringstream ok
; ok
<< "" // Suppress a warning.
42 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
43 << "c::f::old" << std::endl
44 << "c::f::post" << std::endl
45 << "b::f::old" << std::endl
46 << "b::f::post" << std::endl
47 << "a::f::post" << std::endl
53 struct err
{}; // Global decl so visible in MSVC10 lambdas.
56 std::ostringstream ok
;
58 #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
59 #define BOOST_CONTRACT_TEST_entry_inv 0
61 #define BOOST_CONTRACT_TEST_entry_inv 1
69 a_entering_inv
= b_entering_inv
= c_entering_inv
=
70 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv
, true, false);
73 ok
.str(""); ok
// Test nothing failed.
75 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
76 << "c::static_inv" << std::endl
77 << "b::static_inv" << std::endl
78 << "b::inv" << std::endl
79 << "a::static_inv" << std::endl
83 BOOST_TEST(out
.eq(ok
.str()));
85 boost::contract::set_exit_invariant_failure(
86 [] (boost::contract::from
) { throw err(); });
91 a_entering_inv
= b_entering_inv
= c_entering_inv
=
92 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv
, true, false);
98 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
99 << "c::static_inv" << std::endl
100 << "b::static_inv" << std::endl
101 << "b::inv" << std::endl
102 << "a::static_inv" << std::endl
103 // Test no failure here.
105 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
109 BOOST_TEST(out
.eq(ok
.str()));
110 } catch(...) { BOOST_TEST(false); }
115 a_entering_inv
= b_entering_inv
= c_entering_inv
=
116 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv
, true, false);
120 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
122 } catch(err
const&) {
126 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
127 << "c::static_inv" << std::endl
128 << "b::static_inv" << std::endl
129 << "b::inv" << std::endl
// Test this failed.
130 #elif !defined(BOOST_CONTRACT_NO_POSTCONDITIONS)
134 BOOST_TEST(out
.eq(ok
.str()));
135 } catch(...) { BOOST_TEST(false); }
140 a_entering_inv
= b_entering_inv
= c_entering_inv
=
141 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv
, true, false);
147 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
148 << "c::static_inv" << std::endl
149 << "b::static_inv" << std::endl
150 << "b::inv" << std::endl
151 << "a::static_inv" << std::endl
153 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
157 BOOST_TEST(out
.eq(ok
.str()));
158 } catch(...) { BOOST_TEST(false); }
163 a_entering_inv
= b_entering_inv
= c_entering_inv
=
164 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv
, true, false);
168 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
170 } catch(err
const&) {
174 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
175 << "c::static_inv" << std::endl
176 << "b::static_inv" << std::endl
177 << "b::inv" << std::endl
// Test this failed (as all did).
178 #elif !defined(BOOST_CONTRACT_NO_POSTCONDITIONS)
182 BOOST_TEST(out
.eq(ok
.str()));
183 } catch(...) { BOOST_TEST(false); }
185 return boost::report_errors();