- Author(s) of this documentation:
- David Coeurjolly
Introduction
Catch Is a C++ framework to help unit test design. More precisely, thanks to couple of C++ macros, Catch enabled unit test are easy to write and easy to review.
In this documentation page, we just describe how to write such test files in DGtal. For a complete documentation of Catch, please refer to Catch.
Catch in DGtal
We illustrate here basic Catch usages using some tests on the PointVector class.
First of all, all test files must include the following header:
Once this header included, there is no need to create a main() function in your cpp file, everything is handled automatically.
Unit tests are decomposed into TEST_CASE and SECTION in a test case. For example, the following code performs some tests on the PointVector class (see exampleCatch.cpp for details).
{
typedef PointVector<4, DGtal::int32_t>
Point;
double t3[]= {1.0,-1.0,2.0,-2.0};
{
}
SECTION(
"Min/Max of vector components")
{
REQUIRE( (*p3.maxElement() == 2.0) );
REQUIRE( (*p3.minElement() == -2.0) );
}
{
}
}
boost::int32_t int32_t
signed 32-bit integer.
TEST_CASE("int container traits", "[int][traits]")
SECTION("Testing constant forward iterators")
REQUIRE(domain.isInside(aPoint))
PointVector< 3, double > RealPoint
When compiling this simple test, the executable handles several commandline options. For example
runs all test cases and sections.
runs all tests with detailed messages
runs all tests with timings for each section.
displays all command-line options.
Please have a look to Catch for further macros and comments. For instance, tags can be associated with sections and test cases allowing to only run specific tests.
Advanced usages
In Catch, experimental macros exist to describe templated tests. For instance, the following code tests some STL containers.
std::list<int>, std::vector<int>, std::set<int>)
{
TestType defaultConstructed;
SECTION(
"Size of the default constructed container")
{
REQUIRE( ( defaultConstructed.size() == 0 ) );
}
defaultConstructed.insert( defaultConstructed.begin(), 5 );
{
REQUIRE( ( defaultConstructed.size() == 1 ) );
}
}
TEMPLATE_TEST_CASE("STL Container test", "Description", std::list< int >, std::vector< int >, std::set< int >)
[exampleCatch-example1]
In this example, 6 test sections are automatically generated. The resulting output (with timings) is:
./exampleCatch "STL Container test" -d yes
Size of the
default constructed container completed in 3.3e-05s
std::list<int> completed in 0.000124s
STL Container
test completed in 0.0002s
std::list<int> completed in 3.5e-05s
STL Container
test completed in 7.7e-05s
Size of the
default constructed container completed in 1.5e-05s
std::vector<int> completed in 8.4e-05s
STL Container
test completed in 0.000133s
std::vector<int> completed in 5.2e-05s
STL Container
test completed in 0.000112s
Size of the
default constructed container completed in 1e-05s
std::set<int> completed in 7.2e-05s
STL Container
test completed in 0.000134s
std::set<int> completed in 5.7e-05s
STL Container
test completed in 0.000116s
=========================================================
HalfEdgeDataStructure::Size Size
void insert(VContainer1 &c1, LContainer2 &c2, unsigned int idx, double v)
bool test(const I &itb, const I &ite)
- Note
- In the current Catch version, the number of templated types must be specified explicitly in the macro name (up to 8 templated types per test case).