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 member function body throws with subcontracting.
8
9 #include "smoke.hpp"
10 #include <boost/optional.hpp>
11 #include <boost/preprocessor/control/iif.hpp>
12 #include <boost/detail/lightweight_test.hpp>
13 #include <sstream>
14
main()15 int main() {
16 std::ostringstream ok;
17
18 a aa; // Test call to derived out-most leaf.
19 s_type s; s.value = "X"; // So body will throw.
20 out.str("");
21 boost::optional<result_type&> r;
22 try {
23 r = aa.f(s);
24 BOOST_TEST(false);
25 } catch(except_error const&) {
26 ok.str(""); ok
27 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
28 << "d::static_inv" << std::endl
29 << "d::inv" << std::endl
30 << "e::static_inv" << std::endl
31 << "e::inv" << std::endl
32 << "c::static_inv" << std::endl
33 << "c::inv" << std::endl
34 << "a::static_inv" << std::endl
35 << "a::inv" << std::endl
36 #endif
37 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
38 << "d::f::pre" << std::endl
39 #endif
40 #ifndef BOOST_CONTRACT_NO_OLDS
41 << "d::f::old" << std::endl
42 << "e::f::old" << std::endl
43 << "c::f::old" << std::endl
44 << "a::f::old" << std::endl
45 #endif
46 << "a::f::body" << std::endl
47 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
48 << "d::static_inv" << std::endl
49 << "d::inv" << std::endl
50 << "e::static_inv" << std::endl
51 << "e::inv" << std::endl
52 << "c::static_inv" << std::endl
53 << "c::inv" << std::endl
54 << "a::static_inv" << std::endl
55 << "a::inv" << std::endl
56 #endif
57 #ifndef BOOST_CONTRACT_NO_EXCEPTS
58 << "d::f::old" << std::endl
59 << "d::f::except" << std::endl
60 << "e::f::old" << std::endl
61 << "e::f::except" << std::endl
62 << "c::f::old" << std::endl
63 << "c::f::except" << std::endl
64 // No old call here because not a base object.
65 << "a::f::except" << std::endl
66 #endif
67 ;
68 BOOST_TEST(out.eq(ok.str()));
69
70 #ifndef BOOST_CONTRACT_NO_OLDS
71 #define BOOST_CONTRACT_TEST_old 1u
72 #else
73 #define BOOST_CONTRACT_TEST_old 0u
74 #endif
75
76 BOOST_TEST(!r); // Boost.Optional result not init (as body threw).
77 BOOST_TEST_EQ(s.value, "X");
78 BOOST_TEST_EQ(s.copies(), BOOST_CONTRACT_TEST_old * 4);
79 BOOST_TEST_EQ(s.evals(), BOOST_CONTRACT_TEST_old * 4);
80 BOOST_TEST_EQ(s.ctors(), s.dtors() + 1); // 1 for local var.
81
82 BOOST_TEST_EQ(aa.x.value, "a");
83 BOOST_TEST_EQ(aa.x.copies(), BOOST_CONTRACT_TEST_old);
84 BOOST_TEST_EQ(aa.x.evals(), BOOST_CONTRACT_TEST_old);
85 BOOST_TEST_EQ(aa.x.ctors(), aa.x.dtors() + 1); // 1 for member var.
86
87 BOOST_TEST_EQ(aa.y.value, "c");
88 BOOST_TEST_EQ(aa.y.copies(), BOOST_CONTRACT_TEST_old);
89 BOOST_TEST_EQ(aa.y.evals(), BOOST_CONTRACT_TEST_old);
90 BOOST_TEST_EQ(aa.y.ctors(), aa.y.dtors() + 1); // 1 for member var.
91
92 BOOST_TEST_EQ(aa.t<'d'>::z.value, "d");
93 BOOST_TEST_EQ(aa.t<'d'>::z.copies(), BOOST_CONTRACT_TEST_old);
94 BOOST_TEST_EQ(aa.t<'d'>::z.evals(), BOOST_CONTRACT_TEST_old);
95 BOOST_TEST_EQ(aa.t<'d'>::z.ctors(), aa.t<'d'>::z.dtors() + 1); // 1 mem.
96
97 BOOST_TEST_EQ(aa.t<'e'>::z.value, "e");
98 BOOST_TEST_EQ(aa.t<'e'>::z.copies(), BOOST_CONTRACT_TEST_old);
99 BOOST_TEST_EQ(aa.t<'e'>::z.evals(), BOOST_CONTRACT_TEST_old);
100 BOOST_TEST_EQ(aa.t<'e'>::z.ctors(), aa.t<'e'>::z.dtors() + 1); // 1 mem.
101
102 #undef BOOST_CONTRACT_TEST_old
103 }
104 return boost::report_errors();
105 }
106
107