Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / contract / test / public_function / ifdef.cpp
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.
8
9 #include "../detail/oteststream.hpp"
10 #include <boost/contract/core/config.hpp>
11 #include <boost/contract/core/virtual.hpp>
12 #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
13     #include <boost/contract/public_function.hpp>
14     #include <boost/contract/base_types.hpp>
15     #include <boost/contract/override.hpp>
16     #include <boost/contract/check.hpp>
17     #include <boost/contract/old.hpp>
18 #endif
19 #include <boost/detail/lightweight_test.hpp>
20 #include <sstream>
21
22 boost::contract::test::detail::oteststream out;
23
24 struct b {
25     #ifndef BOOST_CONTRACT_NO_INVARIANTS
26         static void static_invariant() { out << "b::static_inv" << std::endl; }
27         void invariant() const { out << "b::inv" << std::endl; }
28     #endif
29
30     virtual void f(int x, boost::contract::virtual_* v = 0) = 0;
31 };
32
33 void b::f(int x, boost::contract::virtual_* v) {
34     #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
35         boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(v, x);
36     #endif
37     #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
38         boost::contract::check c = boost::contract::public_function(v, this)
39             #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
40                 .precondition([] { out << "b::f::pre" << std::endl; })
41             #endif
42             #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
43                 .old([] { out << "b::f::old" << std::endl; })
44                 .postcondition([] { out << "b::f::post" << std::endl; })
45             #endif
46         ;
47     #endif
48     out << "b::f::body" << std::endl;
49 }
50
51 struct a
52     #define BASES public b
53     : BASES
54 {
55     #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
56         typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
57         BOOST_CONTRACT_OVERRIDE(f)
58     #endif
59
60     #ifndef BOOST_CONTRACT_NO_INVARIANTS
61         static void static_invariant() { out << "a::static_inv" << std::endl; }
62         void invariant() const { out << "a::inv" << std::endl; }
63     #endif
64
65     virtual void f(int x, boost::contract::virtual_* v = 0) {
66         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
67             boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(v, x);
68         #endif
69         #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
70             boost::contract::check c = boost::contract::public_function<
71                     override_f>(v, &a::f, this, x)
72                 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
73                     .precondition([] { out << "a::f::pre" << std::endl; })
74                 #endif
75                 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
76                     .old([] { out << "a::f::old" << std::endl; })
77                     .postcondition([] { out << "a::f::post" << std::endl; })
78                 #endif
79             ;
80         #endif
81         out << "a::f::body" << std::endl;
82     }
83 };
84
85 int main() {
86     std::ostringstream ok;
87     
88     a aa;
89     out.str("");
90     aa.f(123);
91     ok.str(""); ok
92         #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
93             << "b::static_inv" << std::endl
94             << "b::inv" << std::endl
95             << "a::static_inv" << std::endl
96             << "a::inv" << std::endl
97         #endif
98         #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
99             << "b::f::pre" << std::endl
100         #endif
101         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
102             << "b::f::old" << std::endl
103             << "a::f::old" << std::endl
104         #endif
105         << "a::f::body" << std::endl
106         #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
107             << "b::static_inv" << std::endl
108             << "b::inv" << std::endl
109             << "a::static_inv" << std::endl
110             << "a::inv" << std::endl
111         #endif
112         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
113             << "b::f::old" << std::endl
114             << "b::f::post" << std::endl
115             << "a::f::post" << std::endl
116         #endif
117     ;
118     BOOST_TEST(out.eq(ok.str()));
119
120     return boost::report_errors();
121 }
122