9

I'm using boost.test for unit testing (currently using boost 1.71.0). I have a template class I would like to test with a dataset.

I could only find ways to test on different template parameters (using BOOST_AUTO_TEST_CASE_TEMPLATE) or on a dataset (using BOOST_DATA_TEST_CASE). But i couldn't find a way to mix both template and dataset.

Here is a MWE of what I achieved to do, and commented what I'm trying to do.

test.cpp:

#define BOOST_TEST_MODULE data_template
#include <boost/mpl/list.hpp>
#include <boost/test/data/test_case.hpp>
#include <boost/test/unit_test.hpp>
#include <vector>
#include <list>
#include <numeric> // std::accumulate

template <typename C>
struct Testee{
    C container;
    typename C::value_type f(){
        // just a dummy function to be tested
        return std::accumulate(container.begin(),container.end(),0);
    }
};

typedef boost::mpl::list<std::vector<double>, std::list<double>, std::vector<size_t>> container_types;

template <class T> void test_containers(size_t size)
{
    Testee<T> t;
    t.container = T(size,1);

    BOOST_CHECK_EQUAL(t.f(), size);
}

// test on all container types but only with size=100 (3 test cases)
BOOST_AUTO_TEST_CASE_TEMPLATE(test1, T, container_types)
{
    test_containers<T>(100);
}

// test with several container sizes but only for vector<double> (6 test cases)
BOOST_DATA_TEST_CASE(test2, boost::unit_test::data::make({0, 1, 2, 10, 100, 1000}), size)
{
    test_containers<std::vector<double>>(size);
}

// What I'm looking for looks like:
// // test for each combination of container type and size (6*3=18 test cases)
// BOOST_DATA_TEST_CASE_TEMPLATE(test3, T, container_types, boost::unit_test::data::make({0, 1, 2, 10, 100, 1000}), size)
// {
//     test_containers<T>(size);
// }

CMakeLists.txt:

CMAKE_MINIMUM_REQUIRED( VERSION 3.5 )
PROJECT(data_template_test
    LANGUAGES CXX)

set (CMAKE_CXX_FLAGS "-std=c++17 -Wall ${CMAKE_CXX_FLAGS}")

find_package(Boost 1.59 REQUIRED COMPONENTS unit_test_framework)
add_executable(${PROJECT_NAME} test.cpp)
target_link_libraries(${PROJECT_NAME} Boost::unit_test_framework)
target_compile_definitions(${PROJECT_NAME} PRIVATE BOOST_TEST_DYN_LINK)

The MWE produces 9 test cases that pass without errors, but I want the combination of all types with all sizes, so 3x6=18 test cases. Is there any way to do that using boost-test? Maybe using some more verbose / less friendly API?

Thanks for helping!

1
  • Hi, did you ever find a solution to testing this? Though I don't know how this would be implemented, as each template might require different data. I'm thinking about a fixture that is specialized with data.
    – namezero
    Jan 13, 2020 at 19:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.