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