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 all derived and base classes without exit invariants.
8
9 #define BOOST_CONTRACT_TEST_NO_A_INV
10 #define BOOST_CONTRACT_TEST_NO_B_INV
11 #define BOOST_CONTRACT_TEST_NO_C_INV
12 #include "decl.hpp"
13
14 #include <boost/preprocessor/control/iif.hpp>
15 #include <boost/detail/lightweight_test.hpp>
16 #include <sstream>
17
18 struct err {}; // Global decl so visible in MSVC10 lambdas.
19
main()20 int main() {
21 std::ostringstream ok;
22 ok.str(""); ok // Test nothing fails.
23 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
24 // No invariants.
25 << "c::static_inv" << std::endl
26 << "b::static_inv" << std::endl
27 << "a::static_inv" << std::endl
28 #endif
29 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
30 << "c::f::pre" << std::endl
31 #endif
32 #ifndef BOOST_CONTRACT_NO_OLDS
33 << "c::f::old" << std::endl
34 << "b::f::old" << std::endl
35 << "a::f::old" << std::endl
36 #endif
37 << "a::f::body" << std::endl
38 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
39 << "c::static_inv" << std::endl
40 << "b::static_inv" << std::endl
41 << "a::static_inv" << std::endl
42 #endif
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
49 #endif
50 ;
51
52 boost::contract::set_exit_invariant_failure(
53 [] (boost::contract::from) { throw err(); });
54
55 a aa;
56
57 #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
58 #define BOOST_CONTRACT_TEST_entry_inv 0
59 #else
60 #define BOOST_CONTRACT_TEST_entry_inv 1
61 #endif
62
63 a_exit_inv = true;
64 b_exit_inv = true;
65 c_exit_inv = true;
66 a_entering_inv = b_entering_inv = c_entering_inv =
67 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
68 out.str("");
69 aa.f();
70 BOOST_TEST(out.eq(ok.str()));
71
72 a_exit_inv = false;
73 b_exit_inv = true;
74 c_exit_inv = true;
75 a_entering_inv = b_entering_inv = c_entering_inv =
76 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
77 out.str("");
78 aa.f();
79 BOOST_TEST(out.eq(ok.str()));
80
81 a_exit_inv = true;
82 b_exit_inv = false;
83 a_entering_inv = b_entering_inv = c_entering_inv =
84 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
85 out.str("");
86 aa.f();
87 BOOST_TEST(out.eq(ok.str()));
88
89 a_exit_inv = true;
90 b_exit_inv = true;
91 c_exit_inv = false;
92 a_entering_inv = b_entering_inv = c_entering_inv =
93 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
94 out.str("");
95 aa.f();
96 BOOST_TEST(out.eq(ok.str()));
97
98 a_exit_inv = false;
99 b_exit_inv = false;
100 c_exit_inv = false;
101 a_entering_inv = b_entering_inv = c_entering_inv =
102 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
103 out.str("");
104 aa.f();
105 BOOST_TEST(out.eq(ok.str()));
106
107 #undef BOOST_CONTRACT_TEST_entry_inv
108 return boost::report_errors();
109 }
110
111