• Advertisement
Sign in to follow this  
  • entries
    195
  • comments
    198
  • views
    104077

testing again

Sign in to follow this  

97 views


// *
// * aoi/test/test.impl
// *
// * Implementation for testing functionality
// *

#include
#include
#include
#include
#include
#include
#include

namespace aoi {
namespace test {
namespace {
class TestRunner
: public thread::ClassLevelLockable
{
public:
std::vector module_tests_;
} * volatile the_test_runner = 0;

void __cdecl delete_test_runner(void) {
utility::CreateStatic::Destroy(the_test_runner);
}
}

void add_module_test(std::auto_ptr tc) {
if (!test_build) return;
TestRunner::Lock l;
if (!the_test_runner) {
the_test_runner = utility::CreateStatic::Create();
atexit(delete_test_runner);
}
ModuleTest * test = tc.release();
the_test_runner->module_tests_.push_back(test);
}

bool run_tests(TestFlags flags) {
if (!test_build) return true; // no tests so pass by default
TestRunner::Lock l;
if (!the_test_runner) return true; // no tests so pass by default
bool pass = true;
for (size_t i = 0; i < the_test_runner->module_tests_.size(); i++) {
ModuleTest * p = the_test_runner->module_tests_;
for (size_t j = 0; j < p->tests_.size(); j++) {
time_t start = time(0);
std::cout << p->module_name_ << "::" << p->tests_[j].get<0>() << std::endl;
std::stringstream cache_stream;
std::ostream dummy_stream(0);
std::ostream * output_stream = (flags == verbose ? &cache_stream : &dummy_stream);
bool result = true;
p->set_up(*output_stream);
try {
result = p->tests_[j].get<1>()(*output_stream);
} catch (::aoi::test::TestException & te) {
*output_stream << "assert(" << te.exp_ << ")\nFile: "
<< te.file_
<< "\nLine: " << te.line_
<< std::endl;
result = false;
} catch (std::exception & e) {
*output_stream << "exception(" << e.what() << ") thrown." << std::endl;
result = false;
} catch (...) {
*output_stream << "Unknown exception thrown." << std::endl;
result = false;
}
p->tear_down(*output_stream);
if (flags == verbose &&
!(cache_stream.str().empty())) std::cout << cache_stream.rdbuf();
if (result == p->tests_[j].get<2>()) {
std::cout << "Passed" << std::endl;
} else {
std::cout << "Failed" << std::endl;
pass = false;
}
time_t end = time(0);
std::cout << (end - start) << std::endl << std::endl;
}
}
return pass;
}

ModuleTest::ModuleTest(const std::string & module_name)
: module_name_(module_name) {}

ModuleTest::~ModuleTest() {}

void ModuleTest::set_up(std::ostream &) {}

void ModuleTest::tear_down(std::ostream &) {}

TestException::TestException(const char * exp, const char * file, int line)
: exp_(exp), file_(file), line_(line) {
}
TestException::TestException(const TestException & other)
: exp_(other.exp_), file_(other.file_), line_(other.line_) {
}
TestException::~TestException() {
}

}
}
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement