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