【发布时间】:2014-01-17 13:12:24
【问题描述】:
在编写单元测试时,我经常想调用带有参数组合的函数。例如,我有一个函数声明为
void tester_func(int p1, double p2, std::string const& p3);
以及一些选定的参数
std::vector<int> vec_p1 = { 1, 2, 666 };
std::vector<double> vec_p2 = { 3.14159, 0.0001 };
std::vector<std::string> vec_p3 = { "Method_Smart", "Method_Silly" };
我现在做的只是
for(auto const& p1 : vec_p1)
for(auto const& p2 : vec_p2)
for(auto const& p3 : vec_p3)
tester_func(p1, p2, p3);
但是,Sean Parent 建议避免显式循环并改用std:: 算法。在上述情况下,如何遵循这一建议?有什么成语吗?编写可变参数模板的最简洁方法是什么? 没有 C++11 功能的最佳方法是什么?
【问题讨论】:
-
答案可能取决于您使用的单元测试框架。
-
提升单元测试。
tester_func自动记录所有错误。唯一重要的是为所有参数组合调用该函数。 -
在您链接到的视频中,他明确指出,只要循环体是单个语句/赋值,有时(节省打字)使用范围 for 循环是可以的。我觉得你在这里做的没问题。
-
C++ 标准库似乎缺少一种算法,该算法可以生成/迭代多个容器的笛卡尔积。这种算法的实现可以在 stackoverlow 上找到,例如stackoverflow.com/a/13841673/2128694
-
查看 AWESOME cppitertools 软件包中的产品模板:github.com/ryanhaining/cppitertools