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

标签 c++ boost boost-test

我正在使用 Boost 单元测试为我的项目执行单元测试。我需要测试一些非类型模板,但是似乎使用宏 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
}

最佳答案

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

#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;
}
打印
Running 5 test cases...

*** No errors detected
奖金提示
为了节省打字时间,您可以使用可变参数模板别名:
template <typename T, T... vv> using vlist =
    boost::mpl::list<boost::mpl::integral_c<T, vv>... >;
现在您可以将列表定义为:
Live On Coliru
using test_types = vlist<int, 0, 1, 2, 4, 6>;

关于c++ - 有没有办法使用 Boost Test 测试非类型模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65065729/

相关文章:

c++ - 自定义 STL 序列的最小嵌套 typedef 集?

c++ - Visual Studio 和 Boost::Test

c++ - 我应该将单元测试放在单独的库中吗?单独的子目录?

c++ - Friend C++ 类的非常量模板化版本

c++ - 从当前目录检查文件大小

multithreading - C++ 中的内存栅栏/屏障 : does boost or other libraries have them?

C++ 自动检测 shared_ptr 与 NULL 比较的位置

c++ - 在 C++ 中使用 MessagePack 反序列化异构映射

简单图像处理示例中的 C++AMP 异常

c++ - boost 测试库: Multiple definition error