• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 throw form constructor .old() (in middle branch of inheritance tree).
8 
9 #include "../detail/oteststream.hpp"
10 #include <boost/contract/constructor.hpp>
11 #include <boost/contract/base_types.hpp>
12 #include <boost/contract/check.hpp>
13 #include <boost/detail/lightweight_test.hpp>
14 #include <sstream>
15 
16 boost::contract::test::detail::oteststream out;
17 
18 struct c
19     #define BASES private boost::contract::constructor_precondition<c>
20     : BASES
21 {
22     typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
23     #undef BASES
24 
static_invariantc25     static void static_invariant() { out << "c::static_inv" << std::endl; }
invariantc26     void invariant() const { out << "c::inv" << std::endl; }
27 
cc28     c() :
29         boost::contract::constructor_precondition<c>([] {
30             out << "c::ctor::pre" << std::endl;
31         })
32     {
33         boost::contract::check c = boost::contract::constructor(this)
__anon6cc885030202null34             .old([] { out << "c::ctor::old" << std::endl; })
__anon6cc885030302null35             .postcondition([] { out << "c::ctor::post" << std::endl; })
__anon6cc885030402null36             .except([] { out << "c::ctor::except" << std::endl; })
37         ;
38         out << "c::ctor::body" << std::endl;
39         // Do not throw (from inheritance root).
40     }
41 };
42 
43 struct b_err {}; // Global decl so visible in MSVC10 lambdas.
44 
45 struct b
46     #define BASES private boost::contract::constructor_precondition<b>, public c
47     : BASES
48 {
49     typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
50     #undef BASES
51 
static_invariantb52     static void static_invariant() { out << "b::static_inv" << std::endl; }
invariantb53     void invariant() const { out << "b::inv" << std::endl; }
54 
bb55     b() :
56         boost::contract::constructor_precondition<b>([] {
57             out << "b::ctor::pre" << std::endl;
58         })
59     {
60         boost::contract::check c = boost::contract::constructor(this)
__anon6cc885030602null61             .old([] {
62                 out << "b::ctor::old" << std::endl;
63                 throw b_err(); // Test this throws (from mid branch).
64             })
__anon6cc885030702null65             .postcondition([] { out << "b::ctor::post" << std::endl; })
__anon6cc885030802null66             .except([] { out << "b::ctor::except" << std::endl; })
67         ;
68         out << "b::ctor::body" << std::endl;
69     }
70 };
71 
72 struct a
73     #define BASES private boost::contract::constructor_precondition<a>, public b
74     : BASES
75 {
76     typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
77     #undef BASES
78 
static_invarianta79     static void static_invariant() { out << "a::static_inv" << std::endl; }
invarianta80     void invariant() const { out << "a::inv" << std::endl; }
81 
aa82     a() :
83         boost::contract::constructor_precondition<a>([] {
84             out << "a::ctor::pre" << std::endl;
85         })
86     {
87         boost::contract::check c = boost::contract::constructor(this)
__anon6cc885030a02null88             .old([] { out << "a::ctor::old" << std::endl; })
__anon6cc885030b02null89             .postcondition([] { out << "a::ctor::post" << std::endl; })
__anon6cc885030c02null90             .except([] { out << "a::ctor::except" << std::endl; })
91         ;
92         out << "a::ctor::body" << std::endl;
93         // Do not throw (from inheritance leaf).
94     }
95 };
96 
main()97 int main() {
98     std::ostringstream ok;
99 
100     boost::contract::set_old_failure([] (boost::contract::from) { throw; });
101 
102     try {
103         out.str("");
104         a aa;
105         #ifndef BOOST_CONTRACT_NO_OLDS
106                 BOOST_TEST(false);
107             } catch(b_err const&) {
108         #endif
109         ok.str(""); ok
110             #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
111                 << "a::ctor::pre" << std::endl
112                 << "b::ctor::pre" << std::endl
113                 << "c::ctor::pre" << std::endl
114             #endif
115 
116             #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
117                 << "c::static_inv" << std::endl
118             #endif
119             #ifndef BOOST_CONTRACT_NO_OLDS
120                 << "c::ctor::old" << std::endl
121             #endif
122             << "c::ctor::body" << std::endl
123             #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
124                 << "c::static_inv" << std::endl
125                 << "c::inv" << std::endl
126             #endif
127             #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
128                 << "c::ctor::post" << std::endl
129             #endif
130 
131             #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
132                 << "b::static_inv" << std::endl
133             #endif
134             #ifndef BOOST_CONTRACT_NO_OLDS
135                 << "b::ctor::old" << std::endl // Test this threw.
136             #else
137                 << "b::ctor::body" << std::endl
138                 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
139                     << "b::static_inv" << std::endl
140                     << "b::inv" << std::endl
141                 #endif
142 
143                 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
144                     << "a::static_inv" << std::endl
145                 #endif
146                 << "a::ctor::body" << std::endl
147                 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
148                     << "a::static_inv" << std::endl
149                     << "a::inv" << std::endl
150                 #endif
151             #endif
152         ;
153         BOOST_TEST(out.eq(ok.str()));
154     } catch(...) { BOOST_TEST(false); }
155 
156     return boost::report_errors();
157 }
158 
159