mirror of
https://github.com/boostorg/contract.git
synced 2026-02-27 04:52:22 +00:00
added .old(...)
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
// Test free function contracts.
|
||||
|
||||
#include "../aux_/oteststream.hpp"
|
||||
#include "../aux_/cpcnt.hpp"
|
||||
#include "../aux_/counter.hpp"
|
||||
#include <boost/contract/function.hpp>
|
||||
#include <boost/contract/oldof.hpp>
|
||||
#include <boost/contract/assert.hpp>
|
||||
@@ -13,20 +13,23 @@
|
||||
|
||||
boost::contract::aux::test::oteststream out;
|
||||
|
||||
struct x_tag; typedef boost::contract::aux::test::cpcnt<x_tag, int> x_type;
|
||||
struct y_tag; typedef boost::contract::aux::test::cpcnt<y_tag, int> y_type;
|
||||
struct x_tag; typedef boost::contract::aux::test::counter<x_tag, int> x_type;
|
||||
struct y_tag; typedef boost::contract::aux::test::counter<y_tag, int> y_type;
|
||||
|
||||
bool swap(x_type& x, y_type& y) {
|
||||
bool result;
|
||||
boost::shared_ptr<x_type const> old_x =
|
||||
BOOST_CONTRACT_OLDOF(x_type::eval(x));
|
||||
boost::shared_ptr<y_type const> old_y =
|
||||
BOOST_CONTRACT_OLDOF(y_type::eval(y));
|
||||
boost::shared_ptr<y_type const> old_y;
|
||||
boost::contract::guard c = boost::contract::function()
|
||||
.precondition([&] {
|
||||
out << "swap::pre" << std::endl;
|
||||
BOOST_CONTRACT_ASSERT(x.value != y.value);
|
||||
})
|
||||
.old([&] {
|
||||
out << "swap::old" << std::endl;
|
||||
old_y = BOOST_CONTRACT_OLDOF(y_type::eval(y));
|
||||
})
|
||||
.postcondition([&] {
|
||||
out << "swap::post" << std::endl;
|
||||
BOOST_CONTRACT_ASSERT(x.value == old_y->value);
|
||||
@@ -34,6 +37,7 @@ bool swap(x_type& x, y_type& y) {
|
||||
BOOST_CONTRACT_ASSERT(result == (old_x->value != old_y->value));
|
||||
})
|
||||
;
|
||||
|
||||
out << "swap::body" << std::endl;
|
||||
if(x.value == y.value) return result = false;
|
||||
int save_x = x.value;
|
||||
@@ -45,24 +49,32 @@ bool swap(x_type& x, y_type& y) {
|
||||
int main() {
|
||||
std::ostringstream ok;
|
||||
|
||||
x_type x; x.value = 123;
|
||||
y_type y; y.value = 456;
|
||||
{
|
||||
x_type x; x.value = 123;
|
||||
y_type y; y.value = 456;
|
||||
|
||||
out.str("");
|
||||
bool r = swap(x, y);
|
||||
ok.str(""); ok
|
||||
<< "swap::pre" << std::endl
|
||||
<< "swap::body" << std::endl
|
||||
<< "swap::post" << std::endl
|
||||
;
|
||||
BOOST_TEST(out.eq(ok.str()));
|
||||
out.str("");
|
||||
bool r = swap(x, y);
|
||||
ok.str(""); ok
|
||||
<< "swap::pre" << std::endl
|
||||
<< "swap::old" << std::endl
|
||||
<< "swap::body" << std::endl
|
||||
<< "swap::post" << std::endl
|
||||
;
|
||||
BOOST_TEST(out.eq(ok.str()));
|
||||
|
||||
BOOST_TEST(r);
|
||||
BOOST_TEST_EQ(x.value, 456);
|
||||
BOOST_TEST_EQ(y.value, 123);
|
||||
}
|
||||
|
||||
BOOST_TEST_EQ(x_type::copies(), 1);
|
||||
BOOST_TEST_EQ(x_type::evals(), 1);
|
||||
BOOST_TEST_EQ(x_type::ctors(), x_type::dtors()); // No leak.
|
||||
|
||||
BOOST_TEST(r);
|
||||
BOOST_TEST_EQ(x.value, 456);
|
||||
BOOST_TEST_EQ(y.value, 123);
|
||||
|
||||
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(y_type::copies(), 1);
|
||||
BOOST_TEST_EQ(y_type::evals(), 1);
|
||||
BOOST_TEST_EQ(y_type::ctors(), y_type::dtors()); // No leak.
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user