test.hpp 688 B

1234567891011121314151617181920212223242526272829303132
  1. #include <functional>
  2. #include <iostream>
  3. using namespace std;
  4. class TestResult {
  5. public:
  6. bool success;
  7. string err_reason;
  8. TestResult(bool success, string err_reason = "") : success(success), err_reason(err_reason) {}
  9. };
  10. class Test {
  11. public:
  12. string name;
  13. function<TestResult *(void)> f;
  14. Test(string name, std::function<TestResult *(void)> testFunc) : name(name), f(testFunc) {}
  15. TestResult *run() {
  16. cout << endl << "*** Running " << name << " test" << endl;
  17. TestResult *res = this->f();
  18. if (res->success) {
  19. cout << "*** Finished " << name << " test" << endl;
  20. } else {
  21. cerr << name << " test failed. Reason: " << res->err_reason << endl;
  22. }
  23. return res;
  24. }
  25. };