有没有办法使用 Boost Test 来测试非类型模板?

And*_*rea 4 c++ boost boost-test

我正在使用 Boost Unit Test 为我的项目执行单元测试。我需要测试一些非类型模板,但是似乎使用宏BOOST_AUTO_TEST_CASE_TEMPLATE(test_case_name, formal_type_parameter_name, collection_of_types)我只能测试类型模板。我想使用collection_of_types不是 由 组成{ int, float, ...},但是{0,1,2, ...}

这是我想做的事情的一个例子:

#include <boost/test/included/unit_test.hpp>
#include <boost/mpl/list.hpp>

typedef boost::mpl::list<0, 1, 2, 4, 6> test_types;

BOOST_AUTO_TEST_CASE_TEMPLATE( my_test, T, test_types )
{
  test_template<T>* test_tmpl = new test_template<T>();

  // other code
}
Run Code Online (Sandbox Code Playgroud)

seh*_*ehe 6

您始终可以将静态常量包装在类型中。对于整数类型,有std::integral_constant或者确实有 Boost MPL 类似物:

住在科里鲁

#define BOOST_TEST_MODULE sotest
#define BOOST_TEST_MAIN

#include <boost/mpl/list.hpp>
#include <boost/test/included/unit_test.hpp>

template <int> struct test_template {};

typedef boost::mpl::list<
    boost::mpl::integral_c<int, 0>,
    boost::mpl::integral_c<int, 1>,
    boost::mpl::integral_c<int, 2>,
    boost::mpl::integral_c<int, 4>,
    boost::mpl::integral_c<int, 6>
> test_types;

BOOST_AUTO_TEST_CASE_TEMPLATE(my_test, T, test_types) {
    test_template<T::value>* test_tmpl = new test_template<T::value>();

    // other code
    delete test_tmpl;
}
Run Code Online (Sandbox Code Playgroud)

印刷

Running 5 test cases...

*** No errors detected
Run Code Online (Sandbox Code Playgroud)

额外提示

为了节省打字时间,您可以使用可变参数模板别名:

template <typename T, T... vv> using vlist =
    boost::mpl::list<boost::mpl::integral_c<T, vv>... >;
Run Code Online (Sandbox Code Playgroud)

现在您可以将列表定义为:

住在科里鲁

using test_types = vlist<int, 0, 1, 2, 4, 6>;
Run Code Online (Sandbox Code Playgroud)