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 derived and grandparent classes (ends) with exit invariants.
9 #undef BOOST_CONTRACT_TEST_NO_A_INV
10 #define BOOST_CONTRACT_TEST_NO_B_INV
11 #undef 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 << "c::inv" << std::endl
24 << "b::static_inv" << std::endl
25 << "a::static_inv" << std::endl
26 << "a::inv" << std::endl
28 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
29 << "c::f::pre" << std::endl
31 #ifndef BOOST_CONTRACT_NO_OLDS
32 << "c::f::old" << std::endl
33 << "b::f::old" << std::endl
34 << "a::f::old" << std::endl
36 << "a::f::body" << std::endl
41 std::string
ok_end() {
42 std::ostringstream ok
; ok
<< "" // Suppress a warning.
43 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
44 << "c::f::old" << std::endl
45 << "c::f::post" << std::endl
46 << "b::f::old" << std::endl
47 << "b::f::post" << std::endl
48 << "a::f::post" << std::endl
54 struct err
{}; // Global decl so visible in MSVC10 lambdas.
57 std::ostringstream ok
;
59 #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
60 #define BOOST_CONTRACT_TEST_entry_inv 0
62 #define BOOST_CONTRACT_TEST_entry_inv 1
70 a_entering_inv
= b_entering_inv
= c_entering_inv
=
71 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv
, true, false);
74 ok
.str(""); ok
// Test nothing failed.
76 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
77 << "c::static_inv" << std::endl
78 << "c::inv" << std::endl
79 << "b::static_inv" << std::endl
80 << "a::static_inv" << std::endl
81 << "a::inv" << std::endl
85 BOOST_TEST(out
.eq(ok
.str()));
87 boost::contract::set_exit_invariant_failure(
88 [] (boost::contract::from
) { throw err(); });
93 a_entering_inv
= b_entering_inv
= c_entering_inv
=
94 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv
, true, false);
98 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
100 } catch(err
const&) {
104 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
105 << "c::static_inv" << std::endl
106 << "c::inv" << std::endl
107 << "b::static_inv" << std::endl
108 << "a::static_inv" << std::endl
109 << "a::inv" << std::endl
// Test this failed.
110 #elif !defined(BOOST_CONTRACT_NO_POSTCONDITIONS)
114 BOOST_TEST(out
.eq(ok
.str()));
115 } catch(...) { BOOST_TEST(false); }
120 a_entering_inv
= b_entering_inv
= c_entering_inv
=
121 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv
, true, false);
127 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
128 << "c::static_inv" << std::endl
129 << "c::inv" << std::endl
130 << "b::static_inv" << std::endl
131 << "a::static_inv" << std::endl
132 << "a::inv" << std::endl
136 BOOST_TEST(out
.eq(ok
.str()));
137 } catch(...) { BOOST_TEST(false); }
142 a_entering_inv
= b_entering_inv
= c_entering_inv
=
143 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv
, true, false);
147 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
149 } catch(err
const&) {
153 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
154 << "c::static_inv" << std::endl
155 << "c::inv" << std::endl
// Test this failed.
156 #elif !defined(BOOST_CONTRACT_NO_POSTCONDITIONS)
160 BOOST_TEST(out
.eq(ok
.str()));
161 } catch(...) { BOOST_TEST(false); }
166 a_entering_inv
= b_entering_inv
= c_entering_inv
=
167 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv
, true, false);
171 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
173 } catch(err
const&) {
177 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
178 << "c::static_inv" << std::endl
179 << "c::inv" << std::endl
// Test this failed (as all did).
180 #elif !defined(BOOST_CONTRACT_NO_POSTCONDITIONS)
184 BOOST_TEST(out
.eq(ok
.str()));
185 } catch(...) { BOOST_TEST(false); }
187 return boost::report_errors();