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
19std::string ok_c() {
20 std::ostringstream ok; ok << "" // Suppress a warning.
21 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
22 << "a::ctor::pre" << std::endl
23 << "b::ctor::pre" << std::endl
24 << "c::ctor::pre" << std::endl
25 #endif
26
27 #ifndef BOOST_CONTRACT_NO_OLDS
28 << "c::ctor::old" << std::endl
29 #endif
30 << "c::ctor::body" << std::endl
31 // No failure here.
32 ;
33 return ok.str();
34}
35
36std::string ok_b() {
37 std::ostringstream ok; ok
38 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
39 << "c::inv" << std::endl
40 #endif
41 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
42 << "c::ctor::post" << std::endl
43 #endif
44
45 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
46 << "b::static_inv" << std::endl
47 #endif
48 #ifndef BOOST_CONTRACT_NO_OLDS
49 << "b::ctor::old" << std::endl
50 #endif
51 << "b::ctor::body" << std::endl
52 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
53 << "b::static_inv" << std::endl // This can fail.
54 #endif
55 ;
56 return ok.str();
57}
58
59std::string ok_a() {
60 std::ostringstream ok; ok
61 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
62 << "b::inv" << std::endl
63 #endif
64 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
65 << "b::ctor::post" << std::endl
66 #endif
67
68 #ifndef BOOST_CONTRACT_NO_OLDS
69 << "a::ctor::old" << std::endl
70 #endif
71 << "a::ctor::body" << std::endl
72 // No failure here.
73 ;
74 return ok.str();
75}
76
77std::string ok_end() {
78 std::ostringstream ok; ok << "" // Suppress a warning.
79 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
80 << "a::inv" << std::endl
81 #endif
82 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
83 << "a::ctor::post" << std::endl
84 #endif
85 ;
86 return ok.str();
87}
88
89struct err {}; // Global decl so visible in MSVC10 lambdas.
90
91int main() {
92 std::ostringstream ok;
93
94 #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
95 #define BOOST_CONTRACT_TEST_entry_inv 0
96 #else
97 #define BOOST_CONTRACT_TEST_entry_inv 1
98 #endif
99
100 a_exit_static_inv = true;
101 b_exit_static_inv = true;
102 c_exit_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);
105 {
106 out.str(s: "");
107 a aa;
108 ok.str(s: ""); ok // Test nothing fails.
109 << ok_c()
110 << ok_b()
111 << ok_a()
112 << ok_end()
113 ;
114 BOOST_TEST(out.eq(ok.str()));
115 }
116
117 boost::contract::set_exit_invariant_failure(
118 [] (boost::contract::from) { throw err(); });
119
120 a_exit_static_inv = false;
121 b_exit_static_inv = true;
122 c_exit_static_inv = true;
123 a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
124 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
125 try {
126 out.str(s: "");
127 a aa;
128 ok.str(s: ""); ok
129 << ok_c()
130 << ok_b()
131 << ok_a() // Test no a::static_inv so no failure.
132 << ok_end()
133 ;
134 BOOST_TEST(out.eq(ok.str()));
135 } catch(...) { BOOST_TEST(false); }
136
137 a_exit_static_inv = true;
138 b_exit_static_inv = false;
139 c_exit_static_inv = true;
140 a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
141 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
142 try {
143 out.str(s: "");
144 a aa;
145 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
146 BOOST_TEST(false);
147 } catch(err const&) {
148 #endif
149 ok.str(s: ""); ok
150 << ok_c()
151 << ok_b() // Test b::static_inv failed.
152 #ifdef BOOST_CONTRACT_NO_EXIT_INVARIANTS
153 << ok_a()
154 << ok_end()
155 #endif
156 ;
157 BOOST_TEST(out.eq(ok.str()));
158 } catch(...) { BOOST_TEST(false); }
159
160 a_exit_static_inv = true;
161 b_exit_static_inv = true;
162 c_exit_static_inv = false;
163 a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
164 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
165 try {
166 out.str(s: "");
167 a aa;
168 ok.str(s: ""); ok
169 << ok_c() // Test no c::static_inv so no failure.
170 << ok_b()
171 << ok_a()
172 << ok_end()
173 ;
174 BOOST_TEST(out.eq(ok.str()));
175 } catch(...) { BOOST_TEST(false); }
176
177 a_exit_static_inv = false;
178 b_exit_static_inv = false;
179 c_exit_static_inv = false;
180 a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
181 BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
182 try {
183 out.str(s: "");
184 a aa;
185 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
186 BOOST_TEST(false);
187 } catch(err const&) {
188 #endif
189 ok.str(s: ""); ok
190 << ok_c()
191 << ok_b() // Test b::static_inv failed.
192 #ifdef BOOST_CONTRACT_NO_EXIT_INVARIANTS
193 << ok_a()
194 << ok_end()
195 #endif
196 ;
197 BOOST_TEST(out.eq(ok.str()));
198 } catch(...) { BOOST_TEST(false); }
199
200 return boost::report_errors();
201}
202
203

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of boost/libs/contract/test/constructor/decl_exit_static_inv_mid.cpp