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 function subcontracting via virtual functions.
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;
18 c& ca = aa; // Test polymorphic virtual call (via reference to base c).
19 s_type s; s.value = "A";
20 out.str("");
21 result_type& r = ca.f(s);
22
23 ok.str(""); ok
24 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
25 << "d::static_inv" << std::endl
26 << "d::inv" << std::endl
27 << "e::static_inv" << std::endl
28 << "e::inv" << std::endl
29 << "c::static_inv" << std::endl
30 << "c::inv" << std::endl
31 << "a::static_inv" << std::endl
32 << "a::inv" << std::endl
33 #endif
34 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
35 << "d::f::pre" << std::endl
36 << "e::f::pre" << std::endl
37 << "c::f::pre" << std::endl
38 << "a::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_POSTCONDITIONS
58 << "d::f::old" << std::endl
59 << "d::f::post" << std::endl
60 << "e::f::old" << std::endl
61 << "e::f::post" << std::endl
62 << "c::f::old" << std::endl
63 << "c::f::post" << std::endl
64 // No old call here because not a base object.
65 << "a::f::post" << 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_EQ(r.value, "A");
77 BOOST_TEST_EQ(s.value, "acde");
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 local var.
81
82 // Cannot access x via ca, but only via aa.
83 BOOST_TEST_EQ(aa.x.value, "aA");
84 BOOST_TEST_EQ(aa.x.copies(), BOOST_CONTRACT_TEST_old);
85 BOOST_TEST_EQ(aa.x.evals(), BOOST_CONTRACT_TEST_old);
86 BOOST_TEST_EQ(aa.x.ctors(), aa.x.dtors() + 1); // 1 data member.
87
88 BOOST_TEST_EQ(ca.y.value, "cA");
89 BOOST_TEST_EQ(ca.y.copies(), BOOST_CONTRACT_TEST_old);
90 BOOST_TEST_EQ(ca.y.evals(), BOOST_CONTRACT_TEST_old);
91 BOOST_TEST_EQ(ca.y.ctors(), ca.y.dtors() + 1); // 1 data member.
92
93 BOOST_TEST_EQ(ca.t<'d'>::z.value, "dA");
94 BOOST_TEST_EQ(ca.t<'d'>::z.copies(), BOOST_CONTRACT_TEST_old);
95 BOOST_TEST_EQ(ca.t<'d'>::z.evals(), BOOST_CONTRACT_TEST_old);
96 BOOST_TEST_EQ(ca.t<'d'>::z.ctors(), ca.t<'d'>::z.dtors() + 1); // 1 member.
97
98 BOOST_TEST_EQ(ca.t<'e'>::z.value, "eA");
99 BOOST_TEST_EQ(ca.t<'e'>::z.copies(), BOOST_CONTRACT_TEST_old);
100 BOOST_TEST_EQ(ca.t<'e'>::z.evals(), BOOST_CONTRACT_TEST_old);
101 BOOST_TEST_EQ(ca.t<'e'>::z.ctors(), ca.t<'e'>::z.dtors() + 1); // 1 member.
102
103 #undef BOOST_CONTRACT_TEST_old
104 return boost::report_errors();
105 }
106
107