|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 003trace.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #d0d0d0; background-color: #080808; }
body { font-family: monospace; color: #d0d0d0; background-color: #080808; }
* { font-size: 1em; }
.cSpecial { color: #008000; }
.Constant { color: #008080; }
.PreProc { color: #c000c0; }
.Comment { color: #8080ff; }
.Delimiter { color: #c000c0; }
.CommentedCode { color: #6c6c6c; }
.Identifier { color: #008080; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: The goal of this skeleton is to make programs more easy to understand and</span>
<span class="Comment">//: more malleable, easy to rewrite in radical ways without accidentally</span>
<span class="Comment">//: breaking some corner case. Tests further both goals. They help</span>
<span class="Comment">//: understandability by letting one make small changes and get feedback. What</span>
<span class="Comment">//: if I wrote this line like so? What if I removed this function call, is it</span>
<span class="Comment">//: really necessary? Just try it, see if the tests pass. Want to explore</span>
<span class="Comment">//: rewriting this bit in this way? Tests put many refactorings on a firmer</span>
<span class="Comment">//: footing.</span>
<span class="Comment">//:</span>
<span class="Comment">//: But the usual way we write tests seems incomplete. Refactorings tend to</span>
<span class="Comment">//: work in the small, but don't help with changes to function boundaries. If</span>
<span class="Comment">//: you want to extract a new function you have to manually test-drive it to</span>
<span class="Comment">//: create tests for it. If you want to inline a function its tests are no</span>
<span class="Comment">//: longer valid. In both cases you end up having to reorganize code as well as</span>
<span class="Comment">//: tests, an error-prone activity.</span>
<span class="Comment">//:</span>
<span class="Comment">//: This file tries to fix this problem by supporting domain-driven testing</span>
<span class="Comment">//: We try to focus on the domain of inputs the program should work on. All</span>
<span class="Comment">//: tests invoke the program in a single way: by calling run() with different</span>
<span class="Comment">//: inputs. The program operates on the input and logs _facts_ it deduces to a</span>
<span class="Comment">//: trace:</span>
<span class="Comment">//: trace("label") << "fact 1: " << val;</span>
<span class="Comment">//:</span>
<span class="Comment">//: The tests check for facts:</span>
<span class="Comment">//: :(scenario foo)</span>
<span class="Comment">//: 34 # call run() with this input</span>
<span class="Comment">//: +label: fact 1: 34 # trace should have logged this at the end</span>
<span class="Comment">//: -label: fact 1: 35 # trace should never contain such a line</span>
<span class="Comment">//:</span>
<span class="Comment">//: Since we never call anything but the run() function directly, we never have</span>
<span class="Comment">//: to rewrite the tests when we reorganize the internals of the program. We</span>
<span class="Comment">//: just have to make sure our rewrite deduces the same facts about the domain,</span>
<span class="Comment">//: and that's something we're going to have to do anyway.</span>
<span class="Comment">//:</span>
<span class="Comment">//: To avoid the combinatorial explosion of integration tests, we organize the</span>
<span class="Comment">//: program into different layers, and each fact is logged to the trace with a</span>
<span class="Comment">//: specific label. Individual tests can focus on specific labels. In essence,</span>
<span class="Comment">//: validating the facts logged with a specific label is identical to calling</span>
<span class="Comment">//: some internal subsystem.</span>
<span class="Comment">//:</span>
<span class="Comment">//: Traces interact salubriously with layers. Thanks to our ordering</span>
<span class="Comment">//: directives, each layer can contain its own tests. They may rely on other</span>
<span class="Comment">//: layers, but when a test fails its usually due to breakage in the same</span>
<span class="Comment">//: layer. When multiple tests fail, it's usually useful to debug the very</span>
<span class="Comment">//: first test to fail. This is in contrast with the traditional approach,</span>
<span class="Comment">//: where changes can cause breakages in faraway subsystems, and picking the</span>
<span class="Comment">//: right test to debug can be an important skill to pick up.</span>
<span class="Comment">//:</span>
<span class="Comment">//: To build robust tests, trace facts about your domain rather than details of</span>
<span class="Comment">//: how you computed them.</span>
<span class="Comment">//:</span>
<span class="Comment">//: More details: <a href="http://akkartik.name/blog/tracing-tests">http://akkartik.name/blog/tracing-tests</a></span>
<span class="Comment">//:</span>
<span class="Comment">//: ---</span>
<span class="Comment">//:</span>
<span class="Comment">//: Between layers and domain-driven testing, programming starts to look like a</span>
<span class="Comment">//: fundamentally different activity. Instead of a) superficial, b) local rules</span>
<span class="Comment">//: on c) code [like <a href="http://blog.bbv.ch/2013/06/05/clean-code-cheat-sheet],">http://blog.bbv.ch/2013/06/05/clean-code-cheat-sheet],</a></span>
<span class="Comment">//: we allow programmers to engage with the a) deep, b) global structure of the</span>
<span class="Comment">//: c) domain. If you can systematically track discontinuities in the domain</span>
<span class="Comment">//: you don't care if the code used gotos as long as it passed the tests. If</span>
<span class="Comment">//: tests become more robust to run it becomes easier to try out radically</span>
<span class="Comment">//: different implementations for the same program. If code is super-easy to</span>
<span class="Comment">//: rewrite, it becomes less important what indentation style it uses, or that</span>
<span class="Comment">//: the objects are appropriately encapsulated, or that the functions are</span>
<span class="Comment">//: referentially transparent.</span>
<span class="Comment">//:</span>
<span class="Comment">//: Instead of plumbing, programming becomes building and gradually refining a</span>
<span class="Comment">//: map of the environment the program must operate under. Whether a program is</span>
<span class="Comment">//: 'correct' at a given point in time is a red herring; what matters is</span>
<span class="Comment">//: avoiding regression by monotonically nailing down the more 'eventful' parts</span>
<span class="Comment">//: of the terrain. It helps readers new and old and rewards curiosity to</span>
<span class="Comment">//: organize large programs in self-similar hiearchies of example scenarios</span>
<span class="Comment">//: colocated with the code that makes them work.</span>
<span class="Comment">//:</span>
<span class="Comment">//: "Programming properly should be regarded as an activity by which</span>
<span class="Comment">//: programmers form a mental model, rather than as production of a program."</span>
<span class="Comment">//: -- Peter Naur (<a href="http://alistair.cockburn.us/ASD+book+extract%3A+%22Naur,+Ehn,+Musashi%22)">http://alistair.cockburn.us/ASD+book+extract%3A+%22Naur,+Ehn,+Musashi%22)</a></span>
<span class="Delimiter">:(before "int main")</span>
<span class="Comment">// End Tracing // hack to ensure most code in this layer comes before anything else</span>
<span class="Delimiter">:(before "End Tracing")</span>
bool Hide_warnings = <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Setup")</span>
<span class="CommentedCode">//? cerr << "AAA setup\n"; //? 2</span>
Hide_warnings = <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Types")</span>
struct trace_line <span class="Delimiter">{</span>
int depth<span class="Delimiter">;</span> <span class="Comment">// optional field just to help browse traces later</span>
string label<span class="Delimiter">;</span>
string contents<span class="Delimiter">;</span>
trace_line<span class="Delimiter">(</span>string l<span class="Delimiter">,</span> string c<span class="Delimiter">)</span> :depth<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">),</span> label<span class="Delimiter">(</span>l<span class="Delimiter">),</span> contents<span class="Delimiter">(</span>c<span class="Delimiter">)</span> <span class="Delimiter">{}</span>
trace_line<span class="Delimiter">(</span>int d<span class="Delimiter">,</span> string l<span class="Delimiter">,</span> string c<span class="Delimiter">)</span> :depth<span class="Delimiter">(</span>d<span class="Delimiter">),</span> label<span class="Delimiter">(</span>l<span class="Delimiter">),</span> contents<span class="Delimiter">(</span>c<span class="Delimiter">)</span> <span class="Delimiter">{}</span>
<span class="Delimiter">};</span>
<span class="Delimiter">:(before "End Tracing")</span>
struct trace_stream <span class="Delimiter">{</span>
vector<trace_line> past_lines<span class="Delimiter">;</span>
<span class="Comment">// accumulator for current line</span>
ostringstream* curr_stream<span class="Delimiter">;</span>
string curr_layer<span class="Delimiter">;</span>
int curr_depth<span class="Delimiter">;</span>
string dump_layer<span class="Delimiter">;</span>
trace_stream<span class="Delimiter">()</span> :curr_stream<span class="Delimiter">(</span><span class="Constant">NULL</span><span class="Delimiter">),</span> curr_depth<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{}</span>
~trace_stream<span class="Delimiter">()</span> <span class="Delimiter">{</span> if <span class="Delimiter">(</span>curr_stream<span class="Delimiter">)</span> delete curr_stream<span class="Delimiter">;</span> <span class="Delimiter">}</span>
ostringstream& stream<span class="Delimiter">(</span>string layer<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> stream<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">,</span> layer<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
ostringstream& stream<span class="Delimiter">(</span>int depth<span class="Delimiter">,</span> string layer<span class="Delimiter">)</span> <span class="Delimiter">{</span>
newline<span class="Delimiter">();</span>
curr_stream = new ostringstream<span class="Delimiter">;</span>
curr_layer = layer<span class="Delimiter">;</span>
curr_depth = depth<span class="Delimiter">;</span>
<span class="Identifier">return</span> *curr_stream<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">// be sure to call this before messing with curr_stream or curr_layer</span>
void newline<span class="Delimiter">()</span> <span class="Delimiter">{</span>
if <span class="Delimiter">(</span>!curr_stream<span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
string curr_contents = curr_stream<span class="Delimiter">-></span>str<span class="Delimiter">();</span>
past_lines<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>trace_line<span class="Delimiter">(</span>curr_depth<span class="Delimiter">,</span> trim<span class="Delimiter">(</span>curr_layer<span class="Delimiter">),</span> curr_contents<span class="Delimiter">));</span> <span class="Comment">// preserve indent in contents</span>
if <span class="Delimiter">(</span>curr_layer == dump_layer || curr_layer == <span class="Constant">"dump"</span> || dump_layer == <span class="Constant">"all"</span> ||
<span class="Delimiter">(</span>!Hide_warnings && curr_layer == <span class="Constant">"warn"</span><span class="Delimiter">))</span>
<span class="CommentedCode">//? if (dump_layer == "all" && (Current_routine->id == 3 || curr_layer == "schedule")) //? 1</span>
cerr << curr_layer << <span class="Constant">": "</span> << curr_contents << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
delete curr_stream<span class="Delimiter">;</span>
curr_stream = <span class="Constant">NULL</span><span class="Delimiter">;</span>
curr_layer<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
curr_depth = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">// Useful for debugging.</span>
string readable_contents<span class="Delimiter">(</span>string layer<span class="Delimiter">)</span> <span class="Delimiter">{</span> <span class="Comment">// missing layer = everything</span>
newline<span class="Delimiter">();</span>
ostringstream output<span class="Delimiter">;</span>
layer = trim<span class="Delimiter">(</span>layer<span class="Delimiter">);</span>
for <span class="Delimiter">(</span>vector<trace_line>::iterator p = past_lines<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != past_lines<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span>
if <span class="Delimiter">(</span>layer<span class="Delimiter">.</span>empty<span class="Delimiter">()</span> || layer == p<span class="Delimiter">-></span>label<span class="Delimiter">)</span> <span class="Delimiter">{</span>
if <span class="Delimiter">(</span>p<span class="Delimiter">-></span>depth<span class="Delimiter">)</span>
output << std::setw<span class="Delimiter">(</span><span class="Constant">4</span><span class="Delimiter">)</span> << p<span class="Delimiter">-></span>depth << <span class="Constant">' '</span><span class="Delimiter">;</span>
output << p<span class="Delimiter">-></span>label << <span class="Constant">": "</span> << p<span class="Delimiter">-></span>contents << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span> output<span class="Delimiter">.</span>str<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">};</span>
^L
trace_stream* Trace_stream = <span class="Constant">NULL</span><span class="Delimiter">;</span>
<span class="Comment">// Top-level helper. IMPORTANT: can't nest.</span>
<span class="PreProc">#define trace(</span><span class="Delimiter">...</span><span class="PreProc">) !Trace_stream ? cerr </span><span class="Comment">/*</span><span class="Comment">print nothing</span><span class="Comment">*/</span><span class="PreProc"> : Trace_stream</span><span class="Delimiter">-></span><span class="PreProc">stream(__VA_ARGS__)</span>
<span class="Comment">// Warnings should go straight to cerr by default since calls to trace() have</span>
<span class="Comment">// some unfriendly constraints (they delay printing, they can't nest)</span>
<span class="PreProc">#define raise ((!Trace_stream || !Hide_warnings) ? cerr </span><span class="Comment">/*</span><span class="Comment">do print</span><span class="Comment">*/</span><span class="PreProc"> : Trace_stream</span><span class="Delimiter">-></span><span class="PreProc">stream(</span><span class="Constant">"warn"</span><span class="PreProc">))</span>
<span class="Comment">// A separate helper for debugging. We should only trace domain-specific</span>
<span class="Comment">// facts. For everything else use log.</span>
<span class="PreProc">#define xlog </span>if<span class="PreProc"> (</span><span class="Constant">false</span><span class="PreProc">) log</span>
<span class="Comment">// To turn on logging replace 'xlog' with 'log'.</span>
<span class="PreProc">#define log cerr</span>
<span class="Delimiter">:(before "End Types")</span>
<span class="Comment">// raise << die exits after printing -- unless Hide_warnings is set.</span>
struct die <span class="Delimiter">{};</span>
<span class="Delimiter">:(before "End Tracing")</span>
ostream& operator<<<span class="Delimiter">(</span>ostream& os<span class="Delimiter">,</span> unused die<span class="Delimiter">)</span> <span class="Delimiter">{</span>
if <span class="Delimiter">(</span>Hide_warnings<span class="Delimiter">)</span> <span class="Identifier">return</span> os<span class="Delimiter">;</span>
os << <span class="Constant">"dying"</span><span class="Delimiter">;</span>
if <span class="Delimiter">(</span>Trace_stream<span class="Delimiter">)</span> Trace_stream<span class="Delimiter">-></span>newline<span class="Delimiter">();</span>
exit<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="PreProc">#define CLEAR_TRACE </span>delete<span class="PreProc"> Trace_stream</span><span class="Delimiter">,</span><span class="PreProc"> Trace_stream = </span>new<span class="PreProc"> trace_stream</span><span class="Delimiter">;</span>
<span class="PreProc">#define DUMP(layer) </span>if<span class="PreProc"> (Trace_stream) cerr << Trace_stream</span><span class="Delimiter">-></span><span class="PreProc">readable_contents(layer)</span><span class="Delimiter">;</span>
<span class="Comment">// All scenarios save their traces in the repo, just like code. This gives</span>
<span class="Comment">// future readers more meat when they try to make sense of a new project.</span>
static string Trace_dir = <span class="Constant">".traces/"</span><span class="Delimiter">;</span>
string Trace_file<span class="Delimiter">;</span>
<span class="Comment">// Trace_stream is a resource, lease_tracer uses RAII to manage it.</span>
struct lease_tracer <span class="Delimiter">{</span>
lease_tracer<span class="Delimiter">()</span> <span class="Delimiter">{</span> Trace_stream = new trace_stream<span class="Delimiter">;</span> <span class="Delimiter">}</span>
~lease_tracer<span class="Delimiter">()</span> <span class="Delimiter">{</span>
<span class="CommentedCode">//? cerr << "write to file? " << Trace_file << "$\n"; //? 2</span>
if <span class="Delimiter">(</span>!Trace_file<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span>
<span class="CommentedCode">//? cerr << "writing\n"; //? 2</span>
ofstream fout<span class="Delimiter">((</span>Trace_dir+Trace_file<span class="Delimiter">).</span>c_str<span class="Delimiter">());</span>
fout << Trace_stream<span class="Delimiter">-></span>readable_contents<span class="Delimiter">(</span><span class="Constant">""</span><span class="Delimiter">);</span>
fout<span class="Delimiter">.</span>close<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
delete Trace_stream<span class="Delimiter">,</span> Trace_stream = <span class="Constant">NULL</span><span class="Delimiter">,</span> Trace_file = <span class="Constant">""</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">};</span>
<span class="PreProc">#define START_TRACING_UNTIL_END_OF_SCOPE lease_tracer leased_tracer</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Test Setup")</span>
START_TRACING_UNTIL_END_OF_SCOPE
<span class="CommentedCode">//? Trace_stream->dump_layer = "all"; //? 1</span>
<span class="PreProc">#define CHECK_TRACE_CONTENTS(</span><span class="Delimiter">...</span><span class="PreProc">) check_trace_contents(__FUNCTION__</span><span class="Delimiter">,</span><span class="PreProc"> </span><span class="Constant">__FILE__</span><span class="Delimiter">,</span><span class="PreProc"> </span><span class="Constant">__LINE__</span><span class="Delimiter">,</span><span class="PreProc"> __VA_ARGS__)</span>
<span class="Delimiter">:(before "End Tracing")</span>
bool check_trace_contents<span class="Delimiter">(</span>string FUNCTION<span class="Delimiter">,</span> string FILE<span class="Delimiter">,</span> int LINE<span class="Delimiter">,</span> string expected<span class="Delimiter">)</span> <span class="Delimiter">{</span> <span class="Comment">// missing layer == anywhere</span>
vector<string> expected_lines = split<span class="Delimiter">(</span>expected<span class="Delimiter">,</span> <span class="Constant">"^D"</span><span class="Delimiter">);</span>
long long int curr_expected_line = <span class="Constant">0</span><span class="Delimiter">;</span>
while <span class="Delimiter">(</span>curr_expected_line < SIZE<span class="Delimiter">(</span>expected_lines<span class="Delimiter">)</span> && expected_lines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>curr_expected_line<span class="Delimiter">).</span>empty<span class="Delimiter">())</span>
++curr_expected_line<span class="Delimiter">;</span>
if <span class="Delimiter">(</span>curr_expected_line == SIZE<span class="Delimiter">(</span>expected_lines<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
Trace_stream<span class="Delimiter">-></span>newline<span class="Delimiter">();</span>
string layer<span class="Delimiter">,</span> contents<span class="Delimiter">;</span>
split_layer_contents<span class="Delimiter">(</span>expected_lines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>curr_expected_line<span class="Delimiter">),</span> &layer<span class="Delimiter">,</span> &contents<span class="Delimiter">);</span>
for <span class="Delimiter">(</span>vector<trace_line>::iterator p = Trace_stream<span class="Delimiter">-></span>past_lines<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != Trace_stream<span class="Delimiter">-></span>past_lines<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="CommentedCode">//? cerr << "AAA " << layer << ' ' << p->label << '\n'; //? 1</span>
if <span class="Delimiter">(</span>layer != p<span class="Delimiter">-></span>label<span class="Delimiter">)</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="CommentedCode">//? cerr << "BBB ^" << contents << "$ ^" << p->contents << "$\n"; //? 1</span>
if <span class="Delimiter">(</span>contents != trim<span class="Delimiter">(</span>p<span class="Delimiter">-></span>contents<span class="Delimiter">))</span>
<span class="Identifier">continue</span><span class="Delimiter">;</span>
<span class="CommentedCode">//? cerr << "CCC\n"; //? 1</span>
++curr_expected_line<span class="Delimiter">;</span>
while <span class="Delimiter">(</span>curr_expected_line < SIZE<span class="Delimiter">(</span>expected_lines<span class="Delimiter">)</span> && expected_lines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>curr_expected_line<span class="Delimiter">).</span>empty<span class="Delimiter">())</span>
++curr_expected_line<span class="Delimiter">;</span>
if <span class="Delimiter">(</span>curr_expected_line == SIZE<span class="Delimiter">(</span>expected_lines<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>
split_layer_contents<span class="Delimiter">(</span>expected_lines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>curr_expected_line<span class="Delimiter">),</span> &layer<span class="Delimiter">,</span> &contents<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
++Num_failures<span class="Delimiter">;</span>
cerr << <span class="Constant">"</span><span class="cSpecial">\n</span><span class="Constant">F - "</span> << FUNCTION << <span class="Constant">"("</span> << FILE << <span class="Constant">":"</span> << LINE << <span class="Constant">"): missing ["</span> << contents << <span class="Constant">"] in trace:</span><span class="cSpecial">\n</span><span class="Constant">"</span><span class="Delimiter">;</span>
DUMP<span class="Delimiter">(</span>layer<span class="Delimiter">);</span>
<span class="CommentedCode">//? exit(0); //? 1</span>
Passed = <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
void split_layer_contents<span class="Delimiter">(</span>const string& s<span class="Delimiter">,</span> string* layer<span class="Delimiter">,</span> string* contents<span class="Delimiter">)</span> <span class="Delimiter">{</span>
static const string delim<span class="Delimiter">(</span><span class="Constant">": "</span><span class="Delimiter">);</span>
size_t pos = s<span class="Delimiter">.</span>find<span class="Delimiter">(</span>delim<span class="Delimiter">);</span>
if <span class="Delimiter">(</span>pos == string::npos<span class="Delimiter">)</span> <span class="Delimiter">{</span>
*layer = <span class="Constant">""</span><span class="Delimiter">;</span>
*contents = trim<span class="Delimiter">(</span>s<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
else <span class="Delimiter">{</span>
*layer = trim<span class="Delimiter">(</span>s<span class="Delimiter">.</span>substr<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">,</span> pos<span class="Delimiter">));</span>
*contents = trim<span class="Delimiter">(</span>s<span class="Delimiter">.</span>substr<span class="Delimiter">(</span>pos+SIZE<span class="Delimiter">(</span>delim<span class="Delimiter">)));</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
^L
int trace_count<span class="Delimiter">(</span>string layer<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> trace_count<span class="Delimiter">(</span>layer<span class="Delimiter">,</span> <span class="Constant">""</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
int trace_count<span class="Delimiter">(</span>string layer<span class="Delimiter">,</span> string line<span class="Delimiter">)</span> <span class="Delimiter">{</span>
Trace_stream<span class="Delimiter">-></span>newline<span class="Delimiter">();</span>
long result = <span class="Constant">0</span><span class="Delimiter">;</span>
for <span class="Delimiter">(</span>vector<trace_line>::iterator p = Trace_stream<span class="Delimiter">-></span>past_lines<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != Trace_stream<span class="Delimiter">-></span>past_lines<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
if <span class="Delimiter">(</span>layer == p<span class="Delimiter">-></span>label<span class="Delimiter">)</span>
if <span class="Delimiter">(</span>line == <span class="Constant">""</span> || line == p<span class="Delimiter">-></span>contents<span class="Delimiter">)</span>
++result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="PreProc">#define CHECK_TRACE_WARNS() CHECK(trace_count(</span><span class="Constant">"warn"</span><span class="PreProc">) > </span><span class="Constant">0</span><span class="PreProc">)</span>
<span class="PreProc">#define CHECK_TRACE_DOESNT_WARN() \</span>
<span class="PreProc"> </span>if<span class="PreProc"> (trace_count(</span><span class="Constant">"warn"</span><span class="PreProc">) > </span><span class="Constant">0</span><span class="PreProc">) </span><span class="Delimiter">{</span><span class="PreProc"> \</span>
<span class="PreProc"> ++Num_failures</span><span class="Delimiter">;</span><span class="PreProc"> \</span>
<span class="PreProc"> cerr << </span><span class="Constant">"</span><span class="cSpecial">\n</span><span class="Constant">F - "</span><span class="PreProc"> << __FUNCTION__ << </span><span class="Constant">"("</span><span class="PreProc"> << </span><span class="Constant">__FILE__</span><span class="PreProc"> << </span><span class="Constant">":"</span><span class="PreProc"> << </span><span class="Constant">__LINE__</span><span class="PreProc"> << </span><span class="Constant">"): unexpected warnings</span><span class="cSpecial">\n</span><span class="Constant">"</span><span class="Delimiter">;</span><span class="PreProc"> \</span>
<span class="PreProc"> DUMP(</span><span class="Constant">"warn"</span><span class="PreProc">)</span><span class="Delimiter">;</span><span class="PreProc"> \</span>
<span class="PreProc"> Passed = </span><span class="Constant">false</span><span class="Delimiter">;</span><span class="PreProc"> \</span>
<span class="PreProc"> </span><span class="Identifier">return</span><span class="Delimiter">;</span><span class="PreProc"> \</span>
<span class="PreProc"> </span><span class="Delimiter">}</span>
bool trace_doesnt_contain<span class="Delimiter">(</span>string layer<span class="Delimiter">,</span> string line<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> trace_count<span class="Delimiter">(</span>layer<span class="Delimiter">,</span> line<span class="Delimiter">)</span> == <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
bool trace_doesnt_contain<span class="Delimiter">(</span>string expected<span class="Delimiter">)</span> <span class="Delimiter">{</span>
vector<string> tmp = split<span class="Delimiter">(</span>expected<span class="Delimiter">,</span> <span class="Constant">": "</span><span class="Delimiter">);</span>
<span class="Identifier">return</span> trace_doesnt_contain<span class="Delimiter">(</span>tmp<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">),</span> tmp<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">));</span>
<span class="Delimiter">}</span>
<span class="PreProc">#define CHECK_TRACE_DOESNT_CONTAIN(</span><span class="Delimiter">...</span><span class="PreProc">) CHECK(trace_doesnt_contain(__VA_ARGS__))</span>
^L
vector<string> split<span class="Delimiter">(</span>string s<span class="Delimiter">,</span> string delim<span class="Delimiter">)</span> <span class="Delimiter">{</span>
vector<string> result<span class="Delimiter">;</span>
size_t begin=<span class="Constant">0</span><span class="Delimiter">,</span> end=s<span class="Delimiter">.</span>find<span class="Delimiter">(</span>delim<span class="Delimiter">);</span>
while <span class="Delimiter">(</span><span class="Constant">true</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
if <span class="Delimiter">(</span>end == string::npos<span class="Delimiter">)</span> <span class="Delimiter">{</span>
result<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>string<span class="Delimiter">(</span>s<span class="Delimiter">,</span> begin<span class="Delimiter">,</span> string::npos<span class="Delimiter">));</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
result<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>string<span class="Delimiter">(</span>s<span class="Delimiter">,</span> begin<span class="Delimiter">,</span> end-begin<span class="Delimiter">));</span>
begin = SIZE<span class="Delimiter">(</span>end+delim<span class="Delimiter">);</span>
end = s<span class="Delimiter">.</span>find<span class="Delimiter">(</span>delim<span class="Delimiter">,</span> begin<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
string trim<span class="Delimiter">(</span>const string& s<span class="Delimiter">)</span> <span class="Delimiter">{</span>
string::const_iterator first = s<span class="Delimiter">.</span>begin<span class="Delimiter">();</span>
while <span class="Delimiter">(</span>first != s<span class="Delimiter">.</span>end<span class="Delimiter">()</span> && isspace<span class="Delimiter">(</span>*first<span class="Delimiter">))</span>
++first<span class="Delimiter">;</span>
if <span class="Delimiter">(</span>first == s<span class="Delimiter">.</span>end<span class="Delimiter">())</span> <span class="Identifier">return</span> <span class="Constant">""</span><span class="Delimiter">;</span>
string::const_iterator last = --s<span class="Delimiter">.</span>end<span class="Delimiter">();</span>
while <span class="Delimiter">(</span>last != s<span class="Delimiter">.</span>begin<span class="Delimiter">()</span> && isspace<span class="Delimiter">(</span>*last<span class="Delimiter">))</span>
--last<span class="Delimiter">;</span>
++last<span class="Delimiter">;</span>
<span class="Identifier">return</span> string<span class="Delimiter">(</span>first<span class="Delimiter">,</span> last<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Includes")</span>
<span class="PreProc">#include</span><span class="Constant"><vector></span>
using std::vector<span class="Delimiter">;</span>
<span class="PreProc">#include</span><span class="Constant"><list></span>
using std::list<span class="Delimiter">;</span>
<span class="PreProc">#include</span><span class="Constant"><map></span>
using std::map<span class="Delimiter">;</span>
<span class="PreProc">#include</span><span class="Constant"><set></span>
using std::set<span class="Delimiter">;</span>
<span class="PreProc">#include</span><span class="Constant"><algorithm></span>
<span class="PreProc">#include</span><span class="Constant"><iostream></span>
using std::istream<span class="Delimiter">;</span>
using std::ostream<span class="Delimiter">;</span>
using std::cin<span class="Delimiter">;</span>
using std::cout<span class="Delimiter">;</span>
using std::cerr<span class="Delimiter">;</span>
<span class="PreProc">#include</span><span class="Constant"><iomanip></span>
<span class="PreProc">#include</span><span class="Constant"><sstream></span>
using std::istringstream<span class="Delimiter">;</span>
using std::ostringstream<span class="Delimiter">;</span>
<span class="PreProc">#include</span><span class="Constant"><fstream></span>
using std::ifstream<span class="Delimiter">;</span>
using std::ofstream<span class="Delimiter">;</span>
<span class="PreProc">#define unused __attribute__((unused))</span>
<span class="Delimiter">:(before "End Globals")</span>
<span class="Comment">//: In future layers we'll use the depth field as follows:</span>
<span class="Comment">//:</span>
<span class="Comment">//: Mu 'applications' will be able to use depths 1-99 as they like.</span>
<span class="Comment">//: Depth 100 will be for scheduling (more on that later).</span>
const int Scheduling_depth = <span class="Constant">100</span><span class="Delimiter">;</span>
<span class="Comment">//: Primitive statements will occupy 101-9998</span>
const int Initial_callstack_depth = <span class="Constant">101</span><span class="Delimiter">;</span>
const int Max_callstack_depth = <span class="Constant">9998</span><span class="Delimiter">;</span>
<span class="Comment">//: (ignore this until the call layer)</span>
<span class="Delimiter">:(before "End Globals")</span>
int Callstack_depth = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Setup")</span>
Callstack_depth = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Comment">//: Finally, details of primitive mu statements will occupy depth 9999 (more on that later as well)</span>
<span class="Delimiter">:(before "End Globals")</span>
const int Primitive_recipe_depth = <span class="Constant">9999</span><span class="Delimiter">;</span>
<span class="Comment">//:</span>
<span class="Comment">//: This framework should help us hide some details at each level, mixing</span>
<span class="Comment">//: static ideas like layers with the dynamic notion of call-stack depth.</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
|