1

My data-driven Boost test-case takes several parameters. I chose to group them in a std::tuple. So the resulting dataset consists in a list of tuples.
Beyond a 1-element tuple (test1 below), the compilation fails (test2 below) :

#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK

#include <boost/test/included/unit_test.hpp>
#include <boost/test/data/test_case.hpp>

typedef std::tuple<int> type1;
BOOST_DATA_TEST_CASE(test1, std::list<type1>({{1}}), i)
{
        BOOST_CHECK(i);
}

typedef std::tuple<int, int> type2;
BOOST_DATA_TEST_CASE(test2, std::list<type2>({{1, 0}}), i)
{
        BOOST_CHECK(std::get<0>(i));
        BOOST_CHECK(! std::get<1>(i));
}

Resulting error message:

$ g++ -o boost_data_test_case  boost_data_test_case.cpp -I ~/workspace/boost_1_81_0/   &&  ./boost_data_test_case
In file included from boost_data_test_case.cpp:6:
~/workspace/boost_1_81_0/boost/test/data/test_case.hpp: In instantiation of ‘void boost::unit_test::data::ds_detail::test_case_gen<TestCase, DataSet>::operator()(Arg&& ...) const [with Arg = {const int&, const int&}; TestCase = test2case; DataSet = boost::unit_test::data::monomorphic::collection<std::list<std::tuple<int, int> > >]’:
~/workspace/boost_1_81_0/boost/test/data/for_each_sample.hpp:55:11:   required from ‘void boost::unit_test::data::invoke_action_impl(const Action&, T&&, const boost::unit_test::data::index_sequence<I ...>&) [with Action = boost::unit_test::data::ds_detail::test_case_gen<test2case, boost::unit_test::data::monomorphic::collection<std::list<std::tuple<int, int> > > >; T = const std::tuple<int, int>&; long unsigned int ...I = {0, 1}]’
~/workspace/boost_1_81_0/boost/test/data/for_each_sample.hpp:64:23:   required from ‘void boost::unit_test::data::invoke_action(const Action&, T&&, std::true_type) [with Action = boost::unit_test::data::ds_detail::test_case_gen<test2case, boost::unit_test::data::monomorphic::collection<std::list<std::tuple<int, int> > > >; T = const std::tuple<int, int>&; std::true_type = std::integral_constant<bool, true>]’
~/workspace/boost_1_81_0/boost/test/data/for_each_sample.hpp:90:22:   required from ‘typename std::enable_if<boost::unit_test::data::monomorphic::is_dataset<DataSet>::value, void>::type boost::unit_test::data::for_each_sample(const DataSet&, const Action&, boost::unit_test::data::size_t) [with DataSet = boost::unit_test::data::monomorphic::collection<std::list<std::tuple<int, int> > >; Action = boost::unit_test::data::ds_detail::test_case_gen<test2case, boost::unit_test::data::monomorphic::collection<std::list<std::tuple<int, int> > > >; typename std::enable_if<boost::unit_test::data::monomorphic::is_dataset<DataSet>::value, void>::type = void]’
~/workspace/boost_1_81_0/boost/test/data/test_case.hpp:160:34:   required from ‘boost::unit_test::test_unit* boost::unit_test::data::ds_detail::test_case_gen<TestCase, DataSet>::next() const [with TestCase = test2case; DataSet = boost::unit_test::data::monomorphic::collection<std::list<std::tuple<int, int> > >]’
~/workspace/boost_1_81_0/boost/test/data/test_case.hpp:157:24:   required from here
~/workspace/boost_1_81_0/boost/test/data/test_case.hpp:195:37: error: no matching function for call to ‘bind(<unresolved overloaded function type>, const int&, const int&)’
  195 |                            std::bind( &TestCase::template test_method<Arg...>,
      |                            ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  196 |                                       boost_bind_rvalue_holder_helper(std::forward<Arg>(arg))...)));
      |                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from ~/workspace/boost_1_81_0/boost/smart_ptr/detail/shared_count.hpp:37,
                 from ~/workspace/boost_1_81_0/boost/smart_ptr/shared_ptr.hpp:17,
                 from ~/workspace/boost_1_81_0/boost/shared_ptr.hpp:17,
                 from ~/workspace/boost_1_81_0/boost/test/execution_monitor.hpp:24,
                 from ~/workspace/boost_1_81_0/boost/test/impl/compiler_log_formatter.ipp:22,
                 from ~/workspace/boost_1_81_0/boost/test/included/unit_test.hpp:18,
                 from boost_data_test_case.cpp:5:
