finished example section. recompiled all tests and examples.

This commit is contained in:
Lorenzo Caminiti
2016-05-12 08:22:34 -07:00
parent 0c8a75df83
commit 1aa41fd14e
48 changed files with 1812 additions and 1593 deletions

View File

@@ -2,21 +2,23 @@
//[mitchell02_stack
#include <boost/contract.hpp>
#include <boost/optional.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <vector>
#include <cassert>
template<typename T>
class stack {
public:
friend class boost::contract::access;
void invariant() const {
BOOST_CONTRACT_ASSERT(count() >= 0); // Non-negative count.
}
public:
/* Creation */
// Create empty stack.
stack() {
auto c = boost::contract::constructor(this)
boost::contract::guard c = boost::contract::constructor(this)
.postcondition([&] {
BOOST_CONTRACT_ASSERT(count() == 0); // Empty.
})
@@ -25,21 +27,22 @@ public:
// Destroy stack.
virtual ~stack() {
auto c = boost::contract::destructor(this); // Check invariants.
// Check invariants.
boost::contract::guard c = boost::contract::destructor(this);
}
/* Basic Queries */
// Number of items.
int count() const {
auto c = boost::contract::public_function(this); // Check invariants.
// Check invariants.
boost::contract::guard c = boost::contract::public_function(this);
return items_.size();
}
// Item at index in [1, count()] (as in Eiffel).
T const& item_at(int index) const {
auto c = boost::contract::public_function(this)
boost::contract::guard c = boost::contract::public_function(this)
.precondition([&] {
BOOST_CONTRACT_ASSERT(index > 0); // Positive index.
BOOST_CONTRACT_ASSERT(index <= count()); // Index within count.
@@ -54,7 +57,7 @@ public:
// If no items.
bool is_empty() const {
bool result;
auto c = boost::contract::public_function(this)
boost::contract::guard c = boost::contract::public_function(this)
.postcondition([&] {
// Consistent with count.
BOOST_CONTRACT_ASSERT(result == (count() == 0));
@@ -67,7 +70,7 @@ public:
// Top item.
T const& item() const {
boost::optional<T const&> result; // Avoid extra construction of T.
auto c = boost::contract::public_function(this)
boost::contract::guard c = boost::contract::public_function(this)
.precondition([&] {
BOOST_CONTRACT_ASSERT(count() > 0); // Not empty.
})
@@ -84,8 +87,8 @@ public:
// Push item to the top.
void put(T const& new_item) {
auto old_count = BOOST_CONTRACT_OLDOF(count());
auto c = boost::contract::public_function(this)
boost::contract::old_ptr<int> old_count = BOOST_CONTRACT_OLDOF(count());
boost::contract::guard c = boost::contract::public_function(this)
.postcondition([&] {
BOOST_CONTRACT_ASSERT(count() == *old_count + 1); // Count inc.
BOOST_CONTRACT_ASSERT(item() == new_item); // Item set.
@@ -97,8 +100,8 @@ public:
// Pop top item.
void remove() {
auto old_count = BOOST_CONTRACT_OLDOF(count());
auto c = boost::contract::public_function(this)
boost::contract::old_ptr<int> old_count = BOOST_CONTRACT_OLDOF(count());
boost::contract::guard c = boost::contract::public_function(this)
.precondition([&] {
BOOST_CONTRACT_ASSERT(count() > 0); // Not empty.
})
@@ -116,15 +119,15 @@ private:
int main() {
stack<int> s;
BOOST_TEST_EQ(s.count(), 0);
assert(s.count() == 0);
s.put(123);
BOOST_TEST_EQ(s.item(), 123);
assert(s.item() == 123);
s.remove();
BOOST_TEST(s.is_empty());
assert(s.is_empty());
return boost::report_errors();
return 0;
}
//]