• 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 all derived and base classes with postconditions.
8 
9 #undef BOOST_CONTRACT_TEST_NO_A_POST
10 #undef 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             << "b::f::post" << std::endl
68             << "a::f::post" << std::endl
69         #endif
70     ;
71     BOOST_TEST(out.eq(ok.str()));
72 
73     boost::contract::set_postcondition_failure(
74             [] (boost::contract::from) { throw err(); });
75 
76     a_post = false;
77     b_post = true;
78     c_post = true;
79     out.str("");
80     try {
81         aa.f();
82         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
83                 BOOST_TEST(false);
84             } catch(err const&) {
85         #endif
86         ok.str(""); ok
87             << ok_begin()
88             #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
89                 << "c::f::old" << std::endl
90                 << "c::f::post" << std::endl
91                 << "b::f::old" << std::endl
92                 << "b::f::post" << std::endl
93                 << "a::f::post" << std::endl // Test this failed.
94             #endif
95         ;
96         BOOST_TEST(out.eq(ok.str()));
97     } catch(...) { BOOST_TEST(false); }
98 
99     a_post = true;
100     b_post = false;
101     c_post = true;
102     out.str("");
103     try {
104         aa.f();
105         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
106                 BOOST_TEST(false);
107             } catch(err const&) {
108         #endif
109         ok.str(""); ok
110             << ok_begin()
111             #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
112                 << "c::f::old" << std::endl
113                 << "c::f::post" << std::endl
114                 << "b::f::old" << std::endl
115                 << "b::f::post" << std::endl // Test this failed.
116             #endif
117         ;
118         BOOST_TEST(out.eq(ok.str()));
119     } catch(...) { BOOST_TEST(false); }
120 
121     a_post = true;
122     b_post = true;
123     c_post = false;
124     out.str("");
125     try {
126         aa.f();
127         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
128                 BOOST_TEST(false);
129             } catch(err const&) {
130         #endif
131         ok.str(""); ok
132             << ok_begin()
133             #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
134                 << "c::f::old" << std::endl
135                 << "c::f::post" << std::endl // Test this failed.
136             #endif
137         ;
138         BOOST_TEST(out.eq(ok.str()));
139     } catch(...) { BOOST_TEST(false); }
140 
141     a_post = false;
142     b_post = false;
143     c_post = false;
144     out.str("");
145     try {
146         aa.f();
147         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
148                 BOOST_TEST(false);
149             } catch(err const&) {
150         #endif
151         ok.str(""); ok
152             << ok_begin()
153             #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
154                 << "c::f::old" << std::endl
155                 << "c::f::post" << std::endl // Test this failed (as all did).
156             #endif
157         ;
158         BOOST_TEST(out.eq(ok.str()));
159     } catch(...) { BOOST_TEST(false); }
160 
161     return boost::report_errors();
162 }
163 
164