finished very first draft of reference docs

This commit is contained in:
Lorenzo Caminiti
2016-05-18 19:51:45 -07:00
parent 1aa41fd14e
commit 3fef54b33e
36 changed files with 1981 additions and 431 deletions

View File

@@ -187,6 +187,9 @@ test-suite function
[ subdir-run-with-no function : body_throw ]
[ subdir-run-with-no function : old_throw ]
[ subdir-run-with-no function : check_if ]
[ subdir-compile-fail function : no_check_if_error ]
;
test-suite result
@@ -202,13 +205,13 @@ test-suite old
[ subdir-run-with-no old : auto ]
[ subdir-run-with-no old : no_macros ]
[ subdir-run-with-no old : no_macros_noncopyable ]
[ subdir-compile-fail old : no_make_old_error ]
[ subdir-compile-fail old : no_make_old_noncopyable_error ]
[ subdir-run-with-no old : noncopyable ]
[ subdir-compile-fail old : noncopyable_error ]
[ subdir-run-with-no old : no_equal ]
[ subdir-compile-fail old : no_equal_error ]
;
test-suite disable

View File

@@ -0,0 +1,59 @@
// Test assertions skipped when operations to check them missing (e.g., `==`).
#include <boost/contract/function.hpp>
#include <boost/contract/guard.hpp>
#include <boost/contract/assert.hpp>
#include <boost/contract/call_if.hpp>
#include <boost/bind.hpp>
#include <boost/type_traits/has_equal_to.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <functional>
#include <vector>
unsigned equal_skips;
template<typename T>
void push_back(std::vector<T>& vect, T const& val) {
boost::contract::guard c = boost::contract::function()
.postcondition([&] {
BOOST_CONTRACT_ASSERT(
boost::contract::call_if<boost::has_equal_to<T> >(
boost::bind(std::equal_to<T>(), boost::cref(vect.back()),
boost::cref(val))
).else_([] { ++equal_skips; return true; })
);
})
;
vect.push_back(val);
}
struct j { // Type without operator==.
explicit j(int i) : j_(i) {}
private:
int j_;
};
int main() {
std::vector<int> vi;
equal_skips = 0;
push_back(vi, 123);
BOOST_TEST_EQ(equal_skips, 0);
unsigned const cnt =
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
1
#else
0
#endif
;
j jj(456);
std::vector<j> vj;
equal_skips = 0;
push_back(vj, jj);
BOOST_TEST_EQ(equal_skips, cnt);
return boost::report_errors();
}

View File

@@ -0,0 +1,35 @@
// Test assertion error when operations to check them missing (e.g., `==`).
#include <boost/contract/function.hpp>
#include <boost/contract/guard.hpp>
#include <boost/contract/assert.hpp>
#include <vector>
template<typename T>
void push_back(std::vector<T>& vect, T const& val) {
boost::contract::guard c = boost::contract::function()
.postcondition([&] {
BOOST_CONTRACT_ASSERT(vect.back() == val); // Error (j has no ==).
})
;
vect.push_back(val);
}
struct j { // Type without operator==.
explicit j(int i) : j_(i) {}
private:
int j_;
};
int main() {
std::vector<int> vi;
push_back(vi, 123);
j jj(456);
std::vector<j> vj;
push_back(vj, jj);
return 0;
}

View File

@@ -1,9 +1,8 @@
// Test old value skipped when operations to check them missing (e.g., `==`).
// Test assertions skipped when operations to check them missing (e.g., `==`).
#include <boost/contract/function.hpp>
#include <boost/contract/guard.hpp>
#include <boost/contract/old.hpp>
#include <boost/contract/assert.hpp>
#include <boost/contract/call_if.hpp>
#include <boost/bind.hpp>

View File

@@ -1,9 +1,8 @@
// Test old value error when operations to check them missing (e.g., `==`).
// Test assertion error when operations to check them missing (e.g., `==`).
#include <boost/contract/function.hpp>
#include <boost/contract/guard.hpp>
#include <boost/contract/old.hpp>
#include <boost/contract/assert.hpp>
#include <vector>

View File

