我正在使用 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/