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 entry 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>
19 std::string
ok_end() {
20 std::ostringstream ok
; ok
21 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
22 << "c::f::pre" << std::endl
24 #ifndef BOOST_CONTRACT_NO_OLDS
25 << "c::f::old" << std::endl
26 << "b::f::old" << std::endl
27 << "a::f::old" << std::endl
29 << "a::f::body" << std::endl
30 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
31 << "c::inv" << std::endl
32 << "b::static_inv" << std::endl
33 << "b::inv" << std::endl
34 << "a::inv" << std::endl
36 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
37 << "c::f::old" << std::endl
38 << "c::f::post" << std::endl
39 << "b::f::old" << std::endl
40 << "b::f::post" << std::endl
41 << "a::f::post" << std::endl
47 struct err
{}; // Global decl so visible in MSVC10 lambdas.
50 std::ostringstream ok
;
52 #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
53 #define BOOST_CONTRACT_TEST_entry_inv 0
55 #define BOOST_CONTRACT_TEST_entry_inv 1
60 a_entry_static_inv
= true;
61 b_entry_static_inv
= true;
62 c_entry_static_inv
= true;
63 a_entering_static_inv
= b_entering_static_inv
= c_entering_static_inv
=
64 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv
, true, false);
67 ok
.str(""); ok
// Test nothing failed.
68 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
69 << "c::inv" << std::endl
70 << "b::static_inv" << std::endl
71 << "b::inv" << std::endl
72 << "a::inv" << std::endl
76 BOOST_TEST(out
.eq(ok
.str()));
78 boost::contract::set_entry_invariant_failure(
79 [] (boost::contract::from
) { throw err(); });
81 a_entry_static_inv
= false;
82 b_entry_static_inv
= true;
83 c_entry_static_inv
= true;
84 a_entering_static_inv
= b_entering_static_inv
= c_entering_static_inv
=true;
89 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
90 << "c::inv" << std::endl
91 << "b::static_inv" << std::endl
92 << "b::inv" << std::endl
93 << "a::inv" << std::endl
97 BOOST_TEST(out
.eq(ok
.str()));
98 } catch(...) { BOOST_TEST(false); }
100 a_entry_static_inv
= true;
101 b_entry_static_inv
= false;
102 c_entry_static_inv
= true;
103 a_entering_static_inv
= b_entering_static_inv
= c_entering_static_inv
=
104 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv
, true, false);
108 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
110 } catch(err
const&) {
113 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
114 << "c::inv" << std::endl
115 << "b::static_inv" << std::endl
// Test this fail.
120 BOOST_TEST(out
.eq(ok
.str()));
121 } catch(...) { BOOST_TEST(false); }
123 a_entry_static_inv
= true;
124 b_entry_static_inv
= true;
125 c_entry_static_inv
= false;
126 a_entering_static_inv
= b_entering_static_inv
= c_entering_static_inv
=true;
131 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
132 << "c::inv" << std::endl
133 << "b::static_inv" << std::endl
134 << "b::inv" << std::endl
135 << "a::inv" << std::endl
139 BOOST_TEST(out
.eq(ok
.str()));
140 } catch(...) { BOOST_TEST(false); }
142 a_entry_static_inv
= false;
143 b_entry_static_inv
= false;
144 c_entry_static_inv
= false;
145 a_entering_static_inv
= b_entering_static_inv
= c_entering_static_inv
=
146 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv
, true, false);
150 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
152 } catch(err
const&) {
155 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
156 << "c::inv" << std::endl
157 // Test this failed (as all did).
158 << "b::static_inv" << std::endl
163 BOOST_TEST(out
.eq(ok
.str()));
164 } catch(...) { BOOST_TEST(false); }
166 #undef BOOST_CONTRACT_TEST_entry_inv
167 return boost::report_errors();