diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/contract/example/cline90 | |
parent | Initial commit. (diff) | |
download | ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.tar.xz ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/contract/example/cline90')
-rw-r--r-- | src/boost/libs/contract/example/cline90/calendar.cpp | 93 | ||||
-rw-r--r-- | src/boost/libs/contract/example/cline90/stack.cpp | 93 | ||||
-rw-r--r-- | src/boost/libs/contract/example/cline90/vector.hpp | 101 | ||||
-rw-r--r-- | src/boost/libs/contract/example/cline90/vector_axx.hpp | 101 | ||||
-rw-r--r-- | src/boost/libs/contract/example/cline90/vector_main.cpp | 23 | ||||
-rw-r--r-- | src/boost/libs/contract/example/cline90/vstack.cpp | 239 |
6 files changed, 650 insertions, 0 deletions
diff --git a/src/boost/libs/contract/example/cline90/calendar.cpp b/src/boost/libs/contract/example/cline90/calendar.cpp new file mode 100644 index 00000000..1dc76ca1 --- /dev/null +++ b/src/boost/libs/contract/example/cline90/calendar.cpp @@ -0,0 +1,93 @@ + +// Copyright (C) 2008-2018 Lorenzo Caminiti +// Distributed under the Boost Software License, Version 1.0 (see accompanying +// file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt). +// See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html + +//[cline90_calendar +#include <boost/contract.hpp> +#include <cassert> + +class calendar { + friend class boost::contract::access; + + void invariant() const { + BOOST_CONTRACT_ASSERT(month() >= 1); + BOOST_CONTRACT_ASSERT(month() <= 12); + BOOST_CONTRACT_ASSERT(date() >= 1); + BOOST_CONTRACT_ASSERT(date() <= days_in(month())); + } + +public: + calendar() : month_(1), date_(31) { + boost::contract::check c = boost::contract::constructor(this) + .postcondition([&] { + BOOST_CONTRACT_ASSERT(month() == 1); + BOOST_CONTRACT_ASSERT(date() == 31); + }) + ; + } + + virtual ~calendar() { + // Check invariants. + boost::contract::check c = boost::contract::destructor(this); + } + + int month() const { + // Check invariants. + boost::contract::check c = boost::contract::public_function(this); + return month_; + } + + int date() const { + // Check invariants. + boost::contract::check c = boost::contract::public_function(this); + return date_; + } + + void reset(int new_month) { + boost::contract::check c = boost::contract::public_function(this) + .precondition([&] { + BOOST_CONTRACT_ASSERT(new_month >= 1); + BOOST_CONTRACT_ASSERT(new_month <= 12); + }) + .postcondition([&] { + BOOST_CONTRACT_ASSERT(month() == new_month); + }) + ; + + month_ = new_month; + } + +private: + static int days_in(int month) { + int result; + boost::contract::check c = boost::contract::function() + .precondition([&] { + BOOST_CONTRACT_ASSERT(month >= 1); + BOOST_CONTRACT_ASSERT(month <= 12); + }) + .postcondition([&] { + BOOST_CONTRACT_ASSERT(result >= 1); + BOOST_CONTRACT_ASSERT(result <= 31); + }) + ; + + return result = 31; // For simplicity, assume all months have 31 days. + } + + int month_, date_; +}; + +int main() { + calendar cal; + assert(cal.date() == 31); + assert(cal.month() == 1); + + cal.reset(8); // Set month + assert(cal.month() == 8); + + return 0; +} +//] + diff --git a/src/boost/libs/contract/example/cline90/stack.cpp b/src/boost/libs/contract/example/cline90/stack.cpp new file mode 100644 index 00000000..b02c844f --- /dev/null +++ b/src/boost/libs/contract/example/cline90/stack.cpp @@ -0,0 +1,93 @@ + +// Copyright (C) 2008-2018 Lorenzo Caminiti +// Distributed under the Boost Software License, Version 1.0 (see accompanying +// file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt). +// See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html + +//[cline90_stack +#include <boost/contract.hpp> +#include <cassert> + +// NOTE: Incomplete contract assertions, addressing only `empty` and `full`. +template<typename T> +class stack + #define BASES private boost::contract::constructor_precondition<stack<T> > + : BASES +{ + friend class boost::contract::access; + + typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types; + #undef BASES + +public: + explicit stack(int capacity) : + boost::contract::constructor_precondition<stack>([&] { + BOOST_CONTRACT_ASSERT(capacity >= 0); + }), + data_(new T[capacity]), capacity_(capacity), size_(0) + { + boost::contract::check c = boost::contract::constructor(this) + .postcondition([&] { + BOOST_CONTRACT_ASSERT(empty()); + BOOST_CONTRACT_ASSERT(full() == (capacity == 0)); + }) + ; + + for(int i = 0; i < capacity_; ++i) data_[i] = T(); + } + + virtual ~stack() { + boost::contract::check c = boost::contract::destructor(this); + delete[] data_; + } + + bool empty() const { + boost::contract::check c = boost::contract::public_function(this); + return size_ == 0; + } + + bool full() const { + boost::contract::check c = boost::contract::public_function(this); + return size_ == capacity_; + } + + void push(T const& value) { + boost::contract::check c = boost::contract::public_function(this) + .precondition([&] { + BOOST_CONTRACT_ASSERT(!full()); + }) + .postcondition([&] { + BOOST_CONTRACT_ASSERT(!empty()); + }) + ; + + data_[size_++] = value; + } + + T pop() { + boost::contract::check c = boost::contract::public_function(this) + .precondition([&] { + BOOST_CONTRACT_ASSERT(!empty()); + }) + .postcondition([&] { + BOOST_CONTRACT_ASSERT(!full()); + }) + ; + + return data_[--size_]; + } + +private: + T* data_; + int capacity_; + int size_; +}; + +int main() { + stack<int> s(3); + s.push(123); + assert(s.pop() == 123); + return 0; +} +//] + diff --git a/src/boost/libs/contract/example/cline90/vector.hpp b/src/boost/libs/contract/example/cline90/vector.hpp new file mode 100644 index 00000000..6e712621 --- /dev/null +++ b/src/boost/libs/contract/example/cline90/vector.hpp @@ -0,0 +1,101 @@ + +// Copyright (C) 2008-2018 Lorenzo Caminiti +// Distributed under the Boost Software License, Version 1.0 (see accompanying +// file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt). +// See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html + +//[cline90_vector +#ifndef VECTOR_HPP_ +#define VECTOR_HPP_ + +#include <boost/contract.hpp> + +// NOTE: Incomplete contract assertions, addressing only `size`. +template<typename T> +class vector + #define BASES private boost::contract::constructor_precondition<vector<T> > + : BASES +{ + friend class boost::contract::access; + + typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types; + #undef BASES + + void invariant() const { + BOOST_CONTRACT_ASSERT(size() >= 0); + } + +public: + explicit vector(int count = 10) : + boost::contract::constructor_precondition<vector>([&] { + BOOST_CONTRACT_ASSERT(count >= 0); + }), + data_(new T[count]), + size_(count) + { + boost::contract::check c = boost::contract::constructor(this) + .postcondition([&] { + BOOST_CONTRACT_ASSERT(size() == count); + }) + ; + + for(int i = 0; i < size_; ++i) data_[i] = T(); + } + + virtual ~vector() { + boost::contract::check c = boost::contract::destructor(this); + delete[] data_; + } + + int size() const { + boost::contract::check c = boost::contract::public_function(this); + return size_; // Non-negative result already checked by invariant. + } + + void resize(int count) { + boost::contract::check c = boost::contract::public_function(this) + .precondition([&] { + BOOST_CONTRACT_ASSERT(count >= 0); + }) + .postcondition([&] { + BOOST_CONTRACT_ASSERT(size() == count); + }) + ; + + T* slice = new T[count]; + for(int i = 0; i < count && i < size_; ++i) slice[i] = data_[i]; + delete[] data_; + data_ = slice; + size_ = count; + } + + T& operator[](int index) { + boost::contract::check c = boost::contract::public_function(this) + .precondition([&] { + BOOST_CONTRACT_ASSERT(index >= 0); + BOOST_CONTRACT_ASSERT(index < size()); + }) + ; + + return data_[index]; + } + + T const& operator[](int index) const { + boost::contract::check c = boost::contract::public_function(this) + .precondition([&] { + BOOST_CONTRACT_ASSERT(index >= 0); + BOOST_CONTRACT_ASSERT(index < size()); + }) + ; + + return data_[index]; + } + +private: + T* data_; + int size_; +}; + +#endif // #include guard +//] + diff --git a/src/boost/libs/contract/example/cline90/vector_axx.hpp b/src/boost/libs/contract/example/cline90/vector_axx.hpp new file mode 100644 index 00000000..4a3582e6 --- /dev/null +++ b/src/boost/libs/contract/example/cline90/vector_axx.hpp @@ -0,0 +1,101 @@ + +// Copyright (C) 2008-2018 Lorenzo Caminiti +// Distributed under the Boost Software License, Version 1.0 (see accompanying +// file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt). +// See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html + +//[cline90_vector_axx +// Extra spaces, newlines, etc. for visual alignment with this library code. + + + + + +template<typename T> +class vector { + + + + + + + + +legal: // Class invariants (legal). + size() >= 0; + + +public: + explicit vector(int count = 10) : + data_(new T[count]), + size_(count) + { + for(int i = 0; i < size_; ++i) data_[i] = T(); + } + + + + + + + + + + + virtual ~vector() { delete[] data_; } + + + + + int size() const { return size_; } + + + + + void resize(int count) { + T* slice = new T[count]; + for(int i = 0; i < count && i < size_; ++i) slice[i] = data_[i]; + delete[] data_; + data_ = slice; + size_ = count; + } + + + + + + + + + + + T& operator[](int index) { return data_[index]; } + + + + + + + + + + + T& operator[](int index) const { return data_[index]; } + + + + +axioms: // Preconditions (require) and postconditions (promise) for each func. + [int count; require count >= 0; promise size() == count] vector(count); + [int count; require count >= 0; promise size() == count] resize(count); + [int index; require index >= 0 && index < size()] (*this)[x]; // Op[]. + [int index; require index >= 0 && index < size()] (*this)[x] const; // Op[]. + +private: + T* data_; + int size_; +}; + +// End. +//] + diff --git a/src/boost/libs/contract/example/cline90/vector_main.cpp b/src/boost/libs/contract/example/cline90/vector_main.cpp new file mode 100644 index 00000000..28fa3e91 --- /dev/null +++ b/src/boost/libs/contract/example/cline90/vector_main.cpp @@ -0,0 +1,23 @@ + +// Copyright (C) 2008-2018 Lorenzo Caminiti +// Distributed under the Boost Software License, Version 1.0 (see accompanying +// file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt). +// See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html + +//[cline90_vector_main +#include "vector.hpp" +#include <cassert> + +int main() { + vector<int> v (3); + assert(v.size() == 3); + + v[0] = 123; + v.resize(2); + assert(v[0] == 123); + assert(v.size() == 2); + + return 0; +} +//] + diff --git a/src/boost/libs/contract/example/cline90/vstack.cpp b/src/boost/libs/contract/example/cline90/vstack.cpp new file mode 100644 index 00000000..b210d40a --- /dev/null +++ b/src/boost/libs/contract/example/cline90/vstack.cpp @@ -0,0 +1,239 @@ + +// Copyright (C) 2008-2018 Lorenzo Caminiti +// Distributed under the Boost Software License, Version 1.0 (see accompanying +// file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt). +// See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html + +//[cline90_vstack +#include "vector.hpp" +#include <boost/contract.hpp> +#include <boost/optional.hpp> +#include <cassert> + +// NOTE: Incomplete contract assertions, addressing only `empty` and `full`. +template<typename T> +class abstract_stack { +public: + abstract_stack() { + boost::contract::check c = boost::contract::constructor(this) + .postcondition([&] { + // AXIOM as empty() cannot actually be checked here to avoid + // calling pure virtual function length() during construction). + BOOST_CONTRACT_ASSERT_AXIOM(empty()); + }) + ; + } + + virtual ~abstract_stack() { + boost::contract::check c = boost::contract::destructor(this); + } + + bool full() const { + bool result; + boost::contract::check c = boost::contract::public_function(this) + .postcondition([&] { + BOOST_CONTRACT_ASSERT(result == (length() == capacity())); + }) + ; + + return result = (length() == capacity()); + } + + bool empty() const { + bool result; + boost::contract::check c = boost::contract::public_function(this) + .postcondition([&] { + BOOST_CONTRACT_ASSERT(result = (length() == 0)); + }) + ; + + return result = (length() == 0); + } + + virtual int length(boost::contract::virtual_* v = 0) const = 0; + virtual int capacity(boost::contract::virtual_* v = 0) const = 0; + + virtual T const& item(boost::contract::virtual_* v = 0) const = 0; + + virtual void push(T const& value, boost::contract::virtual_* v = 0) = 0; + virtual T const& pop(boost::contract::virtual_* v = 0) = 0; + + virtual void clear(boost::contract::virtual_* v = 0) = 0; +}; + +template<typename T> +int abstract_stack<T>::length(boost::contract::virtual_* v) const { + int result; + boost::contract::check c = boost::contract::public_function(v, result, this) + .postcondition([&] (int const& result) { + BOOST_CONTRACT_ASSERT(result >= 0); + }) + ; + assert(false); + return result; +} + +template<typename T> +int abstract_stack<T>::capacity(boost::contract::virtual_* v) const { + int result; + boost::contract::check c = boost::contract::public_function(v, result, this) + .postcondition([&] (int const& result) { + BOOST_CONTRACT_ASSERT(result >= 0); + }) + ; + assert(false); + return result; +} + +template<typename T> +T const& abstract_stack<T>::item(boost::contract::virtual_* v) const { + boost::optional<T const&> result; + boost::contract::check c = boost::contract::public_function(v, result, this) + .precondition([&] { + BOOST_CONTRACT_ASSERT(!empty()); + }) + ; + assert(false); + return *result; +} + +template<typename T> +void abstract_stack<T>::push(T const& value, boost::contract::virtual_* v) { + boost::contract::check c = boost::contract::public_function(v, this) + .precondition([&] { + BOOST_CONTRACT_ASSERT(!full()); + }) + .postcondition([&] { + BOOST_CONTRACT_ASSERT(!empty()); + }) + ; + assert(false); +} + +template<typename T> +T const& abstract_stack<T>::pop(boost::contract::virtual_* v) { + boost::optional<T const&> result; + boost::contract::old_ptr<T> old_item = BOOST_CONTRACT_OLDOF(v, item()); + boost::contract::check c = boost::contract::public_function(v, result, this) + .precondition([&] { + BOOST_CONTRACT_ASSERT(!empty()); + }) + .postcondition([&] (boost::optional<T const&> const& result) { + BOOST_CONTRACT_ASSERT(!full()); + BOOST_CONTRACT_ASSERT(*result == *old_item); + }) + ; + assert(false); + return *result; +} + +template<typename T> +void abstract_stack<T>::clear(boost::contract::virtual_* v) { + boost::contract::check c = boost::contract::public_function(v, this) + .postcondition([&] { + BOOST_CONTRACT_ASSERT(empty()); + }) + ; + assert(false); +} + +template<typename T> +class vstack + #define BASES private boost::contract::constructor_precondition< \ + vstack<T> >, public abstract_stack<T> + : BASES +{ + friend class boost::contract::access; + + typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types; + #undef BASES + + void invariant() const { + BOOST_CONTRACT_ASSERT(length() >= 0); + BOOST_CONTRACT_ASSERT(length() < capacity()); + } + + BOOST_CONTRACT_OVERRIDES(length, capacity, item, push, pop, clear) + +public: + explicit vstack(int count = 10) : + boost::contract::constructor_precondition<vstack>([&] { + BOOST_CONTRACT_ASSERT(count >= 0); + }), + vect_(count), // OK, executed after precondition so count >= 0. + len_(0) + { + boost::contract::check c = boost::contract::constructor(this) + .postcondition([&] { + BOOST_CONTRACT_ASSERT(length() == 0); + BOOST_CONTRACT_ASSERT(capacity() == count); + }) + ; + } + + virtual ~vstack() { + boost::contract::check c = boost::contract::destructor(this); + } + + // Inherited from abstract_stack. + + virtual int length(boost::contract::virtual_* v = 0) const /* override */ { + int result; + boost::contract::check c = boost::contract::public_function< + override_length>(v, result, &vstack::length, this); + return result = len_; + } + + virtual int capacity(boost::contract::virtual_* v = 0) + const /* override */ { + int result; + boost::contract::check c = boost::contract::public_function< + override_capacity>(v, result, &vstack::capacity, this); + return result = vect_.size(); + } + + virtual T const& item(boost::contract::virtual_* v = 0) + const /* override */ { + boost::optional<T const&> result; + boost::contract::check c = boost::contract::public_function< + override_item>(v, result, &vstack::item, this); + return *(result = vect_[len_ - 1]); + } + + virtual void push(T const& value, boost::contract::virtual_* v = 0) + /* override */ { + boost::contract::check c = boost::contract::public_function< + override_push>(v, &vstack::push, this, value); + vect_[len_++] = value; + } + + virtual T const& pop(boost::contract::virtual_* v = 0) /* override */ { + boost::optional<T const&> result; + boost::contract::check c = boost::contract::public_function< + override_pop>(v, result, &vstack::pop, this); + return *(result = vect_[--len_]); + } + + virtual void clear(boost::contract::virtual_* v = 0) /* override */ { + boost::contract::check c = boost::contract::public_function< + override_clear>(v, &vstack::clear, this); + len_ = 0; + } + +private: + vector<T> vect_; + int len_; +}; + +int main() { + vstack<int> s(3); + assert(s.capacity() == 3); + + s.push(123); + assert(s.length() == 1); + assert(s.pop() == 123); + + return 0; +} +//] + |