From 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 20:24:20 +0200 Subject: Adding upstream version 14.2.21. Signed-off-by: Daniel Baumann --- src/boost/libs/spirit/test/qi/optional.cpp | 111 +++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/boost/libs/spirit/test/qi/optional.cpp (limited to 'src/boost/libs/spirit/test/qi/optional.cpp') diff --git a/src/boost/libs/spirit/test/qi/optional.cpp b/src/boost/libs/spirit/test/qi/optional.cpp new file mode 100644 index 00000000..22855dee --- /dev/null +++ b/src/boost/libs/spirit/test/qi/optional.cpp @@ -0,0 +1,111 @@ +/*============================================================================= + Copyright (c) 2001-2011 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "test.hpp" + +struct adata +{ + int a; + boost::optional b; +}; + +BOOST_FUSION_ADAPT_STRUCT( + adata, + (int, a) + (boost::optional, b) +) + +struct test_attribute_type +{ + template + void operator()(Attribute&, Context&, bool&) const + { + BOOST_TEST(typeid(Attribute).name() == typeid(boost::optional).name()); + } +}; + +int +main() +{ + using spirit_test::test; + using spirit_test::test_attr; + + using boost::spirit::qi::_1; + using boost::spirit::qi::int_; + using boost::spirit::qi::omit; + using boost::spirit::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 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(); + 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 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"); + } + + { + namespace phx = boost::phoenix; + + boost::optional n = 0; + BOOST_TEST((test("1234", (-int_)[phx::ref(n) = _1]))); + BOOST_TEST(n.get() == 1234); + + n = boost::optional(); + BOOST_TEST((test("abcd", (-int_)[phx::ref(n) = _1], false))); + BOOST_TEST(!n); + } + + { + std::vector 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); + } + + return boost::report_errors(); +} -- cgit v1.2.3