• 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 derived and grandparent classes (ends) with postconditions.
8 
9 #undef BOOST_CONTRACT_TEST_NO_A_POST
10 #define BOOST_CONTRACT_TEST_NO_B_POST
11 #undef 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_c()18 std::string ok_c() {
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         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
38             << "c::ctor::post" << std::endl
39         #endif
40     ;
41     return ok.str();
42 }
43 
ok_ba()44 std::string ok_ba() {
45     std::ostringstream ok; ok
46         #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
47             << "b::static_inv" << std::endl
48         #endif
49         #ifndef BOOST_CONTRACT_NO_OLDS
50             << "b::ctor::old" << std::endl
51         #endif
52         << "b::ctor::body" << std::endl
53         #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
54             << "b::static_inv" << std::endl
55             << "b::inv" << std::endl
56         #endif
57         // No b::ctor::post here.
58 
59         #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
60             << "a::static_inv" << std::endl
61         #endif
62         #ifndef BOOST_CONTRACT_NO_OLDS
63             << "a::ctor::old" << std::endl
64         #endif
65         << "a::ctor::body" << std::endl
66         #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
67             << "a::static_inv" << std::endl
68             << "a::inv" << std::endl
69         #endif
70         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
71             << "a::ctor::post" << std::endl
72         #endif
73     ;
74     return ok.str();
75 }
76 
77 struct err {}; // Global decl so visible in MSVC10 lambdas.
78 
main()79 int main() {
80     std::ostringstream ok;
81 
82     a_post = true;
83     b_post = true;
84     c_post = true;
85     {
86         out.str("");
87         a aa;
88         ok.str(""); ok // Test nothing failed.
89             << ok_c()
90             << ok_ba()
91         ;
92         BOOST_TEST(out.eq(ok.str()));
93     }
94 
95     boost::contract::set_postcondition_failure(
96             [] (boost::contract::from) { throw err(); });
97 
98     a_post = false;
99     b_post = true;
100     c_post = true;
101     try {
102         out.str("");
103         a aa;
104         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
105                 BOOST_TEST(false);
106             } catch(err const&) {
107         #endif
108         ok.str(""); ok
109             << ok_c()
110             << ok_ba() // Test a::ctor::post failed.
111         ;
112         BOOST_TEST(out.eq(ok.str()));
113     } catch(...) { BOOST_TEST(false); }
114 
115     a_post = true;
116     b_post = false;
117     c_post = true;
118     {
119         out.str("");
120         a aa;
121         ok.str(""); ok
122             << ok_c()
123             << ok_ba() // Test no b::ctor::post so no failure.
124         ;
125         BOOST_TEST(out.eq(ok.str()));
126     }
127 
128     a_post = true;
129     b_post = true;
130     c_post = false;
131     try {
132         out.str("");
133         a aa;
134         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
135                 BOOST_TEST(false);
136             } catch(err const&) {
137         #endif
138         ok.str(""); ok
139             << ok_c() // Test c::ctor::post failed.
140             #ifdef BOOST_CONTRACT_NO_POSTCONDITIONS
141                 << ok_ba()
142             #endif
143         ;
144         BOOST_TEST(out.eq(ok.str()));
145     } catch(...) { BOOST_TEST(false); }
146 
147     a_post = false;
148     b_post = false;
149     c_post = false;
150     try {
151         out.str("");
152         a aa;
153         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
154                 BOOST_TEST(false);
155             } catch(err const&) {
156         #endif
157         ok.str(""); ok
158             << ok_c() // Test c::ctor::post failed (as all did).
159             #ifdef BOOST_CONTRACT_NO_POSTCONDITIONS
160                 << ok_ba()
161             #endif
162         ;
163         BOOST_TEST(out.eq(ok.str()));
164     } catch(...) { BOOST_TEST(false); }
165 
166     return boost::report_errors();
167 }
168 
169