]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/contract/test/destructor/decl_exit_static_inv_mid.cpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / libs / contract / test / destructor / decl_exit_static_inv_mid.cpp
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 only middle base class with exit static invariants.
8
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
12 #include "decl.hpp"
13
14 #include <boost/preprocessor/control/iif.hpp>
15 #include <boost/detail/lightweight_test.hpp>
16 #include <sstream>
17 #include <string>
18
19 std::string ok_a() {
20 std::ostringstream ok; ok
21 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
22 << "a::inv" << std::endl
23 #endif
24 #ifndef BOOST_CONTRACT_NO_OLDS
25 << "a::dtor::old" << std::endl
26 #endif
27 << "a::dtor::body" << std::endl
28 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
29 << "a::dtor::post" << std::endl
30 #endif
31 ;
32 return ok.str();
33 }
34
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
40 #endif
41 #ifndef BOOST_CONTRACT_NO_OLDS
42 << "b::dtor::old" << std::endl
43 #endif
44 << "b::dtor::body" << std::endl
45 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
46 << "b::static_inv" << std::endl // This can fail.
47 #endif
48 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
49 << (!failed ? "b::dtor::post\n" : "")
50 #endif
51 ;
52 return ok.str();
53 }
54
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
59 #endif
60 #ifndef BOOST_CONTRACT_NO_OLDS
61 << "c::dtor::old" << std::endl
62 #endif
63 << "c::dtor::body" << std::endl
64 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
65 << (threw ? "c::inv\n" : "")
66 #endif
67 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
68 << (!threw ? "c::dtor::post\n" : "")
69 #endif
70 ;
71 return ok.str();
72 }
73
74 struct err {}; // Global decl so visible in MSVC10 lambdas.
75
76 int main() {
77 std::ostringstream ok;
78
79 #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
80 #define BOOST_CONTRACT_TEST_entry_inv 0
81 #else
82 #define BOOST_CONTRACT_TEST_entry_inv 1
83 #endif
84 #ifdef BOOST_CONTRACT_NO_EXIT_INVARIANTS
85 #define BOOST_CONTRACT_TEST_exit_inv 0
86 #else
87 #define BOOST_CONTRACT_TEST_exit_inv 1
88 #endif
89
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);
95 {
96 a aa;
97 out.str("");
98 }
99 ok.str(""); ok // Test nothing failed.
100 << ok_a()
101 << ok_b()
102 << ok_c()
103 ;
104 BOOST_TEST(out.eq(ok.str()));
105
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).
109 });
110
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);
116 try {
117 {
118 a aa;
119 ok.str("");
120 out.str("");
121 }
122 ok.str(""); ok
123 << ok_a() // Test no exit a::static_inv so no failure here.
124 << ok_b()
125 << ok_c()
126 ;
127 BOOST_TEST(out.eq(ok.str()));
128 } catch(...) { BOOST_TEST(false); }
129
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);
135 try {
136 {
137 a aa;
138 ok.str(""); ok
139 << ok_a()
140 // Test exit b::static_inv failed...
141 << ok_b(BOOST_CONTRACT_TEST_exit_inv)
142 ;
143 out.str("");
144 }
145 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
146 BOOST_TEST(false);
147 } catch(err const&) {
148 #endif
149 ok // ...then exec other dtors and check inv on throw (as dtor threw).
150 << ok_c(BOOST_CONTRACT_TEST_exit_inv)
151 ;
152 BOOST_TEST(out.eq(ok.str()));
153 } catch(...) { BOOST_TEST(false); }
154
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);
160 try {
161 {
162 a aa;
163 ok.str("");
164 out.str("");
165 }
166 ok.str(""); ok
167 << ok_a()
168 << ok_b()
169 // Test no exit c::static_inv so no failure here.
170 << ok_c()
171 ;
172 BOOST_TEST(out.eq(ok.str()));
173 } catch(...) { BOOST_TEST(false); }
174
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).
178 });
179
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);
185 {
186 a aa;
187 out.str("");
188 }
189 ok.str(""); ok
190 // Test no exit a::static_inv so no faliure here.
191 << ok_a()
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.
195 << ok_c()
196 ;
197 BOOST_TEST(out.eq(ok.str()));
198
199 #undef BOOST_CONTRACT_TEST_entry_inv
200 #undef BOOST_CONTRACT_TEST_exit_inv
201 return boost::report_errors();
202 }
203