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