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/spirit/test/x3/optional.cpp | |
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/spirit/test/x3/optional.cpp')
-rw-r--r-- | src/boost/libs/spirit/test/x3/optional.cpp | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/src/boost/libs/spirit/test/x3/optional.cpp b/src/boost/libs/spirit/test/x3/optional.cpp new file mode 100644 index 00000000..cd7f6d32 --- /dev/null +++ b/src/boost/libs/spirit/test/x3/optional.cpp @@ -0,0 +1,113 @@ +/*============================================================================= + Copyright (c) 2001-2015 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 <boost/detail/lightweight_test.hpp> +#include <boost/spirit/home/x3.hpp> +#include <boost/fusion/adapted/struct.hpp> +#include <boost/fusion/include/vector.hpp> + +#include <iostream> +#include "test.hpp" +#include "utils.hpp" + +struct adata +{ + int a; + boost::optional<int> b; +}; + +BOOST_FUSION_ADAPT_STRUCT(adata, + a, b +) + +struct test_attribute_type +{ + template <typename Context> + void operator()(Context& ctx) const + { + BOOST_TEST(typeid(decltype(_attr(ctx))).name() == typeid(boost::optional<int>).name()); + } +}; + +int +main() +{ + using boost::spirit::x3::traits::is_optional; + + static_assert(is_optional<boost::optional<int>>(), "is_optional problem"); + + using spirit_test::test; + using spirit_test::test_attr; + + using boost::spirit::x3::int_; + using boost::spirit::x3::omit; + using boost::spirit::x3::ascii::char_; + + { + BOOST_TEST((test("1234", -int_))); + BOOST_TEST((test("abcd", -int_, false))); + } + + { // test propagation of unused + using boost::fusion::at_c; + using boost::fusion::vector; + + vector<char, char> v; + BOOST_TEST((test_attr("a1234c", char_ >> -omit[int_] >> char_, v))); + BOOST_TEST((at_c<0>(v) == 'a')); + BOOST_TEST((at_c<1>(v) == 'c')); + + v = boost::fusion::vector<char, char>(); + BOOST_TEST((test_attr("a1234c", char_ >> omit[-int_] >> char_, v))); + BOOST_TEST((at_c<0>(v) == 'a')); + BOOST_TEST((at_c<1>(v) == 'c')); + + char ch; + BOOST_TEST((test_attr(",c", -(',' >> char_), ch))); + BOOST_TEST((ch == 'c')); + } + + { // test action + boost::optional<int> n = 0; + BOOST_TEST((test_attr("1234", (-int_)[test_attribute_type()], n))); + BOOST_TEST((n.get() == 1234)); + } + + { + std::string s; + BOOST_TEST((test_attr("abc", char_ >> -(char_ >> char_), s))); + BOOST_TEST(s == "abc"); + } + + { + boost::optional<int> n = 0; + auto f = [&](auto& ctx){ n = _attr(ctx); }; + + BOOST_TEST((test("1234", (-int_)[f]))); + BOOST_TEST(n.get() == 1234); + + n = boost::optional<int>(); + BOOST_TEST((test("abcd", (-int_)[f], false))); + BOOST_TEST(!n); + } + + { + std::vector<adata> v; + BOOST_TEST((test_attr("a 1 2 a 2", *('a' >> int_ >> -int_), v + , char_(' ')))); + BOOST_TEST(2 == v.size() && + 1 == v[0].a && v[0].b && 2 == *(v[0].b) && + 2 == v[1].a && !v[1].b); + } + + { // test move only types + boost::optional<move_only> o; + BOOST_TEST(test_attr("s", -synth_move_only, o)); + BOOST_TEST(o); + } + + return boost::report_errors(); +} |