How to define a "sample" for boost test dataset with an arity > 1

248 views Asked by At

I'd prefer to group my function params and expected results together in a logical group but I cannot figure out how to create a boost dataset sample with arity > 1 on my own without zipping together other data sets. Below is some code that I am trying to achieve

It feels like I should be able to do what I want.

#define BOOST_TEST_MODULE dataset_test
#include <boost/test/data/monomorphic.hpp>
#include <boost/test/data/test_case.hpp>
#include <boost/test/included/unit_test.hpp>

int my_function_to_test(int left, int middle, int right)
{
   if(left > middle)
      return left + right;
   else if(right > middle)
      return left + middle;

   return left + middle + right;
}

// This works ok - but I don't like the defining each sample across 4 datasets
auto lefts    = boost::unit_test::data::make({1, 43, 5, 6435, 564, 457, 457});
auto middles  = boost::unit_test::data::make({9, 4, 43, 12, 4, 99, 7});
auto rights   = boost::unit_test::data::make({44, 11, 22, 88, 99, 66, 77});
auto expected = boost::unit_test::data::make({-1, -2, -3, -4, -5, -6, -7});
auto data     = lefts ^ middles ^ rights ^ expected;

// I'd rather something like this type of dataset
struct Sample
{
   int left;
   int middle;
   int right;
   int expected;
};
// Here I can neatly define the 3 params and the result together, which is more readble
std::vector<Sample> data2 = {
   {1, 9, 44, -1},     // Test _0
   {43, 4, 11, -2},    // Test _1
   {5, 43, 22, -3},    // Test _2
   {6435, 12, 88, -4}, // Test _3
   {564, 4, 99, -5},   // Test _4
   {457, 99, 66, -6},  // Test _5
   {457, 7, 77 - 7}    // Test _6
};

// Also tried this and other variations
//auto data3=boost::unit_test::data::make<std::tuple<int,int,int,int>>({
//   {1, 9, 44, -1},     // Test _0
//   {43, 4, 11, -2},    // Test _1
//});

BOOST_DATA_TEST_CASE(test1, data, left, middle, right, expected_result)
{
   auto result = my_function_to_test(left, middle, right);
   BOOST_TEST(result == expected_result);
}

BOOST_DATA_TEST_CASE(test2, data2, left, middle, right, expected_result)
{
   auto result = my_function_to_test(left, middle, right);
   BOOST_TEST(result == expected);
}
1

There are 1 answers

0
vines On

Turns out that using Sample = std::tuple<int, int, int, int>; indeed makes it work as expected, with arity 4.

It doesn't seem to be documented (yet): https://github.com/boostorg/test/issues/300