• 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 public static member function contract compilation on/off.
8 
9 #include "../detail/oteststream.hpp"
10 #include <boost/contract/core/config.hpp>
11 #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
12     #include <boost/contract/public_function.hpp>
13     #include <boost/contract/check.hpp>
14     #include <boost/contract/old.hpp>
15 #endif
16 #include <boost/detail/lightweight_test.hpp>
17 #include <sstream>
18 
19 boost::contract::test::detail::oteststream out;
20 
21 struct a {
22     #ifndef BOOST_CONTRACT_NO_INVARIANTS
static_invarianta23         static void static_invariant() { out << "a::static_inv" << std::endl; }
invarianta24         void invariant() const { out << "a::inv" << std::endl; }
25     #endif
26 
fa27     static void f(int x) {
28         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
29             boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(x);
30         #endif
31         #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
32             boost::contract::check c = boost::contract::public_function<a>()
33                 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
34                     .precondition([] { out << "a::f::pre" << std::endl; })
35                 #endif
36                 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
37                     .old([] { out << "a::f::old" << std::endl; })
38                     .postcondition([] { out << "a::f::post" << std::endl; })
39                 #endif
40             ;
41         #endif
42         out << "a::f::body" << std::endl;
43     }
44 };
45 
main()46 int main() {
47     std::ostringstream ok;
48 
49     out.str("");
50     a::f(123);
51     ok.str(""); ok
52         #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
53             << "a::static_inv" << std::endl
54         #endif
55         #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
56             << "a::f::pre" << std::endl
57         #endif
58         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
59             << "a::f::old" << std::endl
60         #endif
61         << "a::f::body" << std::endl
62         // Test no post (but still static inv) because body threw.
63         #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
64             << "a::static_inv" << std::endl
65         #endif
66         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
67             << "a::f::post" << std::endl
68         #endif
69     ;
70     BOOST_TEST(out.eq(ok.str()));
71 
72     return boost::report_errors();
73 }
74 
75