main.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. *
  3. * Copyright (c) 2002
  4. * John Maddock
  5. *
  6. * Use, modification and distribution are subject to the
  7. * Boost Software License, Version 1.0. (See accompanying file
  8. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. */
  11. #include <iostream>
  12. #include <fstream>
  13. #include <iterator>
  14. #include <cassert>
  15. #include <boost/test/execution_monitor.hpp>
  16. #include "./regex_comparison.hpp"
  17. void test_match(const std::string& re, const std::string& text, const std::string& description)
  18. {
  19. double time;
  20. results r(re, description);
  21. std::cout << "Testing: \"" << re << "\" against \"" << description << "\"" << std::endl;
  22. if(time_greta == true)
  23. {
  24. // time = g::time_match(re, text);
  25. // r.greta_time = time;
  26. // std::cout << "\tGRETA regex: " << time << "s\n";
  27. }
  28. if(time_safe_greta == true)
  29. {
  30. // time = gs::time_match(re, text);
  31. // r.safe_greta_time = time;
  32. // std::cout << "\tSafe GRETA regex: " << time << "s\n";
  33. }
  34. if(time_dynamic_xpressive == true)
  35. {
  36. time = dxpr::time_match(re, text);
  37. r.dynamic_xpressive_time = time;
  38. std::cout << "\tdynamic xpressive regex: " << time << "s\n";
  39. }
  40. if(time_static_xpressive == true)
  41. {
  42. time = sxpr::time_match(re, text);
  43. r.static_xpressive_time = time;
  44. std::cout << "\tstatic xpressive regex: " << time << "s\n";
  45. }
  46. if(time_boost == true)
  47. {
  48. time = b::time_match(re, text);
  49. r.boost_time = time;
  50. std::cout << "\tBoost regex: " << time << "s\n";
  51. }
  52. //if(time_posix == true)
  53. //{
  54. // time = posix::time_match(re, text);
  55. // r.posix_time = time;
  56. // std::cout << "\tPOSIX regex: " << time << "s\n";
  57. //}
  58. //if(time_pcre == true)
  59. //{
  60. // time = pcr::time_match(re, text);
  61. // r.pcre_time = time;
  62. // std::cout << "\tPCRE regex: " << time << "s\n";
  63. //}
  64. r.finalise();
  65. result_list.push_back(r);
  66. }
  67. void test_find_all(const std::string& re, const std::string& text, const std::string& description)
  68. {
  69. std::cout << "Testing: " << re << std::endl;
  70. double time;
  71. results r(re, description);
  72. #if defined(_MSC_VER) && (_MSC_VER >= 1300)
  73. if(time_greta == true)
  74. {
  75. // time = g::time_find_all(re, text);
  76. // r.greta_time = time;
  77. // std::cout << "\tGRETA regex: " << time << "s\n";
  78. }
  79. if(time_safe_greta == true)
  80. {
  81. // time = gs::time_find_all(re, text);
  82. // r.safe_greta_time = time;
  83. // std::cout << "\tSafe GRETA regex: " << time << "s\n";
  84. }
  85. #endif
  86. if(time_dynamic_xpressive == true)
  87. {
  88. time = dxpr::time_find_all(re, text);
  89. r.dynamic_xpressive_time = time;
  90. std::cout << "\tdynamic xpressive regex: " << time << "s\n";
  91. }
  92. if(time_static_xpressive == true)
  93. {
  94. time = sxpr::time_find_all(re, text);
  95. r.static_xpressive_time = time;
  96. std::cout << "\tstatic xpressive regex: " << time << "s\n";
  97. }
  98. if(time_boost == true)
  99. {
  100. time = b::time_find_all(re, text);
  101. r.boost_time = time;
  102. std::cout << "\tBoost regex: " << time << "s\n";
  103. }
  104. //if(time_posix == true)
  105. //{
  106. // time = posix::time_find_all(re, text);
  107. // r.posix_time = time;
  108. // std::cout << "\tPOSIX regex: " << time << "s\n";
  109. //}
  110. //if(time_pcre == true)
  111. //{
  112. // time = pcr::time_find_all(re, text);
  113. // r.pcre_time = time;
  114. // std::cout << "\tPCRE regex: " << time << "s\n";
  115. //}
  116. r.finalise();
  117. result_list.push_back(r);
  118. }
  119. //int cpp_main(int argc, char**const argv)
  120. int main(int argc, char**const argv)
  121. {
  122. // start by processing the command line args:
  123. if(argc < 2)
  124. return show_usage();
  125. int result = 0;
  126. for(int c = 1; c < argc; ++c)
  127. {
  128. result += handle_argument(argv[c]);
  129. }
  130. if(result)
  131. return result;
  132. if(test_matches)
  133. {
  134. // these are from the regex docs:
  135. test_match("^([0-9]+)(\\-| |$)(.*)$", "100- this is a line of ftp response which contains a message string");
  136. test_match("([[:digit:]]{4}[- ]){3}[[:digit:]]{3,4}", "1234-5678-1234-456");
  137. // these are from http://www.regxlib.com/
  138. test_match("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$", "john_maddock@compuserve.com");
  139. test_match("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$", "foo12@foo.edu");
  140. test_match("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$", "bob.smith@foo.tv");
  141. test_match("^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$", "EH10 2QQ");
  142. test_match("^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$", "G1 1AA");
  143. test_match("^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$", "SW1 1ZZ");
  144. test_match("^[[:digit:]]{1,2}/[[:digit:]]{1,2}/[[:digit:]]{4}$", "4/1/2001");
  145. test_match("^[[:digit:]]{1,2}/[[:digit:]]{1,2}/[[:digit:]]{4}$", "12/12/2001");
  146. test_match("^[-+]?[[:digit:]]*\\.?[[:digit:]]*$", "123");
  147. test_match("^[-+]?[[:digit:]]*\\.?[[:digit:]]*$", "+3.14159");
  148. test_match("^[-+]?[[:digit:]]*\\.?[[:digit:]]*$", "-3.14159");
  149. output_xml_results(true, "Short Matches", "short_matches.xml");
  150. }
  151. std::string twain;
  152. if(test_short_twain)
  153. {
  154. load_file(twain, "short_twain.txt");
  155. test_find_all("Twain", twain);
  156. test_find_all("Huck[[:alpha:]]+", twain);
  157. test_find_all("[[:alpha:]]+ing", twain);
  158. test_find_all("^[^\n]*?Twain", twain);
  159. test_find_all("Tom|Sawyer|Huckleberry|Finn", twain);
  160. test_find_all("(Tom|Sawyer|Huckleberry|Finn).{0,30}river|river.{0,30}(Tom|Sawyer|Huckleberry|Finn)", twain);
  161. output_xml_results(false, "Moderate Searches", "short_twain_search.xml");
  162. }
  163. if(test_long_twain)
  164. {
  165. load_file(twain, "3200.txt");
  166. test_find_all("Twain", twain);
  167. test_find_all("Huck[[:alpha:]]+", twain);
  168. test_find_all("[[:alpha:]]+ing", twain);
  169. test_find_all("^[^\n]*?Twain", twain);
  170. test_find_all("Tom|Sawyer|Huckleberry|Finn", twain);
  171. //time_posix = false;
  172. test_find_all("(Tom|Sawyer|Huckleberry|Finn).{0,30}river|river.{0,30}(Tom|Sawyer|Huckleberry|Finn)", twain);
  173. //time_posix = true;
  174. output_xml_results(false, "Long Searches", "long_twain_search.xml");
  175. }
  176. return 0;
  177. }