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