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 public static member function contract compilation on/off (w/ macros).
8
9 #include "../detail/oteststream.hpp"
10 #include "../detail/unprotected_commas.hpp"
11 #include <boost/contract/core/config.hpp>
12 #include <boost/contract_macro.hpp>
13 #include <boost/detail/lightweight_test.hpp>
14 #include <sstream>
15
16 boost::contract::test::detail::oteststream out;
17
18 struct a {
19 BOOST_CONTRACT_STATIC_INVARIANT({
20 boost::contract::test::detail::unprotected_commas<void, void, void>::
21 call();
22 out << "a::static_inv" << std::endl;
23 })
24
25 BOOST_CONTRACT_INVARIANT({
26 boost::contract::test::detail::unprotected_commas<void, void, void>::
27 call();
28 out << "a::inv" << std::endl;
29 })
30
fa31 static void f(int x) {
32 BOOST_CONTRACT_OLD_PTR(
33 boost::contract::test::detail::unprotected_commas<int, void, void>::
34 type1
35 )(
36 old_x,
37 (boost::contract::test::detail::unprotected_commas<void, void,
38 void>::same(x))
39 );
40 BOOST_CONTRACT_STATIC_PUBLIC_FUNCTION(boost::contract::test::detail::
41 unprotected_commas<a, void, void>::type1)
42 BOOST_CONTRACT_PRECONDITION([] {
43 boost::contract::test::detail::unprotected_commas<
44 void, void, void>::call();
45 out << "a::f::pre" << std::endl;
46 })
47 BOOST_CONTRACT_OLD([] {
48 boost::contract::test::detail::unprotected_commas<
49 void, void, void>::call();
50 out << "a::f::old" << std::endl;
51 })
52 BOOST_CONTRACT_POSTCONDITION([] {
53 boost::contract::test::detail::unprotected_commas<
54 void, void, void>::call();
55 out << "a::f::post" << std::endl;
56 })
57 ;
58 out << "a::f::body" << std::endl;
59 }
60 };
61
main()62 int main() {
63 std::ostringstream ok;
64
65 out.str("");
66 a::f(123);
67 ok.str(""); ok
68 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
69 << "a::static_inv" << std::endl
70 #endif
71 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
72 << "a::f::pre" << std::endl
73 #endif
74 #ifndef BOOST_CONTRACT_NO_OLDS
75 << "a::f::old" << std::endl
76 #endif
77 << "a::f::body" << std::endl
78 // Test no post (but still static inv) because body threw.
79 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
80 << "a::static_inv" << std::endl
81 #endif
82 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
83 << "a::f::post" << std::endl
84 #endif
85 ;
86 BOOST_TEST(out.eq(ok.str()));
87
88 return boost::report_errors();
89 }
90
91