test_typeof2.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // test_typeof2.cpp
  3. //
  4. // Copyright 2008 David Jenkins. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #define BOOST_TYPEOF_LIMIT_SIZE 200
  8. #define BOOST_TYPEOF_EMULATION 1
  9. #include <string>
  10. #include <map>
  11. #include <list>
  12. #include <stack>
  13. #include <boost/version.hpp>
  14. #include <boost/xpressive/xpressive_static.hpp>
  15. #include <boost/xpressive/regex_actions.hpp>
  16. #include <boost/xpressive/xpressive_typeof.hpp>
  17. #include <boost/typeof/std/stack.hpp>
  18. #include <boost/typeof/std/list.hpp>
  19. #include <boost/test/unit_test.hpp>
  20. // I couldn't find these registrations anywhere else, so I put them here
  21. // They are necessary for this program to compile
  22. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  23. BOOST_TYPEOF_REGISTER_TEMPLATE(boost::mpl::int_, (int))
  24. BOOST_TYPEOF_REGISTER_TEMPLATE(boost::reference_wrapper, (typename))
  25. // Here's the test for typeof registration, to be used on static regular expressions
  26. #define TYPEOF_TEST(Expr) { BOOST_PROTO_AUTO(Dummy, Expr); }
  27. namespace xp = boost::xpressive;
  28. ///////////////////////////////////////////////////////////////////////////////
  29. // test_actions
  30. // regular expressions from test_actions.cpp
  31. void test_actions()
  32. {
  33. using namespace boost::xpressive;
  34. // regexes from test_actions.cpp
  35. std::string result;
  36. TYPEOF_TEST((+_w)[ xp::ref(result) += _ ] >> *(' ' >> (+_w)[ xp::ref(result) += ',' + _ ]));
  37. TYPEOF_TEST((+_w)[ xp::ref(result) += _ ] >> *(' ' >> (+_w)[ xp::ref(result) += ',' + _ ]) >> repeat<4>(_));
  38. std::list<int> result2;
  39. TYPEOF_TEST((+_d)[ xp::ref(result2)->*push_back( as<int>(_) ) ]
  40. >> *(' ' >> (+_d)[ xp::ref(result2)->*push_back( as<int>(_) ) ]));
  41. std::map<std::string, int> result3;
  42. TYPEOF_TEST(( (s1= +_w) >> "=>" >> (s2= +_d) )[ xp::ref(result3)[s1] = as<int>(s2) ]);
  43. placeholder< std::map<std::string, int> > const _map5 = {{}};
  44. TYPEOF_TEST(( (s1= +_w) >> "=>" >> (s2= +_d) )[ _map5[s1] = as<int>(s2) ]);
  45. smatch what;
  46. placeholder< std::map<std::string, int> > const _map6 = {{}};
  47. std::map<std::string, int> result6;
  48. what.let(_map6 = result6); // bind the argument!
  49. local<int> left, right;
  50. std::stack<int> stack_;
  51. reference<std::stack<int> > stack(stack_);
  52. cregex expression2, factor2, term2, group2;
  53. TYPEOF_TEST( '(' >> by_ref(expression2) >> ')');
  54. TYPEOF_TEST( (+_d)[ push(stack, as<int>(_)) ] | group2);
  55. TYPEOF_TEST(factor2 >> *(
  56. ('*' >> factor2)
  57. [ right = top(stack)
  58. , pop(stack)
  59. , left = top(stack)
  60. , pop(stack)
  61. , push(stack, left * right)
  62. ]
  63. ));
  64. TYPEOF_TEST(term2 >> *(
  65. ('+' >> term2)
  66. [ right = top(stack)
  67. , pop(stack)
  68. , left = top(stack)
  69. , pop(stack)
  70. , push(stack, left + right)
  71. ]
  72. ));
  73. }
  74. #ifndef BOOST_XPRESSIVE_NO_WREGEX
  75. struct City
  76. {
  77. std::wstring name;
  78. char const* nickname;
  79. int population;
  80. };
  81. BOOST_TYPEOF_REGISTER_TYPE(City)
  82. #endif
  83. ///////////////////////////////////////////////////////////////////////////////
  84. // test_symbols
  85. // regular expressions from test_symbols.cpp
  86. void test_symbols()
  87. {
  88. using namespace boost::xpressive;
  89. std::string result;
  90. std::map<std::string,std::string> map10;
  91. TYPEOF_TEST((a1=map10)[ xp::ref(result) = a1 ] >> *(' ' >> (a1=map10)[ xp::ref(result) += ',' + a1 ]));
  92. TYPEOF_TEST((a1=map10)[ xp::ref(result) = a1 ]
  93. >> *((a1=map10)[ xp::ref(result) += ',', xp::ref(result) += a1 ]));
  94. std::list<int> result12;
  95. std::map<std::string,int> map12;
  96. TYPEOF_TEST((a1=map12)[ xp::ref(result12)->*push_back( a1 ) ]
  97. >> *(' ' >> (a1=map12)[ xp::ref(result12)->*push_back( a1 ) ]));
  98. placeholder< std::map<std::string, int> > const _map13 = {};
  99. BOOST_PROTO_AUTO(pair13, ( (a1=map10) >> "=>" >> (a2= map12) )[ _map13[a1] = a2 ]);
  100. smatch what;
  101. std::map<std::string, int> result13;
  102. what.let(_map13 = result13);
  103. TYPEOF_TEST(pair13 >> *(+_s >> pair13));
  104. int result14 = 0;
  105. std::map<std::string,int> map1a;
  106. std::map<std::string,int> map2a;
  107. std::map<std::string,int> map3a;
  108. TYPEOF_TEST((a1=map1a)[ xp::ref(result14) += a1 ]
  109. >> (a2=map2a)[ xp::ref(result) += a2 ]
  110. >> (a3=map3a)[ xp::ref(result) += a3 ]
  111. );
  112. {
  113. TYPEOF_TEST(icase(a1= map10) [ xp::ref(result) = a1 ]
  114. >> repeat<3>( (' ' >> icase(a1= map10) [ xp::ref(result) += ',', xp::ref(result) += a1 ]) )
  115. );
  116. TYPEOF_TEST(*((a1= map1a) | (a1= map2a) | 'e') [ xp::ref(result) += (a1 | "9") ]);
  117. }
  118. #ifndef BOOST_XPRESSIVE_NO_WREGEX
  119. City result17a, result17b;
  120. std::map<std::wstring, City> map17;
  121. TYPEOF_TEST((a1= map17)[ xp::ref(result17a) = a1 ] >> +_s
  122. >> (a1= map17)[ xp::ref(result17b) = a1 ]);
  123. #else
  124. // This test is empty
  125. #endif
  126. }
  127. bool three_or_six(xp::csub_match const &sub)
  128. {
  129. return sub.length() == 3 || sub.length() == 6;
  130. }
  131. ///////////////////////////////////////////////////////////////////////////////
  132. // test_assert
  133. // regular expressions from test_assert.cpp
  134. void test_assert()
  135. {
  136. using namespace boost::xpressive;
  137. std::string result;
  138. TYPEOF_TEST((bow >> +_w >> eow)[ check(&three_or_six) ]);
  139. TYPEOF_TEST((bow >> +_w >> eow)[ check(length(_)==3 || length(_)==6) ]);
  140. int const days_per_month[] =
  141. {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 31, 31};
  142. mark_tag month(1), day(2);
  143. // Note: if you uncomment the lines below,
  144. // the BOOST_TYPEOF_LIMIT_SIZE is exceeded
  145. TYPEOF_TEST((
  146. // Month must be between 1 and 12 inclusive
  147. (month= _d >> !_d) [ check(as<int>(_) >= 1
  148. && as<int>(_) <= 12) ]
  149. //>> '/'
  150. // // Day must be between 1 and 31 inclusive
  151. //>> (day= _d >> !_d) [ check(as<int>(_) >= 1
  152. // && as<int>(_) <= 31) ]
  153. //>> '/'
  154. // // Only consider years between 1970 and 2038
  155. //>> (_d >> _d >> _d >> _d) [ check(as<int>(_) >= 1970
  156. // && as<int>(_) <= 2038) ]
  157. )
  158. // Ensure the month actually has that many days.
  159. [ check( ref(days_per_month)[as<int>(month)-1] >= as<int>(day) ) ]);
  160. }