/opt/rh/devtoolset-9/root/usr/include/c++/9/functional:808:5: note: candidate: ‘template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...)’
  808 |     bind(_Func&& __f, _BoundArgs&&... __args)
      |     ^~~~
/opt/rh/devtoolset-9/root/usr/include/c++/9/functional:808:5: note:   template argument deduction/substitution failed:
In file included from boost_data_test_case.cpp:6:
~/workspace/boost_1_81_0/boost/test/data/test_case.hpp:195:37: note:   couldn’t deduce template parameter ‘_Func’
  195 |                            std::bind( &TestCase::template test_method<Arg...>,
      |                            ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  196 |                                       boost_bind_rvalue_holder_helper(std::forward<Arg>(arg))...)));
      |                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from ~/workspace/boost_1_81_0/boost/smart_ptr/detail/shared_count.hpp:37,
                 from ~/workspace/boost_1_81_0/boost/smart_ptr/shared_ptr.hpp:17,
                 from ~/workspace/boost_1_81_0/boost/shared_ptr.hpp:17,
                 from ~/workspace/boost_1_81_0/boost/test/execution_monitor.hpp:24,
                 from ~/workspace/boost_1_81_0/boost/test/impl/compiler_log_formatter.ipp:22,
                 from ~/workspace/boost_1_81_0/boost/test/included/unit_test.hpp:18,
                 from boost_data_test_case.cpp:5:
/opt/rh/devtoolset-9/root/usr/include/c++/9/functional:832:5: note: candidate: ‘template<class _Result, class _Func, class ... _BoundArgs> typename std::_Bindres_helper<_Result, _Func, _BoundArgs>::type std::bind(_Func&&, _BoundArgs&& ...)’
  832 |     bind(_Func&& __f, _BoundArgs&&... __args)
      |     ^~~~
/opt/rh/devtoolset-9/root/usr/include/c++/9/functional:832:5: note:   template argument deduction/substitution failed:
In file included from boost_data_test_case.cpp:6:
~/workspace/boost_1_81_0/boost/test/data/test_case.hpp:195:37: note:   couldn’t deduce template parameter ‘_Result’
  195 |                            std::bind( &TestCase::template test_method<Arg...>,
      |                            ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  196 |                                       boost_bind_rvalue_holder_helper(std::forward<Arg>(arg))...)));
      |                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
  • 1
    std::tuple doesn't have first/second... Commented Sep 29 at 12:21
  • 1
    Why use tuple at all? You're not template (meta) programming are you? A struct with fields you can name yourself is both more type safe and more easy to spot which members mean what Commented Sep 29 at 12:53
  • Beyond a 1-element tuple (test1 below), the compilation fails (test2 below) -- BOOST_CHECK(i.first); BOOST_CHECK(! i.second); -- Let's extend your initial code a bit. If your tuple had 3 or 4 elements, would you expect that there would be a .third and .fourth members? Of course not. See std::get<> Commented Sep 29 at 13:21
  • @PaulMcKenzie: good point, i've fixed the example Commented Sep 29 at 13:27
  • 1
    @Francois Heh, well that makes sense then :) Commented Sep 29 at 13:36

1 Answer 1

3

If you check documentation of BOOST_DATA_TEST_CASE

Then you should noticed that tuple is bind to multiple variables. Structured binding is used to unpack tuple.

#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK

#include <boost/test/data/test_case.hpp>
#include <boost/test/included/unit_test.hpp>
#include <list>
#include <tuple>

typedef std::tuple<int> type1;
BOOST_DATA_TEST_CASE(test1, std::list<type1>({{1}}), i) {
    BOOST_CHECK(i);
}

typedef std::tuple<int, int> type2;
BOOST_DATA_TEST_CASE(test2, std::list<type2>({{1, 0}}), a, b) {
    BOOST_CHECK(a);
    BOOST_CHECK(!b);
}

Live demo
Falling test example

Running 3 test cases...
/app/example.cpp(17): error: in "test2/_1": check !b has failed
Failure occurred in a following context:
    a = 3; b = -1; 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the fix. Too bad that no required or recommended type for 'dataset' is mentioned in the official Boost examples.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.