1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 :(before "End Types")
71 struct trace_line {
72 int depth;
73 string label;
74 string contents;
75 trace_line(string l, string c) :depth(0), label(l), contents(c) {}
76 trace_line(int d, string l, string c) :depth(d), label(l), contents(c) {}
77 };
78
79 :(before "End Globals")
80 bool Hide_errors = false;
81 bool Dump_trace = false;
82 string Dump_label = "";
83 :(before "End Reset")
84 Hide_errors = false;
85 Dump_trace = false;
86 Dump_label = "";
87
88
89
90 :(before "End Commandline Options(*arg)")
91 else if (is_equal(*arg, "--trace")) {
92 Save_trace = true;
93 }
94 :(before "End Commandline Parsing")
95 if (Save_trace) {
96 cerr << "initializing trace\n";
97 Trace_stream = new trace_stream;
98 }
99 :(code)
100 void cleanup_main() {
101 if (!Trace_stream) return;
102 if (Save_trace)
103 Trace_stream->save();
104 delete Trace_stream;
105 Trace_stream = NULL;
106 }
107 :(before "End One-time Setup")
108 atexit(cleanup_main);
109
110 :(before "End Types")
111
112
113
114 const int Max_depth = 9999;
115 const int Error_depth = 0;
116
117 struct trace_stream {
118 vector<trace_line> past_lines;
119
120 ostringstream* curr_stream;
121 string curr_label;
122 int curr_depth;
123 int callstack_depth;
124 int collect_depth;
125 ofstream null_stream;
126 trace_stream() :curr_stream(NULL), curr_depth(Max_depth), callstack_depth(0), collect_depth(Max_depth) {}
127 ~trace_stream() { if (curr_stream) delete curr_stream; }
128
129 ostream& stream(string label) {
130 return stream(Max_depth, label);
131 }
132
133 ostream& stream(int depth, string label) {
134 if (depth > collect_depth) return null_stream;
135 curr_stream = new ostringstream;
136 curr_label = label;
137 curr_depth = depth;
138 return *curr_stream;
139 }
140
141 void save() {
142 cerr << "saving trace to 'last_run'\n";
143 ofstream fout("last_run");
144 fout << readable_contents("");
145 fout.close();
146 }
147
148
149 void newline();
150
151 string readable_contents(string label);
152 };
153
154 :(code)
155 void trace_stream::newline() {
156 if (!curr_stream) return;
157 string curr_contents = curr_stream->str();
158 if (!curr_contents.empty()) {
159 past_lines.push_back(trace_line(curr_depth, trim(curr_label), curr_contents));
160 if ((!Hide_errors && curr_label == "error")
161 || Dump_trace
162 || (!Dump_label.empty() && curr_label == Dump_label))
163 cerr << curr_label << ": " << curr_contents << '\n';
164 }
165 delete curr_stream;
166 curr_stream = NULL;
167 curr_label.clear();
168 curr_depth = Max_depth;
169 }
170
171 string trace_stream::readable_contents(string label) {
172 ostringstream output;
173 label = trim(label);
174 for (vector<trace_line>::iterator p = past_lines.begin(); p != past_lines.end(); ++p)
175 if (label.empty() || label == p->label) {
176 output << std::setw(4) << p->depth << ' ' << p->label << ": " << p->contents << '\n';
177 }
178 return output.str();
179 }
180
181 :(before "End Globals")
182 trace_stream* Trace_stream = NULL;
183 int Trace_errors = 0;
184
185 :(before "End Includes")
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 :(before "End Test Teardown")
204 if (Passed && !Hide_errors && trace_contains_errors()) {
205 Passed = false;
206 }
207 :(code)
208 bool trace_contains_errors() {
209 return Trace_errors > 0 || trace_count("error") > 0;
210 }
211
212 :(before "End Types")
213 struct end {};
214 :(code)
215 ostream& operator<<(ostream& os, end ) {
216 if (Trace_stream) Trace_stream->newline();
217 return os;
218 }
219
220 :(before "End Globals")
221 bool Save_trace = false;
222
223
224 :(before "End Types")
225 struct lease_tracer {
226 lease_tracer();
227 ~lease_tracer();
228 };
229 :(code)
230 lease_tracer::lease_tracer() { Trace_stream = new trace_stream; }
231 lease_tracer::~lease_tracer() {
232 if (Save_trace) Trace_stream->save();
233 delete Trace_stream, Trace_stream = NULL;
234 }
235 :(before "End Includes")
236
237 :(before "End Test Setup")
238 START_TRACING_UNTIL_END_OF_SCOPE
239
240 :(before "End Includes")
241
242
243
244
245 if (Passed && trace_contains_errors()) { \
246 cerr << "\nF - " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): unexpected errors\n"; \
247 DUMP("error"); \
248 Passed = false; \
249 return; \
250 }
251
252
253 if (Passed && trace_count(label) != (count)) { \
254 cerr << "\nF - " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): trace_count of " << label << " should be " << count << '\n'; \
255 cerr << " got " << trace_count(label) << '\n'; \
256 DUMP(label); \
257 Passed = false; \
258 return; \
259 }
260
261
262
263 :(code)
264 bool check_trace_contents(string FUNCTION, string FILE, int LINE, string expected) {
265 if (!Passed) return false;
266 if (!Trace_stream) return false;
267 vector<string> expected_lines = split(expected, "^D");
268 int curr_expected_line = 0;
269 while (curr_expected_line < SIZE(expected_lines) && expected_lines.at(curr_expected_line).empty())
270 ++curr_expected_line;
271 if (curr_expected_line == SIZE(expected_lines)) return true;
272 string label, contents;
273 split_label_contents(expected_lines.at(curr_expected_line), &label, &contents);
274 for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
275 if (label != p->label) continue;
276 if (contents != trim(p->contents)) continue;
277 ++curr_expected_line;
278 while (curr_expected_line < SIZE(expected_lines) && expected_lines.at(curr_expected_line).empty())
279 ++curr_expected_line;
280 if (curr_expected_line == SIZE(expected_lines)) return true;
281 split_label_contents(expected_lines.at(curr_expected_line), &label, &contents);
282 }
283
284 if (line_exists_anywhere(label, contents)) {
285 cerr << "\nF - " << FUNCTION << "(" << FILE << ":" << LINE << "): line [" << label << ": " << contents << "] out of order in trace:\n";
286 DUMP("");
287 }
288 else {
289 cerr << "\nF - " << FUNCTION << "(" << FILE << ":" << LINE << "): missing [" << contents << "] in trace:\n";
290 DUMP(label);
291 }
292 Passed = false;
293 return false;
294 }
295
296 void split_label_contents(const string& s, string* label, string* contents) {
297 static const string delim(": ");
298 size_t pos = s.find(delim);
299 if (pos == string::npos) {
300 *label = "";
301 *contents = trim(s);
302 }
303 else {
304 *label = trim(s.substr(0, pos));
305 *contents = trim(s.substr(pos+SIZE(delim)));
306 }
307 }
308
309 bool line_exists_anywhere(const string& label, const string& contents) {
310 for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
311 if (label != p->label) continue;
312 if (contents == trim(p->contents)) return true;
313 }
314 return false;
315 }
316
317 int trace_count(string label) {
318 return trace_count(label, "");
319 }
320
321 int trace_count(string label, string line) {
322 if (!Trace_stream) return 0;
323 long result = 0;
324 for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
325 if (label == p->label) {
326 if (line == "" || trim(line) == trim(p->contents))
327 ++result;
328 }
329 }
330 return result;
331 }
332
333 int trace_count_prefix(string label, string prefix) {
334 if (!Trace_stream) return 0;
335 long result = 0;
336 for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
337 if (label == p->label) {
338 if (starts_with(trim(p->contents), trim(prefix)))
339 ++result;
340 }
341 }
342 return result;
343 }
344
345 bool trace_doesnt_contain(string label, string line) {
346 return trace_count(label, line) == 0;
347 }
348
349 bool trace_doesnt_contain(string expected) {
350 vector<string> tmp = split_first(expected, ": ");
351 return trace_doesnt_contain(tmp.at(0), tmp.at(1));
352 }
353
354 vector<string> split(string s, string delim) {
355 vector<string> result;
356 size_t begin=0, end=s.find(delim);
357 while (true) {
358 if (end == string::npos) {
359 result.push_back(string(s, begin, string::npos));
360 break;
361 }
362 result.push_back(string(s, begin, end-begin));
363 begin = end+SIZE(delim);
364 end = s.find(delim, begin);
365 }
366 return result;
367 }
368
369 vector<string> split_first(string s, string delim) {
370 vector<string> result;
371 size_t end=s.find(delim);
372 result.push_back(string(s, 0, end));
373 if (end != string::npos)
374 result.push_back(string(s, end+SIZE(delim), string::npos));
375 return result;
376 }
377
378 string trim(const string& s) {
379 string::const_iterator first = s.begin();
380 while (first != s.end() && isspace(*first))
381 ++first;
382 if (first == s.end()) return "";
383
384 string::const_iterator last = --s.end();
385 while (last != s.begin() && isspace(*last))
386 --last;
387 ++last;
388 return string(first, last);
389 }
390
391 :(before "End Includes")
392
393 using std::vector;
394
395 using std::list;
396
397 using std::set;
398
399
400 using std::istringstream;
401 using std::ostringstream;
402
403
404 using std::ifstream;
405 using std::ofstream;
406
407 :(before "End Globals")
408
409
410
411
412
413 extern const int Initial_callstack_depth = 101;
414 extern const int Max_callstack_depth = 9989;
415
416
417
418
419