Imported Upstream version 1.71.0
[platform/upstream/boost.git] / libs / contract / test / function / except_throw.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 throw from free function .old().
8
9 #include "../detail/oteststream.hpp"
10 #include <boost/contract/function.hpp>
11 #include <boost/contract/check.hpp>
12 #include <boost/detail/lightweight_test.hpp>
13 #include <sstream>
14
15 boost::contract::test::detail::oteststream out;
16
17 struct err {}; // Global decl so visible in MSVC10 lambdas.
18
19 void f() {
20     boost::contract::check c = boost::contract::function()
21         .precondition([] { out << "f::pre" << std::endl; })
22         .old([] {
23             out << "f::old" << std::endl;
24             throw err(); // Test .old() throws.
25         })
26         .postcondition([] { out << "f::post" << std::endl; })
27     ;
28     out << "f::body" << std::endl;
29 }
30
31 int main() {
32     std::ostringstream ok;
33
34     boost::contract::set_old_failure([] (boost::contract::from) { throw; });
35
36     try {
37         out.str("");
38         f();
39         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
40             BOOST_TEST(false);
41         #endif
42     } catch(err const&) {
43         ok.str(""); ok << "" // Suppress a warning.
44             #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
45                 << "f::pre" << std::endl
46             #endif
47             #ifndef BOOST_CONTRACT_NO_OLDS
48                 << "f::old" << std::endl // Test this threw.
49             #endif
50         ;
51         BOOST_TEST(out.eq(ok.str()));
52     } catch(...) { BOOST_TEST(false); }
53
54     return boost::report_errors();
55 }
56