@@ -1,182 +1,4 @@
// Test old values without BOOST_CONTRACT_OLDOF macro.
#include "../detail/oteststream.hpp"
#include "../detail/counter.hpp"
#include <boost/contract/function.hpp>
#include <boost/contract/public_function.hpp>
#include <boost/contract/base_types.hpp>
#include <boost/contract/override.hpp>
#include <boost/contract/assert.hpp>
#include <boost/contract/old.hpp>
#include <boost/contract/guard.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <cassert>
boost::contract::test::detail::oteststream out;
struct i_tag; typedef boost::contract::test::detail::counter<i_tag, int> i_type;
struct j_tag; typedef boost::contract::test::detail::counter<j_tag, int> j_type;
struct b {
virtual void swap(i_type& i, j_type& j, boost::contract::virtual_* v = 0);
};
void b::swap(i_type& i, j_type& j, boost::contract::virtual_* v) {
boost::contract::old_ptr<i_type> old_i = boost::contract::make_old(v,
boost::contract::copy_old(v) ?
i_type::eval(i)
:
boost::contract::null_old()
);
boost::contract::old_ptr<j_type> old_j;
boost::contract::guard c = boost::contract::public_function(v, this)
.precondition([&] {
out << "b::swap::pre" << std::endl;
BOOST_CONTRACT_ASSERT(i.value != j.value);
})
.old([&] {
out << "b::swap::old" << std::endl;
old_j = boost::contract::make_old(v, boost::contract::copy_old(v) ?
j_type::eval(j) : boost::contract::null_old());
})
.postcondition([&] {
out << "b::swap::post" << std::endl;
BOOST_CONTRACT_ASSERT(i.value == old_j->value);
BOOST_CONTRACT_ASSERT(j.value == old_i->value);
})
;
assert(false);
};
struct a
#define BASES public b
: BASES
{
typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
#undef BASES
void swap(i_type& i, j_type& j, boost::contract::virtual_* v = 0)
/* override */ {
boost::contract::guard c = boost::contract::public_function<
override_swap>(v, &a::swap, this, i, j);
out << "a::swap::body" << std::endl;
int t = i.value;
i.value = j.value;
j.value = t;
}
BOOST_CONTRACT_OVERRIDE(swap)
};
struct x_tag;
typedef boost::contract::test::detail::counter<x_tag, char> x_type;
struct y_tag;
typedef boost::contract::test::detail::counter<y_tag, char> y_type;
void swap(x_type& x, y_type& y) {
boost::contract::old_ptr<x_type> old_x = boost::contract::make_old(
boost::contract::copy_old() ?
x_type::eval(x)
:
boost::contract::null_old()
);
boost::contract::old_ptr<y_type> 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::make_old(boost::contract::copy_old() ?
y_type::eval(y) : boost::contract::null_old());
})
.postcondition([&] {
out << "swap::post" << std::endl;
BOOST_CONTRACT_ASSERT(x.value == old_y->value);
BOOST_CONTRACT_ASSERT(y.value == old_x->value);
})
;
out << "swap::body" << std::endl;
char t = x.value;
x.value = y.value;
y.value = t;
}
int main() {
std::ostringstream ok;
out.str("");
x_type x; x.value = 'a';
y_type y; y.value = 'b';
swap(x, y);
ok.str(""); ok
#ifndef BOOST_CONTRACT_NO_PRECONDITIONS
<< "swap::pre" << std::endl
#endif
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
<< "swap::old" << std::endl
#endif
<< "swap::body" << std::endl
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
<< "swap::post" << std::endl
#endif
;
BOOST_TEST(out.eq(ok.str()));
unsigned const cnt =
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
1
#else
0
#endif
;
BOOST_TEST_EQ(x.value, 'b');
BOOST_TEST_EQ(x.copies(), cnt);
BOOST_TEST_EQ(x.evals(), cnt);
BOOST_TEST_EQ(x.ctors(), x.dtors() + 1); // 1 for local var.
BOOST_TEST_EQ(y.value, 'a');
BOOST_TEST_EQ(y.copies(), cnt);
BOOST_TEST_EQ(y.evals(), cnt);
BOOST_TEST_EQ(y.ctors(), y.dtors() + 1); // 1 for local var.
a aa;
i_type i; i.value = 1;
j_type j; j.value = 2;
out.str("");
aa.swap(i, j);
ok.str(""); ok
#ifndef BOOST_CONTRACT_NO_PRECONDITIONS
<< "b::swap::pre" << std::endl
#endif
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
<< "b::swap::old" << std::endl
#endif
<< "a::swap::body" << std::endl
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
<< "b::swap::old" << std::endl
<< "b::swap::post" << std::endl
#endif
;
BOOST_TEST(out.eq(ok.str()));
BOOST_TEST_EQ(i.value, 2);
BOOST_TEST_EQ(i.copies(), cnt);
BOOST_TEST_EQ(i.evals(), cnt);
BOOST_TEST_EQ(i.ctors(), i.dtors() + 1); // 1 for local var.
BOOST_TEST_EQ(j.value, 1);
BOOST_TEST_EQ(j.copies(), cnt);
BOOST_TEST_EQ(j.evals(), cnt);
BOOST_TEST_EQ(j.ctors(), j.dtors() + 1); // 1 for local var.
return boost::report_errors();
}
#define BOOST_CONTRACT_TEST_OLD_PTR_TYPE boost::contract::old_ptr
#include "no_macros.hpp"

