• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 virt pub func body throws with subcontr from middle of inheritance tree.
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     c cc; // Test call to class at mid- inheritance tree (as base with bases).
19     s_type s; s.value = "X"; // So body will throw.
20     out.str("");
21     boost::optional<result_type&> r;
22     try {
23         r = cc.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             #endif
35             #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
36                 << "d::f::pre" << std::endl
37             #endif
38             #ifndef BOOST_CONTRACT_NO_OLDS
39                 << "d::f::old" << std::endl
40                 << "e::f::old" << std::endl
41                 << "c::f::old" << std::endl
42             #endif
43             << "c::f::body" << std::endl
44             #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
45                 << "d::static_inv" << std::endl
46                 << "d::inv" << std::endl
47                 << "e::static_inv" << std::endl
48                 << "e::inv" << std::endl
49                 << "c::static_inv" << std::endl
50                 << "c::inv" << std::endl
51             #endif
52             #ifndef BOOST_CONTRACT_NO_EXCEPTS
53                 << "d::f::old" << std::endl
54                 << "d::f::except" << std::endl
55                 << "e::f::old" << std::endl
56                 << "e::f::except" << std::endl
57                 // No old call here because not a base object.
58                 << "c::f::except" << std::endl
59             #endif
60         ;
61         BOOST_TEST(out.eq(ok.str()));
62 
63         #ifndef BOOST_CONTRACT_NO_OLDS
64             #define BOOST_CONTRACT_TEST_old 1u
65         #else
66             #define BOOST_CONTRACT_TEST_old 0u
67         #endif
68 
69         BOOST_TEST(!r); // Boost.Optional result not init (as body threw).
70         BOOST_TEST_EQ(s.value, "X");
71         BOOST_TEST_EQ(s.copies(), BOOST_CONTRACT_TEST_old * 3);
72         BOOST_TEST_EQ(s.evals(), BOOST_CONTRACT_TEST_old * 3);
73         BOOST_TEST_EQ(s.ctors(), s.dtors() + 1); // 1 for local var.
74 
75         BOOST_TEST_EQ(cc.y.value, "c");
76         BOOST_TEST_EQ(cc.y.copies(), BOOST_CONTRACT_TEST_old);
77         BOOST_TEST_EQ(cc.y.evals(), BOOST_CONTRACT_TEST_old);
78         BOOST_TEST_EQ(cc.y.ctors(), cc.y.dtors() + 1); // 1 for member var.
79 
80         BOOST_TEST_EQ(cc.t<'d'>::z.value, "d");
81         BOOST_TEST_EQ(cc.t<'d'>::z.copies(), BOOST_CONTRACT_TEST_old);
82         BOOST_TEST_EQ(cc.t<'d'>::z.evals(), BOOST_CONTRACT_TEST_old);
83         BOOST_TEST_EQ(cc.t<'d'>::z.ctors(), cc.t<'d'>::z.dtors() + 1); // 1 mem.
84 
85         BOOST_TEST_EQ(cc.t<'e'>::z.value, "e");
86         BOOST_TEST_EQ(cc.t<'e'>::z.copies(), BOOST_CONTRACT_TEST_old);
87         BOOST_TEST_EQ(cc.t<'e'>::z.evals(), BOOST_CONTRACT_TEST_old);
88         BOOST_TEST_EQ(cc.t<'e'>::z.ctors(), cc.t<'e'>::z.dtors() + 1); // 1 mem.
89 
90         #undef BOOST_CONTRACT_TEST_old
91     }
92     return boost::report_errors();
93 }
94 
95