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/phoenix/test/statement | |
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/phoenix/test/statement')
-rw-r--r-- | src/boost/libs/phoenix/test/statement/bug5715.cpp | 37 | ||||
-rw-r--r-- | src/boost/libs/phoenix/test/statement/exceptions.cpp | 194 | ||||
-rw-r--r-- | src/boost/libs/phoenix/test/statement/if_tests.cpp | 70 | ||||
-rw-r--r-- | src/boost/libs/phoenix/test/statement/loops_tests.cpp | 85 | ||||
-rw-r--r-- | src/boost/libs/phoenix/test/statement/switch_tests.cpp | 76 |
5 files changed, 462 insertions, 0 deletions
diff --git a/src/boost/libs/phoenix/test/statement/bug5715.cpp b/src/boost/libs/phoenix/test/statement/bug5715.cpp new file mode 100644 index 00000000..4e99d928 --- /dev/null +++ b/src/boost/libs/phoenix/test/statement/bug5715.cpp @@ -0,0 +1,37 @@ +/*============================================================================= + Copyright (c) 2005-2007 Dan Marsden + Copyright (c) 2005-2007 Joel de Guzman + Copyright (c) 2014 John Fletcher + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +==============================================================================*/ + +// Check for Bug5715 + +#include <boost/phoenix/statement/sequence.hpp> +#include <boost/phoenix/bind.hpp> + +#include <boost/core/lightweight_test.hpp> + +namespace test +{ + int x = 0; + int y = 0; + int z = 0; + + void f() { ++x; ++y; } + void g() { --x; ++z; } + +} +int main() +{ + ( + boost::phoenix::bind(test::f), + boost::phoenix::bind(test::g) + )(); + BOOST_TEST(test::x == 0); + BOOST_TEST(test::y == 1); + BOOST_TEST(test::z == 1); + return boost::report_errors(); +} diff --git a/src/boost/libs/phoenix/test/statement/exceptions.cpp b/src/boost/libs/phoenix/test/statement/exceptions.cpp new file mode 100644 index 00000000..d2584f1d --- /dev/null +++ b/src/boost/libs/phoenix/test/statement/exceptions.cpp @@ -0,0 +1,194 @@ +/*============================================================================= + Copyright (c) 2005-2007 Dan Marsden + Copyright (c) 2005-2007 Joel de Guzman + Copyright (c) 2015 John Fletcher + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +==============================================================================*/ + +#include <stdexcept> +#include <string> + +#include <boost/phoenix/core.hpp> +#include <boost/phoenix/operator.hpp> +#include <boost/phoenix/statement.hpp> +#include <boost/phoenix/scope/local_variable.hpp> +#include <boost/phoenix/bind/bind_member_function.hpp> + +#include <boost/detail/lightweight_test.hpp> + +struct base_exception : std::exception +{ + explicit + base_exception(char const *msg) + : _sliced(true), _what(msg) + {} + + base_exception(base_exception const &other) + : _sliced(true), _what(other._what) + { + } + + char const *what() const BOOST_NOEXCEPT_OR_NOTHROW + { + if (_sliced) { return "sliced ..."; } + return _what; + } + + bool _sliced; + char const *_what; +}; + +struct extended_exception : base_exception +{ + explicit + extended_exception(char const *msg) + : base_exception(msg) + { + // mark as not sliced + _sliced = false; + } + + extended_exception(extended_exception const &other) + : base_exception(other) + { + // mark as not sliced + _sliced = false; + } +}; + +int main() +{ + using boost::phoenix::throw_; + using boost::phoenix::try_; + using boost::phoenix::ref; + using boost::phoenix::local_names::_e; + using boost::phoenix::bind; + using std::exception; + using std::string; + using std::runtime_error; + namespace phx = boost::phoenix; + + { + try + { + throw_(runtime_error("error"))(); + BOOST_ERROR("exception should have been thrown"); + } + catch(runtime_error& err) + { + BOOST_TEST(err.what() == string("error")); + } + } + + { + try + { + try + { + throw runtime_error("error"); + } + catch(exception&) + { + throw_()(); + BOOST_ERROR("exception should have been rethrown"); + } + } + catch(exception& err) + { + BOOST_TEST(err.what() == string("error")); + } + } + + { + bool caught_exception = false; + + try_ + [ throw_(runtime_error("error")) ] + .catch_<exception>(_e) // captured but unused + [ + ref(caught_exception) = true + ](); + + BOOST_TEST(caught_exception); + } + + { + bool caught_exception = false; + string what; + + try_ + [ throw_(runtime_error("error")) ] + .catch_<exception>(_e) + [ + ref(caught_exception) = true + // ambiguous with std::ref + , phx::ref(what) = phx::bind(&exception::what, _e) + ](); + + BOOST_TEST(caught_exception); + BOOST_TEST(what == string("error")); + } + + { + bool caught_exception = false; + string what; + + try_ + [ throw_(extended_exception("error")) ] + .catch_<base_exception>(_e) // A thrown object should not be copied due to slicing. + [ + ref(caught_exception) = true + // ambiguous with std::ref + , phx::ref(what) = phx::bind(&exception::what, _e) + ](); + + BOOST_TEST(caught_exception); + BOOST_TEST(what == string("error")); + } + + { + bool caught_exception = false; + + try_ + [ throw_(runtime_error("error")) ] + .catch_all + [ ref(caught_exception) = true ](); + BOOST_TEST(caught_exception); + } + + { + bool caught_correct_exception = false; + string what; + + try_ + [ throw_(runtime_error("error")) ] + .catch_<string>() + [ ref(caught_correct_exception) = false ] + .catch_<exception>(_e) + [ + ref(caught_correct_exception) = true + // ambiguous with std::ref + , phx::ref(what) = phx::bind(&exception::what, _e) + ](); + + BOOST_TEST(caught_correct_exception); + BOOST_TEST(what == string("error")); + } + + { + bool caught_correct_exception = false; + + try_ + [ throw_(runtime_error("error")) ] + .catch_<string>() + [ ref(caught_correct_exception) = false ] + .catch_all + [ ref(caught_correct_exception) = true](); + + BOOST_TEST(caught_correct_exception); + } + + return boost::report_errors(); +} diff --git a/src/boost/libs/phoenix/test/statement/if_tests.cpp b/src/boost/libs/phoenix/test/statement/if_tests.cpp new file mode 100644 index 00000000..1828a49d --- /dev/null +++ b/src/boost/libs/phoenix/test/statement/if_tests.cpp @@ -0,0 +1,70 @@ +/*============================================================================= + Copyright (c) 2001-2007 Joel de Guzman + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +==============================================================================*/ +#include <iostream> +#include <vector> +#include <algorithm> +#include <boost/detail/lightweight_test.hpp> +#include <boost/phoenix/core.hpp> +#include <boost/phoenix/statement.hpp> +#include <boost/phoenix/operator.hpp> + +int +main() +{ + using boost::phoenix::arg_names::arg1; + using boost::phoenix::if_; + using boost::phoenix::ref; + + int init[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + std::vector<int> v(init, init+10); + + std::cout << std::dec; + int x = 0; + + for_each(v.begin(), v.end(), + if_(arg1 > 3 && arg1 <= 8) + [ + std::cout << arg1 << ", ", + ref(x) += arg1 + ] + ); + + std::cout << std::endl; + BOOST_TEST(x == 4+5+6+7+8); + + x = 0; + int y = 0; + int z = 0; + + for_each(v.begin(), v.end(), + if_(arg1 > 5) + [ + std::cout << arg1 << " > 5\n", + ref(x) += arg1 + ] + .else_ + [ + if_(arg1 == 5) + [ + std::cout << arg1 << " == 5\n", + ref(z) += arg1 + ] + .else_ + [ + std::cout << arg1 << " < 5\n", + ref(y) += arg1 + ] + ] + ); + + std::cout << std::endl; + BOOST_TEST(x == 6+7+8+9+10); + BOOST_TEST(y == 1+2+3+4); + BOOST_TEST(z == 5); + + return boost::report_errors(); +} diff --git a/src/boost/libs/phoenix/test/statement/loops_tests.cpp b/src/boost/libs/phoenix/test/statement/loops_tests.cpp new file mode 100644 index 00000000..f3f2743e --- /dev/null +++ b/src/boost/libs/phoenix/test/statement/loops_tests.cpp @@ -0,0 +1,85 @@ +/*============================================================================= + Copyright (c) 2001-2007 Joel de Guzman + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +==============================================================================*/ +#include <iostream> +#include <vector> +#include <algorithm> +#include <boost/detail/lightweight_test.hpp> +#include <boost/phoenix/core.hpp> +#include <boost/phoenix/statement.hpp> +#include <boost/phoenix/operator.hpp> + +int +main() +{ + using boost::phoenix::arg_names::arg1; + using boost::phoenix::do_; + using boost::phoenix::ref; + using boost::phoenix::val; + + using std::cout; + using std::endl; + using std::for_each; + using std::vector; + + int init[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + vector<int> v(init, init+10); + vector<int> t = v; + cout << endl; + int x = 0; + + for_each(v.begin(), v.end(), + ( + while_(arg1--) + [ + cout << arg1 << ", ", + ++ref(x) + ], + cout << val("\n") + ) + ); + + BOOST_TEST(x == 1+2+3+4+5+6+7+8+9+10); + cout << endl; + v = t; + x = 0; + + for_each(v.begin(), v.end(), + ( + do_ + [ + cout << arg1 << ", ", + ++ref(x) + ] + .while_(arg1--), + cout << val("\n") + ) + ); + + BOOST_TEST(x == 2+3+4+5+6+7+8+9+10+11); + cout << endl; + v = t; + x = 0; + + int iii; + for_each(v.begin(), v.end(), + ( + for_(ref(iii) = 0, ref(iii) < arg1, ++ref(iii)) + [ + cout << arg1 << ", ", + ++ref(x) + ], + cout << val("\n") + ) + ); + + BOOST_TEST(x == 1+2+3+4+5+6+7+8+9+10); + cout << endl; + v = t; + x = 0; + + return boost::report_errors(); +} diff --git a/src/boost/libs/phoenix/test/statement/switch_tests.cpp b/src/boost/libs/phoenix/test/statement/switch_tests.cpp new file mode 100644 index 00000000..d7247982 --- /dev/null +++ b/src/boost/libs/phoenix/test/statement/switch_tests.cpp @@ -0,0 +1,76 @@ +/*============================================================================= + Copyright (c) 2001-2007 Joel de Guzman + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +==============================================================================*/ + +#include <iostream> +#include <vector> +#include <algorithm> +#include <boost/detail/lightweight_test.hpp> +#include <boost/phoenix/core.hpp> +#include <boost/phoenix/statement.hpp> +#include <boost/phoenix/operator.hpp> + +int +main() +{ + using boost::phoenix::arg_names::_1; + using boost::phoenix::case_; + using boost::phoenix::switch_; + using boost::phoenix::ref; + using boost::phoenix::val; + + using std::cout; + using std::endl; + using std::vector; + using std::for_each; + + int init[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + vector<int> v(init, init+10); + + for_each(v.begin(), v.end(), + switch_(_1) + [ + // wierd case, why not just use if(...), but valid, nonetheless + case_<4>(cout << val("<4>") << endl) + ] + ); + + cout << endl; + for_each(v.begin(), v.end(), + switch_(_1) + [ + // wierd case, but valid, nonetheless + default_(cout << val("<any...>") << endl) + ] + ); + + cout << endl; + + for_each(v.begin(), v.end(), + switch_(_1) + [ + case_<1>(cout << val("<1>") << endl), + case_<2>(cout << val("<2>") << endl), + case_<3>(cout << val("<3>") << endl), + case_<4>(cout << val("<4>") << endl) + ] + ); + + cout << endl; + + for_each(v.begin(), v.end(), + switch_(_1) + [ + case_<1>(cout << val("<1>") << endl), + case_<2>(cout << val("<2>") << endl), + case_<3>(cout << val("<3>") << endl), + case_<4>(cout << val("<4>") << endl), + default_(cout << val("<over 4>") << endl) + ] + ); + + return boost::report_errors(); +} |