2013-03-06 06:30:33 +00:00
|
|
|
/********************************************************************************
|
2015-02-15 20:56:45 +00:00
|
|
|
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
|
2016-04-11 18:15:20 +00:00
|
|
|
* Copyright (c) 2010-2016 Daniel Chappuis *
|
2013-03-06 06:30:33 +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 *
|
|
|
|
* 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: *
|
|
|
|
* *
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not claim *
|
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
|
|
#ifndef TEST_H
|
|
|
|
#define TEST_H
|
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
/// Reactphysics3D namespace
|
|
|
|
namespace reactphysics3d {
|
|
|
|
|
2013-03-09 07:34:57 +00:00
|
|
|
// Macros
|
2018-08-05 14:10:13 +00:00
|
|
|
#define rp3d_test(condition) applyTest(condition, #condition, __FILE__, __LINE__);
|
|
|
|
#define rp3d_fail(text) applyFail(text, __FILE__, __LINE__);
|
2013-03-09 07:34:57 +00:00
|
|
|
|
|
|
|
// Class Test
|
2013-03-06 06:30:33 +00:00
|
|
|
/**
|
2013-03-09 07:34:57 +00:00
|
|
|
* This abstract class represents a unit test. To create a unit test, you simply
|
|
|
|
* need to create a class that inherits from the Test class, override the run() method and
|
|
|
|
* use the test() and fail() macros.
|
2013-03-06 06:30:33 +00:00
|
|
|
*/
|
|
|
|
class Test {
|
|
|
|
|
|
|
|
private :
|
|
|
|
|
|
|
|
// ---------- Attributes ---------- //
|
|
|
|
|
2014-08-09 08:27:41 +00:00
|
|
|
/// Name of the test
|
|
|
|
std::string mName;
|
|
|
|
|
2013-03-06 06:30:33 +00:00
|
|
|
/// Number of tests that passed
|
2013-03-09 07:34:57 +00:00
|
|
|
long mNbPassedTests;
|
2013-03-06 06:30:33 +00:00
|
|
|
|
|
|
|
/// Number of tests that failed
|
2013-03-09 07:34:57 +00:00
|
|
|
long mNbFailedTests;
|
2013-03-06 06:30:33 +00:00
|
|
|
|
|
|
|
/// Output stream
|
|
|
|
std::ostream* mOutputStream;
|
|
|
|
|
|
|
|
// ---------- Methods ---------- //
|
|
|
|
|
|
|
|
/// Copy constructor is private
|
|
|
|
Test(const Test&);
|
|
|
|
|
|
|
|
/// Assignment operator is private
|
|
|
|
Test& operator=(const Test& test);
|
|
|
|
|
|
|
|
protected :
|
|
|
|
|
2013-03-09 07:34:57 +00:00
|
|
|
// ---------- Methods ---------- //
|
|
|
|
|
|
|
|
/// Called to test a boolean condition.
|
|
|
|
/// This method should not be called directly in your test but you should
|
|
|
|
/// call test() instead (macro)
|
|
|
|
void applyTest(bool condition, const std::string& testText,
|
|
|
|
const char* filename, long lineNumber);
|
|
|
|
|
|
|
|
/// Called when a test has failed.
|
|
|
|
/// This method should not be called directly in your test buy you should
|
|
|
|
/// call fail() instead (macro)
|
|
|
|
void applyFail(const std::string& testText, const char* filename, long lineNumber);
|
|
|
|
|
2013-03-06 06:30:33 +00:00
|
|
|
public :
|
|
|
|
|
|
|
|
// ---------- Methods ---------- //
|
|
|
|
|
|
|
|
/// Constructor
|
2014-08-09 08:27:41 +00:00
|
|
|
Test(const std::string& name, std::ostream* stream = &std::cout);
|
2013-03-06 06:30:33 +00:00
|
|
|
|
|
|
|
/// Destructor
|
2014-06-24 21:31:13 +00:00
|
|
|
virtual ~Test();
|
2013-03-06 06:30:33 +00:00
|
|
|
|
|
|
|
/// Return the number of passed tests
|
2013-03-09 07:34:57 +00:00
|
|
|
long getNbPassedTests() const;
|
2013-03-06 06:30:33 +00:00
|
|
|
|
|
|
|
/// Return the number of failed tests
|
2013-03-09 07:34:57 +00:00
|
|
|
long getNbFailedTests() const;
|
2013-03-06 06:30:33 +00:00
|
|
|
|
|
|
|
/// Return the output stream
|
|
|
|
const std::ostream* getOutputStream() const;
|
|
|
|
|
|
|
|
/// Set the output stream
|
2013-03-09 07:34:57 +00:00
|
|
|
void setOutputStream(std::ostream *stream);
|
2013-03-06 06:30:33 +00:00
|
|
|
|
|
|
|
/// Run the unit test
|
|
|
|
virtual void run() = 0;
|
|
|
|
|
2013-03-09 07:34:57 +00:00
|
|
|
/// Called when a test passed
|
|
|
|
void succeed();
|
|
|
|
|
2013-03-06 06:30:33 +00:00
|
|
|
/// Reset the unit test
|
|
|
|
virtual void reset();
|
|
|
|
|
|
|
|
/// Display the report of the unit test and return the number of failed tests
|
2013-03-09 07:34:57 +00:00
|
|
|
long report() const;
|
2013-03-06 06:30:33 +00:00
|
|
|
};
|
|
|
|
|
2013-03-09 07:34:57 +00:00
|
|
|
// Called when a test passed
|
|
|
|
inline void Test::succeed() {
|
|
|
|
mNbPassedTests++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the unit test
|
2013-03-06 06:30:33 +00:00
|
|
|
inline void Test::reset() {
|
|
|
|
mNbPassedTests = 0;
|
|
|
|
mNbFailedTests = 0;
|
|
|
|
}
|
|
|
|
|
2013-03-09 07:34:57 +00:00
|
|
|
// Return the number of passed tests
|
|
|
|
inline long Test::getNbPassedTests() const {
|
2013-03-06 06:30:33 +00:00
|
|
|
return mNbPassedTests;
|
|
|
|
}
|
|
|
|
|
2013-03-09 07:34:57 +00:00
|
|
|
// Return the number of failed tests
|
|
|
|
inline long Test::getNbFailedTests() const {
|
2013-03-06 06:30:33 +00:00
|
|
|
return mNbFailedTests;
|
|
|
|
}
|
|
|
|
|
2013-03-09 07:34:57 +00:00
|
|
|
// Return the output stream
|
2013-03-06 06:30:33 +00:00
|
|
|
inline const std::ostream* Test::getOutputStream() const {
|
|
|
|
return mOutputStream;
|
|
|
|
}
|
|
|
|
|
2013-03-09 07:34:57 +00:00
|
|
|
// Set the output stream
|
|
|
|
inline void Test::setOutputStream(std::ostream* stream) {
|
2013-03-06 06:30:33 +00:00
|
|
|
mOutputStream = stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|