diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2016-03-10 23:57:19 -0800 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2016-03-10 23:57:19 -0800 |
commit | 3fada0dac6d82cb76707312a979be4ef950a7f03 (patch) | |
tree | ec5be5ae01e74df7665292561dee483136bdb8f9 | |
parent | 9cd7658210aedc83c21637c716737e59f8c64f24 (diff) | |
download | mu-3fada0dac6d82cb76707312a979be4ef950a7f03.tar.gz |
2759
Clean up tangle/ helper.
-rw-r--r-- | tangle/001trace.cc | 227 | ||||
-rw-r--r-- | tangle/001trace.test.cc | 76 | ||||
-rw-r--r-- | tangle/030tangle.cc | 6 | ||||
-rw-r--r-- | tangle/030tangle.test.cc | 14 | ||||
-rw-r--r-- | tangle/boot.cc | 12 |
5 files changed, 19 insertions, 316 deletions
diff --git a/tangle/001trace.cc b/tangle/001trace.cc index a02cacd4..67dceba2 100644 --- a/tangle/001trace.cc +++ b/tangle/001trace.cc @@ -1,12 +1,10 @@ bool Hide_warnings = false; struct trace_stream { - vector<pair<string, pair<int, string> > > past_lines; // [(layer label, frame, line)] - map<string, int> frame; + vector<pair<string, string> > past_lines; // [(layer label, line)] // accumulator for current line ostringstream* curr_stream; string curr_layer; - string dump_layer; trace_stream() :curr_stream(NULL) {} ~trace_stream() { if (curr_stream) delete curr_stream; } @@ -17,46 +15,25 @@ struct trace_stream { return *curr_stream; } - // be sure to call this before messing with curr_stream or curr_layer or frame + // be sure to call this before messing with curr_stream or curr_layer void newline() { if (!curr_stream) return; string curr_contents = curr_stream->str(); curr_contents.erase(curr_contents.find_last_not_of("\r\n")+1); - past_lines.push_back(pair<string, pair<int, string> >(curr_layer, pair<int, string>(frame[curr_layer], curr_contents))); - if (curr_layer == "dump") - cerr << with_newline(curr_stream->str()); - else if ((!dump_layer.empty() && prefix_match(dump_layer, curr_layer)) - || (!Hide_warnings && curr_layer == "warn")) - cerr << curr_layer << "/" << frame[curr_layer] << ": " << with_newline(curr_stream->str()); + past_lines.push_back(pair<string, string>(curr_layer, curr_contents)); delete curr_stream; curr_stream = NULL; } - string readable_contents(string layer) { // missing layer = everything, frame, hierarchical layers + string readable_contents(string layer) { // missing layer = everything newline(); ostringstream output; - string real_layer, frame; - parse_layer_and_frame(layer, &real_layer, &frame); - for (vector<pair<string, pair<int, string> > >::iterator p = past_lines.begin(); p != past_lines.end(); ++p) - if (layer.empty() || prefix_match(real_layer, p->first)) - output << p->first << "/" << p->second.first << ": " << with_newline(p->second.second); + for (vector<pair<string, string> >::iterator p = past_lines.begin(); p != past_lines.end(); ++p) + if (layer.empty() || layer == p->first) + output << p->first << ": " << with_newline(p->second); return output.str(); } - void dump_browseable_contents(string layer) { - ofstream dump("dump"); - dump << "<div class='frame' frame_index='1'>start</div>\n"; - for (vector<pair<string, pair<int, string> > >::iterator p = past_lines.begin(); p != past_lines.end(); ++p) { - if (p->first != layer) continue; - dump << "<div class='frame"; - if (p->second.first > 1) dump << " hidden"; - dump << "' frame_index='" << p->second.first << "'>"; - dump << p->second.second; - dump << "</div>\n"; - } - dump.close(); - } - string with_newline(string s) { if (s[s.size()-1] != '\n') return s+'\n'; return s; @@ -72,12 +49,10 @@ trace_stream* Trace_stream = NULL; // Warnings should go straight to cerr by default since calls to trace() have // some unfriendly constraints (they delay printing, they can't nest) #define raise ((!Trace_stream || !Hide_warnings) ? cerr /*do print*/ : Trace_stream->stream("warn")) << __FILE__ << ":" << __LINE__ << " " -// Just debug logging without any test support. -#define dbg cerr << __FUNCTION__ << '(' << __FILE__ << ':' << __LINE__ << ") " // raise << die exits after printing -- unless Hide_warnings is set. struct die {}; -ostream& operator<<(ostream& os, unused die) { +ostream& operator<<(ostream& os, __attribute__((unused)) die) { if (Hide_warnings) return os; os << "dying"; exit(1); @@ -95,80 +70,9 @@ struct lease_tracer { #define START_TRACING_UNTIL_END_OF_SCOPE lease_tracer leased_tracer; -void trace_all(const string& label, const list<string>& in) { - for (list<string>::const_iterator p = in.begin(); p != in.end(); ++p) - trace(label) << *p; -} - -bool check_trace_contents(string FUNCTION, string FILE, int LINE, string expected) { // missing layer == anywhere, frame, hierarchical layers - vector<string> expected_lines = split(expected, ""); - size_t curr_expected_line = 0; - while (curr_expected_line < expected_lines.size() && expected_lines[curr_expected_line].empty()) - ++curr_expected_line; - if (curr_expected_line == expected_lines.size()) return true; - Trace_stream->newline(); - ostringstream output; - string layer, frame, contents; - parse_layer_frame_contents(expected_lines[curr_expected_line], &layer, &frame, &contents); - for (vector<pair<string, pair<int, string> > >::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { - if (!layer.empty() && !prefix_match(layer, p->first)) - continue; - - if (!frame.empty() && strtol(frame.c_str(), NULL, 0) != p->second.first) - continue; - - if (contents != p->second.second) - continue; - - ++curr_expected_line; - while (curr_expected_line < expected_lines.size() && expected_lines[curr_expected_line].empty()) - ++curr_expected_line; - if (curr_expected_line == expected_lines.size()) return true; - parse_layer_frame_contents(expected_lines[curr_expected_line], &layer, &frame, &contents); - } - - ++Num_failures; - cerr << "\nF " << FUNCTION << "(" << FILE << ":" << LINE << "): missing [" << contents << "] in trace:\n"; - DUMP(layer); - Passed = false; - return false; -} - -void parse_layer_frame_contents(const string& orig, string* layer, string* frame, string* contents) { - string layer_and_frame; - parse_contents(orig, ": ", &layer_and_frame, contents); - parse_layer_and_frame(layer_and_frame, layer, frame); -} - -void parse_contents(const string& s, const string& delim, string* prefix, string* contents) { - string::size_type pos = s.find(delim); - if (pos == NOT_FOUND) { - *prefix = ""; - *contents = s; - } - else { - *prefix = s.substr(0, pos); - *contents = s.substr(pos+delim.size()); - } -} - -void parse_layer_and_frame(const string& orig, string* layer, string* frame) { - size_t last_slash = orig.rfind('/'); - if (last_slash == NOT_FOUND - || last_slash == orig.size()-1 // trailing slash indicates hierarchical layer - || orig.find_last_not_of("0123456789") != last_slash) { - *layer = orig; - *frame = ""; - } - else { - *layer = orig.substr(0, last_slash); - *frame = orig.substr(last_slash+1); - } -} - -bool check_trace_contents(string FUNCTION, string FILE, int LINE, string layer, string expected) { // empty layer == everything, multiple layers, hierarchical layers +bool check_trace_contents(string FUNCTION, string FILE, int LINE, string layer, string expected) { // empty layer == everything vector<string> expected_lines = split(expected, ""); size_t curr_expected_line = 0; while (curr_expected_line < expected_lines.size() && expected_lines[curr_expected_line].empty()) @@ -176,11 +80,10 @@ bool check_trace_contents(string FUNCTION, string FILE, int LINE, string layer, if (curr_expected_line == expected_lines.size()) return true; Trace_stream->newline(); ostringstream output; - vector<string> layers = split(layer, ","); - for (vector<pair<string, pair<int, string> > >::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { - if (!layer.empty() && !any_prefix_match(layers, p->first)) + for (vector<pair<string, string> >::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { + if (!layer.empty() && layer != p->first) continue; - if (p->second.second != expected_lines[curr_expected_line]) + if (p->second != expected_lines[curr_expected_line]) continue; ++curr_expected_line; while (curr_expected_line < expected_lines.size() && expected_lines[curr_expected_line].empty()) @@ -197,35 +100,18 @@ bool check_trace_contents(string FUNCTION, string FILE, int LINE, string layer, #define CHECK_TRACE_CONTENTS(...) check_trace_contents(__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__) -int trace_count(string layer) { - return trace_count(layer, ""); -} - int trace_count(string layer, string line) { Trace_stream->newline(); long result = 0; - vector<string> layers = split(layer, ","); - for (vector<pair<string, pair<int, string> > >::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { - if (any_prefix_match(layers, p->first)) - if (line == "" || p->second.second == line) - ++result; - } - return result; -} - -int trace_count(string layer, int frame, string line) { - Trace_stream->newline(); - long result = 0; - vector<string> layers = split(layer, ","); - for (vector<pair<string, pair<int, string> > >::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { - if (any_prefix_match(layers, p->first) && p->second.first == frame) - if (line == "" || p->second.second == line) + for (vector<pair<string, string> >::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { + if (layer == p->first) + if (line == "" || p->second == line) ++result; } return result; } -#define CHECK_TRACE_WARNS() CHECK(trace_count("warn") > 0) +#define CHECK_TRACE_WARNS() CHECK(trace_count("warn", "") > 0) #define CHECK_TRACE_DOESNT_WARN() \ if (trace_count("warn") > 0) { \ ++Num_failures; \ @@ -239,68 +125,10 @@ bool trace_doesnt_contain(string layer, string line) { return trace_count(layer, line) == 0; } -bool trace_doesnt_contain(string expected) { - vector<string> tmp = split(expected, ": "); - return trace_doesnt_contain(tmp[0], tmp[1]); -} - -bool trace_doesnt_contain(string layer, int frame, string line) { - return trace_count(layer, frame, line) == 0; -} - #define CHECK_TRACE_DOESNT_CONTAIN(...) CHECK(trace_doesnt_contain(__VA_ARGS__)) -// manage layer counts in Trace_stream using RAII -struct lease_trace_frame { - string layer; - lease_trace_frame(string l) :layer(l) { - if (!Trace_stream) return; - Trace_stream->newline(); - ++Trace_stream->frame[layer]; - } - ~lease_trace_frame() { - if (!Trace_stream) return; - Trace_stream->newline(); - --Trace_stream->frame[layer]; - } -}; -#define new_trace_frame(layer) lease_trace_frame leased_frame(layer); - -bool check_trace_contents(string FUNCTION, string FILE, int LINE, string layer, int frame, string expected) { // multiple layers, hierarchical layers - vector<string> expected_lines = split(expected, ""); // hack: doesn't handle newlines in embedded in lines - size_t curr_expected_line = 0; - while (curr_expected_line < expected_lines.size() && expected_lines[curr_expected_line].empty()) - ++curr_expected_line; - if (curr_expected_line == expected_lines.size()) return true; - Trace_stream->newline(); - ostringstream output; - vector<string> layers = split(layer, ","); - for (vector<pair<string, pair<int, string> > >::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { - if (!layer.empty() && !any_prefix_match(layers, p->first)) - continue; - if (p->second.first != frame) - continue; - if (p->second.second != expected_lines[curr_expected_line]) - continue; - ++curr_expected_line; - while (curr_expected_line < expected_lines.size() && expected_lines[curr_expected_line].empty()) - ++curr_expected_line; - if (curr_expected_line == expected_lines.size()) return true; - } - - ++Num_failures; - cerr << "\nF " << FUNCTION << "(" << FILE << ":" << LINE << "): missing [" << expected_lines[curr_expected_line] << "] in trace/" << frame << ":\n"; - DUMP(layer); - Passed = false; - return false; -} - -#define CHECK_TRACE_TOP(layer, expected) CHECK_TRACE_CONTENTS(layer, 1, expected) - - - vector<string> split(string s, string delim) { vector<string> result; string::size_type begin=0, end=s.find(delim); @@ -315,26 +143,3 @@ vector<string> split(string s, string delim) { } return result; } - -bool any_prefix_match(const vector<string>& pats, const string& needle) { - if (pats.empty()) return false; - if (*pats[0].rbegin() != '/') - // prefix match not requested - return find(pats.begin(), pats.end(), needle) != pats.end(); - // first pat ends in a '/'; assume all pats do. - for (vector<string>::const_iterator p = pats.begin(); p != pats.end(); ++p) - if (headmatch(needle, *p)) return true; - return false; -} - -bool prefix_match(const string& pat, const string& needle) { - if (*pat.rbegin() != '/') - // prefix match not requested - return pat == needle; - return headmatch(needle, pat); -} - -bool headmatch(const string& s, const string& pat) { - if (pat.size() > s.size()) return false; - return std::mismatch(pat.begin(), pat.end(), s.begin()).first == pat.end(); -} diff --git a/tangle/001trace.test.cc b/tangle/001trace.test.cc index e0db457c..4e403b65 100644 --- a/tangle/001trace.test.cc +++ b/tangle/001trace.test.cc @@ -42,88 +42,12 @@ void test_trace_orders_across_layers() { CHECK_TRACE_CONTENTS("", "foobarqux"); } -void test_trace_orders_across_layers2() { - trace("test layer 1") << "foo"; - trace("test layer 2") << "bar"; - trace("test layer 1") << "qux"; - CHECK_TRACE_CONTENTS("foobarqux"); -} - -void test_trace_checks_ordering_spanning_multiple_layers() { - trace("layer1") << "foo"; - trace("layer2") << "bar"; - trace("layer1") << "qux"; - CHECK_TRACE_CONTENTS("layer1: foolayer2: barlayer1: qux"); -} - -void test_trace_segments_within_layers() { - trace("test layer 1") << "foo"; - trace("test layer 2") << "bar"; - new_trace_frame("test layer 1"); - trace("test layer 1") << "qux"; - CHECK_TRACE_CONTENTS("test layer 1", "fooqux"); - CHECK_TRACE_CONTENTS("test layer 1", 0, "foo"); - CHECK_TRACE_DOESNT_CONTAIN("test layer 1", 1, "foo"); -} - -void test_trace_checks_ordering_across_layers_and_frames() { - trace("test layer 1") << "foo"; - trace("test layer 2") << "bar"; - new_trace_frame("test layer 1"); - trace("test layer 1") << "qux"; - CHECK_TRACE_CONTENTS("test layer 1/0: footest layer 2: bartest layer 1: qux"); - CHECK_TRACE_CONTENTS("test layer 1: footest layer 2: bartest layer 1/1: qux"); -} - -void trace_test_fn(int n) { - if (n == 0) return; - new_trace_frame("foo"); - trace("foo") << "before: " << n; - trace_test_fn(n-1); - trace("foo") << "after: " << n; -} - -void test_trace_keeps_level_together() { - CHECK_TRACE_CONTENTS("foo", ""); - trace_test_fn(4); - CHECK_TRACE_CONTENTS("foo", 2, "before: 3after: 3"); -} - -void test_trace_supports_multiple_layers() { - trace("test layer 1") << "foo"; - trace("test layer 2") << "bar"; - trace("test layer 1") << "qux"; - CHECK_TRACE_CONTENTS("test layer 1,test layer 2", "foobarqux"); -} - -void test_trace_supports_hierarchical_layers() { - trace("test layer/a") << "foo"; - trace("different layer/c") << "foo 2"; - trace("test layer/b") << "bar"; - CHECK_TRACE_CONTENTS("test layer/", "foobar"); -} - void test_trace_supports_count() { trace("test layer 1") << "foo"; trace("test layer 1") << "foo"; CHECK_EQ(trace_count("test layer 1", "foo"), 2); } -void test_trace_supports_count2() { - trace("test layer 1") << "foo"; - trace("test layer 1") << "bar"; - CHECK_EQ(trace_count("test layer 1"), 2); -} - -// pending: DUMP tests -// pending: readable_contents() adds newline if necessary. -// pending: RAISE also prints to stderr. -// pending: RAISE doesn't print to stderr if Hide_warnings is set. -// pending: RAISE doesn't have to be saved if Hide_warnings is set, just printed. -// pending: RAISE prints to stderr if Trace_stream is NULL. -// pending: RAISE prints to stderr if Trace_stream is NULL even if Hide_warnings is set. -// pending: RAISE << ... die() doesn't die if Hide_warnings is set. - // can't check trace because trace methods call 'split' diff --git a/tangle/030tangle.cc b/tangle/030tangle.cc index c4a3db5f..b536f242 100644 --- a/tangle/030tangle.cc +++ b/tangle/030tangle.cc @@ -56,7 +56,7 @@ string Toplevel = "run"; int tangle(int argc, const char* argv[]) { list<Line> result; for (int i = 1; i < argc; ++i) { -//? cerr << "new file " << argv[i] << '\n'; //? 1 +//? cerr << "new file " << argv[i] << '\n'; Toplevel = "run"; ifstream in(argv[i]); tangle(in, argv[i], result); @@ -150,9 +150,9 @@ void process_next_hunk(istream& in, const string& directive, const string& filen list<Line> result; string name = next_tangle_token(directive_stream); emit_test(name, hunk, result); -//? cerr << out.size() << " " << result.size() << '\n'; //? 1 +//? cerr << out.size() << " " << result.size() << '\n'; out.insert(out.end(), result.begin(), result.end()); -//? cerr << out.size() << " " << result.size() << '\n'; //? 1 +//? cerr << out.size() << " " << result.size() << '\n'; return; } diff --git a/tangle/030tangle.test.cc b/tangle/030tangle.test.cc index 7a329803..0c7b94f3 100644 --- a/tangle/030tangle.test.cc +++ b/tangle/030tangle.test.cc @@ -50,7 +50,6 @@ void test_tangle_with_multiple_filenames_after() { istringstream in2(":(after b)\nd\n"); tangle(in2, "bar", dummy); CHECK_TRACE_CONTENTS("tangle", "ab#line 2 \"bar\"d#line 3 \"foo\"c"); -//? exit(0); //? 1 } void test_tangle_skip_tanglecomments() { @@ -225,19 +224,6 @@ void test_tangle_can_hide_warnings_in_scenarios() { CHECK(lines.empty()); } -void test_tangle_can_handle_in_scenarios() { - istringstream in(":(scenario does_bar)\n% Hide_warnings = true;\nabc def\n+layer1: pqr\n+layer2: xyz"); - list<Line> lines; - tangle(in, lines); - CHECK_EQ(lines.front().contents, "void test_does_bar() {"); lines.pop_front(); - CHECK_EQ(lines.front().contents, " Trace_file = \"does_bar\";"); lines.pop_front(); - CHECK_EQ(lines.front().contents, " Hide_warnings = true;"); lines.pop_front(); - CHECK_EQ(lines.front().contents, " run(\"abc def\\n\");"); lines.pop_front(); - CHECK_EQ(lines.front().contents, " CHECK_TRACE_CONTENTS(\"layer1: pqrlayer2: xyz\");"); lines.pop_front(); - CHECK_EQ(lines.front().contents, "}"); lines.pop_front(); - CHECK(lines.empty()); -} - void test_tangle_supports_strings_in_scenarios() { istringstream in(":(scenario does_bar)\nabc \"def\"\n+layer1: pqr\n+layer2: \"xyz\""); list<Line> lines; diff --git a/tangle/boot.cc b/tangle/boot.cc index 86385965..33379f24 100644 --- a/tangle/boot.cc +++ b/tangle/boot.cc @@ -1,21 +1,11 @@ -#define unused __attribute__((unused)) - #include<assert.h> -#include<cstdio> -#include<cstring> -#include<cstdlib> #include<vector> using std::vector; #include<list> using std::list; -#include<stack> -using std::stack; #include<utility> using std::pair; -#include<map> -using std::map; -#include<algorithm> #include<string> using std::string; @@ -24,7 +14,6 @@ const size_t NOT_FOUND = string::npos; #include<iostream> using std::istream; using std::ostream; -using std::iostream; using std::cin; using std::cout; using std::cerr; @@ -35,7 +24,6 @@ using std::ostringstream; #include<fstream> using std::ifstream; -using std::ofstream; #include <locale> using std::isspace; // unicode-aware |