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