1 /*=============================================================================
2     Copyright (c) 2003 Jonathan de Halleux (dehalleux@pelikhan.com)
3     http://spirit.sourceforge.net/
4 
5     Use, modification and distribution is subject to the Boost Software
6     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7     http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 ///////////////////////////////////////////////////////////////////////////////
10 // Test suite for insert_at_actor
11 ///////////////////////////////////////////////////////////////////////////////
12 
13 #include "action_tests.hpp"
14 #include <map>
15 #include <cstring>
16 #include <iostream>
17 #include <boost/spirit/include/classic_spirit.hpp>
18 #include <boost/spirit/include/classic_insert_at_actor.hpp>
19 
insert_at_single_argument_test()20 void insert_at_single_argument_test()
21 {
22     using namespace BOOST_SPIRIT_CLASSIC_NS;
23 
24     const char* cp = "(one,0),(two,1),(three,2)";
25     const char* cp_first = cp;
26     const char* cp_last = cp + std::strlen(cp);
27     const char* cp_i[] = {"one","two","three"};
28     int i;
29     typedef std::map<std::string,int> map_string_type;
30     map_string_type c;
31     map_string_type::const_iterator it_find;
32     std::string str;
33 
34     scanner<char const*> scan( cp_first, cp_last );
35     match<> hit;
36 
37     hit = list_p(
38             confix_p(
39                 '(',
40                     (*alpha_p)[ assign(str)]
41                     >>ch_p(',')
42                     >> int_p[ insert_at_a(c,str)]
43                     ,
44                 ')'
45                 )
46          ,
47         ch_p(',')
48         ).parse(scan);
49 
50     BOOST_CHECK(hit);
51     BOOST_CHECK_EQUAL(scan.first, scan.last);
52     BOOST_CHECK_EQUAL( c.size(), static_cast<map_string_type::size_type>(3));
53     for (i=0;i<3;++i)
54     {
55         it_find = c.find(cp_i[i]);
56         BOOST_CHECK( it_find != c.end() );
57         BOOST_CHECK_EQUAL( i,it_find->second);
58         BOOST_CHECK_EQUAL( cp_i[i],it_find->first);
59     }
60 }
61 
insert_at_two_arguments_test()62 void insert_at_two_arguments_test()
63 {
64     using namespace BOOST_SPIRIT_CLASSIC_NS;
65 
66     const char* cp = "(0,one),(1,two),(2,three)";
67     const char* cp_first = cp;
68     const char* cp_last = cp + std::strlen(cp);
69     const char* cp_i[] = {"one","two","three"};
70     int i;
71     typedef std::map<int,std::string> map_string_type;
72     map_string_type c;
73     map_string_type::const_iterator it_find;
74     std::string str;
75 
76     scanner<char const*> scan( cp_first, cp_last );
77     match<> hit;
78 
79     hit = list_p(
80             confix_p(
81                 '(',
82                     int_p[ assign(i)]
83                     >>ch_p(',')
84                     >> (*alpha_p)[ insert_at_a(c,i)]
85                     ,
86                 ')'
87                 )
88          ,
89         ch_p(',')
90         ).parse(scan);
91 
92     BOOST_CHECK(hit);
93     BOOST_CHECK_EQUAL(scan.first, scan.last);
94     BOOST_CHECK_EQUAL( c.size(), static_cast<map_string_type::size_type>(3));
95     for (i=0;i<3;++i)
96     {
97         it_find = c.find(i);
98         BOOST_CHECK( it_find != c.end() );
99         BOOST_CHECK_EQUAL( i,it_find->first);
100         BOOST_CHECK_EQUAL( cp_i[i],it_find->second);
101     }
102 }
103 
insert_at_action_test()104 void insert_at_action_test()
105 {
106     insert_at_single_argument_test();
107     insert_at_two_arguments_test();
108 }
109 
110