1 /*=============================================================================
2 Copyright (c) 2013 Carl Barron
3 Copyright (c) 2015 Joel de Guzman
4
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8
9 #include <boost/detail/lightweight_test.hpp>
10 #include <boost/spirit/home/x3.hpp>
11 #include <boost/fusion/include/at.hpp>
12 #include <boost/fusion/include/vector.hpp>
13 #include <boost/fusion/include/adapt_struct.hpp>
14 #include <boost/mpl/int.hpp>
15 #include <boost/optional.hpp>
16
17 #include <iostream>
18 #include <numeric>
19 #include <vector>
20 #include "test.hpp"
21
22 struct roman
23 {
24 boost::optional<int> a;
25 boost::optional<int> b;
26 boost::optional<int> c;
27 };
28
BOOST_FUSION_ADAPT_STRUCT(roman,a,b,c)29 BOOST_FUSION_ADAPT_STRUCT(roman,
30 a, b, c
31 )
32
33 int eval(roman const & c)
34 {
35 return c.a.get_value_or(0) + c.b.get_value_or(0) + c.c.get_value_or(0);
36 }
37
38 int
main()39 main()
40 {
41 using spirit_test::test;
42 using spirit_test::test_attr;
43 using boost::spirit::x3::symbols;
44
45 { // construction from initializer-list
46 symbols<int> const ones =
47 {
48 {"I", 1}, {"II", 2}, {"III", 3}, {"IV", 4},
49 {"V", 5}, {"VI", 6}, {"VII", 7}, {"VIII", 8},
50 {"IX", 9}
51 };
52 symbols<int> const tens =
53 {
54 {"X", 10}, {"XX", 20}, {"XXX", 30}, {"XL", 40},
55 {"L", 50}, {"LX", 60}, {"LXX", 70}, {"LXXX", 80},
56 {"XC", 90}
57 };
58 symbols<int> const hundreds
59 {
60 {"C", 100}, {"CC", 200}, {"CCC", 300}, {"CD", 400},
61 {"D", 500}, {"DC", 600}, {"DCC", 700}, {"DCCC", 800},
62 {"CM", 900}
63 };
64
65 auto number = -hundreds >> -tens >> -ones;
66
67 roman r;
68 BOOST_TEST((test_attr("CDXLII", number, r)));
69 BOOST_TEST(eval(r) == 442);
70 }
71
72 { // construction from initializer-list without attribute
73 symbols<> foo = {"a1", "a2", "a3"};
74
75 BOOST_TEST((test("a3", foo)));
76 }
77
78 { // assignment from initializer-list
79 symbols<> foo;
80 foo = {"a1", "a2", "a3"};
81
82 BOOST_TEST((test("a3", foo)));
83 }
84
85 return boost::report_errors();
86 }
87