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/function | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.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/function')
12 files changed, 744 insertions, 0 deletions
diff --git a/src/boost/libs/contract/test/function/decl.hpp b/src/boost/libs/contract/test/function/decl.hpp new file mode 100644 index 00000000..360f7c5d --- /dev/null +++ b/src/boost/libs/contract/test/function/decl.hpp @@ -0,0 +1,40 @@ + +#ifndef BOOST_CONTRACT_TEST_FUNCTION_DECL_HPP_ +#define BOOST_CONTRACT_TEST_FUNCTION_DECL_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 with and without pre and post declarations. + +#include "../detail/oteststream.hpp" +#include <boost/contract/function.hpp> +#include <boost/contract/check.hpp> +#include <boost/contract/assert.hpp> + +boost::contract::test::detail::oteststream out; + +bool f_pre = true, f_post = true; +void f() { + boost::contract::check c = boost::contract::function() + #ifndef BOOST_CONTRACT_TEST_NO_F_PRE + .precondition([] { + out << "f::pre" << std::endl; + BOOST_CONTRACT_ASSERT(f_pre); + }) + #endif + .old([] { out << "f::old" << std::endl; }) + #ifndef BOOST_CONTRACT_TEST_NO_F_POST + .postcondition([] { + out << "f::post" << std::endl; + BOOST_CONTRACT_ASSERT(f_post); + }) + #endif + ; + out << "f::body" << std::endl; +} + +#endif // #include guard + diff --git a/src/boost/libs/contract/test/function/decl_post_all.cpp b/src/boost/libs/contract/test/function/decl_post_all.cpp new file mode 100644 index 00000000..59dce860 --- /dev/null +++ b/src/boost/libs/contract/test/function/decl_post_all.cpp @@ -0,0 +1,64 @@ + +// 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 with postconditions. + +#undef BOOST_CONTRACT_TEST_NO_F_POST +#include "decl.hpp" + +#include <boost/detail/lightweight_test.hpp> +#include <sstream> +#include <string> + +std::string ok_f() { + std::ostringstream ok; ok + #ifndef BOOST_CONTRACT_NO_PRECONDITIONS + << "f::pre" << std::endl + #endif + #ifndef BOOST_CONTRACT_NO_OLDS + << "f::old" << std::endl + #endif + << "f::body" << std::endl + #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS + << "f::post" << std::endl // This can fail. + #endif + ; + return ok.str(); +} + +struct err {}; // Global decl so visible in MSVC10 lambdas. + +int main() { + std::ostringstream ok; + + f_post = true; + out.str(""); + f(); + ok.str(""); ok // Test nothing failed. + << ok_f() + ; + BOOST_TEST(out.eq(ok.str())); + + boost::contract::set_postcondition_failure( + [] (boost::contract::from) { throw err(); }); + + f_post = false; + out.str(""); + try { + f(); + #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS + BOOST_TEST(false); + } catch(err const&) { + #endif + ok.str(""); ok + << ok_f() // Test f::post failed. + ; + BOOST_TEST(out.eq(ok.str())); + } catch(...) { BOOST_TEST(false); } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/contract/test/function/decl_post_none.cpp b/src/boost/libs/contract/test/function/decl_post_none.cpp new file mode 100644 index 00000000..514e9858 --- /dev/null +++ b/src/boost/libs/contract/test/function/decl_post_none.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 without postconditions. + +#define BOOST_CONTRACT_TEST_NO_F_POST +#include "decl.hpp" + +#include <boost/detail/lightweight_test.hpp> +#include <sstream> + +int main() { + std::ostringstream ok; ok // Test nothing fails. + #ifndef BOOST_CONTRACT_NO_PRECONDITIONS + << "f::pre" << std::endl + #endif + #ifndef BOOST_CONTRACT_NO_OLDS + << "f::old" << std::endl + #endif + << "f::body" << std::endl + ; + + f_post = true; + out.str(""); + f(); + BOOST_TEST(out.eq(ok.str())); + + f_post = false; + out.str(""); + f(); + BOOST_TEST(out.eq(ok.str())); + + return boost::report_errors(); +} + diff --git a/src/boost/libs/contract/test/function/decl_pre_all.cpp b/src/boost/libs/contract/test/function/decl_pre_all.cpp new file mode 100644 index 00000000..1ff33a55 --- /dev/null +++ b/src/boost/libs/contract/test/function/decl_pre_all.cpp @@ -0,0 +1,73 @@ + +// 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 with preconditions. + +#undef BOOST_CONTRACT_TEST_NO_F_PRE +#include "decl.hpp" + +#include <boost/detail/lightweight_test.hpp> +#include <sstream> +#include <string> + +std::string ok_f(bool failed = false) { + std::ostringstream ok; ok << "" // Suppress a warning. + #ifndef BOOST_CONTRACT_NO_PRECONDITIONS + << "f::pre" << std::endl // Test no failure here. + #endif + ; + if(!failed) ok + #ifndef BOOST_CONTRACT_NO_OLDS + << "f::old" << std::endl + #endif + << "f::body" << std::endl + #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS + << "f::post" << std::endl + #endif + ; + return ok.str(); +} + +struct err {}; // Global decl so visible in MSVC10 lambdas. + +int main() { + std::ostringstream ok; + + f_pre = true; + out.str(""); + f(); + ok.str(""); ok + << ok_f() + ; + BOOST_TEST(out.eq(ok.str())); + + #ifdef BOOST_CONTRACT_NO_PRECONDITIONS + #define BOOST_CONTRACT_TEST_pre 0 + #else + #define BOOST_CONTRACT_TEST_pre 1 + #endif + + boost::contract::set_precondition_failure( + [] (boost::contract::from) { throw err(); }); + + f_pre = false; + out.str(""); + try { + f(); + #ifndef BOOST_CONTRACT_NO_PRECONDITIONS + BOOST_TEST(false); + } catch(err const&) { + #endif + ok.str(""); ok + << ok_f(BOOST_CONTRACT_TEST_pre) + ; + BOOST_TEST(out.eq(ok.str())); + } catch(...) { BOOST_TEST(false); } + + #undef BOOST_CONTRACT_TEST_pre + return boost::report_errors(); +} + diff --git a/src/boost/libs/contract/test/function/decl_pre_none.cpp b/src/boost/libs/contract/test/function/decl_pre_none.cpp new file mode 100644 index 00000000..a9c51132 --- /dev/null +++ b/src/boost/libs/contract/test/function/decl_pre_none.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 without preconditions. + +#define BOOST_CONTRACT_TEST_NO_F_PRE +#include "decl.hpp" + +#include <boost/detail/lightweight_test.hpp> +#include <sstream> + +int main() { + std::ostringstream ok; ok // Test nothing fails. + #ifndef BOOST_CONTRACT_NO_OLDS + << "f::old" << std::endl + #endif + << "f::body" << std::endl + #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS + << "f::post" << std::endl + #endif + ; + + f_pre = true; + out.str(""); + f(); + BOOST_TEST(out.eq(ok.str())); + + f_pre = false; + out.str(""); + f(); + BOOST_TEST(out.eq(ok.str())); + + return boost::report_errors(); +} + diff --git a/src/boost/libs/contract/test/function/ifdef.cpp b/src/boost/libs/contract/test/function/ifdef.cpp new file mode 100644 index 00000000..fe9a1cee --- /dev/null +++ b/src/boost/libs/contract/test/function/ifdef.cpp @@ -0,0 +1,58 @@ + +// 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 contract compilation on/off. + +#include "../detail/oteststream.hpp" +#include <boost/contract/core/config.hpp> +#ifndef BOOST_CONTRACT_NO_FUNCTIONS + #include <boost/contract/function.hpp> + #include <boost/contract/check.hpp> + #include <boost/contract/old.hpp> +#endif +#include <boost/detail/lightweight_test.hpp> +#include <sstream> + +boost::contract::test::detail::oteststream out; + +void f(int x) { + #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS + boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(x); + #endif + #ifndef BOOST_CONTRACT_NO_FUNCTIONS + boost::contract::check c = boost::contract::function() + #ifndef BOOST_CONTRACT_NO_PRECONDITIONS + .precondition([] { out << "f::pre" << std::endl; }) + #endif + #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS + .old([] { out << "f::old" << std::endl; }) + .postcondition([] { out << "f::post" << std::endl; }) + #endif + ; + #endif + out << "f::body" << std::endl; +} + +int main() { + std::ostringstream ok; + out.str(""); + f(123); + ok.str(""); ok + #ifndef BOOST_CONTRACT_NO_PRECONDITIONS + << "f::pre" << std::endl + #endif + #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS + << "f::old" << std::endl + #endif + << "f::body" << std::endl + #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS + << "f::post" << std::endl + #endif + ; + BOOST_TEST(out.eq(ok.str())); + return boost::report_errors(); +} + diff --git a/src/boost/libs/contract/test/function/ifdef_macro.cpp b/src/boost/libs/contract/test/function/ifdef_macro.cpp new file mode 100644 index 00000000..ffd7e668 --- /dev/null +++ b/src/boost/libs/contract/test/function/ifdef_macro.cpp @@ -0,0 +1,95 @@ + +// 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 contract compilation on/off (including EXCEPT, using macro interface). + +#include "../detail/oteststream.hpp" +#include "../detail/unprotected_commas.hpp" +#include <boost/contract_macro.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <sstream> + +boost::contract::test::detail::oteststream out; + +struct except_error {}; + +void f(int x) { + BOOST_CONTRACT_OLD_PTR( + boost::contract::test::detail::unprotected_commas<int, void, void>:: + type1 + )( + old_x, + (boost::contract::test::detail::unprotected_commas<void, void, void>:: + same(x)) + ); + BOOST_CONTRACT_FUNCTION() + BOOST_CONTRACT_PRECONDITION([] { + boost::contract::test::detail::unprotected_commas<void, void, void> + ::call(); + out << "f::pre" << std::endl; + }) + BOOST_CONTRACT_OLD([] { + boost::contract::test::detail::unprotected_commas<void, void, void> + ::call(); + out << "f::old" << std::endl; + }) + BOOST_CONTRACT_POSTCONDITION([] { + boost::contract::test::detail::unprotected_commas<void, void, void> + ::call(); + out << "f::post" << std::endl; + }) + BOOST_CONTRACT_EXCEPT([] { // Test EXCEPT macro (at least 1 time here). + boost::contract::test::detail::unprotected_commas<void, void, void> + ::call(); + out << "f::except" << std::endl; + }) + ; + out << "f::body" << std::endl; + if(x == -1) throw except_error(); +} + +int main() { + std::ostringstream ok; + + out.str(""); + f(123); + ok.str(""); ok + #ifndef BOOST_CONTRACT_NO_PRECONDITIONS + << "f::pre" << std::endl + #endif + #ifndef BOOST_CONTRACT_NO_OLDS + << "f::old" << std::endl + #endif + << "f::body" << std::endl + #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS + << "f::post" << std::endl + #endif + ; + BOOST_TEST(out.eq(ok.str())); + + boost::contract::set_except_failure([] (boost::contract::from) {}); + out.str(""); + try { + f(-1); // Test throw and EXCEPT macro (test only here... that's fine). + BOOST_TEST(false); + } catch(except_error const&) {} // OK. + ok.str(""); ok + #ifndef BOOST_CONTRACT_NO_PRECONDITIONS + << "f::pre" << std::endl + #endif + #ifndef BOOST_CONTRACT_NO_OLDS + << "f::old" << std::endl + #endif + << "f::body" << std::endl + #ifndef BOOST_CONTRACT_NO_EXCEPTS + << "f::except" << std::endl + #endif + ; + BOOST_TEST(out.eq(ok.str())); + + return boost::report_errors(); +} + diff --git a/src/boost/libs/contract/test/function/smoke.cpp b/src/boost/libs/contract/test/function/smoke.cpp new file mode 100644 index 00000000..a4a733fd --- /dev/null +++ b/src/boost/libs/contract/test/function/smoke.cpp @@ -0,0 +1,99 @@ + +// 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 free function contracts. + +#include "../detail/oteststream.hpp" +#include "../detail/counter.hpp" +#include <boost/contract/function.hpp> +#include <boost/contract/old.hpp> +#include <boost/contract/assert.hpp> +#include <boost/contract/check.hpp> +#include <boost/preprocessor/control/iif.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <sstream> + +boost::contract::test::detail::oteststream out; + +struct x_tag; typedef boost::contract::test::detail::counter<x_tag, int> x_type; +struct y_tag; typedef boost::contract::test::detail::counter<y_tag, int> y_type; + +bool swap(x_type& x, y_type& y) { + bool result; + boost::contract::old_ptr<x_type> old_x = + BOOST_CONTRACT_OLDOF(x_type::eval(x)); + boost::contract::old_ptr<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_OLDOF(y_type::eval(y)); + }) + .postcondition([&] { + out << "swap::post" << std::endl; + BOOST_CONTRACT_ASSERT(x.value == old_y->value); + BOOST_CONTRACT_ASSERT(y.value == old_x->value); + BOOST_CONTRACT_ASSERT(result == (old_x->value != old_y->value)); + }) + ; + + out << "swap::body" << std::endl; + if(x.value == y.value) return result = false; + int save_x = x.value; + x.value = y.value; + y.value = save_x; + return result = true; +} + +int main() { + std::ostringstream ok; + + { + x_type x; x.value = 123; + y_type y; y.value = 456; + + out.str(""); + bool r = 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())); + + BOOST_TEST(r); + BOOST_TEST_EQ(x.value, 456); + BOOST_TEST_EQ(y.value, 123); + } + + #ifndef BOOST_CONTRACT_NO_OLDS + #define BOOST_CONTRACT_TEST_old 1u + #else + #define BOOST_CONTRACT_TEST_old 0u + #endif + + BOOST_TEST_EQ(x_type::copies(), BOOST_CONTRACT_TEST_old); + BOOST_TEST_EQ(x_type::evals(), BOOST_CONTRACT_TEST_old); + BOOST_TEST_EQ(x_type::ctors(), x_type::dtors()); // No leak. + + BOOST_TEST_EQ(y_type::copies(), BOOST_CONTRACT_TEST_old); + BOOST_TEST_EQ(y_type::evals(), BOOST_CONTRACT_TEST_old); + BOOST_TEST_EQ(y_type::ctors(), y_type::dtors()); // No leak. + + #undef BOOST_CONTRACT_TEST_old + return boost::report_errors(); +} + diff --git a/src/boost/libs/contract/test/function/throwing_body.cpp b/src/boost/libs/contract/test/function/throwing_body.cpp new file mode 100644 index 00000000..7a64ba1d --- /dev/null +++ b/src/boost/libs/contract/test/function/throwing_body.cpp @@ -0,0 +1,55 @@ + +// 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 throw from free function body. + +#include "../detail/oteststream.hpp" +#include <boost/contract/function.hpp> +#include <boost/contract/check.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <sstream> + +boost::contract::test::detail::oteststream out; + +struct err {}; // Global decl so visible in MSVC10 lambdas. + +void f() { + boost::contract::check c = boost::contract::function() + .precondition([] { out << "f::pre" << std::endl; }) + .old([] { out << "f::old" << std::endl; }) + .postcondition([] { out << "f::post" << std::endl; }) + .except([] { out << "f::except" << std::endl; }) + ; + out << "f::body" << std::endl; + throw err(); // Test body throws. +} + +int main() { + std::ostringstream ok; + + try { + out.str(""); + f(); + BOOST_TEST(false); + } catch(err const&) { + ok.str(""); ok + #ifndef BOOST_CONTRACT_NO_PRECONDITIONS + << "f::pre" << std::endl + #endif + #ifndef BOOST_CONTRACT_NO_OLDS + << "f::old" << std::endl + #endif + << "f::body" << std::endl // Test this threw. + #ifndef BOOST_CONTRACT_NO_EXCEPTS + << "f::except" << std::endl; + #endif + ; + BOOST_TEST(out.eq(ok.str())); + } catch(...) { BOOST_TEST(false); } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/contract/test/function/throwing_old.cpp b/src/boost/libs/contract/test/function/throwing_old.cpp new file mode 100644 index 00000000..9bae8662 --- /dev/null +++ b/src/boost/libs/contract/test/function/throwing_old.cpp @@ -0,0 +1,59 @@ + +// 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 throw from free function .old(). + +#include "../detail/oteststream.hpp" +#include <boost/contract/function.hpp> +#include <boost/contract/check.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <sstream> + +boost::contract::test::detail::oteststream out; + +struct err {}; // Global decl so visible in MSVC10 lambdas. + +void f() { + boost::contract::check c = boost::contract::function() + .precondition([] { out << "f::pre" << std::endl; }) + .old([] { + out << "f::old" << std::endl; + throw err(); // Test this throws. + }) + .postcondition([] { out << "f::post" << std::endl; }) + .except([] { out << "f::except" << std::endl; }) + ; + out << "f::body" << std::endl; +} + +int main() { + std::ostringstream ok; + + boost::contract::set_old_failure([] (boost::contract::from) { throw; }); + + try { + out.str(""); + f(); + #ifndef BOOST_CONTRACT_NO_OLDS + BOOST_TEST(false); + } catch(err const&) { + #endif + ok.str(""); ok + #ifndef BOOST_CONTRACT_NO_PRECONDITIONS + << "f::pre" << std::endl + #endif + #ifndef BOOST_CONTRACT_NO_OLDS + << "f::old" << std::endl // Test this threw. + #else + << "f::body" << std::endl + #endif + ; + BOOST_TEST(out.eq(ok.str())); + } catch(...) { BOOST_TEST(false); } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/contract/test/function/throwing_post.cpp b/src/boost/libs/contract/test/function/throwing_post.cpp new file mode 100644 index 00000000..16d24813 --- /dev/null +++ b/src/boost/libs/contract/test/function/throwing_post.cpp @@ -0,0 +1,62 @@ + +// 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 throw from free function .post(). + +#include "../detail/oteststream.hpp" +#include <boost/contract/function.hpp> +#include <boost/contract/check.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <sstream> + +boost::contract::test::detail::oteststream out; + +struct err {}; // Global decl so visible in MSVC10 lambdas. + +void f() { + boost::contract::check c = boost::contract::function() + .precondition([] { out << "f::pre" << std::endl; }) + .old([] { out << "f::old" << std::endl; }) + .postcondition([] { + out << "f::post" << std::endl; + throw err(); // Test this throws. + }) + .except([] { out << "f::except" << std::endl; }) + ; + out << "f::body" << std::endl; +} + +int main() { + std::ostringstream ok; + + boost::contract::set_postcondition_failure( + [] (boost::contract::from) { throw; }); + + try { + out.str(""); + f(); + #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS + BOOST_TEST(false); + } catch(err const&) { + #endif + ok.str(""); ok << "" // Suppress a warning. + #ifndef BOOST_CONTRACT_NO_PRECONDITIONS + << "f::pre" << std::endl + #endif + #ifndef BOOST_CONTRACT_NO_OLDS + << "f::old" << std::endl + #endif + << "f::body" << std::endl + #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS + << "f::post" << std::endl // Test this threw. + #endif + ; + BOOST_TEST(out.eq(ok.str())); + } catch(...) { BOOST_TEST(false); } + + return boost::report_errors(); +} + diff --git a/src/boost/libs/contract/test/function/throwing_pre.cpp b/src/boost/libs/contract/test/function/throwing_pre.cpp new file mode 100644 index 00000000..3f2669d0 --- /dev/null +++ b/src/boost/libs/contract/test/function/throwing_pre.cpp @@ -0,0 +1,63 @@ + +// 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 throw from free function .pre(). + +#include "../detail/oteststream.hpp" +#include <boost/contract/function.hpp> +#include <boost/contract/check.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <sstream> + +boost::contract::test::detail::oteststream out; + +struct err {}; // Global decl so visible in MSVC10 lambdas. + +void f() { + boost::contract::check c = boost::contract::function() + .precondition([] { + out << "f::pre" << std::endl; + throw err(); // Test this throws. + }) + .old([] { out << "f::old" << std::endl; }) + .postcondition([] { out << "f::post" << std::endl; }) + .except([] { out << "f::except" << std::endl; }) + ; + out << "f::body" << std::endl; +} + +int main() { + std::ostringstream ok; + + boost::contract::set_precondition_failure( + [] (boost::contract::from) { throw; }); + + try { + out.str(""); + f(); + #ifndef BOOST_CONTRACT_NO_PRECONDITIONS + BOOST_TEST(false); + } catch(err const&) { + #endif + ok.str(""); ok + #ifndef BOOST_CONTRACT_NO_PRECONDITIONS + << "f::pre" << std::endl // Test this threw. + #else + #ifndef BOOST_CONTRACT_NO_OLDS + << "f::old" << std::endl + #endif + << "f::body" << std::endl + #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS + << "f::post" << std::endl + #endif + #endif + ; + BOOST_TEST(out.eq(ok.str())); + } catch(...) { BOOST_TEST(false); } + + return boost::report_errors(); +} + |