186
test/old/no_macros.hpp Normal file
View File

@@ -0,0 +1,186 @@
// Test old values without BOOST_CONTRACT_OLDOF macro.
#ifndef BOOST_CONTRACT_TEST_OLD_PTR_TYPE
#error "must define BOOST_CONTRACT_TEST_OLD_PTR_TYPE"
#endif
#include "../detail/oteststream.hpp"
#include "../detail/counter.hpp"
#include <boost/contract/function.hpp>
#include <boost/contract/public_function.hpp>
#include <boost/contract/base_types.hpp>
#include <boost/contract/override.hpp>
#include <boost/contract/assert.hpp>
#include <boost/contract/old.hpp>
#include <boost/contract/guard.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <cassert>
boost::contract::test::detail::oteststream out;
struct i_tag; typedef boost::contract::test::detail::counter<i_tag, int> i_type;
struct j_tag; typedef boost::contract::test::detail::counter<j_tag, int> j_type;
struct b {
virtual void swap(i_type& i, j_type& j, boost::contract::virtual_* v = 0);
};
void b::swap(i_type& i, j_type& j, boost::contract::virtual_* v) {
BOOST_CONTRACT_TEST_OLD_PTR_TYPE<i_type> old_i = boost::contract::make_old(
v, boost::contract::copy_old(v) ?
i_type::eval(i)
:
boost::contract::null_old()
);
BOOST_CONTRACT_TEST_OLD_PTR_TYPE<j_type> old_j;
boost::contract::guard c = boost::contract::public_function(v, this)
.precondition([&] {
out << "b::swap::pre" << std::endl;
BOOST_CONTRACT_ASSERT(i.value != j.value);
})
.old([&] {
out << "b::swap::old" << std::endl;
old_j = boost::contract::make_old(v, boost::contract::copy_old(v) ?
j_type::eval(j) : boost::contract::null_old());
})
.postcondition([&] {
out << "b::swap::post" << std::endl;
BOOST_CONTRACT_ASSERT(i.value == old_j->value);
BOOST_CONTRACT_ASSERT(j.value == old_i->value);
})
;
assert(false);
};
struct a
#define BASES public b
: BASES
{
typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
#undef BASES
void swap(i_type& i, j_type& j, boost::contract::virtual_* v = 0)
/* override */ {
boost::contract::guard c = boost::contract::public_function<
override_swap>(v, &a::swap, this, i, j);
out << "a::swap::body" << std::endl;
int t = i.value;
i.value = j.value;
j.value = t;
}
BOOST_CONTRACT_OVERRIDE(swap)
};
struct x_tag;
typedef boost::contract::test::detail::counter<x_tag, char> x_type;
struct y_tag;
typedef boost::contract::test::detail::counter<y_tag, char> y_type;
void swap(x_type& x, y_type& y) {
BOOST_CONTRACT_TEST_OLD_PTR_TYPE<x_type> old_x = boost::contract::make_old(
boost::contract::copy_old() ?
x_type::eval(x)
:
boost::contract::null_old()
);
BOOST_CONTRACT_TEST_OLD_PTR_TYPE<y_type> 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::make_old(boost::contract::copy_old() ?
y_type::eval(y) : boost::contract::null_old());
})
.postcondition([&] {
out << "swap::post" << std::endl;
BOOST_CONTRACT_ASSERT(x.value == old_y->value);
BOOST_CONTRACT_ASSERT(y.value == old_x->value);
})
;
out << "swap::body" << std::endl;
char t = x.value;
x.value = y.value;
y.value = t;
}
int main() {
std::ostringstream ok;
out.str("");
x_type x; x.value = 'a';
y_type y; y.value = 'b';
swap(x, y);
ok.str(""); ok
#ifndef BOOST_CONTRACT_NO_PRECONDITIONS
<< "swap::pre" << std::endl
#endif
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
<< "swap::old" << std::endl
#endif
<< "swap::body" << std::endl
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
<< "swap::post" << std::endl
#endif
;
BOOST_TEST(out.eq(ok.str()));
unsigned const cnt =
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
1
#else
0
#endif
;
BOOST_TEST_EQ(x.value, 'b');
BOOST_TEST_EQ(x.copies(), cnt);
BOOST_TEST_EQ(x.evals(), cnt);
BOOST_TEST_EQ(x.ctors(), x.dtors() + 1); // 1 for local var.
BOOST_TEST_EQ(y.value, 'a');
BOOST_TEST_EQ(y.copies(), cnt);
BOOST_TEST_EQ(y.evals(), cnt);
BOOST_TEST_EQ(y.ctors(), y.dtors() + 1); // 1 for local var.
a aa;
i_type i; i.value = 1;
j_type j; j.value = 2;
out.str("");
aa.swap(i, j);
ok.str(""); ok
#ifndef BOOST_CONTRACT_NO_PRECONDITIONS
<< "b::swap::pre" << std::endl
#endif
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
<< "b::swap::old" << std::endl
#endif
<< "a::swap::body" << std::endl
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
<< "b::swap::old" << std::endl
<< "b::swap::post" << std::endl
#endif
;
BOOST_TEST(out.eq(ok.str()));
BOOST_TEST_EQ(i.value, 2);
BOOST_TEST_EQ(i.copies(), cnt);
BOOST_TEST_EQ(i.evals(), cnt);
BOOST_TEST_EQ(i.ctors(), i.dtors() + 1); // 1 for local var.
BOOST_TEST_EQ(j.value, 1);
BOOST_TEST_EQ(j.copies(), cnt);
BOOST_TEST_EQ(j.evals(), cnt);
BOOST_TEST_EQ(j.ctors(), j.dtors() + 1); // 1 for local var.
return boost::report_errors();
}

