// Warning: zero automated tests // Includes #include "termbox/termbox.h" #include #define SIZE(X) (assert((X).size() < (1LL<<(sizeof(int)*8-2))), static_cast((X).size())) #include #include using std::istream; using std::ostream; using std::iostream; using std::cin; using std::cout; using std::cerr; #include #include #include using std::string; #include using std::vector; #include using std::set; #include using std::ostringstream; #include using std::ifstream; using std::ofstream; #include using std::map; #include using std::pair; // End Includes // Types struct trace_line { string contents; string label; int depth; // 0 is 'sea level'; positive integers are progressively 'deeper' and lower level trace_line(string c, string l, int d) { contents = c; label = l; depth = d; } }; struct trace_stream { vector past_lines; }; enum search_direction { FORWARD, BACKWARD }; // End Types // from http://stackoverflow.com/questions/152643/idiomatic-c-for-reading-from-a-const-map template typename T::mapped_type& get(T& map, typename T::key_type const& key) { typename T::iterator iter(map.find(key)); assert(iter != map.end()); return iter->second; } template typename T::mapped_type const& get(const T& map, typename T::key_type const& key) { typename T::const_iterator iter(map.find(key)); assert(iter != map.end()); return iter->second; } template typename T::mapped_type const& put(T& map, typename T::key_type const& key, typename T::mapped_type const& value) { // map[key] requires mapped_type to have a zero-arg (default) constructor map.insert(std::make_pair(key, value)).first->second = value; return value; } template bool contains_key(T& map, typename T::key_type const& key) { return map.find(key) != map.end(); } template typename T::mapped_type& get_or_insert(T& map, typename T::key_type const& key) { return map[key]; } // Globals trace_stream* Trace_stream = NULL; ofstream Trace_file; int Cursor_row = 0; // screen coordinate set Visible; int Top_of_screen = 0; // trace coordinate int Left_of_screen = 0; // trace coordinate int Last_printed_row = 0; // screen coordinate map Trace_index; // screen row -> trace index string Current_search_pattern = ""; search_direction Current_search_direction = FORWARD; // End Globals bool has_data(istream& in) { return in && !in.eof(); } void skip_whitespace_but_not_newline(istream& in) { while (true) { if (!has_data(in)) break; else if (in.peek() == '\n') break; else if (isspace(in.peek())) in.get(); else break; } } void load_trace(const char* filename) { ifstream tin(filename); if (!tin) { cerr << "no such file: " << filename << '\n'; exit(1); } Trace_stream = new trace_stream; while (has_data(tin)) { tin >> std::noskipws; skip_whitespace_but_not_newline(tin); if (!isdigit(tin.peek())) { string dummy; getline(tin, dummy); continue; } tin >> std::skipws; int depth; tin >> depth; string label; tin >> label; if (*--label.end() == ':') label.erase(--label.end()); string line; getline(tin, line); Trace_stream->past_lines.push_back(trace_line(line, label, depth)); } cerr << "lines read: " << Trace_stream->past_lines.size() << '\n'; } void refresh_screen_rows() { // Top_of_screen, Visible -> Trace_index int screen_row = 0, index = 0; Trace_index.clear(); for (screen_row = 0, index = Top_of_screen;
void empty_list_when_none_added(void **state);
void contains_one_element(void **state);
void first_element_correct(void **state);
void contains_two_elements(void **state);
void first_and_second_elements_correct(void **state);
void contains_three_elements(void **state);
void first_three_elements_correct(void **state);
void add_twice_at_beginning_adds_once(void **state);
void add_twice_in_middle_adds_once(void **state);
void add_twice_at_end_adds_once(void **state);
void find_first_exists(void **state);
void find_second_exists(void **state);
void find_third_exists(void **state);
void find_returns_null(void **state);
void find_on_empty_returns_null(void **state);
void find_twice_returns_second_when_two_match(void **state);
void find_five_times_finds_fifth(void **state);
void find_twice_returns_first_when_two_match_and_reset(void **state);
p_of_screen = get(Trace_index, Cursor_row); Cursor_row = 0; refresh_screen_rows(); } else if (key == 'h' || key == TB_KEY_ARROW_LEFT) { // pan screen one character left if (Left_of_screen > 0) --Left_of_screen; } else if (key == 'l' || key == TB_KEY_ARROW_RIGHT) { // pan screen one character right ++Left_of_screen; } else if (key == 'H') { // pan screen one screen-width left Left_of_screen -= (tb_width() - 5); if (Left_of_screen < 0) Left_of_screen = 0; } else if (key == 'L') { // pan screen one screen-width right Left_of_screen += (tb_width() - 5); } else if (key == 'J' || key == TB_KEY_PGDN || key == TB_KEY_CTRL_F) { // page-down if (Trace_index.find(tb_height()-1) != Trace_index.end()) { Top_of_screen = get(Trace_index, tb_height()-1) + 1; refresh_screen_rows(); } } else if (key == 'K' || key == TB_KEY_PGUP || key == TB_KEY_CTRL_B) { // page-up is more convoluted for (int screen_row = tb_height(); screen_row > 0 && Top_of_screen > 0; --screen_row) { --Top_of_screen; if (Top_of_screen <= 0) break; while (Top_of_screen > 0 && !contains_key(Visible, Top_of_screen)) --Top_of_screen; } if (Top_of_screen >= 0) refresh_screen_rows(); } else if (key == 'g' || key == TB_KEY_HOME) { Top_of_screen = 0; Last_printed_row = 0; Cursor_row = 0; refresh_screen_rows(); } else if (key == 'G' || key == TB_KEY_END) { // go to bottom of trace; largely like page-up, interestingly Top_of_screen = SIZE(Trace_stream->past_lines)-1; for (int screen_row = tb_height(); screen_row > 0 && Top_of_screen > 0; --screen_row) { --Top_of_screen; if (Top_of_screen <= 0) break; while (Top_of_screen > 0 && !contains_key(Visible, Top_of_screen)) --Top_of_screen; } refresh_screen_rows(); render(); // move cursor to bottom Cursor_row = Last_printed_row; refresh_screen_rows(); } else if (key == TB_KEY_CARRIAGE_RETURN) { // expand lines under current by one level assert(contains_key(Trace_index, Cursor_row)); int start_index = get(Trace_index, Cursor_row); int index = 0; // simultaneously compute end_index and min_depth int min_depth = 9999; for (index = start_index+1; index < SIZE(Trace_stream->past_lines); ++index) { if (contains_key(Visible, index)) break; trace_line& curr_line = Trace_stream->past_lines.at(index); assert(curr_line.depth > Trace_stream->past_lines.at(start_index).depth); if (curr_line.depth < min_depth) min_depth = curr_line.depth; } int end_index = index; // mark as visible all intervening indices at min_depth for (index = start_index; index < end_index; ++index) { trace_line& curr_line = Trace_stream->past_lines.at(index); if (curr_line.depth == min_depth) { Visible.insert(index); } } refresh_screen_rows(); } else if (key == TB_KEY_BACKSPACE || key == TB_KEY_BACKSPACE2) { // collapse all lines under current assert(contains_key(Trace_index, Cursor_row)); int start_index = get(Trace_index, Cursor_row); int index = 0; // end_index is the next line at a depth same as or lower than start_index int initial_depth = Trace_stream->past_lines.at(start_index).depth; for (index = start_index+1; index < SIZE(Trace_stream->past_lines); ++index) { if (!contains_key(Visible, index)) continue; trace_line& curr_line = Trace_stream->past_lines.at(index); if (curr_line.depth <= initial_depth) break; } int end_index = index; // mark as visible all intervening indices at min_depth for (index = start_index+1; index < end_index; ++index) { Visible.erase(index); } refresh_screen_rows(); } else if (key == '/') { if (start_search_editor(FORWARD)) search(Current_search_pattern, Current_search_direction); } else if (key == '?') { if (start_search_editor(BACKWARD)) search(Current_search_pattern, Current_search_direction); } else if (key == 'n') { if (!Current_search_pattern.empty()) search(Current_search_pattern, Current_search_direction); } else if (key == 'N') { if (!Current_search_pattern.empty()) search(Current_search_pattern, opposite(Current_search_direction)); } } tb_clear(); tb_shutdown(); return 0; }