diff options
-rw-r--r-- | 000organization.cc | 5 | ||||
-rw-r--r-- | 002test.cc | 6 | ||||
-rw-r--r-- | 003trace.cc | 41 |
3 files changed, 32 insertions, 20 deletions
diff --git a/000organization.cc b/000organization.cc index cd02d58d..dccbee8e 100644 --- a/000organization.cc +++ b/000organization.cc @@ -98,6 +98,11 @@ #include "function_list" // by convention, files ending with '_list' are auto-generated // Globals +// +// (Code in this section should strictly consist only of single-line variable +// definitions; the makefile will simplistically auto-generate extern +// declarations for them.) +// // End Globals int main(int argc, char* argv[]) { diff --git a/002test.cc b/002test.cc index baa2efa4..b9cb0e15 100644 --- a/002test.cc +++ b/002test.cc @@ -10,16 +10,18 @@ :(before "End Types") typedef void (*test_fn)(void); - -:(before "End Globals") +:(before "Globals") +// move a global ahead into types that we can't generate an extern declaration for const test_fn Tests[] = { #include "test_list" // auto-generated; see makefile }; +:(before "End Globals") bool Run_tests = false; bool Passed = true; // set this to false inside any test to indicate failure long Num_failures = 0; +:(before "End Includes") #define CHECK(X) \ if (!(X)) { \ ++Num_failures; \ diff --git a/003trace.cc b/003trace.cc index bbe5fb80..d8b92ba1 100644 --- a/003trace.cc +++ b/003trace.cc @@ -155,11 +155,19 @@ string trace_stream::readable_contents(string label) { trace_stream* Trace_stream = NULL; int Trace_errors = 0; // used only when Trace_stream is NULL +:(before "End Includes") +#define CLEAR_TRACE delete Trace_stream, Trace_stream = new trace_stream; + // Top-level helper. IMPORTANT: can't nest #define trace(...) !Trace_stream ? cerr /*print nothing*/ : Trace_stream->stream(__VA_ARGS__) +// Just for debugging; 'git log' should never show any calls to 'dbg'. +#define dbg trace(0, "a") +#define DUMP(label) if (Trace_stream) cerr << Trace_stream->readable_contents(label); + // Errors are a special layer. #define raise (!Trace_stream ? (tb_shutdown(),++Trace_errors,cerr) /*do print*/ : Trace_stream->stream(Error_depth, "error")) + // Inside tests, fail any tests that displayed (unexpected) errors. // Expected errors in tests should always be hidden and silently checked for. :(before "End Test Teardown") @@ -168,9 +176,6 @@ if (Passed && !Hide_errors && trace_count("error") > 0) { ++Num_failures; } -// Just for debugging. -#define dbg trace(0, "a") - :(before "End Types") struct end {}; :(code) @@ -180,26 +185,26 @@ ostream& operator<<(ostream& os, unused end) { } :(before "End Globals") -#define CLEAR_TRACE delete Trace_stream, Trace_stream = new trace_stream; - -#define DUMP(label) if (Trace_stream) cerr << Trace_stream->readable_contents(label); - bool Save_trace = false; // Trace_stream is a resource, lease_tracer uses RAII to manage it. +:(before "End Types") struct lease_tracer { - lease_tracer() { Trace_stream = new trace_stream; } - ~lease_tracer() { - if (!Trace_stream) return; // in case tests close Trace_stream - if (Save_trace) { - ofstream fout("last_trace"); - fout << Trace_stream->readable_contents(""); - fout.close(); - } - delete Trace_stream, Trace_stream = NULL; - } + lease_tracer(); + ~lease_tracer(); }; - +:(code) +lease_tracer::lease_tracer() { Trace_stream = new trace_stream; } +lease_tracer::~lease_tracer() { + if (!Trace_stream) return; // in case tests close Trace_stream + if (Save_trace) { + ofstream fout("last_trace"); + fout << Trace_stream->readable_contents(""); + fout.close(); + } + delete Trace_stream, Trace_stream = NULL; +} +:(before "End Includes") #define START_TRACING_UNTIL_END_OF_SCOPE lease_tracer leased_tracer; :(before "End Test Setup") START_TRACING_UNTIL_END_OF_SCOPE |