mirror of
https://github.com/boostorg/contract.git
synced 2026-02-27 04:52:22 +00:00
finshed a first round of test, starting examples
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
#include <boost/contract/base_types.hpp>
|
||||
#include <boost/contract/assert.hpp>
|
||||
#include <boost/contract/oldof.hpp>
|
||||
#include <boost/contract/var.hpp>
|
||||
#include <boost/contract/scoped.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/ref.hpp>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
@@ -25,51 +25,50 @@ struct t
|
||||
|
||||
void invariant() const {
|
||||
out << Id << "::inv" << std::endl;
|
||||
BOOST_CONTRACT_ASSERT(i_ < 0);
|
||||
BOOST_CONTRACT_ASSERT(k_ < 0);
|
||||
}
|
||||
static void static_invariant() {
|
||||
out << Id << "::static_inv" << std::endl;
|
||||
BOOST_CONTRACT_ASSERT(n.value >= 0);
|
||||
BOOST_CONTRACT_ASSERT(l.value >= 0);
|
||||
}
|
||||
|
||||
struct n_tag; typedef boost::contract::aux::test::cpcnt<n_tag, int> n_cnt;
|
||||
static n_cnt n;
|
||||
struct l_tag; typedef boost::contract::aux::test::cpcnt<l_tag, int> l_type;
|
||||
static l_type l;
|
||||
|
||||
struct arg_tag;
|
||||
typedef boost::contract::aux::test::cpcnt<arg_tag, int> arg_cnt;
|
||||
struct z_tag; typedef boost::contract::aux::test::cpcnt<z_tag, int> z_type;
|
||||
|
||||
// MSVC 2010 errors on lambdas in template member initializations...
|
||||
static void constructor_precondition(arg_cnt const& arg) {
|
||||
static void constructor_precondition(z_type const& z) {
|
||||
out << Id << "::ctor::pre" << std::endl;
|
||||
BOOST_CONTRACT_ASSERT(arg.value < 0);
|
||||
BOOST_CONTRACT_ASSERT(z.value < 0);
|
||||
}
|
||||
explicit t(arg_cnt& arg) :
|
||||
explicit t(z_type& z) :
|
||||
boost::contract::constructor_precondition<t<Id> >(
|
||||
boost::bind(&t::constructor_precondition, boost::cref(arg)))
|
||||
boost::bind(&t::constructor_precondition, boost::cref(z)))
|
||||
{
|
||||
boost::shared_ptr<arg_cnt const> old_arg =
|
||||
BOOST_CONTRACT_OLDOF(arg_cnt::eval(arg));
|
||||
boost::shared_ptr<n_cnt const> old_n =
|
||||
BOOST_CONTRACT_OLDOF(n_cnt::eval(n));
|
||||
boost::contract::var contract = boost::contract::constructor(this)
|
||||
boost::shared_ptr<z_type const> old_z =
|
||||
BOOST_CONTRACT_OLDOF(z_type::eval(z));
|
||||
boost::shared_ptr<l_type const> old_l =
|
||||
BOOST_CONTRACT_OLDOF(l_type::eval(l));
|
||||
boost::contract::scoped c = boost::contract::constructor(this)
|
||||
.postcondition([&] {
|
||||
out << Id << "::ctor::post" << std::endl;
|
||||
BOOST_CONTRACT_ASSERT(i_ == old_arg->value);
|
||||
BOOST_CONTRACT_ASSERT(arg.value == n.value);
|
||||
BOOST_CONTRACT_ASSERT(n.value == old_n->value + 1);
|
||||
BOOST_CONTRACT_ASSERT(k_ == old_z->value);
|
||||
BOOST_CONTRACT_ASSERT(z.value == l.value);
|
||||
BOOST_CONTRACT_ASSERT(l.value == old_l->value + 1);
|
||||
})
|
||||
;
|
||||
out << Id << "::ctor::body" << std::endl;
|
||||
i_ = arg.value;
|
||||
arg.value = ++n.value;
|
||||
k_ = z.value;
|
||||
z.value = ++l.value;
|
||||
}
|
||||
|
||||
virtual ~t() { --n.value; }
|
||||
virtual ~t() { --l.value; }
|
||||
|
||||
private:
|
||||
int i_;
|
||||
int k_;
|
||||
};
|
||||
template<char Id> typename t<Id>::n_cnt t<Id>::n;
|
||||
template<char Id> typename t<Id>::l_type t<Id>::l;
|
||||
|
||||
// Test deep inheritance (2 vertical levels), multiple inheritance (4
|
||||
// horizontal levels), and that all public/protected/private part of
|
||||
@@ -77,7 +76,7 @@ template<char Id> typename t<Id>::n_cnt t<Id>::n;
|
||||
// are part of C++ object construction mechanism).
|
||||
struct c
|
||||
#define BASES private boost::contract::constructor_precondition<c>, \
|
||||
public t<'d'>, protected t<'e'>, private t<'f'>
|
||||
public t<'d'>, protected t<'p'>, private t<'q'>, public t<'e'>
|
||||
: BASES
|
||||
{
|
||||
typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
|
||||
@@ -85,50 +84,49 @@ struct c
|
||||
|
||||
void invariant() const {
|
||||
out << "c::inv" << std::endl;
|
||||
BOOST_CONTRACT_ASSERT(i_ < 0);
|
||||
BOOST_CONTRACT_ASSERT(j_ < 0);
|
||||
}
|
||||
static void static_invariant() {
|
||||
out << "c::static_inv" << std::endl;
|
||||
BOOST_CONTRACT_ASSERT(n.value >= 0);
|
||||
BOOST_CONTRACT_ASSERT(m.value >= 0);
|
||||
}
|
||||
|
||||
struct n_tag; typedef boost::contract::aux::test::cpcnt<n_tag, int> n_cnt;
|
||||
static n_cnt n;
|
||||
struct m_tag; typedef boost::contract::aux::test::cpcnt<m_tag, int> m_type;
|
||||
static m_type m;
|
||||
|
||||
struct arg_tag;
|
||||
typedef boost::contract::aux::test::cpcnt<arg_tag, int> arg_cnt;
|
||||
struct y_tag; typedef boost::contract::aux::test::cpcnt<y_tag, int> y_type;
|
||||
|
||||
explicit c(arg_cnt& arg, t<'d'>::arg_cnt& d_arg, t<'e'>::arg_cnt& e_arg,
|
||||
t<'f'>::arg_cnt& f_arg) :
|
||||
explicit c(y_type& y, t<'d'>::z_type& dz, t<'p'>::z_type& pz,
|
||||
t<'q'>::z_type& qz, t<'e'>::z_type& ez) :
|
||||
boost::contract::constructor_precondition<c>([&] {
|
||||
out << "c::ctor::pre" << std::endl;
|
||||
BOOST_CONTRACT_ASSERT(arg.value < 0);
|
||||
BOOST_CONTRACT_ASSERT(y.value < 0);
|
||||
}),
|
||||
t<'d'>(d_arg), t<'e'>(e_arg), t<'f'>(f_arg)
|
||||
t<'d'>(dz), t<'p'>(pz), t<'q'>(qz), t<'e'>(ez)
|
||||
{
|
||||
boost::shared_ptr<arg_cnt const> old_arg =
|
||||
BOOST_CONTRACT_OLDOF(arg_cnt::eval(arg));
|
||||
boost::shared_ptr<n_cnt const> old_n =
|
||||
BOOST_CONTRACT_OLDOF(n_cnt::eval(n));
|
||||
boost::contract::var contract = boost::contract::constructor(this)
|
||||
boost::shared_ptr<y_type const> old_y =
|
||||
BOOST_CONTRACT_OLDOF(y_type::eval(y));
|
||||
boost::shared_ptr<m_type const> old_m =
|
||||
BOOST_CONTRACT_OLDOF(m_type::eval(m));
|
||||
boost::contract::scoped c = boost::contract::constructor(this)
|
||||
.postcondition([&] {
|
||||
out << "c::ctor::post" << std::endl;
|
||||
BOOST_CONTRACT_ASSERT(i_ == old_arg->value);
|
||||
BOOST_CONTRACT_ASSERT(arg.value == n.value);
|
||||
BOOST_CONTRACT_ASSERT(n.value == old_n->value + 1);
|
||||
BOOST_CONTRACT_ASSERT(j_ == old_y->value);
|
||||
BOOST_CONTRACT_ASSERT(y.value == m.value);
|
||||
BOOST_CONTRACT_ASSERT(m.value == old_m->value + 1);
|
||||
})
|
||||
;
|
||||
out << "c::ctor::body" << std::endl;
|
||||
i_ = arg.value;
|
||||
arg.value = ++n.value;
|
||||
j_ = y.value;
|
||||
y.value = ++m.value;
|
||||
}
|
||||
|
||||
virtual ~c() { --n.value; }
|
||||
virtual ~c() { --m.value; }
|
||||
|
||||
private:
|
||||
int i_;
|
||||
int j_;
|
||||
};
|
||||
c::n_cnt c::n;
|
||||
c::m_type c::m;
|
||||
|
||||
// Test not (fully) contracted base is not part of constructor subcontracting.
|
||||
struct b
|
||||
@@ -163,36 +161,34 @@ struct a
|
||||
BOOST_CONTRACT_ASSERT(n.value >= 0);
|
||||
}
|
||||
|
||||
struct n_tag; typedef boost::contract::aux::test::cpcnt<n_tag, int> n_cnt;
|
||||
static n_cnt n;
|
||||
struct n_tag; typedef boost::contract::aux::test::cpcnt<n_tag, int> n_type;
|
||||
static n_type n;
|
||||
|
||||
struct arg_tag;
|
||||
typedef boost::contract::aux::test::cpcnt<arg_tag, int> arg_cnt;
|
||||
struct x_tag; typedef boost::contract::aux::test::cpcnt<x_tag, int> x_type;
|
||||
|
||||
explicit a(arg_cnt& arg, c::arg_cnt& c_arg, t<'d'>::arg_cnt& d_arg,
|
||||
t<'e'>::arg_cnt& e_arg, t<'f'>::arg_cnt& f_arg) :
|
||||
explicit a(x_type& x, c::y_type& y, t<'d'>::z_type& dz,
|
||||
t<'p'>::z_type& pz, t<'q'>::z_type& qz, t<'e'>::z_type& ez) :
|
||||
boost::contract::constructor_precondition<a>([&] {
|
||||
out << "a::ctor::pre" << std::endl;
|
||||
BOOST_CONTRACT_ASSERT(arg.value < 0);
|
||||
BOOST_CONTRACT_ASSERT(x.value < 0);
|
||||
}),
|
||||
b(),
|
||||
c(c_arg, d_arg, e_arg, f_arg)
|
||||
b(), c(y, dz, pz, qz, ez)
|
||||
{
|
||||
boost::shared_ptr<arg_cnt const> old_arg =
|
||||
BOOST_CONTRACT_OLDOF(arg_cnt::eval(arg));
|
||||
boost::shared_ptr<n_cnt const> old_n =
|
||||
BOOST_CONTRACT_OLDOF(n_cnt::eval(n));
|
||||
boost::contract::var contract = boost::contract::constructor(this)
|
||||
boost::shared_ptr<x_type const> old_x =
|
||||
BOOST_CONTRACT_OLDOF(x_type::eval(x));
|
||||
boost::shared_ptr<n_type const> old_n =
|
||||
BOOST_CONTRACT_OLDOF(n_type::eval(n));
|
||||
boost::contract::scoped c = boost::contract::constructor(this)
|
||||
.postcondition([&] {
|
||||
out << "a::ctor::post" << std::endl;
|
||||
BOOST_CONTRACT_ASSERT(i_ == old_arg->value);
|
||||
BOOST_CONTRACT_ASSERT(arg.value == n.value);
|
||||
BOOST_CONTRACT_ASSERT(i_ == old_x->value);
|
||||
BOOST_CONTRACT_ASSERT(x.value == n.value);
|
||||
BOOST_CONTRACT_ASSERT(n.value == old_n->value + 1);
|
||||
})
|
||||
;
|
||||
out << "a::ctor::body" << std::endl;
|
||||
i_ = arg.value;
|
||||
arg.value = ++n.value;
|
||||
i_ = x.value;
|
||||
x.value = ++n.value;
|
||||
}
|
||||
|
||||
virtual ~a() { --n.value; }
|
||||
@@ -200,32 +196,49 @@ struct a
|
||||
private:
|
||||
int i_;
|
||||
};
|
||||
a::n_cnt a::n;
|
||||
a::n_type a::n;
|
||||
|
||||
int main() {
|
||||
std::ostringstream ok;
|
||||
|
||||
t<'f'>::arg_cnt f_arg; f_arg.value = -5;
|
||||
t<'e'>::arg_cnt e_arg; e_arg.value = -4;
|
||||
t<'d'>::arg_cnt d_arg; d_arg.value = -3;
|
||||
c::arg_cnt c_arg; c_arg.value = -2;
|
||||
a::arg_cnt a_arg; a_arg.value = -1;
|
||||
t<'e'>::z_type ez; ez.value = -5;
|
||||
t<'q'>::z_type qz; qz.value = -5;
|
||||
t<'p'>::z_type pz; pz.value = -4;
|
||||
t<'d'>::z_type dz; dz.value = -3;
|
||||
c::y_type y; y.value = -2;
|
||||
a::x_type x; x.value = -1;
|
||||
|
||||
out.str("");
|
||||
a aa(a_arg, c_arg, d_arg, e_arg, f_arg);
|
||||
ok.str("");
|
||||
ok
|
||||
a aa(x, y, dz, pz, qz, ez);
|
||||
ok.str(""); ok
|
||||
// Test all constructor pre checked first.
|
||||
<< "a::ctor::pre" << std::endl
|
||||
|
||||
<< "c::ctor::pre" << std::endl
|
||||
<< "d::ctor::pre" << std::endl
|
||||
|
||||
// Test static inv, but not const inv, checked before constructor body.
|
||||
<< "d::ctor::pre" << std::endl
|
||||
<< "d::static_inv" << std::endl
|
||||
<< "d::ctor::body" << std::endl
|
||||
<< "d::static_inv" << std::endl
|
||||
<< "d::inv" << std::endl
|
||||
<< "d::ctor::post" << std::endl
|
||||
|
||||
// Test check also protected bases (because part of C++ construction).
|
||||
<< "p::ctor::pre" << std::endl
|
||||
<< "p::static_inv" << std::endl
|
||||
<< "p::ctor::body" << std::endl
|
||||
<< "p::static_inv" << std::endl
|
||||
<< "p::inv" << std::endl
|
||||
<< "p::ctor::post" << std::endl
|
||||
|
||||
// Test check also private bases (because part of C++ construction).
|
||||
<< "q::ctor::pre" << std::endl
|
||||
<< "q::static_inv" << std::endl
|
||||
<< "q::ctor::body" << std::endl
|
||||
<< "q::static_inv" << std::endl
|
||||
<< "q::inv" << std::endl
|
||||
<< "q::ctor::post" << std::endl
|
||||
|
||||
<< "e::ctor::pre" << std::endl
|
||||
<< "e::static_inv" << std::endl
|
||||
@@ -234,13 +247,6 @@ int main() {
|
||||
<< "e::inv" << std::endl
|
||||
<< "e::ctor::post" << std::endl
|
||||
|
||||
<< "f::ctor::pre" << std::endl
|
||||
<< "f::static_inv" << std::endl
|
||||
<< "f::ctor::body" << std::endl
|
||||
<< "f::static_inv" << std::endl
|
||||
<< "f::inv" << std::endl
|
||||
<< "f::ctor::post" << std::endl
|
||||
|
||||
<< "c::static_inv" << std::endl
|
||||
<< "c::ctor::body" << std::endl
|
||||
<< "c::static_inv" << std::endl
|
||||
@@ -255,17 +261,19 @@ int main() {
|
||||
;
|
||||
BOOST_TEST(out.eq(ok.str()));
|
||||
|
||||
BOOST_TEST_EQ(a_arg.copies(), 1); BOOST_TEST_EQ(a_arg.evals(), 1);
|
||||
BOOST_TEST_EQ(c_arg.copies(), 1); BOOST_TEST_EQ(c_arg.evals(), 1);
|
||||
BOOST_TEST_EQ(d_arg.copies(), 1); BOOST_TEST_EQ(d_arg.evals(), 1);
|
||||
BOOST_TEST_EQ(e_arg.copies(), 1); BOOST_TEST_EQ(e_arg.evals(), 1);
|
||||
BOOST_TEST_EQ(f_arg.copies(), 1); BOOST_TEST_EQ(f_arg.evals(), 1);
|
||||
BOOST_TEST_EQ(x.copies(), 1); BOOST_TEST_EQ(x.evals(), 1);
|
||||
BOOST_TEST_EQ(y.copies(), 1); BOOST_TEST_EQ(y.evals(), 1);
|
||||
BOOST_TEST_EQ(dz.copies(), 1); BOOST_TEST_EQ(dz.evals(), 1);
|
||||
BOOST_TEST_EQ(pz.copies(), 1); BOOST_TEST_EQ(pz.evals(), 1);
|
||||
BOOST_TEST_EQ(qz.copies(), 1); BOOST_TEST_EQ(qz.evals(), 1);
|
||||
BOOST_TEST_EQ(ez.copies(), 1); BOOST_TEST_EQ(ez.evals(), 1);
|
||||
|
||||
BOOST_TEST_EQ(a::n.copies(), 1); BOOST_TEST_EQ(a::n.evals(), 1);
|
||||
BOOST_TEST_EQ(c::n.copies(), 1); BOOST_TEST_EQ(c::n.evals(), 1);
|
||||
BOOST_TEST_EQ(t<'d'>::n.copies(), 1); BOOST_TEST_EQ(t<'d'>::n.evals(), 1);
|
||||
BOOST_TEST_EQ(t<'e'>::n.copies(), 1); BOOST_TEST_EQ(t<'e'>::n.evals(), 1);
|
||||
BOOST_TEST_EQ(t<'f'>::n.copies(), 1); BOOST_TEST_EQ(t<'f'>::n.evals(), 1);
|
||||
BOOST_TEST_EQ(c::m.copies(), 1); BOOST_TEST_EQ(c::m.evals(), 1);
|
||||
BOOST_TEST_EQ(t<'d'>::l.copies(), 1); BOOST_TEST_EQ(t<'d'>::l.evals(), 1);
|
||||
BOOST_TEST_EQ(t<'p'>::l.copies(), 1); BOOST_TEST_EQ(t<'p'>::l.evals(), 1);
|
||||
BOOST_TEST_EQ(t<'q'>::l.copies(), 1); BOOST_TEST_EQ(t<'q'>::l.evals(), 1);
|
||||
BOOST_TEST_EQ(t<'e'>::l.copies(), 1); BOOST_TEST_EQ(t<'e'>::l.evals(), 1);
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user