• 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 invariant compilation on/off.
8 
9 #include "../detail/oteststream.hpp"
10 #include <boost/contract/constructor.hpp>
11 #include <boost/contract/destructor.hpp>
12 #include <boost/contract/public_function.hpp>
13 #include <boost/contract/check.hpp>
14 #include <boost/detail/lightweight_test.hpp>
15 #include <sstream>
16 
17 boost::contract::test::detail::oteststream out;
18 
19 class a {
20 public:
21     #ifndef BOOST_CONTRACT_NO_INVARIANTS
static_invariant()22         static void static_invariant() {
23             out << "a::static_inv" << std::endl;
24         }
25 
invariant() const26         void invariant() const volatile {
27             out << "a::cv_inv" << std::endl;
28         }
29 
invariant() const30         void invariant() const {
31             out << "a::const_inv" << std::endl;
32         }
33     #endif
34 
a()35     a() { // Test check both cv and const invariant (at exit if no throw).
36         #ifndef BOOST_CONTRACT_NO_CONSTRUCTORS
37             boost::contract::check c= boost::contract::constructor(this);
38         #endif
39         out << "a::ctor::body" << std::endl;
40     }
41 
~a()42     ~a() { // Test check both cv and const invariant (at entry).
43         #ifndef BOOSTT_CONTRACT_NO_DESTRUCTORS
44             boost::contract::check c = boost::contract::destructor(this);
45         #endif
46         out << "a::dtor::body" << std::endl;
47     }
48 
m()49     void m() { // Test check const invariant (at entry and exit).
50         #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
51             boost::contract::check c = boost::contract::public_function(this);
52         #endif
53         out << "a::m::body" << std::endl;
54     }
55 
c() const56     void c() const { // Test check const invariant (at entry and exit).
57         #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
58             boost::contract::check c = boost::contract::public_function(this);
59         #endif
60         out << "a::c::body" << std::endl;
61     }
62 
v()63     void v() volatile { // Test check cv invariant (at entry and exit).
64         #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
65             boost::contract::check c = boost::contract::public_function(this);
66         #endif
67         out << "a::v::body" << std::endl;
68     }
69 
cv() const70     void cv() const volatile { // Test check cv invariant (at entry and exit).
71         #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
72             boost::contract::check c = boost::contract::public_function(this);
73         #endif
74         out << "a::cv::body" << std::endl;
75     }
76 };
77 
main()78 int main() {
79     std::ostringstream ok;
80 
81     {
82         out.str("");
83         a aa;
84         ok.str(""); ok
85             #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
86                 << "a::static_inv" << std::endl
87             #endif
88             << "a::ctor::body" << std::endl
89             #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
90                 << "a::static_inv" << std::endl
91                 << "a::cv_inv" << std::endl
92                 << "a::const_inv" << std::endl
93             #endif
94         ;
95         BOOST_TEST(out.eq(ok.str()));
96 
97         out.str("");
98         aa.m();
99         ok.str(""); ok
100             #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
101                 << "a::static_inv" << std::endl
102                 << "a::const_inv" << std::endl
103             #endif
104             << "a::m::body" << std::endl
105             #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
106                 << "a::static_inv" << std::endl
107                 << "a::const_inv" << std::endl
108             #endif
109         ;
110         BOOST_TEST(out.eq(ok.str()));
111 
112         out.str("");
113         aa.c();
114         ok.str(""); ok
115             #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
116                 << "a::static_inv" << std::endl
117                 << "a::const_inv" << std::endl
118             #endif
119             << "a::c::body" << std::endl
120             #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
121                 << "a::static_inv" << std::endl
122                 << "a::const_inv" << std::endl
123             #endif
124         ;
125         BOOST_TEST(out.eq(ok.str()));
126 
127         out.str("");
128         aa.v();
129         ok.str(""); ok
130             #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
131                 << "a::static_inv" << std::endl
132                 << "a::cv_inv" << std::endl
133             #endif
134             << "a::v::body" << std::endl
135             #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
136                 << "a::static_inv" << std::endl
137                 << "a::cv_inv" << std::endl
138             #endif
139         ;
140         BOOST_TEST(out.eq(ok.str()));
141 
142         out.str("");
143         aa.cv();
144         ok.str(""); ok
145             #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
146                 << "a::static_inv" << std::endl
147                 << "a::cv_inv" << std::endl
148             #endif
149             << "a::cv::body" << std::endl
150             #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
151                 << "a::static_inv" << std::endl
152                 << "a::cv_inv" << std::endl
153             #endif
154         ;
155         BOOST_TEST(out.eq(ok.str()));
156 
157         out.str("");
158     } // Call dtor.
159     ok.str(""); ok
160         #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
161             << "a::static_inv" << std::endl
162             << "a::cv_inv" << std::endl
163             << "a::const_inv" << std::endl
164         #endif
165         << "a::dtor::body" << std::endl
166         #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
167             << "a::static_inv" << std::endl
168         #endif
169     ;
170     BOOST_TEST(out.eq(ok.str()));
171 
172     return boost::report_errors();
173 }
174 
175