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 classes with preconditions.
8 
9 #define BOOST_CONTRACT_TEST_NO_A_PRE
10 #undef BOOST_CONTRACT_TEST_NO_B_PRE
11 #define BOOST_CONTRACT_TEST_NO_C_PRE
12 #include "decl.hpp"
13 
14 #include <boost/detail/lightweight_test.hpp>
15 #include <sstream>
16 #include <string>
17 
ok_after()18 std::string ok_after() {
19     std::ostringstream ok; ok
20         #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
21             << "c::static_inv" << std::endl
22         #endif
23         #ifndef BOOST_CONTRACT_NO_OLDS
24             << "c::ctor::old" << std::endl
25         #endif
26         << "c::ctor::body" << std::endl
27         #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
28             << "c::static_inv" << std::endl
29             << "c::inv" << std::endl
30         #endif
31         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
32             << "c::ctor::post" << std::endl
33         #endif
34 
35         #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
36             << "b::static_inv" << std::endl
37         #endif
38         #ifndef BOOST_CONTRACT_NO_OLDS
39             << "b::ctor::old" << std::endl
40         #endif
41         << "b::ctor::body" << std::endl
42         #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
43             << "b::static_inv" << std::endl
44             << "b::inv" << std::endl
45         #endif
46         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
47             << "b::ctor::post" << std::endl
48         #endif
49 
50         #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
51             << "a::static_inv" << std::endl
52         #endif
53         #ifndef BOOST_CONTRACT_NO_OLDS
54             << "a::ctor::old" << std::endl
55         #endif
56         << "a::ctor::body" << std::endl
57         #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
58             << "a::static_inv" << std::endl
59             << "a::inv" << std::endl
60         #endif
61         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
62             << "a::ctor::post" << std::endl
63         #endif
64     ;
65     return ok.str();
66 }
67 
68 struct err {}; // Global decl so visible in MSVC10 lambdas.
69 
main()70 int main() {
71     std::ostringstream ok;
72 
73     a_pre = true;
74     b_pre = true;
75     c_pre = true;
76     {
77         out.str("");
78         a aa;
79         ok.str(""); ok // Test nothing failed.
80             #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
81                 << "b::ctor::pre" << std::endl
82             #endif
83             << ok_after()
84         ;
85         BOOST_TEST(out.eq(ok.str()));
86     }
87 
88     boost::contract::set_precondition_failure(
89             [] (boost::contract::from) { throw err(); });
90 
91     a_pre = false;
92     b_pre = true;
93     c_pre = true;
94     {
95         out.str("");
96         a aa;
97         ok.str(""); ok
98             #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
99                 << "b::ctor::pre" << std::endl // Test no failure here.
100             #endif
101             << ok_after()
102         ;
103         BOOST_TEST(out.eq(ok.str()));
104     }
105 
106     a_pre = true;
107     b_pre = false;
108     c_pre = true;
109     try {
110         out.str("");
111         a aa;
112         #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
113                 BOOST_TEST(false);
114             } catch(err const&) {
115         #endif
116         ok.str(""); ok
117             #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
118                 << "b::ctor::pre" << std::endl // Test this failed.
119             #else
120                 << ok_after()
121             #endif
122         ;
123         BOOST_TEST(out.eq(ok.str()));
124     } catch(...) { BOOST_TEST(false); }
125 
126 
127     a_pre = true;
128     b_pre = true;
129     c_pre = false;
130     {
131         out.str("");
132         a aa;
133         ok.str(""); ok
134             #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
135                 << "b::ctor::pre" << std::endl // Test no failure here.
136             #endif
137             << ok_after()
138         ;
139         BOOST_TEST(out.eq(ok.str()));
140     }
141 
142     a_pre = false;
143     b_pre = false;
144     c_pre = false;
145     try {
146         out.str("");
147         a aa;
148         #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
149                 BOOST_TEST(false);
150             } catch(err const&) {
151         #endif
152         ok.str(""); ok
153             #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
154                 << "b::ctor::pre" << std::endl // Test this failed (as all did).
155             #else
156                 << ok_after()
157             #endif
158         ;
159         BOOST_TEST(out.eq(ok.str()));
160     } catch(...) { BOOST_TEST(false); }
161 
162     return boost::report_errors();
163 }
164 
165