Как использовать шаблонную функцию в качестве пользовательского предиката для Boost :: Unit-test

Я пытаюсь создать собственный предикат для BOOST_CHECK_PREDICATE, где сам предикат является шаблонной функцией. Мой пример выглядит следующим образом:

#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(a-b) < 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()

Компиляция с MS Visual C ++ 2010 дает следующие ошибки:

3> .. \ boost_test \ testSystem.cpp (42): ошибка C2780: ‘bool
повышение :: test_tools :: tt_detail :: check_frwd (Pred, сопзЬ
повышение :: unit_test :: lazy_ostream
&, Повышение :: test_tools :: const_string, size_t, форсирует :: test_tools :: tt_detail :: tool_level, повысить :: test_tools :: tt_detail :: check_type, сопзЬ
arg0 &, const char *, const Arg1 &, const char *, const Arg2 &const char
*, const Arg3 &, const char *, const Arg4 &, const char *) ‘: ожидается 16 аргументов — 10 при условии 3>
C: \ Boost \ include \ boost-1_51 \ boost / test / test_tools.hpp (523): см.
объявление ‘boost :: test_tools :: tt_detail :: check_frwd’
3> .. \ boost_test \ testSystem.cpp (42): ошибка C2780: ‘bool
повышение :: test_tools :: tt_detail :: check_frwd (Pred, сопзЬ
повышение :: unit_test :: lazy_ostream
&, Повышение :: test_tools :: const_string, size_t, форсирует :: test_tools :: tt_detail :: tool_level, повысить :: test_tools :: tt_detail :: check_type, сопзЬ
arg0 &, const char *, const Arg1 &, const char *, const Arg2 &const char
*, const Arg3 &, const char *) ‘: ожидается 14 аргументов — 10 при условии 3> C: \ Boost \ include \ boost-1_51 \ boost / test / test_tools.hpp (523): см.
объявление ‘boost :: test_tools :: tt_detail :: check_frwd’
3> .. \ boost_test \ testSystem.cpp (42): ошибка C2780: ‘bool
повышение :: test_tools :: tt_detail :: check_frwd (Pred, сопзЬ
повышение :: unit_test :: lazy_ostream
&, Повышение :: test_tools :: const_string, size_t, форсирует :: test_tools :: tt_detail :: tool_level, повысить :: test_tools :: tt_detail :: check_type, сопзЬ
arg0 &, const char *, const Arg1 &, const char *, const Arg2 &const char
*) ‘: ожидается 12 аргументов — 10 при условии 3> C: \ Boost \ include \ boost-1_51 \ boost / test / test_tools.hpp (523): см.
объявление ‘boost :: test_tools :: tt_detail :: check_frwd’
3> .. \ boost_test \ testSystem.cpp (42): ошибка C2896: ‘bool
повышение :: test_tools :: tt_detail :: check_frwd (Pred, сопзЬ
повышение :: unit_test :: lazy_ostream
&, Повышение :: test_tools :: const_string, size_t, форсирует :: test_tools :: tt_detail :: tool_level, повысить :: test_tools :: tt_detail :: check_type, сопзЬ
arg0 &, const char *, const Arg1 &, const char *) ‘: нельзя использовать функцию
шаблон ‘bool is_close_enough (const U &, const V &)’ как функция
аргумент 3> .. \ boost_test \ testSystem.cpp (18): см.
Объявление is_close_enough 3> .. \ boost_test \ testSystem.cpp (42):
ошибка C2784: ‘bool boost :: test_tools :: tt_detail :: check_frwd (Pred, const
повышение :: unit_test :: lazy_ostream
&, Повышение :: test_tools :: const_string, size_t, форсирует :: test_tools :: tt_detail :: tool_level, повысить :: test_tools :: tt_detail :: check_type, сопзЬ
arg0 &, const char *, const Arg1 &, const char *) ‘: не смог вывести
аргумент шаблона для ‘перегруженного типа функции’ из ‘перегруженного
тип функции ‘3>
C: \ Boost \ include \ boost-1_51 \ boost / test / test_tools.hpp (523): см.
объявление ‘boost :: test_tools :: tt_detail :: check_frwd’
3> .. \ boost_test \ testSystem.cpp (42): ошибка C2780: ‘bool
повышение :: test_tools :: tt_detail :: check_frwd (Pred, сопзЬ
повышение :: unit_test :: lazy_ostream
&, Повышение :: test_tools :: const_string, size_t, форсирует :: test_tools :: tt_detail :: tool_level, повысить :: test_tools :: tt_detail :: check_type, сопзЬ
arg0 &, const char *) ‘: ожидается 8 аргументов — 10 предоставлено

Есть идеи, что я здесь делаю не так?

4

Решение

Следующее прекрасно работает с Boost 1.53.0 в Visual Studio 2012 и g ++ 4.8.1. Я думаю, что если вы хотите использовать шаблонную функцию, вам нужно явно указать параметры шаблона. По этой причине я предпочитаю решение с функтором.

#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(a-b) < 2.0;
}

struct is_close_enough_functor
{
template <typename U, typename V>
bool operator()(const U& a, const V& b) const
{
return std::abs(a-b) < 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

Другие решения

Других решений пока нет …