//--------------------------------------------------------------------------- #include #include #include #include #include #include #include #include "subrange_type.h" //--------------------------------------------------------------------------- using namespace std; typedef subrange subrange_15; subrange_15 a(15), b(2), r; bool compr; string filename("a.dat"); int x; class display_fnctr : public unary_function { public: void operator()(const string& str) { cout << str << endl; } }; typedef enum { main_start = 0, alter_a_start = 100, alter_b_start = 150, increment_decrement_start = 200, bitwise_start = 300, arithmetic_start = 400, comparison_start = 500, conversion_start = 600, copy_constructor_start = 700, stl_map_start = 800, write_start = 900, read_start = 1000, assignment_start = 1100, shift_start = 1200 } menu_idx_start; class menu { vector menu_strs; int start_index; char parent; public: menu() : start_index(), parent('z') {} menu(const int idx, const char par = 'z') : start_index(idx), parent(par) {} bool empty_menu() { return menu_strs.size() == 0; } void display() { if (menu_strs.size() == 0) { return; } for_each(menu_strs.begin(), menu_strs.end(), display_fnctr()); cout << endl << "Choice: "; } char get_local_choice() { if (menu_strs.size() == 0) { return 'a'; } char choice; cin >> choice; return choice; } int get_choice() { if (menu_strs.size() == 0) { return start_index; } char choice; cin >> choice; int x = (parent == 'z') ? -2 : -1; //int x = -1; return (choice == 'x') ? x : (start_index + choice - 'a'); } void add_string(const string& str) { menu_strs.push_back(str); } void clear() { menu_strs.clear(); } char parent_menu() { return parent; } }; typedef map menu_map; menu_map menus; typedef void (*test_func)(); typedef map test_function_map; test_function_map test_funcs; void init_main_menu() { menu m(main_start); m.add_string("a) Alter a"); m.add_string("b) Alter b"); m.add_string("c) Increment/decrement tests"); m.add_string("d) Bitwise tests"); m.add_string("e) Arithmetic operator tests"); m.add_string("f) Comparison operator tests"); m.add_string("g) Conversion operator test"); m.add_string("h) Copy constructor test"); m.add_string("i) STL map test"); m.add_string("j) File stream write test"); m.add_string("k) File stream read test"); m.add_string("l) Assignment test"); m.add_string("m) Shift operator tests"); m.add_string("x) Exit"); menus['z'] = m; } void init_alter_a_menu() { menus['a'] = menu(alter_a_start); } void init_alter_b_menu() { menus['b'] = menu(alter_b_start); } void init_inc_dec_menu() { menu m(increment_decrement_start); m.add_string("a) ++a"); m.add_string("b) a++"); m.add_string("c) --a"); m.add_string("d) a--"); m.add_string("e) Alter a"); m.add_string("x) Exit"); menus['c'] = m; } void init_bitwise_tests_menu() { menu m(bitwise_start); m.add_string("a) a & b"); m.add_string("b) a ^ b"); m.add_string("c) a | b"); m.add_string("d) a &= b"); m.add_string("e) a ^= b"); m.add_string("f) a |= b"); m.add_string("g) Alter a"); m.add_string("h) Alter b"); m.add_string("x) Exit"); menus['d'] = m; } void init_arithmetic_ops_menu() { menu m(arithmetic_start); m.add_string("a) a + b"); m.add_string("b) a - b"); m.add_string("c) a * b"); m.add_string("d) a / b"); m.add_string("e) a % b"); m.add_string("f) a += b"); m.add_string("g) a -= b"); m.add_string("h) a *= b"); m.add_string("i) a /= b"); m.add_string("j) a %= b"); m.add_string("k) Alter a"); m.add_string("l) Alter b"); m.add_string("x) Exit"); menus['e'] = m; } void init_comparison_ops_menu() { menu m(comparison_start); m.add_string("a) a == b"); m.add_string("b) a < b"); m.add_string("c) a > b"); m.add_string("d) a <= b"); m.add_string("e) a >= b"); m.add_string("f) a != b"); m.add_string("g) Alter a"); m.add_string("h) Alter b"); m.add_string("x) Exit"); menus['f'] = m; } void init_conversion_op_menu() { menus['g'] = menu(conversion_start); } void init_copy_constructor_test_menu() { menus['h'] = menu(copy_constructor_start); } void init_stl_map_test_menu() { menus['i'] = menu(stl_map_start); } void init_file_stream_write_test_menu() { menu m(write_start); m.add_string("a) Set file name"); m.add_string("b) Write a to file"); m.add_string("x) Exit"); menus['j'] = m; } void init_file_stream_read_test_menu() { menu m(read_start); m.add_string("a) Set file name"); m.add_string("b) Read from file"); m.add_string("x) Exit"); menus['k'] = m; } void init_assignment_tests_menu() { menu m(assignment_start); m.add_string("a) a = b"); m.add_string("b) a = x"); m.add_string("c) x = a"); m.add_string("d) Alter a"); m.add_string("e) Alter b"); m.add_string("x) Exit"); menus['l'] = m; } void init_shift_tests() { menu m(shift_start); m.add_string("a) r = a << x"); m.add_string("b) r = a >> x"); m.add_string("c) a <<= x"); m.add_string("d) a >>= x"); m.add_string("e) Alter a"); m.add_string("x) Exit"); menus['m'] = m; } void init_menus() { init_main_menu(); init_alter_a_menu(); init_alter_b_menu(); init_inc_dec_menu(); init_bitwise_tests_menu(); init_arithmetic_ops_menu(); init_comparison_ops_menu(); init_conversion_op_menu(); init_copy_constructor_test_menu(); init_stl_map_test_menu(); init_file_stream_write_test_menu(); init_file_stream_read_test_menu(); init_assignment_tests_menu(); init_shift_tests(); } void display_choice_prompt() { cout << endl << "Choice: "; } void write_separator() { cout << endl << endl; } void write_a() { cout << "a = " << a << endl; } void write_b() { cout << "b = " << b << endl; } void write_r_a() { cout << "r = " << r << endl; write_a(); } void write_a_b() { write_a(); write_b(); } void write_r_a_b() { cout << "r = " << r << endl; write_a_b(); } void write_compr_a_b() { cout << "compr = " << boolalpha << compr << endl; write_a_b(); } void write_x() { cout << "x = " << x << endl; } void display_values() { write_a(); write_b(); cout << endl; } int display_main_menu() { display_values(); menu m = menus['z']; m.display(); char c = m.get_local_choice(); if (c == 'x') { return -1; } m = menus[c]; m.display(); return m.get_choice(); } int display_menu(char& mid) { menu m; if (mid == 'z') { display_values(); m = menus[mid]; m.display(); char c = m.get_local_choice(); if (c == 'x') { return -1; } mid = c; } m = menus[mid]; m.display(); int res = m.get_choice(); mid = (res < 0 || m.empty_menu()) ? m.parent_menu() : mid; return res; } void display_title(string title) { cout << title << endl << endl; } void do_test(const int test) { if (test >= 0) { test_funcs[test](); } } void wait_for_key() { char ch; cout << endl << "Press a key to continue: "; cin >> ch; cout << endl; } void alter_a() { display_title("Alter a"); cout << "a = " << a << endl; cout << endl << "New value: "; cin >> x; cout << endl; a = x; } void alter_b() { display_title("Alter b"); cout << "b = " << b << endl; cout << endl << "New value: "; cin >> x; cout << endl; b = x; } void pre_inc_a_test() { display_title("++a test"); r = ++a; write_r_a(); wait_for_key(); } void post_inc_a_test() { display_title("a++ test"); r = a++; write_r_a(); wait_for_key(); } void post_dec_a_test() { display_title("--a test"); r = a--; write_r_a(); wait_for_key(); } void pre_dec_a_test() { display_title("a-- test"); r = --a; write_r_a(); wait_for_key(); } void a_and_b_test() { display_title("r = a & b test"); r = a & b; write_r_a_b(); wait_for_key(); } void a_xor_b_test() { display_title("r = a ^ b test"); r = a ^ b; write_r_a_b(); wait_for_key(); } void a_or_b_test() { display_title("r = a | b test"); r = a | b; write_r_a_b(); wait_for_key(); } void a_and_equals_b_test() { display_title("a &= b test"); a &= b; write_a_b(); wait_for_key(); } void a_xor_equals_b_test() { display_title("a &= b test"); a ^= b; write_a_b(); wait_for_key(); } void a_or_equals_b_test() { display_title("a |= b test"); a |= b; write_a_b(); wait_for_key(); } void a_plus_b_test() { display_title("r = a + b test"); r = a + b; write_r_a_b(); wait_for_key(); } void a_minus_b_test() { display_title("r = a - b test"); r = a - b; write_r_a_b(); wait_for_key(); } void a_times_b_test() { display_title("r = a * b test"); r = a * b; write_r_a_b(); wait_for_key(); } void a_div_b_test() { display_title("r = a / b test"); r = a / b; write_r_a_b(); wait_for_key(); } void a_mod_b_test() { display_title("r = a % b test"); r = a % b; write_r_a_b(); wait_for_key(); } void a_plus_equals_b_test() { display_title("a += b test"); a += b; write_a_b(); wait_for_key(); } void a_minus_equals_b_test() { display_title("a -= b test"); a -= b; write_a_b(); wait_for_key(); } void a_times_equals_b_test() { display_title("a *= b test"); a *= b; write_a_b(); wait_for_key(); } void a_div_equals_b_test() { display_title("a /= b test"); a /= b; write_a_b(); wait_for_key(); } void a_mod_equals_b_test() { display_title("a %= b test"); a %= b; write_a_b(); wait_for_key(); } void a_equals_b_test() { display_title("compr = a == b test"); compr = a == b; write_compr_a_b(); wait_for_key(); } void a_less_than_b_test() { display_title("compr = a < b test"); compr = a < b; write_compr_a_b(); wait_for_key(); } void a_greater_than_b_test() { display_title("compr = a > b test"); compr = a > b; write_compr_a_b(); wait_for_key(); } void a_less_than_or_equal_to_b_test() { display_title("compr = a <= b test"); compr = a <= b; write_compr_a_b(); wait_for_key(); } void a_greater_than_or_equal_to_b_test() { display_title("compr = a >= b test"); compr = a >= b; write_compr_a_b(); wait_for_key(); } void a_not_equal_to_b_test() { display_title("compr = a != b test"); compr = a != b; write_compr_a_b(); wait_for_key(); } void conversion_op_test() { display_title("x = operator T() test"); x = a; write_a(); cout << "x = " << a << endl; wait_for_key(); } void copy_constructor_fn(const subrange_15 x) { cout << "copy constructed a = " << x << endl; } void copy_constructor_test() { display_title("Copy constructor test"); write_a(); copy_constructor_fn(a); wait_for_key(); } void stl_map_test() { typedef map string_map; display_title("STL map test"); string_map str_map; subrange_15 x(8); str_map[x] = "eight"; x++; str_map[x] = "nine"; string str; for (string_map::iterator i = str_map.begin(); i != str_map.end(); ++i) { cout << i->first << "->" << i->second << endl; } wait_for_key(); } void alter_filename() { display_title("Alter filename"); cout << "Old filename: " << filename << endl; cout << "New name: "; cin >> filename; cout << endl; } void file_write_test() { display_title("Write subrange to file stream test"); ofstream out_s; out_s.open(filename.c_str()); out_s << a; out_s.close(); } void file_read_test() { display_title("Read subrange from file stream test"); ifstream in_s; in_s.open(filename.c_str()); in_s >> a; in_s.close(); } void a_assign_b() { display_title("a = b"); display_title("Before a = b"); write_a_b(); write_separator(); a = b; cout << endl; display_title("After a = b"); write_a_b(); write_separator(); wait_for_key(); } int enter_x() { int x; cout << "Enter a value for x: "; cin >> x; cout << endl; return x; } void a_assign_x() { display_title("a = x"); x = enter_x(); display_title("Before a = x"); write_x(); write_a(); write_separator(); a = x; cout << endl; display_title("After a = x"); write_x(); write_a(); write_separator(); wait_for_key(); } void x_assign_a() { display_title("x = a"); x = enter_x(); display_title("Before a = x"); write_x(); write_a(); write_separator(); x = a; cout << endl; display_title("After a = x"); write_x(); write_a(); write_separator(); wait_for_key(); } void shift_left() { display_title("r = a << x and r = x << a"); cout << "x: "; cin >> x; write_separator(); display_title("Before r = a << x"); write_r_a(); write_x(); write_separator(); r = a << x; display_title("After r = a << x"); write_r_a(); write_x(); write_separator(); cout << endl << endl; display_title("Before r = x << a"); write_r_a(); write_x(); write_separator(); r = x << a; display_title("After r = x << a"); write_r_a(); write_x(); write_separator(); wait_for_key(); } void shift_right() { display_title("r = a >> x and r = x >> a"); cout << "x: "; cin >> x; write_separator(); display_title("Before r = a >> x"); write_r_a(); write_x(); write_separator(); r = a >> x; display_title("After r = a >> x"); write_r_a(); write_x(); write_separator(); cout << endl << endl; display_title("Before r = x >> a"); write_r_a(); write_x(); write_separator(); r = x >> a; display_title("After r = x >> a"); write_r_a(); write_x(); write_separator(); wait_for_key(); } void shift_left_equals() { display_title("a <<= x and x <<= a"); cout << "x: "; cin >> x; cout << endl << endl; display_title("Before a <<= x"); write_a(); write_x(); write_separator(); a <<= x; display_title("After a <<= x"); write_a(); write_x(); write_separator(); cout << endl << endl; display_title("Before x <<= a"); write_a(); write_x(); write_separator(); x <<= a; display_title("After x <<= a"); write_a(); write_x(); write_separator(); wait_for_key(); } void shift_right_equals() { display_title("a >>= x and x >>= a"); x = enter_x(); display_title("Before a >>= x"); write_a(); write_x(); write_separator(); a >>= x; display_title("After a >>= x"); write_a(); write_x(); write_separator(); cout << endl << endl; display_title("Before x >>= a"); write_a(); write_x(); write_separator(); x >>= a; display_title("After x >>= a"); write_a(); write_x(); write_separator(); wait_for_key(); } void init_test_funcs() { test_funcs[alter_a_start] = alter_a; test_funcs[alter_b_start] = alter_b; test_funcs[increment_decrement_start + 0] = pre_inc_a_test; test_funcs[increment_decrement_start + 1] = post_inc_a_test; test_funcs[increment_decrement_start + 2] = pre_dec_a_test; test_funcs[increment_decrement_start + 3] = post_dec_a_test; test_funcs[increment_decrement_start + 4] = alter_a; test_funcs[bitwise_start + 0] = a_and_b_test; test_funcs[bitwise_start + 1] = a_xor_b_test; test_funcs[bitwise_start + 2] = a_or_b_test; test_funcs[bitwise_start + 3] = a_and_equals_b_test; test_funcs[bitwise_start + 4] = a_xor_equals_b_test; test_funcs[bitwise_start + 5] = a_or_equals_b_test; test_funcs[bitwise_start + 6] = alter_a; test_funcs[bitwise_start + 7] = alter_b; test_funcs[arithmetic_start + 0] = a_plus_b_test; test_funcs[arithmetic_start + 1] = a_minus_b_test; test_funcs[arithmetic_start + 2] = a_times_b_test; test_funcs[arithmetic_start + 3] = a_div_b_test; test_funcs[arithmetic_start + 4] = a_mod_b_test; test_funcs[arithmetic_start + 5] = a_plus_equals_b_test; test_funcs[arithmetic_start + 6] = a_minus_equals_b_test; test_funcs[arithmetic_start + 7] = a_times_equals_b_test; test_funcs[arithmetic_start + 8] = a_div_equals_b_test; test_funcs[arithmetic_start + 9] = a_mod_equals_b_test; test_funcs[arithmetic_start + 10] = alter_a; test_funcs[arithmetic_start + 11] = alter_b; test_funcs[comparison_start + 0] = a_equals_b_test; test_funcs[comparison_start + 1] = a_less_than_b_test; test_funcs[comparison_start + 2] = a_greater_than_b_test; test_funcs[comparison_start + 3] = a_less_than_or_equal_to_b_test; test_funcs[comparison_start + 4] = a_greater_than_or_equal_to_b_test; test_funcs[comparison_start + 5] = a_not_equal_to_b_test; test_funcs[comparison_start + 6] = alter_a; test_funcs[comparison_start + 7] = alter_b; test_funcs[conversion_start] = conversion_op_test; test_funcs[copy_constructor_start] = copy_constructor_test; test_funcs[stl_map_start] = stl_map_test; test_funcs[write_start] = alter_filename; test_funcs[write_start + 1] = file_write_test; test_funcs[read_start] = alter_filename; test_funcs[read_start + 1] = file_read_test; test_funcs[assignment_start + 0] = a_assign_b; test_funcs[assignment_start + 1] = a_assign_x; test_funcs[assignment_start + 2] = x_assign_a; test_funcs[assignment_start + 3] = alter_a; test_funcs[assignment_start + 4] = alter_b; test_funcs[shift_start + 0] = shift_left; test_funcs[shift_start + 1] = shift_right; test_funcs[shift_start + 2] = shift_left_equals; test_funcs[shift_start + 3] = shift_right_equals; test_funcs[shift_start + 4] = alter_a; } int main(int argc, char* argv[]) { int in; init_menus(); init_test_funcs(); char menu_id = 'z'; do { try { in = display_menu(menu_id); do_test(in); } catch (const exception& e) { cout << "ERROR: " << e.what() << endl; } } while (in != -1 || menu_id != 'z'); return 0; } //---------------------------------------------------------------------------