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