]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/x3/plus.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / test / x3 / plus.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2015 Joel de Guzman
3
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #include <string>
8 #include <vector>
9
10 #include <boost/detail/lightweight_test.hpp>
11 #include <boost/spirit/home/x3.hpp>
12 #include <boost/fusion/include/vector.hpp>
13
14 #include <string>
15 #include <iostream>
16 #include "test.hpp"
17
18 struct x_attr
19 {
20 };
21
22 namespace boost { namespace spirit { namespace x3 { namespace traits
23 {
24 template <>
25 struct container_value<x_attr>
26 {
27 typedef char type; // value type of container
28 };
29
30 template <>
31 struct push_back_container<x_attr>
32 {
33 static bool call(x_attr& /*c*/, char /*val*/)
34 {
35 // push back value type into container
36 return true;
37 }
38 };
39 }}}}
40
41 int
42 main()
43 {
44 using spirit_test::test;
45 using spirit_test::test_attr;
46 using boost::spirit::x3::char_;
47 using boost::spirit::x3::alpha;
48 using boost::spirit::x3::upper;
49 using boost::spirit::x3::space;
50 using boost::spirit::x3::digit;
51 //~ using boost::spirit::x3::no_case;
52 using boost::spirit::x3::int_;
53 using boost::spirit::x3::omit;
54 using boost::spirit::x3::lit;
55 //~ using boost::spirit::x3::_1;
56 using boost::spirit::x3::lexeme;
57
58 {
59 BOOST_TEST(test("aaaaaaaa", +char_));
60 BOOST_TEST(test("a", +char_));
61 BOOST_TEST(!test("", +char_));
62 BOOST_TEST(test("aaaaaaaa", +alpha));
63 BOOST_TEST(!test("aaaaaaaa", +upper));
64 }
65
66 {
67 BOOST_TEST(test(" a a aaa aa", +char_, space));
68 BOOST_TEST(test("12345 678 9 ", +digit, space));
69 }
70
71 //~ {
72 //~ BOOST_TEST(test("aBcdeFGH", no_case[+char_]));
73 //~ BOOST_TEST(test("a B cde FGH ", no_case[+char_], space));
74 //~ }
75
76 {
77 std::vector<int> v;
78 BOOST_TEST(test_attr("123 456 789 10", +int_, v, space) && 4 == v.size() &&
79 v[0] == 123 && v[1] == 456 && v[2] == 789 && v[3] == 10);
80 }
81
82 {
83 std::vector<std::string> v;
84 BOOST_TEST(test_attr("a b c d", +lexeme[+alpha], v, space) && 4 == v.size() &&
85 v[0] == "a" && v[1] == "b" && v[2] == "c" && v[3] == "d");
86 }
87
88 {
89 BOOST_TEST(test("Kim Kim Kim", +lit("Kim"), space));
90 }
91
92 // $$$ Fixme $$$
93 /*{
94 // The following 2 tests show that omit does not inhibit explicit attributes
95
96 std::string s;
97 BOOST_TEST(test_attr("bbbb", omit[+char_('b')], s) && s == "bbbb");
98
99 s.clear();
100 BOOST_TEST(test_attr("b b b b ", omit[+char_('b')], s, space) && s == "bbbb");
101 }*/
102
103 { // actions
104 std::string v;
105 auto f = [&](auto& ctx){ v = _attr(ctx); };
106
107 BOOST_TEST(test("bbbb", (+char_)[f]) && 4 == v.size() &&
108 v[0] == 'b' && v[1] == 'b' && v[2] == 'b' && v[3] == 'b');
109 }
110
111 { // more actions
112 std::vector<int> v;
113 auto f = [&](auto& ctx){ v = _attr(ctx); };
114
115 BOOST_TEST(test("1 2 3", (+int_)[f], space) && 3 == v.size() &&
116 v[0] == 1 && v[1] == 2 && v[2] == 3);
117 }
118
119 { // attribute customization
120
121 x_attr x;
122 test_attr("abcde", +char_, x);
123 }
124
125 // single-element fusion vector tests
126 {
127 boost::fusion::vector<std::string> fs;
128 BOOST_TEST((test_attr("12345", +char_, fs))); // ok
129 BOOST_TEST(boost::fusion::at_c<0>(fs) == "12345");
130 }
131
132 return boost::report_errors();
133 }