• 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 making all contract extra declarations (base types, inv, etc.) private.
8 
9 #include "../detail/oteststream.hpp"
10 #include <boost/contract/constructor.hpp>
11 #include <boost/contract/base_types.hpp>
12 #include <boost/contract/check.hpp>
13 #include <boost/detail/lightweight_test.hpp>
14 #include <sstream>
15 
16 boost::contract::test::detail::oteststream out;
17 
18 class b
19     #define BASES private boost::contract::constructor_precondition<b>
20     : BASES
21 {
22     friend class boost::contract::access;
23 
24     typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
25     #undef BASES
26 
static_invariant()27     static void static_invariant() { out << "b::static_inv" << std::endl; }
invariant() const28     void invariant() const { out << "b::inv" << std::endl; }
29 
30 public:
b()31     b() : boost::contract::constructor_precondition<b>([] {
32         out << "b::ctor::pre" << std::endl;
33     }) {
34         boost::contract::check c = boost::contract::constructor(this)
__anon54ddee4a0202null35             .old([] { out << "b::ctor::old" << std::endl; })
__anon54ddee4a0302null36             .postcondition([] { out << "b::ctor::post" << std::endl; })
37         ;
38         out << "b::ctor::body" << std::endl;
39     }
40 };
41 
42 class a
43     #define BASES private boost::contract::constructor_precondition<a>, public b
44     : BASES
45 {
46     friend class boost::contract::access;
47 
48     // Private base types (always OK because never used by ctors).
49     typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
50     #undef BASES
51 
52     // Private invariants.
static_invariant()53     static void static_invariant() { out << "a::static_inv" << std::endl; }
invariant() const54     void invariant() const { out << "a::inv" << std::endl; }
55 
56 public:
a()57     a() : boost::contract::constructor_precondition<a>([] {
58         out << "a::ctor::pre" << std::endl;
59     }) {
60         boost::contract::check c = boost::contract::constructor(this)
__anon54ddee4a0502null61             .old([] { out << "a::ctor::old" << std::endl; })
__anon54ddee4a0602null62             .postcondition([] { out << "a::ctor::post" << std::endl; })
63         ;
64         out << "a::ctor::body" << std::endl;
65     }
66 };
67 
main()68 int main() {
69     std::ostringstream ok;
70 
71     out.str("");
72     a aa;
73     ok.str(""); ok
74         #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
75             << "a::ctor::pre" << std::endl
76             << "b::ctor::pre" << std::endl
77         #endif
78 
79         #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
80             << "b::static_inv" << std::endl
81         #endif
82         #ifndef BOOST_CONTRACT_NO_OLDS
83             << "b::ctor::old" << std::endl
84         #endif
85         << "b::ctor::body" << std::endl
86         #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
87             << "b::static_inv" << std::endl
88             << "b::inv" << std::endl
89         #endif
90         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
91             << "b::ctor::post" << std::endl
92         #endif
93 
94         #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
95             << "a::static_inv" << std::endl
96         #endif
97         #ifndef BOOST_CONTRACT_NO_OLDS
98             << "a::ctor::old" << std::endl
99         #endif
100         << "a::ctor::body" << std::endl
101         #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
102             << "a::static_inv" << std::endl
103             << "a::inv" << std::endl
104         #endif
105         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
106             << "a::ctor::post" << std::endl
107         #endif
108     ;
109     BOOST_TEST(out.eq(ok.str()));
110 
111     out.str("");
112     b bb;
113     ok.str(""); ok
114         #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
115             << "b::ctor::pre" << std::endl
116         #endif
117         #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
118             << "b::static_inv" << std::endl
119         #endif
120         #ifndef BOOST_CONTRACT_NO_OLDS
121             << "b::ctor::old" << std::endl
122         #endif
123         << "b::ctor::body" << std::endl
124         #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
125             << "b::static_inv" << std::endl
126             << "b::inv" << std::endl
127         #endif
128         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
129             << "b::ctor::post" << std::endl
130         #endif
131     ;
132     BOOST_TEST(out.eq(ok.str()));
133 
134     return boost::report_errors();
135 }
136 
137