What is a good way to unit test templated c++ methods/classes/functions?
c++, templates, unit-testing
Solution
Boost test has a macro BOOST_AUTO_TEST_CASE_TEMPLATE that runs a test on a template for each type in a boost::mpl::list.
template<typename T>
T Add(T lh, T rh)
{
return lh + rh;
}
typedef boost::mpl::list<int, float> test_types;
BOOST_AUTO_TEST_CASE_TEMPLATE(Add_1Plus2_Is3, T, test_types)
{
T result = Add(T(1), T(2));
BOOST_CHECK_EQUAL(T(3), result);
}
http://www.boost.org/doc/libs/1_48_0/libs/test/doc/html/utf/user-guide/test-organization/auto-test-case-template.html
Problem
I'm looking for a good way to unit test my templated methods, classes, and functions in c++. I have a feeling that it is not enough to just test using with a single type, and it doesn't feel right to just duplicate the unit tests and replace the types. To be more specific, I'm working on a Matrix class and using Boost's unit testing framework. The Matrix is to work with different types. It will mainly be used for basic types, but I would like it to support concrete types too. Note, I doing this for learning purposes, which is why I'm not using a existing matrix implementation.