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/test/old | |
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/test/old')
-rw-r--r-- | src/boost/libs/contract/test/old/auto.cpp | 38 | ||||
-rw-r--r-- | src/boost/libs/contract/test/old/copyable_traits.cpp | 108 | ||||
-rw-r--r-- | src/boost/libs/contract/test/old/if_copyable.cpp | 133 | ||||
-rw-r--r-- | src/boost/libs/contract/test/old/if_copyable.hpp | 45 | ||||
-rw-r--r-- | src/boost/libs/contract/test/old/if_copyable_error.cpp | 43 | ||||
-rw-r--r-- | src/boost/libs/contract/test/old/if_copyable_macro.cpp | 172 | ||||
-rw-r--r-- | src/boost/libs/contract/test/old/no_macro.cpp | 9 | ||||
-rw-r--r-- | src/boost/libs/contract/test/old/no_macro.hpp | 190 | ||||
-rw-r--r-- | src/boost/libs/contract/test/old/no_macro_if_copyable.cpp | 9 | ||||
-rw-r--r-- | src/boost/libs/contract/test/old/no_make_old_error.cpp | 9 | ||||
-rw-r--r-- | src/boost/libs/contract/test/old/no_make_old_error.hpp | 21 | ||||
-rw-r--r-- | src/boost/libs/contract/test/old/no_make_old_if_copyable_error.cpp | 9 |
12 files changed, 786 insertions, 0 deletions
diff --git a/src/boost/libs/contract/test/old/auto.cpp b/src/boost/libs/contract/test/old/auto.cpp new file mode 100644 index 00000000..97dd87f7 --- /dev/null +++ b/src/boost/libs/contract/test/old/auto.cpp @@ -0,0 +1,38 @@ + +// 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 + +// Test that OLD macro allows to use C++11 auto declarations. + +#include <boost/config.hpp> +#include <boost/contract/old.hpp> +#include <boost/type_traits.hpp> +#include <boost/static_assert.hpp> +#include <boost/detail/lightweight_test.hpp> + +int main() { + int x = -123; + auto old_x = BOOST_CONTRACT_OLDOF(x); + x = 123; + BOOST_STATIC_ASSERT((boost::is_same<decltype(old_x), + boost::contract::old_ptr<int> >::value)); + #ifndef BOOST_CONTRACT_NO_OLDS + BOOST_TEST_EQ(*old_x, -123); + #endif + BOOST_TEST_EQ(x, 123); + + boost::contract::virtual_* v = 0; + char y = 'j'; + auto old_y = BOOST_CONTRACT_OLDOF(v, y); + y = 'k'; + BOOST_STATIC_ASSERT((boost::is_same<decltype(old_y), + boost::contract::old_ptr<char> >::value)); + #ifndef BOOST_CONTRACT_NO_OLDS + BOOST_TEST_EQ(*old_y, 'j'); + #endif + BOOST_TEST_EQ(y, 'k'); + return boost::report_errors(); +} + diff --git a/src/boost/libs/contract/test/old/copyable_traits.cpp b/src/boost/libs/contract/test/old/copyable_traits.cpp new file mode 100644 index 00000000..3792c6fe --- /dev/null +++ b/src/boost/libs/contract/test/old/copyable_traits.cpp @@ -0,0 +1,108 @@ + +// 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 + +// Test specializations of old value copy type traits. + +#include <boost/contract/old.hpp> +#include <boost/type_traits.hpp> +#include <boost/noncopyable.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <cassert> + +template<typename T> +void f(T& x) { + // No OLDOF here so C++11 not required for this test. + boost::contract::old_ptr_if_copyable<T> old_x = boost::contract::make_old( + boost::contract::copy_old() ? x : boost::contract::null_old()); +} + +// Test copyable type but... +struct w { + w() {} + w(w const&) { BOOST_TEST(false); } // Test this doesn't get copied. +}; + +// ...never copy old values for type `w` (because its copy is too expensive). +namespace boost { namespace contract { + template<> + struct is_old_value_copyable<w> : boost::false_type {}; +} } // namespace + +// Test non-copyable type but... +struct p : private boost::noncopyable { // Non-copyable via Boost. + static bool copied; + p() : num_(new int(0)) {} + ~p() { delete num_; } +private: + int* num_; + friend struct boost::contract::old_value_copy<p>; +}; +bool p::copied = false; + +// ...still copy old values for type `p` (using a deep copy). +namespace boost { namespace contract { + template<> + struct old_value_copy<p> { + explicit old_value_copy(p const& old) { + *old_.num_ = *old.num_; // Deep copy pointed value. + p::copied = true; + } + + p const& old() const { return old_; } + + private: + p old_; + }; + + template<> + struct is_old_value_copyable<p> : boost::true_type {}; +} } // namespace + +// Non-copyable type so... +struct n { + n() {} +private: + n(n const&); // Non-copyable (but not via Boost). +}; + +// ...specialize `boost::is_copy_constructible` (no need for this on C++11). +namespace boost { namespace contract { + template<> + struct is_old_value_copyable<n> : boost::false_type {}; +} } // namespace + +int main() { + // NOTE: No test::detail::counter below because that is for copyable types. + + { + int x; // Test has copy ctor so copy olds. + f(x); + } + + { + w x; // Test has copy ctor but never copy olds (see TEST(...) above). + f(x); + } + + p::copied = false; + { + p x; // Test no copy ctor but still old copies. + f(x); + } + #ifndef BOOST_CONTRACT_NO_OLDS + BOOST_TEST(p::copied); + #else + BOOST_TEST(!p::copied); + #endif + + { + n x; // Test no copy ctor so no old copies. + f(x); + } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/contract/test/old/if_copyable.cpp b/src/boost/libs/contract/test/old/if_copyable.cpp new file mode 100644 index 00000000..0c3738f3 --- /dev/null +++ b/src/boost/libs/contract/test/old/if_copyable.cpp @@ -0,0 +1,133 @@ + +// 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 + +// Test old values of non-copyable types. + +#include "if_copyable.hpp" +#include <boost/contract/public_function.hpp> +#include <boost/contract/function.hpp> +#include <boost/contract/base_types.hpp> +#include <boost/contract/override.hpp> +#include <boost/contract/old.hpp> +#include <boost/contract/check.hpp> +#include <boost/contract/assert.hpp> +#include <boost/noncopyable.hpp> +#include <boost/detail/lightweight_test.hpp> + +unsigned old_checks; + +template<typename T> +struct b { + virtual void next(T& x, boost::contract::virtual_* v = 0) { + boost::contract::old_ptr_if_copyable<T> old_x = + BOOST_CONTRACT_OLDOF(v, x); + boost::contract::check c = boost::contract::public_function(v, this) + .postcondition([&] { + if(old_x) { + BOOST_CONTRACT_ASSERT(x == *old_x + T(1)); + ++old_checks; + } + }) + ; + ++x; + } + BOOST_CONTRACT_OVERRIDE(next) +}; + +template<typename T> +struct a + #define BASES public b<T> + : BASES +{ + typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types; + #undef BASES + + virtual void next(T& x, boost::contract::virtual_* v = 0) /* override */ { + boost::contract::old_ptr_if_copyable<T> old_x = + BOOST_CONTRACT_OLDOF(v, x); + boost::contract::check c = boost::contract::public_function< + override_next>(v, &a::next, this, x) + .postcondition([&] { + if(old_x) { + BOOST_CONTRACT_ASSERT(x == *old_x + T(1)); + ++old_checks; + } + }) + ; + ++x; + } + BOOST_CONTRACT_OVERRIDE(next) +}; + +template<typename T> +void next(T& x) { + boost::contract::old_ptr_if_copyable<T> old_x = BOOST_CONTRACT_OLDOF(x); + boost::contract::check c = boost::contract::function() + .postcondition([&] { + if(old_x) { + BOOST_CONTRACT_ASSERT(x == *old_x + T(1)); + ++old_checks; + } + }) + ; + ++x; +} + +int main() { + int i = 1; // Test built-in copyable type. + cp c(1); // Test custom copyable type. + ncp n(1); // Test non-copyable type. + + // Test free functions (old values without `v`). + + unsigned cnt = + #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS + 1 + #else + 0 + #endif + ; + + old_checks = 0; + next(i); + BOOST_TEST_EQ(old_checks, cnt); + + old_checks = 0; + next(c); + BOOST_TEST_EQ(old_checks, cnt); + + old_checks = 0; + next(n); + BOOST_TEST_EQ(old_checks, 0u); + + // Test virtual functions (old values with `v`). + + cnt = + #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS + 2 + #else + 0 + #endif + ; + + a<int> ai; + old_checks = 0; + ai.next(i); + BOOST_TEST_EQ(old_checks, cnt); + + a<cp> ac; + old_checks = 0; + ac.next(c); + BOOST_TEST_EQ(old_checks, cnt); + + a<ncp> an; + old_checks = 0; + an.next(n); + BOOST_TEST_EQ(old_checks, 0u); + + return boost::report_errors(); +} + diff --git a/src/boost/libs/contract/test/old/if_copyable.hpp b/src/boost/libs/contract/test/old/if_copyable.hpp new file mode 100644 index 00000000..6bb6c1f8 --- /dev/null +++ b/src/boost/libs/contract/test/old/if_copyable.hpp @@ -0,0 +1,45 @@ + +#ifndef BOOST_CONTRACT_TEST_IF_COPYABLE_HPP_ +#define BOOST_CONTRACT_TEST_IF_COPYABLE_HPP_ + +// 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 + +// Test old values of non-copyable types. + +#define BOOST_CONTRACT_TEST_IF_COPYABLE_TYPE(class_) \ + public: \ + explicit class_(int value) : value_(value) {} \ + \ + friend class_& operator++(class_& me) { ++me.value_; return me; } \ + \ + friend bool operator>(class_ const& left, class_ const& right) { \ + return left.value_ > right.value_; \ + } \ + \ + friend bool operator==(class_ const& left, class_ const& right) { \ + return left.value_ == right.value_; \ + } \ + \ + friend class_ operator+(class_ const& left, class_ const& right) { \ + return class_(left.value_ + right.value_); \ + } \ + \ + private: \ + int value_; + +struct cp { // Copyable type. + BOOST_CONTRACT_TEST_IF_COPYABLE_TYPE(cp) +}; + +struct ncp { + BOOST_CONTRACT_TEST_IF_COPYABLE_TYPE(ncp) + +private: // Non (publicly) copyable type. + ncp(ncp const& other) : value_(other.value_) {} +}; + +#endif // #include guard + diff --git a/src/boost/libs/contract/test/old/if_copyable_error.cpp b/src/boost/libs/contract/test/old/if_copyable_error.cpp new file mode 100644 index 00000000..34b02973 --- /dev/null +++ b/src/boost/libs/contract/test/old/if_copyable_error.cpp @@ -0,0 +1,43 @@ + +// 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 + +// Test forcing compiler error for old values of non-copyable types. + +#include "if_copyable.hpp" +#include <boost/contract/function.hpp> +#include <boost/contract/old.hpp> +#include <boost/contract/check.hpp> +#include <boost/contract/assert.hpp> +#include <boost/noncopyable.hpp> + + +template<typename T> +void next(T& x) { + boost::contract::old_ptr<T> old_x = BOOST_CONTRACT_OLDOF(x); + boost::contract::check c = boost::contract::function() + .postcondition([&] { + // No need to check `if(old_x) ...` here. + BOOST_CONTRACT_ASSERT(x == *old_x + T(1)); + #ifdef BOOST_CONTRACT_NO_ALL + #error "force error if no contracts (ASSERT expands to nothing)" + #endif + }) + ; + ++x; +} + +int main() { + int i = 1; // Test built-in copyable type. + cp c(1); // Test custom copyable type. + ncp n(1); // Test non-copyable type. + + next(i); // OK. + next(c); // OK. + next(n); // Error. + + return 0; +} + diff --git a/src/boost/libs/contract/test/old/if_copyable_macro.cpp b/src/boost/libs/contract/test/old/if_copyable_macro.cpp new file mode 100644 index 00000000..ac31961a --- /dev/null +++ b/src/boost/libs/contract/test/old/if_copyable_macro.cpp @@ -0,0 +1,172 @@ + +// 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 + +// Test old values of non-copyable types (using macro interface). + +#include "if_copyable.hpp" +#include "../detail/unprotected_commas.hpp" +#include <boost/contract_macro.hpp> +#include <boost/noncopyable.hpp> +#include <boost/detail/lightweight_test.hpp> + +unsigned old_checks; + +template<typename T> +struct b { + virtual void next(T& x, boost::contract::virtual_* v = 0) { + BOOST_CONTRACT_OLD_PTR_IF_COPYABLE( + typename boost::contract::test::detail::unprotected_commas<T, void, + void>::type1 + )( + (boost::contract::test::detail::unprotected_commas<void, void, + void>::same(v)), + old_x, + (boost::contract::test::detail::unprotected_commas<void, void, + void>::same(x)) + ); + BOOST_CONTRACT_PUBLIC_FUNCTION( + boost::contract::test::detail::unprotected_commas<void, void, + void>::same(v), + boost::contract::test::detail::unprotected_commas<void, void, + void>::same(this) + ) + BOOST_CONTRACT_POSTCONDITION([&] { + boost::contract::test::detail::unprotected_commas< + void, void, void>::call(); + if(old_x) { + BOOST_CONTRACT_ASSERT(x == *old_x + T(1)); + ++old_checks; + } + }) + ; + ++x; + } + BOOST_CONTRACT_OVERRIDE(next) +}; + +template<typename T> +struct a + #define BASES public b<T> + : BASES +{ + typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types; + #undef BASES + + virtual void next(T& x, boost::contract::virtual_* v = 0) /* override */ { + BOOST_CONTRACT_OLD_PTR_IF_COPYABLE( + typename boost::contract::test::detail::unprotected_commas<T, void, + void>::type1 + )( + (boost::contract::test::detail::unprotected_commas<void, void, + void>::same(v)), + old_x, + (boost::contract::test::detail::unprotected_commas<void, void, + void>::same(x)) + ); + BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE( + typename boost::contract::test::detail::unprotected_commas< + override_next, void, void>::type1 + )( + boost::contract::test::detail::unprotected_commas<void, void, + void>::same(v), + &a::next, + boost::contract::test::detail::unprotected_commas<void, void, + void>::same(this), + boost::contract::test::detail::unprotected_commas<void, void, + void>::same(x) + ) + BOOST_CONTRACT_POSTCONDITION([&] { + boost::contract::test::detail::unprotected_commas< + void, void, void>::call(); + if(old_x) { + BOOST_CONTRACT_ASSERT(x == *old_x + T(1)); + ++old_checks; + } + }) + ; + ++x; + } + BOOST_CONTRACT_OVERRIDE(next) +}; + +template<typename T> +void next(T& x) { + BOOST_CONTRACT_OLD_PTR_IF_COPYABLE( + typename boost::contract::test::detail::unprotected_commas<T, void, + void>::type1 + )( + old_x, + (boost::contract::test::detail::unprotected_commas<void, void, + void>::same(x)) + ); + BOOST_CONTRACT_FUNCTION() + BOOST_CONTRACT_POSTCONDITION([&] { + boost::contract::test::detail::unprotected_commas<void, void, void> + ::call(); + if(old_x) { + BOOST_CONTRACT_ASSERT(x == *old_x + T(1)); + ++old_checks; + } + }) + ; + ++x; +} + +int main() { + int i = 1; // Test built-in copyable type. + cp c(1); // Test custom copyable type. + ncp n(1); // Test non-copyable type. + + // Test free functions (old values without `v`). + + unsigned cnt = + #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS + 1 + #else + 0 + #endif + ; + + old_checks = 0; + next(i); + BOOST_TEST_EQ(old_checks, cnt); + + old_checks = 0; + next(c); + BOOST_TEST_EQ(old_checks, cnt); + + old_checks = 0; + next(n); + BOOST_TEST_EQ(old_checks, 0u); + + // Test virtual functions (old values with `v`). + + cnt = + #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS + 2 + #else + 0 + #endif + ; + + a<int> ai; + old_checks = 0; + ai.next(i); + BOOST_TEST_EQ(old_checks, cnt); + + a<cp> ac; + old_checks = 0; + ac.next(c); + BOOST_TEST_EQ(old_checks, cnt); + + a<ncp> an; + old_checks = 0; + an.next(n); + BOOST_TEST_EQ(old_checks, 0u); + + return boost::report_errors(); +} + diff --git a/src/boost/libs/contract/test/old/no_macro.cpp b/src/boost/libs/contract/test/old/no_macro.cpp new file mode 100644 index 00000000..d715f6ec --- /dev/null +++ b/src/boost/libs/contract/test/old/no_macro.cpp @@ -0,0 +1,9 @@ + +// 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 + +#define BOOST_CONTRACT_TEST_OLD_PTR_TYPE boost::contract::old_ptr +#include "no_macro.hpp" + diff --git a/src/boost/libs/contract/test/old/no_macro.hpp b/src/boost/libs/contract/test/old/no_macro.hpp new file mode 100644 index 00000000..1e75ac44 --- /dev/null +++ b/src/boost/libs/contract/test/old/no_macro.hpp @@ -0,0 +1,190 @@ + +// 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 + +// Test old values without BOOST_CONTRACT_OLD 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/check.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::check 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::check 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::check 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_OLDS + << "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())); + + #ifndef BOOST_CONTRACT_NO_OLDS + #define BOOST_CONTRACT_TEST_old 1u + #else + #define BOOST_CONTRACT_TEST_old 0u + #endif + + BOOST_TEST_EQ(x.value, 'b'); + BOOST_TEST_EQ(x.copies(), BOOST_CONTRACT_TEST_old); + BOOST_TEST_EQ(x.evals(), BOOST_CONTRACT_TEST_old); + BOOST_TEST_EQ(x.ctors(), x.dtors() + 1); // 1 for local var. + + BOOST_TEST_EQ(y.value, 'a'); + BOOST_TEST_EQ(y.copies(), BOOST_CONTRACT_TEST_old); + BOOST_TEST_EQ(y.evals(), BOOST_CONTRACT_TEST_old); + 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_OLDS + << "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(), BOOST_CONTRACT_TEST_old); + BOOST_TEST_EQ(i.evals(), BOOST_CONTRACT_TEST_old); + BOOST_TEST_EQ(i.ctors(), i.dtors() + 1); // 1 for local var. + + BOOST_TEST_EQ(j.value, 1); + BOOST_TEST_EQ(j.copies(), BOOST_CONTRACT_TEST_old); + BOOST_TEST_EQ(j.evals(), BOOST_CONTRACT_TEST_old); + BOOST_TEST_EQ(j.ctors(), j.dtors() + 1); // 1 for local var. + + #undef BOOST_CONTRACT_TEST_old + return boost::report_errors(); +} + diff --git a/src/boost/libs/contract/test/old/no_macro_if_copyable.cpp b/src/boost/libs/contract/test/old/no_macro_if_copyable.cpp new file mode 100644 index 00000000..88c30c10 --- /dev/null +++ b/src/boost/libs/contract/test/old/no_macro_if_copyable.cpp @@ -0,0 +1,9 @@ + +// 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 + +#define BOOST_CONTRACT_TEST_OLD_PTR_TYPE boost::contract::old_ptr_if_copyable +#include "no_macro.hpp" + diff --git a/src/boost/libs/contract/test/old/no_make_old_error.cpp b/src/boost/libs/contract/test/old/no_make_old_error.cpp new file mode 100644 index 00000000..7d517fa4 --- /dev/null +++ b/src/boost/libs/contract/test/old/no_make_old_error.cpp @@ -0,0 +1,9 @@ + +// 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 + +#define BOOST_CONTRACT_TEST_OLD_PTR_TYPE boost::contract::old_ptr +#include "no_make_old_error.hpp" + diff --git a/src/boost/libs/contract/test/old/no_make_old_error.hpp b/src/boost/libs/contract/test/old/no_make_old_error.hpp new file mode 100644 index 00000000..d637fade --- /dev/null +++ b/src/boost/libs/contract/test/old/no_make_old_error.hpp @@ -0,0 +1,21 @@ + +// 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 + +// 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; + BOOST_CONTRACT_TEST_OLD_PTR_TYPE<int> old_x = boost::contract::copy_old() ? + x : boost::contract::null_old(); // Error (missing make_old(...)). + return 0; +} + diff --git a/src/boost/libs/contract/test/old/no_make_old_if_copyable_error.cpp b/src/boost/libs/contract/test/old/no_make_old_if_copyable_error.cpp new file mode 100644 index 00000000..4800fe8b --- /dev/null +++ b/src/boost/libs/contract/test/old/no_make_old_if_copyable_error.cpp @@ -0,0 +1,9 @@ + +// 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 + +#define BOOST_CONTRACT_TEST_OLD_PTR_TYPE boost::contract::old_ptr_if_copyable +#include "no_make_old_error.hpp" + |