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