How to use a template function as a custom predicate for Boost :: Unit-test

I am trying to create my own predicate for BOOST_CHECK_PREDICATE where the predicate itself is a templated function. My example is as follows:

#define BOOST_TEST_MODULE Module #define BOOST_TEST_MAIN #include <boost/test/unit_test.hpp> // custom predicate template <typename U, typename V> bool is_close_enough(const U& a, const V& b) { return std::abs(ab) < 2.0; } BOOST_AUTO_TEST_SUITE(boostUnitTestLearningTests) BOOST_AUTO_TEST_CASE(Test_Templated_Predicate) { BOOST_CHECK_PREDICATE(is_close_enough, (4)(6)); BOOST_CHECK_PREDICATE(is_template_close_enough, (4.0)(6.5)); } BOOST_AUTO_TEST_SUITE_END() 

Compilation with MS Visual C ++ 2010 gives the following errors:

3> .. \ boost_test \ testSystem.cpp (42): error C2780: 'bool boost :: test_tools :: tt_detail :: check_frwd (Pred, link boost :: unit_test :: lazy_ostream & amp ;, boost :: test_tools :: const_string, size_t, forces :: test_tools :: tt_detail :: tool_level, raise :: test_tools :: tt_detail :: check_type, const Arg0 and, const char *, const Arg1 &, const char *, const Arg2 &, const char *, const Arg3 &, const char *, const Arg4 &, const char *) ': expects 16 arguments - 10 under condition 3>
C: \ Boost \ include \ boost-1_51 \ boost / test / test_tools.hpp (523): see the declaration 'boost :: test_tools :: tt_detail :: check_frwd' 3> .. \ boost_test \ testSystem.cpp (42) : error C2780: 'bool raise :: test_tools :: tt_detail :: check_frwd (Pred, raise raise :: unit_test :: lazy_ostream & amp ;, raise :: test_tools :: const_string, size_t, force :: test_tools :: tt_detail :: tool_level, raise :: test_tools :: tt_detail :: check_type, const Arg0 and, const char *, const Arg1 &, const char *, const Arg2 &, const char *, const Arg3 & const char *) ': expects 14 arguments - 10 provided 3> C: \ Boost \ include \ boost-1_51 \ boost / test / test_tools.hpp (523): see the declaration 'boost :: test_tools :: tt_detail :: check_frwd' 3> .. \ boost_test \ testSystem .cpp (42): error C2780: 'bool raise :: test_tools :: tt_detail :: check_frwd (Pred, link raise :: unit_test :: lazy_os tream & amp ;, raising :: test_tools :: const_string, size_t, forces :: test_tools :: tt_detail :: tool_level, raise :: test_tools :: tt_detail :: check_type, const Arg0 and, const char *, const Arg1 &, const char *, const Arg2 &, const char *) ': expects 12 arguments - 10 provided 3> C: \ Boost \ include \ boost-1_51 \ boost / test / test_tools.hpp (523): see the declaration of' boost: : test_tools :: tt_detail :: check_frwd '3> .. \ boost_test \ testSystem.cpp (42): error C2896:' bool increase :: test_tools :: tt_detail :: check_frwd (Pred, link increase :: unit_test :: lazy_ostream & Amp ;, raise :: test_tools :: const_string, size_t, force :: test_tools :: tt_detail :: tool_level, raise :: test_tools :: tt_detail :: check_type, const Arg0 & const char *, const Arg1 &, const char *) ': cannot use template function' bool is_close_enough (c onst U &, const V &) 'as a function argument 3> .. \ boost_test \ testSystem.cpp (18): see the declaration' is_close_enough '3> .. \ boost_test \ testSystem.cpp (42): error C2784:' bool boost :: test_tools :: tt_detail :: check_frwd (Pred, const increase :: unit_test :: lazy_ostream & amp ;, increase :: test_tools :: const_string, size_t, force :: test_tools :: tt_detail :: tool_level, raise :: test_tools :: tt_detail :: check_type, link Arg0 & const char *, const Arg1 &, const char *) ': failed to deduce the template argument for the "overloaded function type" from the "overloaded" function type' 3>
C: \ Boost \ include \ boost-1_51 \ boost / test / test_tools.hpp (523): see the declaration 'boost :: test_tools :: tt_detail :: check_frwd' 3> .. \ boost_test \ testSystem.cpp (42) : error C2780: 'bool raise :: test_tools :: tt_detail :: check_frwd (Pred, raise raise :: unit_test :: lazy_ostream & amp ;, raise :: test_tools :: const_string, size_t, force :: test_tools :: tt_detail :: tool_level, raise :: test_tools :: tt_detail :: check_type, link Arg0 & const char *) ': expects 8 arguments - 10 provided

Any ideas what I'm doing wrong here?

+4
source share
1 answer

The following works great with Boost 1.53.0 on Visual Studio 2012 and g ++ 4.8.1. I think that if you want to use the template function, you need to explicitly specify the template parameters. For this reason, I prefer a solution with a functor.

 #define BOOST_TEST_MODULE Module #define BOOST_TEST_MAIN #include <boost/test/unit_test.hpp> template <typename U, typename V> bool is_close_enough(const U& a, const V& b) { return std::abs(ab) < 2.0; } struct is_close_enough_functor { template <typename U, typename V> bool operator()(const U& a, const V& b) const { return std::abs(ab) < 2.0; } }; BOOST_AUTO_TEST_SUITE(boostUnitTestLearningTests) BOOST_AUTO_TEST_CASE(Test_Templated_Predicate) { BOOST_CHECK_PREDICATE((is_close_enough<int,int>), (4)(6)); //extra parentheses needed to avoid a problem with the comma inside the macro BOOST_CHECK_PREDICATE(is_close_enough_functor(), (4)(6)); BOOST_CHECK_PREDICATE(is_close_enough_functor(), (4.0)(6.5)); } BOOST_AUTO_TEST_SUITE_END() 
+2
source

Source: https://habr.com/ru/post/1485460/


All Articles