diff --git a/sources/reactphysics3d/testing/TestSuite/Suite.cpp b/sources/reactphysics3d/testing/TestSuite/Suite.cpp new file mode 100644 index 00000000..6e23b097 --- /dev/null +++ b/sources/reactphysics3d/testing/TestSuite/Suite.cpp @@ -0,0 +1,91 @@ +//: TestSuite:Suite.cpp {O} +// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. +// (c) 1995-2004 MindView, Inc. All Rights Reserved. +// See source code use permissions stated in the file 'License.txt', +// distributed with the code package available at www.MindView.net. +#include "Suite.h" +#include +#include +#include +using namespace std; +using namespace TestSuite; + +void Suite::addTest(Test* t) throw(TestSuiteError) { + // Verify test is valid and has a stream: + if(t == 0) + throw TestSuiteError("Null test in Suite::addTest"); + else if(osptr && !t->getStream()) + t->setStream(osptr); + tests.push_back(t); + t->reset(); +} + +void Suite::addSuite(const Suite& s) { + for(size_t i = 0; i < s.tests.size(); ++i) { + assert(tests[i]); + addTest(s.tests[i]); + } +} + +void Suite::free() { + for(size_t i = 0; i < tests.size(); ++i) { + delete tests[i]; + tests[i] = 0; + } +} + +void Suite::run() { + reset(); + for(size_t i = 0; i < tests.size(); ++i) { + assert(tests[i]); + tests[i]->run(); + } +} + +long Suite::report() const { + if(osptr) { + long totFail = 0; + *osptr << "Suite \"" << name + << "\"\n======="; + size_t i; + for(i = 0; i < name.size(); ++i) + *osptr << '='; + *osptr << "=" << endl; + for(i = 0; i < tests.size(); ++i) { + assert(tests[i]); + totFail += tests[i]->report(); + } + *osptr << "======="; + for(i = 0; i < name.size(); ++i) + *osptr << '='; + *osptr << "=" << endl; + return totFail; + } + else + return getNumFailed(); +} + +long Suite::getNumPassed() const { + long totPass = 0; + for(size_t i = 0; i < tests.size(); ++i) { + assert(tests[i]); + totPass += tests[i]->getNumPassed(); + } + return totPass; +} + +long Suite::getNumFailed() const { + long totFail = 0; + for(size_t i = 0; i < tests.size(); ++i) { + assert(tests[i]); + totFail += tests[i]->getNumFailed(); + } + return totFail; +} + +void Suite::reset() { + for(size_t i = 0; i < tests.size(); ++i) { + assert(tests[i]); + tests[i]->reset(); + } +} ///:~ diff --git a/sources/reactphysics3d/testing/TestSuite/Suite.h b/sources/reactphysics3d/testing/TestSuite/Suite.h new file mode 100644 index 00000000..e7329213 --- /dev/null +++ b/sources/reactphysics3d/testing/TestSuite/Suite.h @@ -0,0 +1,46 @@ +//: TestSuite:Suite.h +// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. +// (c) 1995-2004 MindView, Inc. All Rights Reserved. +// See source code use permissions stated in the file 'License.txt', +// distributed with the code package available at www.MindView.net. +#ifndef SUITE_H +#define SUITE_H +#include +#include +#include "../TestSuite/Test.h" +using std::vector; +using std::logic_error; + +namespace TestSuite { + +class TestSuiteError : public logic_error { +public: + TestSuiteError(const string& s = "") + : logic_error(s) {} +}; + +class Suite { + string name; + ostream* osptr; + vector tests; + void reset(); + // Disallowed ops: + Suite(const Suite&); + Suite& operator=(const Suite&); +public: + Suite(const string& name, ostream* osptr = &cout) + : name(name) { this->osptr = osptr; } + string getName() const { return name; } + long getNumPassed() const; + long getNumFailed() const; + const ostream* getStream() const { return osptr; } + void setStream(ostream* osptr) { this->osptr = osptr; } + void addTest(Test* t) throw(TestSuiteError); + void addSuite(const Suite&); + void run(); // Calls Test::run() repeatedly + long report() const; + void free(); // Deletes tests +}; + +} // namespace TestSuite +#endif // SUITE_H ///:~ diff --git a/sources/reactphysics3d/testing/TestSuite/Test.cpp b/sources/reactphysics3d/testing/TestSuite/Test.cpp new file mode 100644 index 00000000..8ad4ac4f --- /dev/null +++ b/sources/reactphysics3d/testing/TestSuite/Test.cpp @@ -0,0 +1,38 @@ +//: TestSuite:Test.cpp {O} +// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. +// (c) 1995-2004 MindView, Inc. All Rights Reserved. +// See source code use permissions stated in the file 'License.txt', +// distributed with the code package available at www.MindView.net. +#include "Test.h" +#include +#include +using namespace std; +using namespace TestSuite; + +void Test::do_test(bool cond, const std::string& lbl, + const char* fname, long lineno) { + if(!cond) + do_fail(lbl, fname, lineno); + else + succeed_(); +} + +void Test::do_fail(const std::string& lbl, + const char* fname, long lineno) { + ++nFail; + if(osptr) { + *osptr << typeid(*this).name() + << "failure: (" << lbl << ") , " << fname + << " (line " << lineno << ")" << endl; + } +} + +long Test::report() const { + if(osptr) { + *osptr << "Test \"" << typeid(*this).name() + << "\":\n\tPassed: " << nPass + << "\tFailed: " << nFail + << endl; + } + return nFail; +} ///:~ diff --git a/sources/reactphysics3d/testing/TestSuite/Test.h b/sources/reactphysics3d/testing/TestSuite/Test.h new file mode 100644 index 00000000..fb85c63a --- /dev/null +++ b/sources/reactphysics3d/testing/TestSuite/Test.h @@ -0,0 +1,55 @@ +//: TestSuite:Test.h +// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. +// (c) 1995-2004 MindView, Inc. All Rights Reserved. +// See source code use permissions stated in the file 'License.txt', +// distributed with the code package available at www.MindView.net. +#ifndef TEST_H +#define TEST_H +#include +#include +#include +using std::string; +using std::ostream; +using std::cout; + +// fail_() has an underscore to prevent collision with +// ios::fail(). For consistency, test_() and succeed_() +// also have underscores. + +#define test_(cond) \ + do_test(cond, #cond, __FILE__, __LINE__) +#define fail_(str) \ + do_fail(str, __FILE__, __LINE__) + +namespace TestSuite { + +class Test { + ostream* osptr; + long nPass; + long nFail; + // Disallowed: + Test(const Test&); + Test& operator=(const Test&); +protected: + void do_test(bool cond, const string& lbl, + const char* fname, long lineno); + void do_fail(const string& lbl, + const char* fname, long lineno); +public: + Test(ostream* osptr = &cout) { + this->osptr = osptr; + nPass = nFail = 0; + } + virtual ~Test() {} + virtual void run() = 0; + long getNumPassed() const { return nPass; } + long getNumFailed() const { return nFail; } + const ostream* getStream() const { return osptr; } + void setStream(ostream* osptr) { this->osptr = osptr; } + void succeed_() { ++nPass; } + long report() const; + virtual void reset() { nPass = nFail = 0; } +}; + +} // namespace TestSuite +#endif // TEST_H ///:~