From 29bc9a09929477a0863163de6c9809c1e6ee957c Mon Sep 17 00:00:00 2001 From: Louis RUBET Date: Fri, 5 Sep 2014 14:39:24 +0200 Subject: [PATCH] Change rpn-general-core.h to rpn-test-core.h --- src/rpn-test-core.h | 137 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/rpn-test-core.h diff --git a/src/rpn-test-core.h b/src/rpn-test-core.h new file mode 100644 index 0000000..846817e --- /dev/null +++ b/src/rpn-test-core.h @@ -0,0 +1,137 @@ +void program::test() +{ + const string test_filename = "test.txt"; + const string stack_size("-> stack size should be "); + const string stack_value("-> stack should be "); + const string cmd_error("-> error should be "); + const string cmd_exit("exit test"); + ifstream test_file(test_filename.c_str()); + + if (test_file.is_open()) + { + string test_title; + string entry; + ret_value ret; + stack stk; + heap hp; + bool indicate_passed = false; + bool failed = false; + int count_tests = 0; + int last_err; + stringstream cerr_buffer; + streambuf* cerr_old_buffer; + + // redirect cerr + cerr_old_buffer = cerr.rdbuf(cerr_buffer.rdbuf()); + + while (!test_file.eof()) + { + getline(test_file, entry); + if (entry.substr(0,2)=="##") + { + // read a test section + if (indicate_passed) + { + if (g_verbose > 0) + cout << "\t"; + cout << "passed " << count_tests << " tests"<< endl; + } + cout << entry; + if (g_verbose == 0) + cout << " .. "; + else + cout << endl; + indicate_passed = true; + count_tests = 0; + } + else if (entry.substr(0,1)=="#") + { + // read a test title + test_title = entry; + if (g_verbose > 0) + cout << "\t" << test_title << endl; + count_tests++; + } + else if (entry.find(stack_size, 0) == 0) + { + // check current stack size + istringstream isub; + int size; + + isub.str(entry.substr(stack_size.size())); + isub>>size; + if (size != (int)stk.size()) + { + cout<= 0; i--) + { + stringstream os; + if (i < (int)(stk.size() - 1)) + stack_is += ", "; + ((object*)stk.seq_obj(i))->show(os); + getline(os, tmp); + stack_is += tmp; + } + if (stack_is != stack_should_be) + { + cout<>err_should_be; + if (err_should_be != last_err) + { + cout< 0) + { + // parse entry and run line + program prog; + ret = program::parse(entry, prog); + if (ret == ret_ok) + { + // run it + (void)prog.run(stk, hp); + last_err = (int)prog.get_err(); + } + } + } + if (indicate_passed) + cout << "passed " << count_tests << " tests"<< endl; + if (! failed) + cout << "test file '"<