test_cycles.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // test_cycles.hpp
  3. //
  4. // Copyright 2008 Eric Niebler. 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. // defining this causes regex_impl objects to be counted, allowing us to detect
  8. // leaks portably.
  9. #define BOOST_XPRESSIVE_DEBUG_CYCLE_TEST
  10. #include <iostream>
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/xpressive/xpressive.hpp>
  13. #if defined(_MSC_VER) && defined(_DEBUG)
  14. # define _CRTDBG_MAP_ALLOC
  15. # include <crtdbg.h>
  16. #endif
  17. using namespace boost::unit_test;
  18. using namespace boost::xpressive;
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // test_main
  21. // regexes referred to by other regexes are kept alive via reference counting.
  22. // but cycles are handled naturally. the following works as expected and doesn't leak.
  23. void test_main()
  24. {
  25. {
  26. sregex v;
  27. {
  28. sregex a,b,c;
  29. a = 'a' >> !by_ref(b);
  30. //std::cout << a << std::endl;
  31. //std::cout << b << std::endl;
  32. //std::cout << c << std::endl;
  33. // just for giggles
  34. c = a;
  35. c = epsilon >> 'a';
  36. b = epsilon >> by_ref(c);
  37. //std::cout << a << std::endl;
  38. //std::cout << b << std::endl;
  39. //std::cout << c << std::endl;
  40. c = epsilon >> by_ref(a);
  41. //std::cout << a << std::endl;
  42. //std::cout << b << std::endl;
  43. //std::cout << c << std::endl;
  44. v = a;
  45. }
  46. std::string const s("aaa");
  47. smatch m;
  48. if(!regex_match(s, m, v))
  49. {
  50. BOOST_ERROR("cycle test 1 failed");
  51. }
  52. }
  53. if(0 != detail::regex_impl<std::string::const_iterator>::instances)
  54. {
  55. BOOST_ERROR("leaks detected (cycle test 1)");
  56. detail::regex_impl<std::string::const_iterator>::instances = 0;
  57. }
  58. {
  59. sregex v;
  60. {
  61. sregex a,b,c;
  62. b = epsilon >> by_ref(c);
  63. a = 'a' >> !by_ref(b);
  64. c = epsilon >> by_ref(a);
  65. //std::cout << a << std::endl;
  66. //std::cout << b << std::endl;
  67. //std::cout << c << std::endl;
  68. v = a;
  69. }
  70. std::string const s("aaa");
  71. smatch m;
  72. if(!regex_match(s, m, v))
  73. {
  74. BOOST_ERROR("cycle test 2 failed");
  75. }
  76. }
  77. if(0 != detail::regex_impl<std::string::const_iterator>::instances)
  78. {
  79. BOOST_ERROR("leaks detected (cycle test 2)");
  80. detail::regex_impl<std::string::const_iterator>::instances = 0;
  81. }
  82. {
  83. sregex v;
  84. {
  85. sregex a,b,c;
  86. b = epsilon >> by_ref(c);
  87. c = epsilon >> by_ref(a);
  88. a = 'a' >> !by_ref(b);
  89. //std::cout << a << std::endl;
  90. //std::cout << b << std::endl;
  91. //std::cout << c << std::endl;
  92. v = a;
  93. }
  94. std::string const s("aaa");
  95. smatch m;
  96. if(!regex_match(s, m, v))
  97. {
  98. BOOST_ERROR("cycle test 3 failed");
  99. }
  100. }
  101. if(0 != detail::regex_impl<std::string::const_iterator>::instances)
  102. {
  103. BOOST_ERROR("leaks detected (cycle test 3)");
  104. detail::regex_impl<std::string::const_iterator>::instances = 0;
  105. }
  106. {
  107. sregex v;
  108. {
  109. sregex a,b,c;
  110. c = epsilon >> by_ref(a);
  111. b = epsilon >> by_ref(c);
  112. a = 'a' >> !by_ref(b);
  113. //std::cout << a << std::endl;
  114. //std::cout << b << std::endl;
  115. //std::cout << c << std::endl;
  116. v = a;
  117. }
  118. std::string const s("aaa");
  119. smatch m;
  120. if(!regex_match(s, m, v))
  121. {
  122. BOOST_ERROR("cycle test 4 failed");
  123. }
  124. }
  125. if(0 != detail::regex_impl<std::string::const_iterator>::instances)
  126. {
  127. BOOST_ERROR("leaks detected (cycle test 4)");
  128. detail::regex_impl<std::string::const_iterator>::instances = 0;
  129. }
  130. {
  131. sregex v;
  132. {
  133. sregex a,b,c;
  134. a = 'a' >> !by_ref(b);
  135. b = epsilon >> by_ref(c);
  136. c = epsilon >> by_ref(a);
  137. sregex d,e;
  138. d = epsilon >> by_ref(e);
  139. e = epsilon >> "aa";
  140. c = d;
  141. //std::cout << a << std::endl;
  142. //std::cout << b << std::endl;
  143. //std::cout << c << std::endl;
  144. //std::cout << e << std::endl;
  145. e = 'a' >> by_ref(c);
  146. //std::cout << "-new loop!\n";
  147. //std::cout << a << std::endl;
  148. //std::cout << b << std::endl;
  149. //std::cout << c << std::endl;
  150. //std::cout << e << std::endl;
  151. v = a;
  152. //std::cout << v << std::endl;
  153. }
  154. std::string const s("aaa");
  155. smatch m;
  156. if(regex_match(s, m, v)) // OK, this shouldn't match
  157. {
  158. BOOST_ERROR("cycle test 5 failed");
  159. }
  160. }
  161. if(0 != detail::regex_impl<std::string::const_iterator>::instances)
  162. {
  163. BOOST_ERROR("leaks detected (cycle test 5)");
  164. detail::regex_impl<std::string::const_iterator>::instances = 0;
  165. }
  166. }
  167. ///////////////////////////////////////////////////////////////////////////////
  168. // init_unit_test_suite
  169. //
  170. test_suite* init_unit_test_suite( int argc, char* argv[] )
  171. {
  172. test_suite *test = BOOST_TEST_SUITE("test_cycles");
  173. test->add(BOOST_TEST_CASE(&test_main));
  174. return test;
  175. }
  176. ///////////////////////////////////////////////////////////////////////////////
  177. // Debug stuff
  178. //
  179. namespace
  180. {
  181. const struct debug_init
  182. {
  183. debug_init()
  184. {
  185. #ifdef _MSC_VER
  186. // Send warnings, errors and asserts to STDERR
  187. _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
  188. _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
  189. _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
  190. _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
  191. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
  192. _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
  193. // Check for leaks at program termination
  194. _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
  195. //_CrtSetBreakAlloc(221);
  196. #endif
  197. }
  198. } dbg;
  199. }