4

When writing unit tests I often want to invoke a function with a combination of parameters. For example, I have a function which is declared as

void tester_func(int p1, double p2, std::string const& p3);

and some selected parameters

std::vector<int> vec_p1 = { 1, 2, 666 };
std::vector<double> vec_p2 = { 3.14159, 0.0001 };
std::vector<std::string> vec_p3 = { "Method_Smart", "Method_Silly" };

What I currently do is simply

for(auto const& p1 : vec_p1)
    for(auto const& p2 : vec_p2)
        for(auto const& p3 : vec_p3)
            tester_func(p1, p2, p3);

However, Sean Parent suggests to avoid explicit loops and use std:: algorithms instead. How could one follow this advice in the above mentioned case? Any idioms? What is the cleanest way to write a variadic template that does this? What is the best way without C++11 features?

levzettelin
  • 2,600
  • 19
  • 32
  • The answer may depend on which unit testing framework you're using. – John Dibling Jan 17 '14 at 13:14
  • Boost UnitTest. The `tester_func` automatically logs all error. It is only important that the function is invoked for all parameter combinations. – levzettelin Jan 17 '14 at 13:19
  • In the video you have linked to, he explicitly states that sometimes( saves on typing) using a range for loop is ok as long as the loop body is a single statement/ assignment. I think what you are doing here is alright. – indeterminately sequenced Jan 17 '14 at 14:06
  • 2
    It seems the C++ standard library is missing an algorithm that generates/iterates over the cartesian product of multiple containers. An implementation of such an algorithm can be found here at stackoverlow, e.g. http://stackoverflow.com/a/13841673/2128694 – Oberon Jan 17 '14 at 14:11
  • I think Boost.Test has an experimental (i.e. not yet released) feature that allows such data driven testing. – TemplateRex Jan 17 '14 at 14:54
  • 1
    Check out the product template from the AWESOME cppitertools package: https://github.com/ryanhaining/cppitertools – Ben Jones Jan 17 '14 at 18:41
  • `iter::starmap(tester_func, iter::product(p1, p2, p3));` – Ryan Haining Aug 27 '15 at 00:21

1 Answers1

1

A reference to the very good solution is given in comments by @Oberon.

But I think there are many different solutions to this problem. Here is my solution:

#include <tuple>
#include <type_traits>

template <class TestFunction, class... Containers, class... Types>
typename std::enable_if<sizeof...(Containers) == sizeof...(Types)>::type
TestNextLevel
    (
        TestFunction testFunction,
        const std::tuple<Containers...>& containersTuple,
        const Types&... parameters
    )
{
    testFunction(parameters...);
}

template <class TestFunction, class... Containers, class... Types>
typename std::enable_if<(sizeof...(Containers) > sizeof...(Types))>::type
TestNextLevel
    (
        TestFunction testFunction,
        const std::tuple<Containers...>& containersTuple,
        const Types&... parameters
    )
{
    for (const auto& element : std::get<sizeof...(Types)>(containersTuple))
    {
        TestNextLevel(testFunction, containersTuple, parameters..., element);
    }
}

template <class TestFunction, class... Containers>
void TestAllCases
    (
        TestFunction testFunction,
        const Containers&... containers
    )
{
    TestNextLevel
        (
            testFunction,
            std::tuple<const Containers&...>(containers...)
        );
}

Example of using:

TestAllCases(tester_func, vec_p1, vec_p2, vec_p3);
Constructor
  • 7,273
  • 2
  • 24
  • 66