View File

@@ -0,0 +1,4 @@
#define BOOST_CONTRACT_TEST_OLD_PTR_TYPE boost::contract::old_ptr_noncopyable
#include "no_macros.hpp"

View File

@@ -1,12 +1,4 @@
// Test error when make_old(...) not used by mistake.
#include <boost/contract/old.hpp>
int main() {
int x = 1;
boost::contract::old_ptr<int> old_x = boost::contract::copy_old() ? x :
boost::contract::null_old(); // Error (missing outer make_old(...)).
return 0;
}
#define BOOST_CONTRACT_TEST_OLD_PTR_TYPE boost::contract::old_ptr
#include "no_make_old_error.hpp"

View File

@@ -0,0 +1,17 @@
// Test error when make_old(...) not used by mistake.
#ifndef BOOST_CONTRACT_TEST_OLD_PTR_TYPE
#error "must define BOOST_CONTRACT_TEST_OLD_PTR_TYPE"
#endif
#include <boost/contract/old.hpp>
int main() {
int x = 1;
// Error (missing outer make_old(...)).
BOOST_CONTRACT_TEST_OLD_PTR_TYPE<int> old_x = boost::contract::copy_old() ?
x : boost::contract::null_old();
return 0;
}

View File

@@ -0,0 +1,4 @@
#define BOOST_CONTRACT_TEST_OLD_PTR_TYPE boost::contract::old_ptr_noncopyable
#include "no_make_old_error.hpp"