ephysics/test/TestSuite.cpp

145 lines
3.9 KiB
C++
Raw Normal View History

2017-05-11 00:40:27 +00:00
/********************************************************************************
2017-06-16 22:34:37 +02:00
* ReactPhysics3D physics library, http://www.ephysics.com *
2017-06-08 22:35:22 +02:00
* Copyright (c) 2010-2016 Daniel Chappuis *
2017-05-11 00:40:27 +00:00
*********************************************************************************
2017-06-08 22:35:22 +02:00
* *
2017-05-11 00:40:27 +00:00
* This software is provided 'as-is', without any express or implied warranty. *
* In no event will the authors be held liable for any damages arising from the *
2017-06-08 22:35:22 +02:00
* use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
2017-05-11 00:40:27 +00:00
* 1. The origin of this software must not be misrepresented; you must not claim *
2017-06-08 22:35:22 +02:00
* that you wrote the original software. If you use this software in a *
* product, an acknowledgment in the product documentation would be *
* appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source distribution. *
* *
2017-05-11 00:40:27 +00:00
********************************************************************************/
// Librairies
2017-06-16 22:34:37 +02:00
#include <test/TestSuite.hpp>
2017-05-11 00:40:27 +00:00
2017-06-16 22:34:37 +02:00
using namespace ephysics;
2017-05-11 00:40:27 +00:00
// Constructor
TestSuite::TestSuite(const std::string& name, std::ostream* outputStream)
2017-06-12 23:35:50 +02:00
: m_name(name), mOutputStream(outputStream) {
2017-05-11 00:40:27 +00:00
}
// Return the number of passed tests
long TestSuite::getNbPassedTests() const {
2017-06-08 22:35:22 +02:00
long nbPassedTests = 0;
2017-05-11 00:40:27 +00:00
2017-06-08 22:35:22 +02:00
for (size_t i=0; i<mTests.size(); i++) {
assert(mTests[i]);
nbPassedTests += mTests[i]->getNbPassedTests();
}
2017-05-11 00:40:27 +00:00
2017-06-08 22:35:22 +02:00
return nbPassedTests;
2017-05-11 00:40:27 +00:00
}
// Return the number of failed tests
long TestSuite::getNbFailedTests() const {
2017-06-08 22:35:22 +02:00
long nbFailedTests = 0;
2017-05-11 00:40:27 +00:00
2017-06-08 22:35:22 +02:00
for (size_t i=0; i<mTests.size(); i++) {
assert(mTests[i]);
nbFailedTests += mTests[i]->getNbFailedTests();
}
2017-05-11 00:40:27 +00:00
2017-06-08 22:35:22 +02:00
return nbFailedTests;
2017-05-11 00:40:27 +00:00
}
// Add a unit test in the test suite
void TestSuite::addTest(Test* test) {
2017-06-08 22:35:22 +02:00
if (test == NULL) {
throw std::invalid_argument("Error : You cannot add a NULL test in the test suite.");
}
else if (mOutputStream != NULL && test->getOutputStream() == NULL) {
test->setOutputStream(mOutputStream);
}
// Add the test to the suite
mTests.push_back(test);
// Reset the added test
test->reset();
2017-05-11 00:40:27 +00:00
}
// Add a test suite to the current test suite
void TestSuite::addTestSuite(const TestSuite& testSuite) {
2017-06-08 22:35:22 +02:00
// Add each test of the test suite to the current one
for (size_t i =0; i < testSuite.mTests.size(); i++) {
assert(testSuite.mTests[i] != NULL);
addTest(testSuite.mTests[i]);
}
2017-05-11 00:40:27 +00:00
}
// Launch the tests of the test suite
void TestSuite::run() {
2017-06-08 22:35:22 +02:00
// Reset all the tests
reset();
2017-05-11 00:40:27 +00:00
2017-06-08 22:35:22 +02:00
// Run all the tests
for (size_t i=0; i < mTests.size(); i++) {
assert(mTests[i] != NULL);
mTests[i]->run();
}
2017-05-11 00:40:27 +00:00
}
// Reset the test suite
void TestSuite::reset() {
2017-06-08 22:35:22 +02:00
for(size_t i=0; i < mTests.size(); ++i) {
assert(mTests[i]);
mTests[i]->reset();
}
2017-05-11 00:40:27 +00:00
}
// Display the tests report and return the number of failed tests
long TestSuite::report() const {
2017-06-08 22:35:22 +02:00
if (mOutputStream != NULL) {
long nbFailedTests = 0;
2017-06-12 23:35:50 +02:00
*mOutputStream << "Test Suite \"" << m_name << "\"\n";
2017-06-08 22:35:22 +02:00
size_t i;
for (i=0; i < 70; i++) {
*mOutputStream << "=";
}
*mOutputStream << "=" << std::endl;
for (i=0; i < mTests.size(); i++) {
assert(mTests[i] != NULL);
nbFailedTests += mTests[i]->report();
}
for (i=0; i < 70; i++) {
*mOutputStream << "=";
}
*mOutputStream << "=" << std::endl;
// Return the number of failed tests
return nbFailedTests;
}
else {
return getNbFailedTests();
}
2017-05-11 00:40:27 +00:00
}
// Delete all the tests
void TestSuite::clear() {
2017-06-08 22:35:22 +02:00
for (size_t i=0; i<mTests.size(); i++) {
delete mTests[i];
mTests[i] = NULL;
}
2017-05-11 00:40:27 +00:00
}