git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@45 92aac97c-a6ce-11dd-a772-7fcde58d38e6
This commit is contained in:
parent
2776f5fbd8
commit
7fff25843e
91
sources/reactphysics3d/testing/TestSuite/Suite.cpp
Normal file
91
sources/reactphysics3d/testing/TestSuite/Suite.cpp
Normal file
|
@ -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 <iostream>
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstddef>
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
} ///:~
|
46
sources/reactphysics3d/testing/TestSuite/Suite.h
Normal file
46
sources/reactphysics3d/testing/TestSuite/Suite.h
Normal file
|
@ -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 <vector>
|
||||||
|
#include <stdexcept>
|
||||||
|
#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<Test*> 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 ///:~
|
38
sources/reactphysics3d/testing/TestSuite/Test.cpp
Normal file
38
sources/reactphysics3d/testing/TestSuite/Test.cpp
Normal file
|
@ -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 <iostream>
|
||||||
|
#include <typeinfo>
|
||||||
|
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;
|
||||||
|
} ///:~
|
55
sources/reactphysics3d/testing/TestSuite/Test.h
Normal file
55
sources/reactphysics3d/testing/TestSuite/Test.h
Normal file
|
@ -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 <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <cassert>
|
||||||
|
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 ///:~
|
Loading…
Reference in New Issue
Block a user