Added TEST_UNSAFE to avoid exception checking in normal tests, as well as an assert_inequal() evaluation in TestableAssertions.h.

release/4.3a0
Alex Cunningham 2010-08-30 20:54:12 +00:00
parent f594ebf562
commit 815d892806
5 changed files with 54 additions and 19 deletions

View File

@ -12,8 +12,8 @@ Test::Test (const SimpleString& testName)
TestRegistry::addTest (this);
}
Test::Test (const SimpleString& testName, const SimpleString& filename, long lineNumber)
: name_(testName), filename_(filename), lineNumber_(lineNumber)
Test::Test (const SimpleString& testName, const SimpleString& filename, long lineNumber, bool safeCheck = true)
: name_(testName), filename_(filename), lineNumber_(lineNumber), safeCheck_(safeCheck)
{
TestRegistry::addTest (this);
}

View File

@ -22,7 +22,7 @@ class Test
{
public:
Test (const SimpleString& testName);
Test (const SimpleString& testName, const SimpleString& filename, long lineNumber);
Test (const SimpleString& testName, const SimpleString& filename, long lineNumber, bool safeCheck);
virtual ~Test() {};
virtual void run (TestResult& result) = 0;
@ -33,6 +33,7 @@ public:
SimpleString getName() const {return name_;}
SimpleString getFilename() const {return filename_;}
long getLineNumber() const {return lineNumber_;}
bool safe() const {return safeCheck_;}
protected:
@ -43,13 +44,27 @@ protected:
Test *next_;
SimpleString filename_;
long lineNumber_; /// This is the line line number of the test, rather than the a single check
bool safeCheck_;
};
/**
* Normal test will wrap execution in a try/catch block to catch exceptions more effectively
*/
#define TEST(testName, testGroup)\
class testGroup##testName##Test : public Test \
{ public: testGroup##testName##Test () : Test (#testName "Test", __FILE__, __LINE__) {} \
{ public: testGroup##testName##Test () : Test (#testName "Test", __FILE__, __LINE__, true) {} \
void run (TestResult& result_);} \
testGroup##testName##Instance; \
void testGroup##testName##Test::run (TestResult& result_)
/**
* For debugging only: use TEST_UNSAFE to allow debuggers to have access to exceptions, as this
* will not wrap execution with a try/catch block
*/
#define TEST_UNSAFE(testName, testGroup)\
class testGroup##testName##Test : public Test \
{ public: testGroup##testName##Test () : Test (#testName "Test", __FILE__, __LINE__, false) {} \
void run (TestResult& result_);} \
testGroup##testName##Instance; \
void testGroup##testName##Test::run (TestResult& result_)

View File

@ -45,6 +45,7 @@ int TestRegistry::run (TestResult& result)
result.testsStarted ();
for (Test *test = tests; test != 0; test = test->getNext ()) {
if (test->safe()) {
try {
test->run (result);
} catch (std::exception& e) {
@ -59,6 +60,10 @@ int TestRegistry::run (TestResult& result)
SimpleString("ExceptionThrown!")));
}
}
else {
test->run (result);
}
}
result.testsEnded ();
return result.getFailureCount();
}

View File

@ -31,4 +31,17 @@ bool assert_equal(const std::vector<V>& expected, const std::vector<V>& actual,
return true;
}
/**
* Allow for testing inequality
*/
template<class V>
bool assert_inequal(const V& expected, const V& actual, double tol = 1e-9) {
if (!actual.equals(expected, tol))
return true;
printf("Erroneously equal:\n");
expected.print("expected");
actual.print("actual");
return false;
}
} // \namespace gtsam

View File

@ -7,6 +7,8 @@
#include <gtsam/CppUnitLite/TestHarness.h>
#include <stdexcept>
#include <gtsam/base/TestableAssertions.h>
#define GTSAM_MAGIC_KEY
#include <gtsam/geometry/Pose2.h>
@ -410,9 +412,9 @@ TEST( TupleConfig, equals )
EXPECT(assert_equal(config1,config2));
EXPECT(assert_equal(config1,config1));
EXPECT(!config1.equals(config3));
EXPECT(!config1.equals(config4));
EXPECT(!config1.equals(config5));
EXPECT(assert_inequal(config1,config3));
EXPECT(assert_inequal(config1,config4));
EXPECT(assert_inequal(config1,config5));
EXPECT(assert_equal(config1, config6));
}