From 815d8928061994e5542ba639854b7e6206c56df6 Mon Sep 17 00:00:00 2001 From: Alex Cunningham Date: Mon, 30 Aug 2010 20:54:12 +0000 Subject: [PATCH] Added TEST_UNSAFE to avoid exception checking in normal tests, as well as an assert_inequal() evaluation in TestableAssertions.h. --- CppUnitLite/Test.cpp | 4 ++-- CppUnitLite/Test.h | 21 ++++++++++++++++++--- CppUnitLite/TestRegistry.cpp | 27 ++++++++++++++++----------- base/TestableAssertions.h | 13 +++++++++++++ tests/testTupleConfig.cpp | 8 +++++--- 5 files changed, 54 insertions(+), 19 deletions(-) diff --git a/CppUnitLite/Test.cpp b/CppUnitLite/Test.cpp index 79d830039..3b59c21b7 100644 --- a/CppUnitLite/Test.cpp +++ b/CppUnitLite/Test.cpp @@ -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); } diff --git a/CppUnitLite/Test.h b/CppUnitLite/Test.h index aac9c8ed9..795bfd18f 100644 --- a/CppUnitLite/Test.h +++ b/CppUnitLite/Test.h @@ -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,17 +44,31 @@ 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_) + /* * Convention for tests: * - "EXPECT" is a test that will not end execution of the series of tests diff --git a/CppUnitLite/TestRegistry.cpp b/CppUnitLite/TestRegistry.cpp index 42cacfe66..47a7e5736 100644 --- a/CppUnitLite/TestRegistry.cpp +++ b/CppUnitLite/TestRegistry.cpp @@ -45,18 +45,23 @@ int TestRegistry::run (TestResult& result) result.testsStarted (); for (Test *test = tests; test != 0; test = test->getNext ()) { - try { + if (test->safe()) { + try { + test->run (result); + } catch (std::exception& e) { + // catch standard exceptions and derivatives + result.addFailure( + Failure(test->getName(), test->getFilename(), test->getLineNumber(), + SimpleString("Exception: ") + SimpleString(e.what()))); + } catch (...) { + // catch all other exceptions + result.addFailure( + Failure(test->getName(), test->getFilename(), test->getLineNumber(), + SimpleString("ExceptionThrown!"))); + } + } + else { test->run (result); - } catch (std::exception& e) { - // catch standard exceptions and derivatives - result.addFailure( - Failure(test->getName(), test->getFilename(), test->getLineNumber(), - SimpleString("Exception: ") + SimpleString(e.what()))); - } catch (...) { - // catch all other exceptions - result.addFailure( - Failure(test->getName(), test->getFilename(), test->getLineNumber(), - SimpleString("ExceptionThrown!"))); } } result.testsEnded (); diff --git a/base/TestableAssertions.h b/base/TestableAssertions.h index f3eef10cd..7704161be 100644 --- a/base/TestableAssertions.h +++ b/base/TestableAssertions.h @@ -31,4 +31,17 @@ bool assert_equal(const std::vector& expected, const std::vector& actual, return true; } +/** + * Allow for testing inequality + */ +template +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 diff --git a/tests/testTupleConfig.cpp b/tests/testTupleConfig.cpp index ebbeb9e26..599c2484f 100644 --- a/tests/testTupleConfig.cpp +++ b/tests/testTupleConfig.cpp @@ -7,6 +7,8 @@ #include #include +#include + #define GTSAM_MAGIC_KEY #include @@ -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)); }