insert_at_test.cpp 3.2 KB

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