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