mirror of
https://github.com/boostorg/contract.git
synced 2026-02-27 17:02:15 +00:00
started to implement bind for free and member functions, but without subcontracting yet
This commit is contained in:
@@ -1,27 +1,33 @@
|
||||
|
||||
#include "../aux_/oteststream.hpp"
|
||||
#include <iostream>
|
||||
#include <boost/contract/oldof.hpp>
|
||||
#include <boost/contract/free_function.hpp>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
#include <sstream>
|
||||
#include <boost/contract/assert.hpp>
|
||||
#include <boost/contract/scoped.hpp>
|
||||
#include <boost/contract/bind.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
// Test free-function.
|
||||
|
||||
boost::contract::aux::test::oteststream out;
|
||||
|
||||
void inv1_contract(int const& x, int const& result,
|
||||
boost::contract::decl c = 0) {
|
||||
void inv1_contract(int const& x, int const& result, boost::contract::call c) {
|
||||
boost::shared_ptr<int const> old_x = BOOST_CONTRACT_OLDOF(c, x);
|
||||
boost::contract::free_function(c)
|
||||
.precondition([&] { BOOST_CONTRACT_ASSERT(x >= 0); })
|
||||
.precondition([&] {
|
||||
std::clog << "inv1::pre" << std::endl;
|
||||
BOOST_CONTRACT_ASSERT(x >= 0);
|
||||
})
|
||||
.postcondition([&] {
|
||||
std::clog << "inv1::post" << std::endl;
|
||||
BOOST_CONTRACT_ASSERT(-x == *old_x);
|
||||
BOOST_CONTRACT_ASSERT(result == *old_x);
|
||||
})
|
||||
;
|
||||
}
|
||||
int inv1(int& x) {
|
||||
std::clog << "inv1" << std::endl;
|
||||
int result;
|
||||
boost::contract::var contract(inv1_contract, x, result);
|
||||
boost::contract::scoped contract = boost::contract::bind(
|
||||
&inv1_contract, x, result);
|
||||
|
||||
std::clog << "inv1::body" << std::endl;
|
||||
result = x;
|
||||
x = -x;
|
||||
return result;
|
||||
@@ -30,32 +36,31 @@ int inv1(int& x) {
|
||||
int inv2(int& x) {
|
||||
int result;
|
||||
boost::shared_ptr<int const> old_x = BOOST_CONTRACT_OLDOF(x);
|
||||
boost::contract::var contract = boost::contract::free_function()
|
||||
.precondition([&] { BOOST_CONTRACT_ASSERT(x >= 0); })
|
||||
boost::contract::scoped contract = boost::contract::free_function()
|
||||
.precondition([&] {
|
||||
std::clog << "inv2::pre" << std::endl;
|
||||
BOOST_CONTRACT_ASSERT(x >= 0);
|
||||
})
|
||||
.postcondition([&] {
|
||||
std::clog << "inv2::post" << std::endl;
|
||||
BOOST_CONTRACT_ASSERT(-x == *old_x);
|
||||
BOOST_CONTRACT_ASSERT(result == *old_x);
|
||||
})
|
||||
;
|
||||
|
||||
std::clog << "inv2::body" << std::endl;
|
||||
result = x;
|
||||
x = -x;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
std::ostringstream ok;
|
||||
int x = 123;
|
||||
inv1(x);
|
||||
std::clog << std::endl;
|
||||
|
||||
out.str("");
|
||||
f();
|
||||
ok.str("");
|
||||
ok
|
||||
<< "f::pre" << std::endl
|
||||
<< "f::body" << std::endl
|
||||
<< "f::post" << std::endl
|
||||
;
|
||||
BOOST_TEST(out.check(ok.str()));
|
||||
|
||||
return boost::report_errors();
|
||||
x = 456;
|
||||
inv2(x);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user