123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418 |
- // Copyright (C) 2008-2018 Lorenzo Caminiti
- // Distributed under the Boost Software License, Version 1.0 (see accompanying
- // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
- // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
- // Test subcontracting with sparse and complex inheritance graph.
- #include "../detail/oteststream.hpp"
- #include <boost/contract/public_function.hpp>
- #include <boost/contract/assert.hpp>
- #include <boost/contract/check.hpp>
- #include <boost/contract/override.hpp>
- #include <boost/contract/base_types.hpp>
- #include <boost/detail/lightweight_test.hpp>
- #include <sstream>
- boost::contract::test::detail::oteststream out;
- struct j {
- static void static_invariant() { out << "j::static_inv" << std::endl; }
- void invariant() const { out << "j::inv" << std::endl; }
- virtual void f(char ch, boost::contract::virtual_* v = 0) {
- boost::contract::check c = boost::contract::public_function(v, this)
- .precondition([&] {
- out << "j::f::pre" << std::endl;
- BOOST_CONTRACT_ASSERT(ch == 'j');
- })
- .old([] { out << "j::f::old" << std::endl; })
- .postcondition([] { out << "j::f::post" << std::endl; })
- ;
- out << "j::f::body" << std::endl;
- }
- };
- struct i {
- static void static_invariant() { out << "i::static_inv" << std::endl; }
- void invariant() const { out << "i::inv" << std::endl; }
- virtual void f(char ch, boost::contract::virtual_* v = 0) {
- boost::contract::check c = boost::contract::public_function(v, this)
- .precondition([&] {
- out << "i::f::pre" << std::endl;
- BOOST_CONTRACT_ASSERT(ch == 'i');
- })
- .old([] { out << "i::f::old" << std::endl; })
- .postcondition([] { out << "i::f::post" << std::endl; })
- ;
- out << "i::f::body" << std::endl;
- }
- };
- struct k {};
- struct h
- #define BASES public j
- : BASES
- {
- typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
- #undef BASES
-
- static void static_invariant() { out << "h::static_inv" << std::endl; }
- void invariant() const { out << "h::inv" << std::endl; }
- virtual void f(char ch, boost::contract::virtual_* v = 0) /* override */ {
- boost::contract::check c = boost::contract::public_function<
- override_f>(v, &h::f, this, ch)
- .precondition([&] {
- out << "h::f::pre" << std::endl;
- BOOST_CONTRACT_ASSERT(ch == 'h');
- })
- .old([] { out << "h::f::old" << std::endl; })
- .postcondition([] { out << "h::f::post" << std::endl; })
- ;
- out << "h::f::body" << std::endl;
- }
- BOOST_CONTRACT_OVERRIDE(f)
- };
- struct e
- #define BASES public virtual i
- : BASES
- {
- typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
- #undef BASES
-
- static void static_invariant() { out << "e::static_inv" << std::endl; }
- void invariant() const { out << "e::inv" << std::endl; }
- virtual void f(char ch, boost::contract::virtual_* v = 0) /* override */ {
- boost::contract::check c = boost::contract::public_function<
- override_f>(v, &e::f, this, ch)
- .precondition([&] {
- out << "e::f::pre" << std::endl;
- BOOST_CONTRACT_ASSERT(ch == 'e');
- })
- .old([] { out << "e::f::old" << std::endl; })
- .postcondition([] { out << "e::f::post" << std::endl; })
- ;
- out << "e::f::body" << std::endl;
- }
- BOOST_CONTRACT_OVERRIDE(f)
- };
- struct d
- #define BASES public k, virtual public i
- : BASES
- {
- typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
- #undef BASES
-
- static void static_invariant() { out << "d::static_inv" << std::endl; }
- void invariant() const { out << "d::inv" << std::endl; }
- };
- struct c {
- static void static_invariant() { out << "c::static_inv" << std::endl; }
- void invariant() const { out << "c::inv" << std::endl; }
-
- virtual void f(char ch, boost::contract::virtual_* v = 0) {
- boost::contract::check c = boost::contract::public_function(v, this)
- .precondition([&] {
- out << "c::f::pre" << std::endl;
- BOOST_CONTRACT_ASSERT(ch == 'c');
- })
- .old([] { out << "c::f::old" << std::endl; })
- .postcondition([] { out << "c::f::post" << std::endl; })
- ;
- out << "c::f::body" << std::endl;
- }
- };
- struct b
- #define BASES public c, public d
- : BASES
- {
- typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
- #undef BASES
-
- static void static_invariant() { out << "b::static_inv" << std::endl; }
- void invariant() const { out << "b::inv" << std::endl; }
- };
- struct x {};
- struct y {};
- struct z {};
- struct a
- #define BASES public b, public x, public e, protected y, public h, \
- private z
- : BASES
- {
- typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
- #undef BASES
-
- static void static_invariant() { out << "a::static_inv" << std::endl; }
- void invariant() const { out << "a::inv" << std::endl; }
- virtual void f(char ch, boost::contract::virtual_* v = 0) /* override */ {
- boost::contract::check c = boost::contract::public_function<
- override_f>(v, &a::f, this, ch)
- .precondition([&] {
- out << "a::f::pre" << std::endl;
- BOOST_CONTRACT_ASSERT(ch == 'a');
- })
- .old([] { out << "a::f::old" << std::endl; })
- .postcondition([] { out << "a::f::post" << std::endl; })
- ;
- out << "a::f::body" << std::endl;
- }
- BOOST_CONTRACT_OVERRIDE(f)
- };
- int main() {
- std::ostringstream ok;
- a aa;
- out.str("");
- aa.f('a');
- ok.str(""); ok
- #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
- << "c::static_inv" << std::endl
- << "c::inv" << std::endl
- << "i::static_inv" << std::endl
- << "i::inv" << std::endl
- << "e::static_inv" << std::endl
- << "e::inv" << std::endl
- << "j::static_inv" << std::endl
- << "j::inv" << std::endl
- << "h::static_inv" << std::endl
- << "h::inv" << std::endl
- << "a::static_inv" << std::endl
- << "a::inv" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
- << "c::f::pre" << std::endl
- << "i::f::pre" << std::endl
- << "e::f::pre" << std::endl
- << "j::f::pre" << std::endl
- << "h::f::pre" << std::endl
- << "a::f::pre" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_OLDS
- << "c::f::old" << std::endl
- << "i::f::old" << std::endl
- << "e::f::old" << std::endl
- << "j::f::old" << std::endl
- << "h::f::old" << std::endl
- << "a::f::old" << std::endl
- #endif
- << "a::f::body" << std::endl
- #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
- << "c::static_inv" << std::endl
- << "c::inv" << std::endl
- << "i::static_inv" << std::endl
- << "i::inv" << std::endl
- << "e::static_inv" << std::endl
- << "e::inv" << std::endl
- << "j::static_inv" << std::endl
- << "j::inv" << std::endl
- << "h::static_inv" << std::endl
- << "h::inv" << std::endl
- << "a::static_inv" << std::endl
- << "a::inv" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
- << "c::f::old" << std::endl
- << "c::f::post" << std::endl
- << "i::f::old" << std::endl
- << "i::f::post" << std::endl
- << "e::f::old" << std::endl
- << "e::f::post" << std::endl
- << "j::f::old" << std::endl
- << "j::f::post" << std::endl
- << "h::f::old" << std::endl
- << "h::f::post" << std::endl
- // No old call here because not a base object.
- << "a::f::post" << std::endl
- #endif
- ;
- BOOST_TEST(out.eq(ok.str()));
- c cc;
- out.str("");
- cc.f('c');
- ok.str(""); ok
- #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
- << "c::static_inv" << std::endl
- << "c::inv" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
- << "c::f::pre" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_OLDS
- << "c::f::old" << std::endl
- #endif
- << "c::f::body" << std::endl
- #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
- << "c::static_inv" << std::endl
- << "c::inv" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
- // No old call here because not a base object.
- << "c::f::post" << std::endl
- #endif
- ;
- BOOST_TEST(out.eq(ok.str()));
-
- d dd;
- out.str("");
- dd.f('i'); // d's f inherited from i.
- ok.str(""); ok
- #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
- << "i::static_inv" << std::endl
- << "i::inv" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
- << "i::f::pre" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_OLDS
- << "i::f::old" << std::endl
- #endif
- << "i::f::body" << std::endl
- #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
- << "i::static_inv" << std::endl
- << "i::inv" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
- // No old call here because not a base object.
- << "i::f::post" << std::endl
- #endif
- ;
- BOOST_TEST(out.eq(ok.str()));
- e ee;
- out.str("");
- ee.f('e');
- ok.str(""); ok
- #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
- << "i::static_inv" << std::endl
- << "i::inv" << std::endl
- << "e::static_inv" << std::endl
- << "e::inv" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
- << "i::f::pre" << std::endl
- << "e::f::pre" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_OLDS
- << "i::f::old" << std::endl
- << "e::f::old" << std::endl
- #endif
- << "e::f::body" << std::endl
- #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
- << "i::static_inv" << std::endl
- << "i::inv" << std::endl
- << "e::static_inv" << std::endl
- << "e::inv" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
- << "i::f::old" << std::endl
- << "i::f::post" << std::endl
- // No old call here because not a base object.
- << "e::f::post" << std::endl
- #endif
- ;
- BOOST_TEST(out.eq(ok.str()));
- i ii;
- out.str("");
- ii.f('i');
- ok.str(""); ok
- #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
- << "i::static_inv" << std::endl
- << "i::inv" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
- << "i::f::pre" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_OLDS
- << "i::f::old" << std::endl
- #endif
- << "i::f::body" << std::endl
- #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
- << "i::static_inv" << std::endl
- << "i::inv" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
- // No old call here because not a base object.
- << "i::f::post" << std::endl
- #endif
- ;
- BOOST_TEST(out.eq(ok.str()));
- h hh;
- out.str("");
- hh.f('h');
- ok.str(""); ok
- #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
- << "j::static_inv" << std::endl
- << "j::inv" << std::endl
- << "h::static_inv" << std::endl
- << "h::inv" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
- << "j::f::pre" << std::endl
- << "h::f::pre" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_OLDS
- << "j::f::old" << std::endl
- << "h::f::old" << std::endl
- #endif
- << "h::f::body" << std::endl
- #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
- << "j::static_inv" << std::endl
- << "j::inv" << std::endl
- << "h::static_inv" << std::endl
- << "h::inv" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
- << "j::f::old" << std::endl
- << "j::f::post" << std::endl
- // No old call here because not a base object.
- << "h::f::post" << std::endl
- #endif
- ;
- BOOST_TEST(out.eq(ok.str()));
- j jj;
- out.str("");
- jj.f('j');
- ok.str(""); ok
- #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
- << "j::static_inv" << std::endl
- << "j::inv" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
- << "j::f::pre" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_OLDS
- << "j::f::old" << std::endl
- #endif
- << "j::f::body" << std::endl
- #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
- << "j::static_inv" << std::endl
- << "j::inv" << std::endl
- #endif
- #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
- // No old call here because not a base object.
- << "j::f::post" << std::endl
- #endif
- ;
- BOOST_TEST(out.eq(ok.str()));
- return boost::report_errors();
- }
|