0

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);
}
sehe
  • 374,641
  • 47
  • 450
  • 633
Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77
  • Isn't [this](https://www.boost.org/doc/libs/1_78_0/libs/test/doc/html/boost_test/tests_organization/test_cases/test_case_generation/generators.html#boost_test.tests_organization.test_cases.test_case_generation.generators.stl) working for you? `make` should work on the vector you defined. – Raffi Dec 30 '21 at 22:18
  • No, tried that as well, it doesn't compile either. – Adrian Cornish Dec 31 '21 at 18:39

0 Answers0