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 contract compilation on/off (including EXCEPT, using macro interface).
8
9 #include "../detail/oteststream.hpp"
10 #include "../detail/unprotected_commas.hpp"
11 #include <boost/contract_macro.hpp>
12 #include <boost/detail/lightweight_test.hpp>
13 #include <sstream>
14
15 boost::contract::test::detail::oteststream out;
16
17 struct except_error {};
18
f(int x)19 void f(int x) {
20 BOOST_CONTRACT_OLD_PTR(
21 boost::contract::test::detail::unprotected_commas<int, void, void>::
22 type1
23 )(
24 old_x,
25 (boost::contract::test::detail::unprotected_commas<void, void, void>::
26 same(x))
27 );
28 BOOST_CONTRACT_FUNCTION()
29 BOOST_CONTRACT_PRECONDITION([] {
30 boost::contract::test::detail::unprotected_commas<void, void, void>
31 ::call();
32 out << "f::pre" << std::endl;
33 })
34 BOOST_CONTRACT_OLD([] {
35 boost::contract::test::detail::unprotected_commas<void, void, void>
36 ::call();
37 out << "f::old" << std::endl;
38 })
39 BOOST_CONTRACT_POSTCONDITION([] {
40 boost::contract::test::detail::unprotected_commas<void, void, void>
41 ::call();
42 out << "f::post" << std::endl;
43 })
44 BOOST_CONTRACT_EXCEPT([] { // Test EXCEPT macro (at least 1 time here).
45 boost::contract::test::detail::unprotected_commas<void, void, void>
46 ::call();
47 out << "f::except" << std::endl;
48 })
49 ;
50 out << "f::body" << std::endl;
51 if(x == -1) throw except_error();
52 }
53
main()54 int main() {
55 std::ostringstream ok;
56
57 out.str("");
58 f(123);
59 ok.str(""); ok
60 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
61 << "f::pre" << std::endl
62 #endif
63 #ifndef BOOST_CONTRACT_NO_OLDS
64 << "f::old" << std::endl
65 #endif
66 << "f::body" << std::endl
67 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
68 << "f::post" << std::endl
69 #endif
70 ;
71 BOOST_TEST(out.eq(ok.str()));
72
73 boost::contract::set_except_failure([] (boost::contract::from) {});
74 out.str("");
75 try {
76 f(-1); // Test throw and EXCEPT macro (test only here... that's fine).
77 BOOST_TEST(false);
78 } catch(except_error const&) {} // OK.
79 ok.str(""); ok
80 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
81 << "f::pre" << std::endl
82 #endif
83 #ifndef BOOST_CONTRACT_NO_OLDS
84 << "f::old" << std::endl
85 #endif
86 << "f::body" << std::endl
87 #ifndef BOOST_CONTRACT_NO_EXCEPTS
88 << "f::except" << std::endl
89 #endif
90 ;
91 BOOST_TEST(out.eq(ok.str()));
92
93 return boost::report_errors();
94 }
95
96