TestRegistry.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* ----------------------------------------------------------------------------
  2. * GTSAM Copyright 2010, Georgia Tech Research Corporation,
  3. * Atlanta, Georgia 30332-0415
  4. * All Rights Reserved
  5. * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
  6. * See LICENSE for the license information
  7. * -------------------------------------------------------------------------- */
  8. #include <exception>
  9. #include "Test.h"
  10. #include "Failure.h"
  11. #include "TestResult.h"
  12. #include "TestRegistry.h"
  13. void TestRegistry::addTest (Test *test)
  14. {
  15. instance ().add (test);
  16. }
  17. int TestRegistry::runAllTests (TestResult& result)
  18. {
  19. return instance ().run (result);
  20. }
  21. TestRegistry& TestRegistry::instance ()
  22. {
  23. static TestRegistry registry;
  24. return registry;
  25. }
  26. void TestRegistry::add (Test *test)
  27. {
  28. if (tests == 0) {
  29. test->setNext(0);
  30. tests = test;
  31. lastTest = test;
  32. return;
  33. }
  34. test->setNext (0);
  35. lastTest->setNext(test);
  36. lastTest = test;
  37. }
  38. int TestRegistry::run (TestResult& result)
  39. {
  40. result.testsStarted ();
  41. for (Test *test = tests; test != 0; test = test->getNext ()) {
  42. if (test->safe()) {
  43. try {
  44. test->run (result);
  45. } catch (std::exception& e) {
  46. // catch standard exceptions and derivatives
  47. result.addFailure(
  48. Failure(test->getName(), test->getFilename(), test->getLineNumber(),
  49. std::string("Exception: ") + std::string(e.what())));
  50. } catch (...) {
  51. // catch all other exceptions
  52. result.addFailure(
  53. Failure(test->getName(), test->getFilename(), test->getLineNumber(),
  54. "ExceptionThrown!"));
  55. }
  56. }
  57. else {
  58. test->run (result);
  59. }
  60. }
  61. result.testsEnded ();
  62. return result.getFailureCount();
  63. }