• 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 with preconditions.
8 
9 #undef BOOST_CONTRACT_TEST_NO_F_PRE
10 #include "decl.hpp"
11 
12 #include <boost/detail/lightweight_test.hpp>
13 #include <sstream>
14 #include <string>
15 
ok_f(bool failed=false)16 std::string ok_f(bool failed = false) {
17     std::ostringstream ok; ok << "" // Suppress a warning.
18         #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
19             << "f::pre" << std::endl // Test no failure here.
20         #endif
21     ;
22     if(!failed) ok
23         #ifndef BOOST_CONTRACT_NO_OLDS
24             << "f::old" << std::endl
25         #endif
26         << "f::body" << std::endl
27         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
28             << "f::post" << std::endl
29         #endif
30     ;
31     return ok.str();
32 }
33 
34 struct err {}; // Global decl so visible in MSVC10 lambdas.
35 
main()36 int main() {
37     std::ostringstream ok;
38 
39     f_pre = true;
40     out.str("");
41     f();
42     ok.str(""); ok
43         << ok_f()
44     ;
45     BOOST_TEST(out.eq(ok.str()));
46 
47     #ifdef BOOST_CONTRACT_NO_PRECONDITIONS
48         #define BOOST_CONTRACT_TEST_pre 0
49     #else
50         #define BOOST_CONTRACT_TEST_pre 1
51     #endif
52 
53     boost::contract::set_precondition_failure(
54             [] (boost::contract::from) { throw err(); });
55 
56     f_pre = false;
57     out.str("");
58     try {
59         f();
60         #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
61                 BOOST_TEST(false);
62             } catch(err const&) {
63         #endif
64         ok.str(""); ok
65             << ok_f(BOOST_CONTRACT_TEST_pre)
66         ;
67         BOOST_TEST(out.eq(ok.str()));
68     } catch(...) { BOOST_TEST(false); }
69 
70     #undef BOOST_CONTRACT_TEST_pre
71     return boost::report_errors();
72 }
73 
74