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 ///////////////////////////////////////////////////////////////////////////////
11 // Test suite for insert_key_actor
12 ///////////////////////////////////////////////////////////////////////////////
13
14 #include "action_tests.hpp"
15 #include <boost/spirit/include/classic_core.hpp>
16 #include <boost/spirit/include/classic_confix.hpp>
17 #include <boost/spirit/include/classic_lists.hpp>
18 #include <map>
19 #include <cstring>
20 #include <iostream>
21 #include <boost/spirit/include/classic_insert_key_actor.hpp>
22 #include <boost/spirit/include/classic_assign_actor.hpp>
23
insert_key_single_argument_test()24 void insert_key_single_argument_test()
25 {
26 using namespace BOOST_SPIRIT_CLASSIC_NS;
27
28 const char* cp = "(one,0),(two,1),(three,2)";
29 const char* cp_first = cp;
30 const char* cp_last = cp + test_impl::string_length(cp);
31 const char* cp_i[] = {"one","two","three"};
32 int i;
33 typedef std::map<int,std::string> map_string_type;
34 map_string_type c;
35 map_string_type::const_iterator it_find;
36 std::string str;
37
38 scanner<char const*> scan( cp_first, cp_last );
39 match<> hit;
40
41 hit = list_p(
42 confix_p(
43 '(',
44 (*alpha_p)[ assign_a(str)]
45 >>ch_p(',')
46 >> int_p[ insert_key_a(c,str)]
47 ,
48 ')'
49 )
50 ,
51 ch_p(',')
52 ).parse(scan);
53 BOOST_CHECK(hit);
54 BOOST_CHECK_EQUAL(scan.first, scan.last);
55 BOOST_CHECK_EQUAL( c.size(), static_cast<map_string_type::size_type>(3));
56 for (i=0;i<3;++i)
57 {
58 it_find = c.find(i);
59 BOOST_CHECK( it_find != c.end() );
60 BOOST_CHECK_EQUAL( i,it_find->first);
61 BOOST_CHECK_EQUAL( cp_i[i],it_find->second);
62 }
63
64 }
65
insert_key_two_arguments_test()66 void insert_key_two_arguments_test()
67 {
68 using namespace BOOST_SPIRIT_CLASSIC_NS;
69
70 const char* cp = "(0,one),(1,two),(2,three)";
71 const char* cp_first = cp;
72 const char* cp_last = cp + test_impl::string_length(cp);
73 const char* cp_i[] = {"one","two","three"};
74 int i;
75 typedef std::map<std::string, int> map_string_type;
76 map_string_type c;
77 map_string_type::const_iterator it_find;
78 std::string str;
79
80 scanner<char const*> scan( cp_first, cp_last );
81 match<> hit;
82
83 hit = list_p(
84 confix_p(
85 '(',
86 int_p[ assign_a(i)]
87 >>ch_p(',')
88 >> (*alpha_p)[ insert_key_a(c,i)]
89 ,
90 ')'
91 )
92 ,
93 ch_p(',')
94 ).parse(scan);
95
96 BOOST_CHECK(hit);
97 BOOST_CHECK_EQUAL(scan.first, scan.last);
98 BOOST_CHECK_EQUAL( c.size(), static_cast<map_string_type::size_type>(3));
99
100 for (i=0;i<3;++i)
101 {
102 it_find = c.find(cp_i[i]);
103 BOOST_CHECK( it_find != c.end() );
104 BOOST_CHECK_EQUAL( i,it_find->second);
105 BOOST_CHECK_EQUAL( cp_i[i],it_find->first);
106 }
107 scan.first = cp;
108
109 }
110
insert_key_action_test()111 void insert_key_action_test()
112 {
113 insert_key_single_argument_test();
114 insert_key_two_arguments_test();
115 }
116
117